|
| 1 | +package io.mosip.registration.processor.stages.packetclassifier.tagging.impl; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertNull; |
| 5 | +import static org.junit.Assert.assertTrue; |
| 6 | +import static org.mockito.ArgumentMatchers.any; |
| 7 | +import static org.mockito.ArgumentMatchers.anyString; |
| 8 | + |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Map; |
| 12 | + |
| 13 | +import org.junit.Before; |
| 14 | +import org.junit.Test; |
| 15 | +import org.junit.runner.RunWith; |
| 16 | +import org.mockito.InjectMocks; |
| 17 | +import org.mockito.Mock; |
| 18 | +import org.mockito.Mockito; |
| 19 | +import org.powermock.core.classloader.annotations.PowerMockIgnore; |
| 20 | +import org.powermock.modules.junit4.PowerMockRunner; |
| 21 | +import org.powermock.reflect.Whitebox; |
| 22 | +import org.springframework.cloud.context.config.annotation.RefreshScope; |
| 23 | + |
| 24 | +import io.mosip.registration.processor.core.anonymous.dto.AnonymousProfileDTO; |
| 25 | +import io.mosip.registration.processor.core.util.JsonUtil; |
| 26 | +import io.mosip.registration.processor.packet.storage.utils.PriorityBasedPacketManagerService; |
| 27 | +import io.mosip.registration.processor.status.dto.SyncRegistrationDto; |
| 28 | +import io.mosip.registration.processor.status.dto.SyncResponseDto; |
| 29 | +import io.mosip.registration.processor.status.entity.SyncRegistrationEntity; |
| 30 | +import io.mosip.registration.processor.status.service.AnonymousProfileService; |
| 31 | +import io.mosip.registration.processor.status.service.SyncRegistrationService; |
| 32 | + |
| 33 | +/** |
| 34 | + * The Class AnonymousProfileTagGeneratorTest. |
| 35 | + */ |
| 36 | +@RefreshScope |
| 37 | +@RunWith(PowerMockRunner.class) |
| 38 | +@PowerMockIgnore({ "javax.management.*", "javax.net.ssl.*", "com.sun.org.apache.xerces.*", |
| 39 | + "javax.xml.*", "org.xml.*" }) |
| 40 | +public class AnonymousProfileTagGeneratorTest { |
| 41 | + |
| 42 | + private static final String TAG_NAME = "anonymous"; |
| 43 | + |
| 44 | + private static final String WORKFLOW_INSTANCE_ID = "8e34c5d5-2ba1-4d69-9e60-31b0a1d1c1d0"; |
| 45 | + |
| 46 | + private static final String REGISTRATION_ID = "10001100010000120260824"; |
| 47 | + |
| 48 | + /** |
| 49 | + * The profile as AnonymousProfileServiceImpl builds it: supervisorId comes from |
| 50 | + * the packet operationsData, the decision and comment are not in the packet at |
| 51 | + * all and start out null. |
| 52 | + */ |
| 53 | + private static final String PROFILE_JSON = "{\"processName\":\"NEW\",\"status\":\"PROCESSING\"," |
| 54 | + + "\"assisted\":[\"110024\",\"SUP001\"],\"supervisorId\":\"SUP001\"," |
| 55 | + + "\"supervisorDecision\":null,\"supervisorComment\":null}"; |
| 56 | + |
| 57 | + @InjectMocks |
| 58 | + private AnonymousProfileTagGenerator anonymousProfileTagGenerator; |
| 59 | + |
| 60 | + @Mock |
| 61 | + private AnonymousProfileService anonymousProfileService; |
| 62 | + |
| 63 | + @Mock |
| 64 | + private PriorityBasedPacketManagerService priorityBasedPacketManagerService; |
| 65 | + |
| 66 | + @Mock |
| 67 | + private SyncRegistrationService<SyncResponseDto, SyncRegistrationDto> syncRegistrationService; |
| 68 | + |
| 69 | + @Before |
| 70 | + public void setup() throws Exception { |
| 71 | + Whitebox.setInternalState(anonymousProfileTagGenerator, "tagName", TAG_NAME); |
| 72 | + Mockito.when(anonymousProfileService.buildJsonStringFromPacketInfo(any(), any(), any(), any(), anyString(), |
| 73 | + anyString())).thenReturn(PROFILE_JSON); |
| 74 | + } |
| 75 | + |
| 76 | + private Map<String, String> generateTags() throws Exception { |
| 77 | + return anonymousProfileTagGenerator.generateTags(WORKFLOW_INSTANCE_ID, REGISTRATION_ID, "NEW", |
| 78 | + new HashMap<>(), null, 0); |
| 79 | + } |
| 80 | + |
| 81 | + private AnonymousProfileDTO taggedProfile(Map<String, String> tags) throws Exception { |
| 82 | + return JsonUtil.readValueWithUnknownProperties(tags.get(TAG_NAME), AnonymousProfileDTO.class); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void supervisorDecisionAndCommentAreAddedFromRegistrationListTest() throws Exception { |
| 87 | + SyncRegistrationEntity syncRegistrationEntity = new SyncRegistrationEntity(); |
| 88 | + syncRegistrationEntity.setSupervisorStatus("APPROVED"); |
| 89 | + syncRegistrationEntity.setSupervisorComment("Verified by supervisor"); |
| 90 | + Mockito.when(syncRegistrationService.findByWorkflowInstanceId(WORKFLOW_INSTANCE_ID)) |
| 91 | + .thenReturn(syncRegistrationEntity); |
| 92 | + |
| 93 | + AnonymousProfileDTO profile = taggedProfile(generateTags()); |
| 94 | + |
| 95 | + assertEquals("APPROVED", profile.getSupervisorDecision()); |
| 96 | + assertEquals("Verified by supervisor", profile.getSupervisorComment()); |
| 97 | + // the packet-sourced field must survive the enrichment round trip |
| 98 | + assertEquals("SUP001", profile.getSupervisorId()); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void rejectedDecisionIsCarriedThroughTest() throws Exception { |
| 103 | + SyncRegistrationEntity syncRegistrationEntity = new SyncRegistrationEntity(); |
| 104 | + syncRegistrationEntity.setSupervisorStatus("REJECTED"); |
| 105 | + syncRegistrationEntity.setSupervisorComment("Poor biometric quality"); |
| 106 | + Mockito.when(syncRegistrationService.findByWorkflowInstanceId(anyString())) |
| 107 | + .thenReturn(syncRegistrationEntity); |
| 108 | + |
| 109 | + AnonymousProfileDTO profile = taggedProfile(generateTags()); |
| 110 | + |
| 111 | + assertEquals("REJECTED", profile.getSupervisorDecision()); |
| 112 | + assertEquals("Poor biometric quality", profile.getSupervisorComment()); |
| 113 | + } |
| 114 | + |
| 115 | + /** A packet with no registration_list record must still get its profile tagged. */ |
| 116 | + @Test |
| 117 | + public void profileIsStillTaggedWhenSyncRecordIsMissingTest() throws Exception { |
| 118 | + Mockito.when(syncRegistrationService.findByWorkflowInstanceId(anyString())).thenReturn(null); |
| 119 | + |
| 120 | + Map<String, String> tags = generateTags(); |
| 121 | + |
| 122 | + assertEquals(PROFILE_JSON, tags.get(TAG_NAME)); |
| 123 | + assertNull(taggedProfile(tags).getSupervisorDecision()); |
| 124 | + assertNull(taggedProfile(tags).getSupervisorComment()); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * The lookup is only attempted once a profile exists, and a build failure still |
| 129 | + * leaves classification unblocked with no tag - the workflow manager then falls |
| 130 | + * back to building the profile from the packet. |
| 131 | + */ |
| 132 | + @Test |
| 133 | + public void noTagAndNoLookupWhenProfileBuildFailsTest() throws Exception { |
| 134 | + Mockito.when(anonymousProfileService.buildJsonStringFromPacketInfo(any(), any(), any(), any(), anyString(), |
| 135 | + anyString())).thenThrow(new RuntimeException("profile build failed")); |
| 136 | + |
| 137 | + assertTrue(generateTags().isEmpty()); |
| 138 | + Mockito.verify(syncRegistrationService, Mockito.never()).findByWorkflowInstanceId(anyString()); |
| 139 | + } |
| 140 | + |
| 141 | + /** A registration_list read failure must not break packet classification. */ |
| 142 | + @Test |
| 143 | + public void classificationContinuesWhenSupervisorLookupFailsTest() throws Exception { |
| 144 | + Mockito.when(syncRegistrationService.findByWorkflowInstanceId(anyString())) |
| 145 | + .thenThrow(new RuntimeException("registration_list unavailable")); |
| 146 | + |
| 147 | + assertTrue(generateTags().isEmpty()); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + public void getRequiredIdObjectFieldNamesTest() throws Exception { |
| 152 | + List<String> result = anonymousProfileTagGenerator.getRequiredIdObjectFieldNames(); |
| 153 | + assertTrue(result.isEmpty()); |
| 154 | + } |
| 155 | + |
| 156 | +} |
0 commit comments