Skip to content

Commit

Permalink
avniproject/avni-webapp#1217 | Add integration test for creation of r…
Browse files Browse the repository at this point in the history
…eset sync records for applicable users when location parent is updated
  • Loading branch information
1t5j0y committed Jun 11, 2024
1 parent 803f0f5 commit 7c3b824
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.avni.server.service;

import org.avni.server.application.projections.BaseProjection;
import org.avni.server.application.projections.LocationProjection;
import org.avni.server.application.projections.VirtualCatchmentProjection;
import org.avni.server.dao.*;
import org.avni.server.domain.*;
Expand All @@ -17,6 +16,7 @@
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -67,15 +67,14 @@ public void recordLocationParentChange(AddressLevel addressLevel, Long oldParent
List<AddressLevel> allChildLocations = locationRepository.getAllChildLocations(lquery);
List<Long> allChildLocationIds = allChildLocations.stream().map(AddressLevel::getId).collect(Collectors.toList());
if (individualRepository.hasSubjectsInLocations(allChildLocationIds)) {
List<LocationProjection> parentLocations = locationRepository.getParents(addressLevel.getParentUuid());
List<LocationProjection> oldParentParentLocations = locationRepository.getParents(locationRepository.findById(oldParentId).get().getUuid());
parentLocations.addAll(oldParentParentLocations);
List<VirtualCatchmentProjection> virtualCatchmentsForAddressLevelIds = locationRepository.getVirtualCatchmentsForAddressLevelIds(parentLocations.stream().map(BaseProjection::getId).collect(Collectors.toList()));
List<Long> addressLevelIdsToBeChecked = new ArrayList<>(Collections.singletonList(addressLevel.getId()));
addressLevelIdsToBeChecked.addAll(locationRepository.getParents(addressLevel.getParentUuid()).stream().map(BaseProjection::getId).collect(Collectors.toList()));
addressLevelIdsToBeChecked.addAll(locationRepository.getParents(locationRepository.findById(oldParentId).get().getUuid()).stream().map(BaseProjection::getId).collect(Collectors.toList()));
List<Long> virtualCatchmentIdsForAddressLevelIds = locationRepository.getVirtualCatchmentsForAddressLevelIds(addressLevelIdsToBeChecked).stream()
.map(VirtualCatchmentProjection::getCatchment_id)
.collect(Collectors.toList());
List<ResetSync> resetSyncRecords = new ArrayList<>();
userRepository.findByCatchment_IdInAndIsVoidedFalse(virtualCatchmentsForAddressLevelIds
.stream()
.map(VirtualCatchmentProjection::getCatchment_id)
.collect(Collectors.toList()))
userRepository.findByCatchment_IdInAndIsVoidedFalse(virtualCatchmentIdsForAddressLevelIds)
.forEach(user -> {
ResetSync resetSync = buildNewResetSync();
resetSync.setUser(user);
Expand Down Expand Up @@ -133,7 +132,7 @@ private boolean isSyncConcept1Changed(UserSyncSettings olderSettings, UserSyncSe

private boolean isSyncConcept2Changed(UserSyncSettings olderSettings, UserSyncSettings newSettings) {
return isChanged(olderSettings.getSyncConcept2(), newSettings.getSyncConcept2()) ||
isConceptValueChanged(olderSettings.getSyncConcept2Values(), newSettings.getSyncConcept2Values());
isConceptValueChanged(olderSettings.getSyncConcept2Values(), newSettings.getSyncConcept2Values());
}

private boolean isCatchmentChanged(List<Long> savedLocationIds, List<Long> locationIdsPassedInRequest) {
Expand All @@ -146,7 +145,7 @@ private boolean hasSubjectsInNewLocation(List<Long> savedLocationIds, List<Long>
List<Long> removedIds = new ArrayList<>(savedLocationIds);
removedIds.removeAll(locationIdsPassedInRequest);
return individualRepository.hasSubjectsInLocations(newlyAddedIds) ||
individualRepository.hasSubjectsInLocations(removedIds);
individualRepository.hasSubjectsInLocations(removedIds);
}

private ResetSync buildNewResetSync() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.avni.server.service.sync;

import org.avni.server.common.AbstractControllerIntegrationTest;
import org.avni.server.dao.LocationRepository;
import org.avni.server.dao.UserRepository;
import org.avni.server.domain.AddressLevel;
import org.avni.server.domain.ResetSync;
import org.avni.server.service.ResetSyncService;
import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.test.context.jdbc.Sql;

import static org.junit.Assert.assertEquals;

@Sql({"/test-data.sql"})
public class ResetSyncServiceIntegrationTest extends AbstractControllerIntegrationTest {
@Autowired
private ResetSyncService resetSyncService;
@Autowired
private LocationRepository locationRepository;
@Autowired
private UserRepository userRepository;

@Before
public void initialize() throws Exception {
super.setUp();
super.setUser(userRepository.findByUuid("8fecc62f-4b6b-4dd4-a27d-fa2587e59d04"));
}

@Test
public void shouldCreateResetSyncRecordForUserWhenCatchmentChangesDueToLocationParentChange() {
AddressLevel al = locationRepository.findByTitleIgnoreCase("GP1.Parent1");
Long oldParentId = al.getParentId();
AddressLevel newParent = locationRepository.findByTitleIgnoreCase("GP2");
al.setParent(newParent);
resetSyncService.recordLocationParentChange(al, oldParentId);
Page<ResetSync> resetSyncRecordsForAffectedUser = resetSyncService.getByLastModifiedForUser(DateTime.parse("2000-01-01").toDateTime(),
DateTime.now(),
userRepository.findByUsername("user-reset-sync-test1@demo"),
PageRequest.of(0, 1));
assertEquals (1, resetSyncRecordsForAffectedUser.getContent().size());
}

@Test
public void shouldNotCreateResetSyncRecordIfThereAreNoSubjectsInTheChangedLocation() {
AddressLevel al = locationRepository.findByTitleIgnoreCase("GP2.Parent2");
Long oldParentId = al.getParentId();
AddressLevel newParent = locationRepository.findByTitleIgnoreCase("GP1");
al.setParent(newParent);
resetSyncService.recordLocationParentChange(al, oldParentId);
Page<ResetSync> resetSyncRecordsForAffectedUser = resetSyncService.getByLastModifiedForUser(DateTime.parse("2000-01-01").toDateTime(),
DateTime.now(),
userRepository.findByUsername("user-reset-sync-test2@demo"),
PageRequest.of(0, 1));
assertEquals (0, resetSyncRecordsForAffectedUser.getContent().size());

}
}
1 change: 1 addition & 0 deletions avni-server-api/src/test/resources/tear-down.sql
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ delete from message_receiver where 1 = 1;
delete from message_rule where 1 = 1;
delete from identifier_user_assignment where 1 = 1;
delete from identifier_source where 1 = 1;
DELETE FROM reset_sync where 1 = 1;
DELETE FROM users where id <> 1;
DELETE FROM operational_subject_type where 1 = 1;
DELETE FROM subject_type where 1 = 1;
Expand Down
32 changes: 31 additions & 1 deletion avni-server-api/src/test/resources/test-data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ DELETE FROM organisation_config;
DELETE from message_request_queue;
DELETE from message_receiver;
DELETE from message_rule;
DELETE FROM reset_sync;
DELETE FROM users;
DELETE FROM subject_type;
DELETE FROM group_privilege;
Expand Down Expand Up @@ -245,9 +246,33 @@ INSERT INTO address_level (id, title, uuid, version, lineage, parent_id, created
VALUES (1, 'Nijhma', 'ae35fe6d-910e-47bd-a0c7-0c10182a4085', 1, '1', NULL, 1, 1, now(), now());
INSERT INTO address_level (id, title, uuid, version, lineage, parent_id, created_by_id, last_modified_by_id, created_date_time, last_modified_date_time)
VALUES (2, 'Naya Gaon', 'a62d5ff9-4480-44f8-ab9f-9fe12e2e1a91', 1, '2', NULL, 1, 1, now(), now());
INSERT INTO address_level (id, organisation_id, title, uuid, version, lineage, parent_id, created_by_id, last_modified_by_id, created_date_time, last_modified_date_time)
VALUES (3, 2, 'GP1', '04c419a4-4fbe-4d54-b4d2-8e669f0e47a4', 1, '3', NULL, 1, 1, now(), now());
INSERT INTO address_level (id, organisation_id, title, uuid, version, lineage, parent_id, created_by_id, last_modified_by_id, created_date_time, last_modified_date_time)
VALUES (4, 2, 'GP1.Parent1', '53de16f0-d8ba-40d0-9a75-7e1b9e8b1969', 1, '3.4', 3, 1, 1, now(), now());
INSERT INTO address_level (id, organisation_id, title, uuid, version, lineage, parent_id, created_by_id, last_modified_by_id, created_date_time, last_modified_date_time)
VALUES (5, 2, 'GP1.Parent1.Child1', 'eb012ab0-680b-4e2d-beda-bc000a50f4cd', 1, '3.4.5', 4, 1, 1, now(), now());
INSERT INTO address_level (id, organisation_id, title, uuid, version, lineage, parent_id, created_by_id, last_modified_by_id, created_date_time, last_modified_date_time)
VALUES (6, 2, 'GP2', '8108d1e6-7de8-4c0e-a2ce-8f3ff72d1540', 1, '6', NULL, 1, 1, now(), now());
INSERT INTO address_level (id, organisation_id, title, uuid, version, lineage, parent_id, created_by_id, last_modified_by_id, created_date_time, last_modified_date_time)
VALUES (7, 2, 'GP2.Parent2', '87d4037f-2d55-485e-8473-8474db421a74', 1, '6.7', 6, 1, 1, now(), now());
INSERT INTO address_level (id, organisation_id, title, uuid, version, lineage, parent_id, created_by_id, last_modified_by_id, created_date_time, last_modified_date_time)
VALUES (8, 2, 'GP2.Parent2.Child2', 'c26bf9e0-7368-490f-ba95-29c82b6a3978', 1, '6.7.8', 7, 1, 1, now(), now());

INSERT INTO catchment (id, name, uuid, version, organisation_id, type, created_by_id, last_modified_by_id, created_date_time, last_modified_date_time)
VALUES
(3, 'CatchmentZ', '640867e4-2374-4a26-b0b9-adebad2d86b8', 0, 2, 'TypeZ', 1, 1, now(), now());
INSERT INTO catchment (id, name, uuid, version, organisation_id, type, created_by_id, last_modified_by_id, created_date_time, last_modified_date_time)
VALUES
(4, 'CatchmentZ2', 'b9ea6198-6dd7-4891-ab17-c5697361f733', 0, 2, 'TypeZ', 1, 1, now(), now());

INSERT INTO catchment_address_mapping (catchment_id, addresslevel_id)
VALUES (1, 1), (1, 2), (2, 2);
VALUES (1, 1), (1, 2), (2, 2), (3, 4), (4, 6);

INSERT INTO users (id, username, uuid, organisation_id, operating_individual_scope, is_org_admin, catchment_id, name)
VALUES (7, 'user-reset-sync-test1@demo', '8fecc62f-4b6b-4dd4-a27d-fa2587e59d04', 2, 'ByCatchment', false, 3, 'user-reset-sync-test1');
INSERT INTO users (id, username, uuid, organisation_id, operating_individual_scope, is_org_admin, catchment_id, name)
VALUES (8, 'user-reset-sync-test2@demo', '8924b997-70c9-431c-9f40-be5c9700018f', 2, 'ByCatchment', false, 4, 'user-reset-sync-test2');

INSERT INTO individual (uuid, address_id, version, date_of_birth, date_of_birth_verified, first_name, last_name, gender_id, organisation_id,
subject_type_id, observations, created_by_id, last_modified_by_id, created_date_time, last_modified_date_time)
Expand Down Expand Up @@ -299,6 +324,11 @@ INSERT INTO individual (address_id, date_of_birth, date_of_birth_verified, first
VALUES (2, '1955-01-05', FALSE, 'Ram', 'Kumari', 1, 'bb312ece-5e2e-490f-ae1d-2896089da81e', 1,
(select id from subject_type where name = 'Individual'), 1, 1, now(), now());

INSERT INTO individual (uuid, address_id, version, date_of_birth, date_of_birth_verified, first_name, last_name, gender_id, organisation_id,
subject_type_id, observations, created_by_id, last_modified_by_id, created_date_time, last_modified_date_time)
VALUES ('74a37894-0cca-4104-811b-34d84ce8f06e', (select id from address_level where title = 'GP1.Parent1'), 1, current_timestamp, FALSE, 'Subject6', 'Subject6', 2, 2,
(select id from subject_type where name = 'Individual'), '{"7c39cb04-4f02-4c49-8f94-a5b697d40365": "9282738493"}'::jsonb, 1, 1, now(), now());

INSERT INTO form (NAME, form_type, uuid, version, created_by_id, last_modified_by_id, created_date_time, last_modified_date_time)
VALUES ('encounter_form', 'Encounter', '2c32a184-6d27-4c51-841d-551ca94594a5', 1, 1, 1, now(), now());

Expand Down

0 comments on commit 7c3b824

Please sign in to comment.