Expand my Community achievements bar.

Who Me Too'd this topic

Avatar

Level 5

hi I have the following model class :


@Model(
adaptables = SlingHttpServletRequest.class,
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class LocationsModel extends ComponentModel implements Validatable {
private static final Logger log = LoggerFactory.getLogger(LocationsModel.class);
private static final String CSS_CLASS_NAME = "locations";

@OSGiService
private GoogleMapAPIConfiguration mapAPIConfiguration;

@ValueMapValue
private String title;

@ValueMapValue
private int zoomLevel;

@ChildResourceFromRequest
private final List<LocationItem> locations = emptyList();

private String locationsJson;
private String key;

public LocationsModel() {
}

@PostConstruct
protected void init() {
if (mapAPIConfiguration != null) {
this.key = mapAPIConfiguration.getKey();
}

try {
this.locationsJson = new ObjectMapper().writeValueAsString(this.locations);
} catch (JsonProcessingException ex) {
log.error("Unable to generate locations to JSON string", ex);
}
}

@Nonnull
@Override
public String getCssClassName() {
return CSS_CLASS_NAME;
}

@Override
public boolean isValid() {
return StringUtils.isNotEmpty(this.key) && !this.locations.isEmpty();
}

public String getTitle() {
return this.title;
}

@Nonnull
public List<LocationItem> getLocations() {
return this.locations;
}

public String getLocationsJson() {
return this.locationsJson;
}

public int getZoomLevel() {
return this.zoomLevel;
}

public String getKey() {
return this.key;
}
}

and the test class is :

@ExtendWith({AemContextExtension.class, MockitoExtension.class})
class LocationsModelTest {

Private final AemContext ctx = new AemContext();
public LocationsModel locationsModel;
@BeforeEach
void setUp() throws Exception {
ctx.addModelsForClasses(LocationsModel.class);
ctx.load().json("/locations/LocationsModelTest.json","/content");
}
@Test
void getZoomLevel() {
ctx.currentResource("/content/location");
System.out.println("ctx "+ctx.request().adaptTo(LocationsModel.class));
locationsModel=ctx.request().adaptTo(LocationsModel.class); // GETTING NULL VALUE HERE

int expected = 10;
int actual = locationsModel.getZoomLevel();
locationsModel.getLocations();
assertEquals(expected,actual);
}
}

I am not able to find out why this line is giving null value ..

the paths are correct and I have provided the JSON also in the same path as mentioned but somehow getting null  in " locationsModel=ctx.request().adaptTo(LocationsModel.class);" 

please help me resolve the issue

Thank you

Who Me Too'd this topic