Skip to content

Commit 3100322

Browse files
authored
Merge pull request #332 from holashchand/issue-988
[ISSUE-988]: Unit tests for registry and credential services to increase coverage
2 parents f161c8f + 8d6ff86 commit 3100322

51 files changed

Lines changed: 3827 additions & 2204 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

java/registry/pom.xml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<janusgraph.version>0.3.1</janusgraph.version>
2626
<maven.compiler.source>1.8</maven.compiler.source>
2727
<maven.compiler.target>1.8</maven.compiler.target>
28-
<jacoco.version>0.8.1</jacoco.version>
28+
<jacoco.version>0.8.11</jacoco.version>
2929
<!-- this version is in sync with elastic-search.jar which here added as dependency-->
3030
<elasticsearch.version>6.6.0</elasticsearch.version>
3131
<revision>2.0.3</revision>
@@ -466,7 +466,7 @@
466466
<plugin>
467467
<groupId>org.apache.maven.plugins</groupId>
468468
<artifactId>maven-surefire-plugin</artifactId>
469-
<version>3.0.0-M5</version>
469+
<version>3.3.0</version>
470470
</plugin>
471471
</plugins>
472472
</pluginManagement>
@@ -570,17 +570,21 @@
570570
<excludes>
571571
<exclude>dev/sunbirdrc/pojos/*</exclude>
572572
<exclude>**/I*.java</exclude>
573+
<exclude>org/drools/**/*</exclude>
573574
</excludes>
574575
</configuration>
575576
</plugin>
576577
<plugin>
577578
<groupId>org.apache.maven.plugins</groupId>
578579
<artifactId>maven-surefire-plugin</artifactId>
580+
<version>3.3.0</version>
579581
<configuration>
582+
<parallel>methods</parallel>
583+
<useUnlimitedThreads/>
580584
<includes>
581585
<include>${runSuite}</include>
582586
</includes>
583-
<forkCount>0</forkCount>
587+
<forkCount>1</forkCount>
584588
</configuration>
585589
</plugin>
586590
</plugins>

java/registry/src/main/java/dev/sunbirdrc/registry/service/SchemaService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ private void ensureCredentialSchema(String title, Object credentialTemplate, Str
177177
if(!signatureEnabled || !Objects.equals(signatureProvider, SignatureV2ServiceImpl.class.getName())) {
178178
return;
179179
}
180+
if(credentialTemplate == null || credentialTemplate == "") return;
180181
try {
181182
credentialSchemaService.ensureCredentialSchema(title, credentialTemplate, status);
182183
} catch (Exception e) {

java/registry/src/main/resources/application.yml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -474,12 +474,12 @@ signature:
474474
# suffixSeperator : specifies the separator used between entity name and suffix. If audit schema name is Teacher_Audit.json, the suffixSeperator is _.
475475

476476
audit:
477-
enabled: ${audit_enabled:false}
478-
vc-enabled: ${audit_vc_enabled:false}
477+
enabled: true
478+
vc-enabled: true
479479
frame:
480-
store: ${audit_frame_store:DATABASE}
481-
suffix: ${audit_suffix:Audit}
482-
suffixSeparator: ${audit_suffixSeparator:_}
480+
store: DATABASE
481+
suffix: Audit
482+
suffixSeparator: _
483483

484484
authentication:
485485
enabled: ${authentication_enabled:true}
@@ -546,3 +546,9 @@ elastic:
546546
# elastic-search connection info
547547
connection_url: ${elastic_search_connection_url:localhost:9200}
548548

549+
idgen:
550+
enabled: true
551+
tenantId: default
552+
healthCheckURL: http://localhost:8088/egov-idgen/health
553+
generateURL: http://localhost:8088/egov-idgen/id/_generate
554+
idFormatURL: http://localhost:8088/egov-idgen/id/_format/add
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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

Comments
 (0)