Skip to content

Commit

Permalink
Merge pull request #332 from holashchand/issue-988
Browse files Browse the repository at this point in the history
[ISSUE-988]: Unit tests for registry and credential services to increase coverage
  • Loading branch information
challabeehyv authored Jul 3, 2024
2 parents f161c8f + 8d6ff86 commit 3100322
Show file tree
Hide file tree
Showing 51 changed files with 3,827 additions and 2,204 deletions.
10 changes: 7 additions & 3 deletions java/registry/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<janusgraph.version>0.3.1</janusgraph.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<jacoco.version>0.8.1</jacoco.version>
<jacoco.version>0.8.11</jacoco.version>
<!-- this version is in sync with elastic-search.jar which here added as dependency-->
<elasticsearch.version>6.6.0</elasticsearch.version>
<revision>2.0.3</revision>
Expand Down Expand Up @@ -466,7 +466,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<version>3.3.0</version>
</plugin>
</plugins>
</pluginManagement>
Expand Down Expand Up @@ -570,17 +570,21 @@
<excludes>
<exclude>dev/sunbirdrc/pojos/*</exclude>
<exclude>**/I*.java</exclude>
<exclude>org/drools/**/*</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<parallel>methods</parallel>
<useUnlimitedThreads/>
<includes>
<include>${runSuite}</include>
</includes>
<forkCount>0</forkCount>
<forkCount>1</forkCount>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ private void ensureCredentialSchema(String title, Object credentialTemplate, Str
if(!signatureEnabled || !Objects.equals(signatureProvider, SignatureV2ServiceImpl.class.getName())) {
return;
}
if(credentialTemplate == null || credentialTemplate == "") return;
try {
credentialSchemaService.ensureCredentialSchema(title, credentialTemplate, status);
} catch (Exception e) {
Expand Down
16 changes: 11 additions & 5 deletions java/registry/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -474,12 +474,12 @@ signature:
# suffixSeperator : specifies the separator used between entity name and suffix. If audit schema name is Teacher_Audit.json, the suffixSeperator is _.

audit:
enabled: ${audit_enabled:false}
vc-enabled: ${audit_vc_enabled:false}
enabled: true
vc-enabled: true
frame:
store: ${audit_frame_store:DATABASE}
suffix: ${audit_suffix:Audit}
suffixSeparator: ${audit_suffixSeparator:_}
store: DATABASE
suffix: Audit
suffixSeparator: _

authentication:
enabled: ${authentication_enabled:true}
Expand Down Expand Up @@ -546,3 +546,9 @@ elastic:
# elastic-search connection info
connection_url: ${elastic_search_connection_url:localhost:9200}

idgen:
enabled: true
tenantId: default
healthCheckURL: http://localhost:8088/egov-idgen/health
generateURL: http://localhost:8088/egov-idgen/id/_generate
idFormatURL: http://localhost:8088/egov-idgen/id/_format/add
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
package dev.sunbirdrc.registry.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import dev.sunbirdrc.pojos.AuditInfo;
import dev.sunbirdrc.pojos.AuditRecord;
import dev.sunbirdrc.registry.exception.AuditFailedException;
import dev.sunbirdrc.registry.helper.SignatureHelper;
import dev.sunbirdrc.registry.middleware.util.Constants;
import dev.sunbirdrc.registry.service.IAuditService;
import dev.sunbirdrc.registry.sink.shard.Shard;
import dev.sunbirdrc.registry.util.Definition;
import dev.sunbirdrc.registry.util.IDefinitionsManager;
import dev.sunbirdrc.registry.util.OSSystemFieldsHelper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {ObjectMapper.class})
@ActiveProfiles(Constants.TEST_ENVIRONMENT)
public class AuditServiceImplTest {

@Value("${audit.enabled}")
private boolean auditEnabled;

@Value("${audit.frame.store}")
private String auditFrameStore;

@Value("${audit.frame.suffix}")
private String auditSuffix;

@Value("${audit.frame.suffixSeparator}")
private String auditSuffixSeparator;

@Value("${audit.vc-enabled:false}")
private boolean auditVCEnabled;

@Mock
private IDefinitionsManager definitionsManager;

@Mock
private OSSystemFieldsHelper systemFieldsHelper;

@Mock
private AuditProviderFactory auditProviderFactory;

@Mock
private SignatureHelper signatureHelper;

@Mock
private ObjectMapper objectMapper;

@InjectMocks
private AuditServiceImpl auditService;

@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
setField(auditService, "auditEnabled", auditEnabled);
setField(auditService, "auditFrameStore", auditFrameStore);
setField(auditService, "auditSuffix", auditSuffix);
setField(auditService, "auditSuffixSeparator", auditSuffixSeparator);
setField(auditService, "auditVCEnabled", auditVCEnabled);
}

private void setField(Object targetObject, String fieldName, Object value) throws Exception {
Field field = targetObject.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(targetObject, value);
}

@Test
public void shouldAudit_ReturnsTrueWhenFileAuditEnabled() throws Exception {
setField(auditService, "auditFrameStore", Constants.FILE);
boolean result = auditService.shouldAudit("EntityType");
assertTrue(result);
}

@Test
public void shouldAudit_ReturnsTrueWhenDBAuditEnabledAndDefinitionNotNull() throws Exception {
when(definitionsManager.getDefinition(anyString())).thenReturn(mock(Definition.class));
setField(auditService, "auditFrameStore", Constants.DATABASE);
boolean result = auditService.shouldAudit("EntityType");
assertTrue(result);
}

@Test
public void shouldAudit_ReturnsFalseWhenNoAuditingEnabled() throws Exception {
setField(auditService, "auditEnabled", false);
boolean result = auditService.shouldAudit("EntityType");
assertFalse(result);
}

@Test
public void isAuditAction_ReturnsAuditActionForMatchingSuffix() {
String entityType = auditSuffix;
String action = auditService.isAuditAction(entityType);
assertEquals(Constants.AUDIT_ACTION_AUDIT, action);
}

@Test
public void isAuditAction_ReturnsSearchActionForNonMatchingSuffix() {
String entityType = "NonMatchingEntity";
String action = auditService.isAuditAction(entityType);
assertEquals(Constants.AUDIT_ACTION_SEARCH, action);
}

@Test
public void createAuditInfo_ReturnsAuditInfoList() {
String auditAction = "AuditAction";
String entityType = "EntityType";
List<AuditInfo> auditInfoList = auditService.createAuditInfo(auditAction, entityType);

// Then
assertEquals(1, auditInfoList.size());
assertEquals(auditAction, auditInfoList.get(0).getOp());
assertEquals("/" + entityType, auditInfoList.get(0).getPath());
}

@Test
public void convertAuditRecordToJson_ReturnsJsonNode() throws IOException {
// Given
AuditRecord auditRecord = new AuditRecord();
JsonNode inputNode = mock(JsonNode.class);
Shard shard = mock(Shard.class);
String vertexLabel = "VertexLabel";
when(objectMapper.writeValueAsString(any())).thenReturn("{}");

// When
JsonNode result = auditService.convertAuditRecordToJson(auditRecord, vertexLabel);

// Then
assertNotNull(result);
assertTrue(result.has(vertexLabel));
}

@Test
public void createAuditInfoWithJson_ReturnsAuditInfoListFromJson() throws JsonProcessingException {
// Given
String auditAction = "AuditAction";
JsonNode differenceJson = mock(JsonNode.class);
String entityType = "EntityType";
when(objectMapper.treeToValue(any(JsonNode.class), eq(AuditInfo[].class)))
.thenReturn(new AuditInfo[]{new AuditInfo()});

// When
List<AuditInfo> auditInfoList = auditService.createAuditInfoWithJson(auditAction, differenceJson, entityType);

// Then
assertNotNull(auditInfoList);
assertEquals(1, auditInfoList.size());
}

@Test
public void testDoAudit() throws AuditFailedException {
// Given
AuditRecord auditRecord = mock(AuditRecord.class);
JsonNode inputNode = mock(JsonNode.class);
Shard shard = mock(Shard.class);
IAuditService auditProvider = mock(IAuditService.class);

when(auditProviderFactory.getAuditService(anyString())).thenReturn(auditProvider);

// When
auditService.doAudit(auditRecord, inputNode, shard);

// Then
verify(auditProvider, times(1)).doAudit(auditRecord, inputNode, shard);
}
}

Loading

0 comments on commit 3100322

Please sign in to comment.