|
| 1 | +package dev.sunbirdrc.registry.service.impl; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import com.fasterxml.jackson.databind.JsonNode; |
| 5 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 6 | +import dev.sunbirdrc.pojos.AuditInfo; |
| 7 | +import dev.sunbirdrc.pojos.AuditRecord; |
| 8 | +import dev.sunbirdrc.registry.exception.AuditFailedException; |
| 9 | +import dev.sunbirdrc.registry.helper.SignatureHelper; |
| 10 | +import dev.sunbirdrc.registry.middleware.util.Constants; |
| 11 | +import dev.sunbirdrc.registry.service.IAuditService; |
| 12 | +import dev.sunbirdrc.registry.sink.shard.Shard; |
| 13 | +import dev.sunbirdrc.registry.util.Definition; |
| 14 | +import dev.sunbirdrc.registry.util.IDefinitionsManager; |
| 15 | +import dev.sunbirdrc.registry.util.OSSystemFieldsHelper; |
| 16 | +import org.junit.Before; |
| 17 | +import org.junit.Test; |
| 18 | +import org.junit.runner.RunWith; |
| 19 | +import org.mockito.InjectMocks; |
| 20 | +import org.mockito.Mock; |
| 21 | +import org.mockito.MockitoAnnotations; |
| 22 | +import org.springframework.beans.factory.annotation.Value; |
| 23 | +import org.springframework.boot.test.context.SpringBootTest; |
| 24 | +import org.springframework.test.context.ActiveProfiles; |
| 25 | +import org.springframework.test.context.junit4.SpringRunner; |
| 26 | + |
| 27 | +import java.io.IOException; |
| 28 | +import java.lang.reflect.Field; |
| 29 | +import java.util.List; |
| 30 | +import static org.junit.jupiter.api.Assertions.*; |
| 31 | +import static org.mockito.Mockito.*; |
| 32 | + |
| 33 | +@RunWith(SpringRunner.class) |
| 34 | +@SpringBootTest(classes = {ObjectMapper.class}) |
| 35 | +@ActiveProfiles(Constants.TEST_ENVIRONMENT) |
| 36 | +public class AuditServiceImplTest { |
| 37 | + |
| 38 | + @Value("${audit.enabled}") |
| 39 | + private boolean auditEnabled; |
| 40 | + |
| 41 | + @Value("${audit.frame.store}") |
| 42 | + private String auditFrameStore; |
| 43 | + |
| 44 | + @Value("${audit.frame.suffix}") |
| 45 | + private String auditSuffix; |
| 46 | + |
| 47 | + @Value("${audit.frame.suffixSeparator}") |
| 48 | + private String auditSuffixSeparator; |
| 49 | + |
| 50 | + @Value("${audit.vc-enabled:false}") |
| 51 | + private boolean auditVCEnabled; |
| 52 | + |
| 53 | + @Mock |
| 54 | + private IDefinitionsManager definitionsManager; |
| 55 | + |
| 56 | + @Mock |
| 57 | + private OSSystemFieldsHelper systemFieldsHelper; |
| 58 | + |
| 59 | + @Mock |
| 60 | + private AuditProviderFactory auditProviderFactory; |
| 61 | + |
| 62 | + @Mock |
| 63 | + private SignatureHelper signatureHelper; |
| 64 | + |
| 65 | + @Mock |
| 66 | + private ObjectMapper objectMapper; |
| 67 | + |
| 68 | + @InjectMocks |
| 69 | + private AuditServiceImpl auditService; |
| 70 | + |
| 71 | + @Before |
| 72 | + public void setUp() throws Exception { |
| 73 | + MockitoAnnotations.initMocks(this); |
| 74 | + setField(auditService, "auditEnabled", auditEnabled); |
| 75 | + setField(auditService, "auditFrameStore", auditFrameStore); |
| 76 | + setField(auditService, "auditSuffix", auditSuffix); |
| 77 | + setField(auditService, "auditSuffixSeparator", auditSuffixSeparator); |
| 78 | + setField(auditService, "auditVCEnabled", auditVCEnabled); |
| 79 | + } |
| 80 | + |
| 81 | + private void setField(Object targetObject, String fieldName, Object value) throws Exception { |
| 82 | + Field field = targetObject.getClass().getDeclaredField(fieldName); |
| 83 | + field.setAccessible(true); |
| 84 | + field.set(targetObject, value); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void shouldAudit_ReturnsTrueWhenFileAuditEnabled() throws Exception { |
| 89 | + setField(auditService, "auditFrameStore", Constants.FILE); |
| 90 | + boolean result = auditService.shouldAudit("EntityType"); |
| 91 | + assertTrue(result); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void shouldAudit_ReturnsTrueWhenDBAuditEnabledAndDefinitionNotNull() throws Exception { |
| 96 | + when(definitionsManager.getDefinition(anyString())).thenReturn(mock(Definition.class)); |
| 97 | + setField(auditService, "auditFrameStore", Constants.DATABASE); |
| 98 | + boolean result = auditService.shouldAudit("EntityType"); |
| 99 | + assertTrue(result); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void shouldAudit_ReturnsFalseWhenNoAuditingEnabled() throws Exception { |
| 104 | + setField(auditService, "auditEnabled", false); |
| 105 | + boolean result = auditService.shouldAudit("EntityType"); |
| 106 | + assertFalse(result); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void isAuditAction_ReturnsAuditActionForMatchingSuffix() { |
| 111 | + String entityType = auditSuffix; |
| 112 | + String action = auditService.isAuditAction(entityType); |
| 113 | + assertEquals(Constants.AUDIT_ACTION_AUDIT, action); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void isAuditAction_ReturnsSearchActionForNonMatchingSuffix() { |
| 118 | + String entityType = "NonMatchingEntity"; |
| 119 | + String action = auditService.isAuditAction(entityType); |
| 120 | + assertEquals(Constants.AUDIT_ACTION_SEARCH, action); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void createAuditInfo_ReturnsAuditInfoList() { |
| 125 | + String auditAction = "AuditAction"; |
| 126 | + String entityType = "EntityType"; |
| 127 | + List<AuditInfo> auditInfoList = auditService.createAuditInfo(auditAction, entityType); |
| 128 | + |
| 129 | + // Then |
| 130 | + assertEquals(1, auditInfoList.size()); |
| 131 | + assertEquals(auditAction, auditInfoList.get(0).getOp()); |
| 132 | + assertEquals("/" + entityType, auditInfoList.get(0).getPath()); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void convertAuditRecordToJson_ReturnsJsonNode() throws IOException { |
| 137 | + // Given |
| 138 | + AuditRecord auditRecord = new AuditRecord(); |
| 139 | + JsonNode inputNode = mock(JsonNode.class); |
| 140 | + Shard shard = mock(Shard.class); |
| 141 | + String vertexLabel = "VertexLabel"; |
| 142 | + when(objectMapper.writeValueAsString(any())).thenReturn("{}"); |
| 143 | + |
| 144 | + // When |
| 145 | + JsonNode result = auditService.convertAuditRecordToJson(auditRecord, vertexLabel); |
| 146 | + |
| 147 | + // Then |
| 148 | + assertNotNull(result); |
| 149 | + assertTrue(result.has(vertexLabel)); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + public void createAuditInfoWithJson_ReturnsAuditInfoListFromJson() throws JsonProcessingException { |
| 154 | + // Given |
| 155 | + String auditAction = "AuditAction"; |
| 156 | + JsonNode differenceJson = mock(JsonNode.class); |
| 157 | + String entityType = "EntityType"; |
| 158 | + when(objectMapper.treeToValue(any(JsonNode.class), eq(AuditInfo[].class))) |
| 159 | + .thenReturn(new AuditInfo[]{new AuditInfo()}); |
| 160 | + |
| 161 | + // When |
| 162 | + List<AuditInfo> auditInfoList = auditService.createAuditInfoWithJson(auditAction, differenceJson, entityType); |
| 163 | + |
| 164 | + // Then |
| 165 | + assertNotNull(auditInfoList); |
| 166 | + assertEquals(1, auditInfoList.size()); |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + public void testDoAudit() throws AuditFailedException { |
| 171 | + // Given |
| 172 | + AuditRecord auditRecord = mock(AuditRecord.class); |
| 173 | + JsonNode inputNode = mock(JsonNode.class); |
| 174 | + Shard shard = mock(Shard.class); |
| 175 | + IAuditService auditProvider = mock(IAuditService.class); |
| 176 | + |
| 177 | + when(auditProviderFactory.getAuditService(anyString())).thenReturn(auditProvider); |
| 178 | + |
| 179 | + // When |
| 180 | + auditService.doAudit(auditRecord, inputNode, shard); |
| 181 | + |
| 182 | + // Then |
| 183 | + verify(auditProvider, times(1)).doAudit(auditRecord, inputNode, shard); |
| 184 | + } |
| 185 | +} |
| 186 | + |
0 commit comments