Skip to content

Commit 0787172

Browse files
committed
Added unit tests for idgen service
1 parent 053ea6f commit 0787172

2 files changed

Lines changed: 174 additions & 0 deletions

File tree

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
package dev.sunbirdrc.registry.service.impl;
2+
import com.fasterxml.jackson.databind.ObjectMapper;
3+
import com.google.gson.Gson;
4+
import dev.sunbirdrc.pojos.ComponentHealthInfo;
5+
import dev.sunbirdrc.pojos.SunbirdRCInstrumentation;
6+
import dev.sunbirdrc.pojos.UniqueIdentifierField;
7+
import dev.sunbirdrc.registry.exception.CustomException;
8+
import dev.sunbirdrc.registry.exception.UniqueIdentifierException.GenerateException;
9+
import dev.sunbirdrc.registry.exception.UniqueIdentifierException.IdFormatException;
10+
import dev.sunbirdrc.registry.exception.UniqueIdentifierException.UnreachableException;
11+
import dev.sunbirdrc.registry.middleware.util.Constants;
12+
import dev.sunbirdrc.registry.middleware.util.JSONUtil;
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.MockitoAnnotations;
19+
import org.springframework.beans.factory.annotation.Autowired;
20+
import org.springframework.beans.factory.annotation.Value;
21+
import org.springframework.boot.test.context.SpringBootTest;
22+
import org.springframework.http.*;
23+
import org.springframework.test.context.ActiveProfiles;
24+
import org.springframework.test.context.junit4.SpringRunner;
25+
import org.springframework.web.client.ResourceAccessException;
26+
27+
import java.io.IOException;
28+
import java.lang.reflect.Field;
29+
import java.util.*;
30+
import static org.junit.Assert.*;
31+
import static org.mockito.ArgumentMatchers.*;
32+
import static org.mockito.Mockito.*;
33+
34+
@RunWith(SpringRunner.class)
35+
@SpringBootTest(classes = {ObjectMapper.class})
36+
@ActiveProfiles(Constants.TEST_ENVIRONMENT)
37+
public class IdGenServiceTest {
38+
39+
@Value("${idgen.generateURL}")
40+
private String generateUrl;
41+
42+
@Value("${idgen.idFormatURL}")
43+
private String idFormatUrl;
44+
45+
@Value("${idgen.healthCheckURL}")
46+
private String healthCheckUrl;
47+
48+
@Value("${idgen.tenantId}")
49+
private String tenantId;
50+
51+
@Value("${idgen.enabled}")
52+
private boolean enabled;
53+
54+
@Mock
55+
private Gson gson;
56+
57+
@Mock
58+
private SunbirdRCInstrumentation watch;
59+
60+
@Mock
61+
private RetryRestTemplate retryRestTemplate;
62+
63+
@InjectMocks
64+
private IdGenService idGenService;
65+
66+
@Autowired
67+
private ObjectMapper objectMapper;
68+
69+
@Before
70+
public void setUp() throws Exception {
71+
MockitoAnnotations.initMocks(this);
72+
73+
// Manually set the injected values in the mock object
74+
setField(idGenService, "generateUrl", generateUrl);
75+
setField(idGenService, "idFormatUrl", idFormatUrl);
76+
setField(idGenService, "healthCheckUrl", healthCheckUrl);
77+
setField(idGenService, "tenantId", tenantId);
78+
setField(idGenService, "enabled", enabled);
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 testGenerateIdSuccessful() throws CustomException, IOException {
89+
List<UniqueIdentifierField> fields = new ArrayList<>();
90+
UniqueIdentifierField field1 = new UniqueIdentifierField();
91+
field1.setField("field1");
92+
fields.add(field1);
93+
94+
HttpEntity<String> entity = new HttpEntity<>("request");
95+
96+
when(gson.toJson(anyMap())).thenReturn("request");
97+
when(retryRestTemplate.postForEntity(eq(generateUrl), any(HttpEntity.class))).thenReturn(new ResponseEntity<>("{\"responseInfo\":{\"status\":\"SUCCESSFUL\"},\"idResponses\":[{\"id\":\"1234\"}]}", HttpStatus.OK));
98+
99+
Map<String, String> result = idGenService.generateId(fields);
100+
101+
assertNotNull(result);
102+
assertEquals("1234", result.get("field1"));
103+
}
104+
105+
@Test(expected = GenerateException.class)
106+
public void testGenerateIdFailure() throws CustomException, IOException {
107+
List<UniqueIdentifierField> fields = new ArrayList<>();
108+
109+
HttpEntity<String> entity = new HttpEntity<>("request");
110+
111+
when(gson.toJson(anyMap())).thenReturn("request");
112+
when(retryRestTemplate.postForEntity(any(), eq(entity))).thenReturn(new ResponseEntity<>("{\"responseInfo\":{\"status\":\"FAILED\"}}", HttpStatus.OK));
113+
114+
idGenService.generateId(fields);
115+
}
116+
117+
@Test(expected = UnreachableException.class)
118+
public void testGenerateIdResourceAccessException() throws CustomException {
119+
List<UniqueIdentifierField> fields = new ArrayList<>();
120+
when(gson.toJson(anyMap())).thenReturn("request");
121+
when(retryRestTemplate.postForEntity(eq(generateUrl), any(HttpEntity.class))).thenThrow(new ResourceAccessException("Exception"));
122+
123+
idGenService.generateId(fields);
124+
}
125+
126+
@Test
127+
public void testSaveIdFormatSuccessful() throws CustomException, IOException {
128+
List<UniqueIdentifierField> fields = new ArrayList<>();
129+
130+
HttpEntity<String> entity = new HttpEntity<>("request");
131+
132+
when(gson.toJson(anyMap())).thenReturn("request");
133+
when(retryRestTemplate.postForEntity(eq(idFormatUrl), any(HttpEntity.class))).thenReturn(new ResponseEntity<>("{\"responseInfo\":{\"status\":\"SUCCESSFUL\"}}", HttpStatus.OK));
134+
idGenService.saveIdFormat(fields);
135+
}
136+
137+
@Test(expected = GenerateException.class)
138+
public void testSaveIdFormatFailure() throws CustomException, IOException {
139+
List<UniqueIdentifierField> fields = new ArrayList<>();
140+
when(gson.toJson(anyMap())).thenReturn("request");
141+
when(retryRestTemplate.postForEntity(eq(idFormatUrl), any(HttpEntity.class))).thenReturn(new ResponseEntity<>("{\"responseInfo\":{\"status\":\"FAILED\"},\"errorMsgs\":[\"Some error\"]}", HttpStatus.OK));
142+
idGenService.saveIdFormat(fields);
143+
}
144+
145+
@Test(expected = UnreachableException.class)
146+
public void testSaveIdFormatResourceAccessException() throws CustomException {
147+
List<UniqueIdentifierField> fields = new ArrayList<>();
148+
when(gson.toJson(anyMap())).thenReturn("request");
149+
when(retryRestTemplate.postForEntity(eq(idFormatUrl), any(HttpEntity.class))).thenThrow(new ResourceAccessException("Exception"));
150+
idGenService.saveIdFormat(fields);
151+
}
152+
153+
@Test
154+
public void testGetHealthInfoWhenHealthy() throws IOException {
155+
when(retryRestTemplate.getForEntity(any())).thenReturn(new ResponseEntity<>("{\"status\":\"UP\"}", HttpStatus.OK));
156+
ComponentHealthInfo healthInfo = idGenService.getHealthInfo();
157+
assertNotNull(healthInfo);
158+
assertTrue(healthInfo.isHealthy());
159+
}
160+
161+
@Test
162+
public void testGetHealthInfoWhenUnhealthy() throws IOException {
163+
when(retryRestTemplate.getForEntity(any())).thenReturn(new ResponseEntity<>("{\"status\":\"DOWN\"}", HttpStatus.OK));
164+
ComponentHealthInfo healthInfo = idGenService.getHealthInfo();
165+
assertNotNull(healthInfo);
166+
assertFalse(healthInfo.isHealthy());
167+
}
168+
}

0 commit comments

Comments
 (0)