Skip to content

Commit

Permalink
Merge branch '10.0.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
himeshr committed Oct 7, 2024
2 parents 448e9ab + 08fa682 commit ee067b2
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.avni.server.domain;

import org.avni.server.util.CollectionUtil;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;
Expand All @@ -12,16 +13,29 @@ public class UserSettings {
public static final String LOCALE = "locale";
public static final String ENABLE_BENEFICIARY_MODE = "showBeneficiaryMode";
public static final String TRACK_LOCATION = "trackLocation";
public static final List<String> DATE_PICKER_MODE_OPTIONS = Arrays.asList("calendar", "spinner");
public static final String DEFAULT_DATE_PICKER_MODE = "calendar";
public static final String SPINNER_DATE_PICKER_MODE = "spinner";
public static final List<String> DATE_PICKER_MODE_OPTIONS = Arrays.asList(DEFAULT_DATE_PICKER_MODE, SPINNER_DATE_PICKER_MODE);

public UserSettings(JsonObject jsonObject) {
this.jsonObject = jsonObject;
}

public static String createDatePickerMode(String datePickerMode) {
if (StringUtils.isEmpty(datePickerMode)) {
return DEFAULT_DATE_PICKER_MODE;
}
return CollectionUtil.findMatchingIgnoreCase(DATE_PICKER_MODE_OPTIONS, datePickerMode);
}

public static Boolean createTrackLocation(Boolean trackLocation) {
return trackLocation != null && trackLocation;
}

public static Boolean createEnableBeneficiaryMode(Boolean enableBeneficiaryMode) {
return enableBeneficiaryMode != null && enableBeneficiaryMode;
}

public String getIdPrefix() {
return UserSettings.getIdPrefix(this.jsonObject);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@ private void write(Row row) throws IDPException {

user.setSettings(new JsonObject()
.with(UserSettings.LOCALE, locale)
.with(UserSettings.TRACK_LOCATION, trackLocation)
.with(UserSettings.TRACK_LOCATION, UserSettings.createTrackLocation(trackLocation))
.withEmptyCheckAndTrim(UserSettings.DATE_PICKER_MODE, UserSettings.createDatePickerMode(datePickerMode))
.with(UserSettings.ENABLE_BENEFICIARY_MODE, beneficiaryMode)
.with(UserSettings.ENABLE_BENEFICIARY_MODE, UserSettings.createEnableBeneficiaryMode(beneficiaryMode))
.withEmptyCheckAndTrim(UserSettings.ID_PREFIX, idPrefix));

user.setOrganisationId(organisation.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public List<CatchmentAddressProjection> getAddressLevelsForCatchment(Catchment c
}
}

@Cacheable(value = ADDRESSES_PER_CATCHMENT_AND_MATCHING_ADDR_LEVELS)
@Cacheable(cacheNames = ADDRESSES_PER_CATCHMENT_AND_MATCHING_ADDR_LEVELS, key="T(java.lang.String).valueOf(#catchment?.id + '_' + T(org.springframework.util.StringUtils).collectionToCommaDelimitedString(#matchingAddressLevelTypeIds)).intern()")
@Transactional
public List<CatchmentAddressProjection> getAddressLevelsForCatchmentAndMatchingAddressLevelTypeIds(Catchment catchment, List<Long> matchingAddressLevelTypeIds) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ private Stream<CatchmentAddressProjection> filterByCatchmentAndSubjectType(Catch
customRegistrationLocationSetting.get().getLocationTypeUUIDs())
.stream()
.map(CHSBaseEntity::getId)
.sorted()
.collect(Collectors.toList());

return addressLevelCache.getAddressLevelsForCatchmentAndMatchingAddressLevelTypeIds(catchment, matchingAddressLevelTypeIds).stream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ public void shouldCreateUpdate() throws IDPException {
dataRow(" Bihar, District1, Block11", " Catchment 6", " username8@example", " User 8", " [email protected] ", " 9455509147 ", "Answer 1"),
catchmentCreated(false),
userCreatedDetails(true));
userCreatedDetails(user("username8@example"), datePickerMode("calendar"), language("en"), trackLocation(false), enableBeneficiaryMode(false), userGroup("Everyone"));

// wrong - username, email, phone number, language, track location, date picker mode, enable beneficiary mode
failure(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Objects;

import static org.avni.server.service.AddressLevelCache.ADDRESSES_PER_CATCHMENT;
import static org.avni.server.service.AddressLevelCache.ADDRESSES_PER_CATCHMENT_AND_MATCHING_ADDR_LEVELS;
import static org.mockito.Mockito.*;

@Sql(value = {"/tear-down.sql"}, executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
Expand Down Expand Up @@ -68,6 +69,11 @@ public class AddressLevelCacheIntegrationTest extends AbstractControllerIntegrat
private ReferenceQueue<Object> keysReferenceQueue;
private ReferenceQueue<Object> valuesReferenceQueue;

List<Long> matchingAddressLevelTypeIds1= Arrays.asList(1l, 2l,3l);
List<Long> matchingAddressLevelTypeIds2Ordered= Arrays.asList(1l, 2l,3l);
List<Long> matchingAddressLevelTypeIds2UnOrdered= Arrays.asList(2l, 1l,3l);
List<Long> matchingAddressLevelTypeIds3= Arrays.asList(4l);

@Before
public void setUpAddressLevelCache() {
addressIdStartIdx = 1L;
Expand Down Expand Up @@ -109,6 +115,10 @@ private Catchment initCatchmentAndMock(long catchment1Id, long startIndex, int n

//stubbing
when(mockLocationRepository.getCatchmentAddressesForCatchmentId(catchment.getId())).thenReturn(catchmentResponseList);
when(mockLocationRepository.getCatchmentAddressesForCatchmentIdAndLocationTypeId(catchment.getId(), matchingAddressLevelTypeIds1)).thenReturn(catchmentResponseList);
when(mockLocationRepository.getCatchmentAddressesForCatchmentIdAndLocationTypeId(catchment.getId(), matchingAddressLevelTypeIds2Ordered)).thenReturn(catchmentResponseList);
when(mockLocationRepository.getCatchmentAddressesForCatchmentIdAndLocationTypeId(catchment.getId(), matchingAddressLevelTypeIds2UnOrdered)).thenReturn(catchmentResponseList);
when(mockLocationRepository.getCatchmentAddressesForCatchmentIdAndLocationTypeId(catchment.getId(), matchingAddressLevelTypeIds3)).thenReturn(new ArrayList<>());

return catchment;
}
Expand All @@ -121,6 +131,41 @@ private ArrayList<CatchmentAddressProjection> getCatchmentAddressProjectionArray
return CatchmentAddressProjectionList;
}

@Test
public void givenAddressLevelCacheIsConfigured_whenCallGetCatchmentAddressesForCatchmentIdAndLevelTypeList_thenDataShouldBeInAddressPerCatchmentAndMatchingAddressLevelCache() {
//Clear cache
Cache addrPerCatchmentCacheAndMatchingAddrLevels = cacheManager.getCache(ADDRESSES_PER_CATCHMENT_AND_MATCHING_ADDR_LEVELS);
addrPerCatchmentCacheAndMatchingAddrLevels.clear();

//Fetch and cache
List<CatchmentAddressProjection> cachedCatchmentAddressesList = addressLevelCache.getAddressLevelsForCatchmentAndMatchingAddressLevelTypeIds(catchment1, matchingAddressLevelTypeIds1);

//Validate cache content
Assert.notNull(cachedCatchmentAddressesList, "addrPerCatchmentCache should have had the data");
Assert.isTrue(CATCHMENT_1_SIZE == cachedCatchmentAddressesList.size(), "addrPerCatchmentCache size should have been 2");

//Validate cache miss the first time
verify(mockLocationRepository).getCatchmentAddressesForCatchmentIdAndLocationTypeId(catchment1.getId(), matchingAddressLevelTypeIds1);

//Invoke cache again for same catchment and level type ids
addressLevelCache.getAddressLevelsForCatchmentAndMatchingAddressLevelTypeIds(catchment1, matchingAddressLevelTypeIds2Ordered);

//Validate cache hits
verifyNoMoreInteractions(mockLocationRepository);

//Invoke cache again for same catchment and level type ids list
addressLevelCache.getAddressLevelsForCatchmentAndMatchingAddressLevelTypeIds(catchment1, matchingAddressLevelTypeIds2UnOrdered);

//Validate cache miss this time due to change in order of level type ids list
verify(mockLocationRepository).getCatchmentAddressesForCatchmentIdAndLocationTypeId(catchment1.getId(), matchingAddressLevelTypeIds2UnOrdered);

//Invoke cache again for same catchment and different level type ids list
addressLevelCache.getAddressLevelsForCatchmentAndMatchingAddressLevelTypeIds(catchment1, matchingAddressLevelTypeIds3);

//Validate cache miss this time due to change in level type ids list
verify(mockLocationRepository).getCatchmentAddressesForCatchmentIdAndLocationTypeId(catchment1.getId(), matchingAddressLevelTypeIds3);
}

@Test
public void givenAddressLevelCacheIsConfigured_whenCallGetCatchmentAddressesForCatchmentId_thenDataShouldBeInAddressPerCatchmentCache() {
//Fetch and cache
Expand Down

0 comments on commit ee067b2

Please sign in to comment.