Skip to content

Commit 54eceb4

Browse files
committed
read call home schema url from property file
1 parent b7dfe57 commit 54eceb4

File tree

4 files changed

+19
-45
lines changed

4 files changed

+19
-45
lines changed

src/main/java/uk/ac/ebi/eva/submission/service/CallHomeService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public CallHomeService(CallHomeEventRepository callHomeEventRepository, SchemaDo
3636

3737
public boolean validateJson(JsonNode jsonPayload) {
3838
String latestTag = schemaDownloader.getLatestTag(SchemaDownloader.TAG_URL);
39-
String schemaURLWithLatestTag = SchemaDownloader.SCHEMA_URL.replace("{tag}", latestTag);
39+
String schemaURLWithLatestTag = schemaDownloader.getCallhomeSchemaURL().replace("{tag}", latestTag);
4040
String schemaContent = schemaDownloader.loadSchemaFromGitHub(schemaURLWithLatestTag);
4141
Schema schema = schemaRegistry.getSchema(schemaContent, InputFormat.JSON);
4242
List<com.networknt.schema.Error> errorList = schema.validate(jsonPayload.toString(), InputFormat.JSON,

src/main/java/uk/ac/ebi/eva/submission/util/SchemaDownloader.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package uk.ac.ebi.eva.submission.util;
22

33
import com.fasterxml.jackson.databind.JsonNode;
4+
import org.springframework.beans.factory.annotation.Value;
45
import org.springframework.cache.annotation.CacheEvict;
56
import org.springframework.cache.annotation.Cacheable;
67
import org.springframework.retry.annotation.Backoff;
@@ -12,7 +13,9 @@
1213
@Component
1314
public class SchemaDownloader {
1415
public static String TAG_URL = "https://api.github.com/repos/EBIvariation/eva-sub-cli/tags";
15-
public static String SCHEMA_URL = "https://raw.githubusercontent.com/EBIvariation/eva-sub-cli/{tag}/eva_sub_cli/etc/call_home_payload_schema.json";
16+
17+
@Value("${callhome.schema.url}")
18+
private String callhomeSchemaURL;
1619

1720
private final RestTemplate restTemplate;
1821

@@ -41,4 +44,7 @@ public void evictLatestTagCache() {}
4144
@Scheduled(fixedRate = 48 * 60 * 60 * 1000)
4245
public void evictSchemaCache() {}
4346

47+
public String getCallhomeSchemaURL() {
48+
return callhomeSchemaURL;
49+
}
4450
}

src/main/resources/application.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ server.servlet.context-path=/eva/webservices/submission-ws
3333

3434
management.metrics.binders.jvm.enabled=false
3535

36+
callhome.schema.url=|callhome.schema-url|
3637
spring.cache.type=simple

src/test/java/uk/ac/ebi/eva/submission/integration/CallHomeWSIntegrationTest.java

Lines changed: 10 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,19 @@
22

33
import com.fasterxml.jackson.databind.JsonNode;
44
import com.fasterxml.jackson.databind.ObjectMapper;
5-
import com.fasterxml.jackson.databind.node.ArrayNode;
65
import com.fasterxml.jackson.databind.node.ObjectNode;
7-
import org.junit.jupiter.api.BeforeAll;
86
import org.junit.jupiter.api.BeforeEach;
97
import org.junit.jupiter.api.Test;
108
import org.springframework.beans.factory.annotation.Autowired;
119
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
1210
import org.springframework.boot.test.context.SpringBootTest;
1311
import org.springframework.boot.test.mock.mockito.MockBean;
12+
import org.springframework.boot.test.mock.mockito.SpyBean;
1413
import org.springframework.http.MediaType;
1514
import org.springframework.test.context.DynamicPropertyRegistry;
1615
import org.springframework.test.context.DynamicPropertySource;
1716
import org.springframework.test.web.servlet.MockMvc;
1817
import org.springframework.transaction.annotation.Transactional;
19-
import org.springframework.web.client.RestTemplate;
2018
import org.testcontainers.containers.PostgreSQLContainer;
2119
import org.testcontainers.junit.jupiter.Container;
2220
import org.testcontainers.junit.jupiter.Testcontainers;
@@ -26,20 +24,13 @@
2624
import uk.ac.ebi.eva.submission.service.GlobusTokenRefreshService;
2725
import uk.ac.ebi.eva.submission.util.SchemaDownloader;
2826

29-
import java.io.BufferedReader;
30-
import java.io.IOException;
31-
import java.io.InputStream;
32-
import java.io.InputStreamReader;
33-
import java.net.URL;
34-
import java.nio.charset.StandardCharsets;
3527
import java.time.ZonedDateTime;
3628
import java.util.List;
3729
import java.util.stream.Collectors;
3830
import java.util.stream.StreamSupport;
3931

4032
import static org.assertj.core.api.Assertions.assertThat;
41-
import static org.mockito.ArgumentMatchers.eq;
42-
import static org.mockito.Mockito.when;
33+
import static org.mockito.Mockito.doThrow;
4334
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
4435
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
4536
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@@ -48,9 +39,12 @@
4839
@AutoConfigureMockMvc
4940
@Testcontainers
5041
public class CallHomeWSIntegrationTest {
42+
private static String callhomeSchemaURL = "https://raw.githubusercontent.com/EBIvariation/eva-sub-cli/main/eva_sub_cli/etc/call_home_payload_schema.json";
43+
5144
@Autowired
5245
private CallHomeEventRepository callHomeEventRepository;
5346

47+
@SpyBean
5448
@Autowired
5549
private SchemaDownloader schemaDownloader;
5650

@@ -63,34 +57,9 @@ public class CallHomeWSIntegrationTest {
6357
@Autowired
6458
private MockMvc mvc;
6559

66-
@MockBean
67-
private static RestTemplate restTemplate;
68-
69-
private static String mockVersion = "v1.0.0";
70-
private static String mockSchemaUrl = SchemaDownloader.SCHEMA_URL.replace("{tag}", mockVersion);
71-
private static String schema = "";
72-
73-
@BeforeAll
74-
public static void downloadSchema() throws IOException {
75-
// get schema from main
76-
String schemaURL = "https://raw.githubusercontent.com/EBIvariation/eva-sub-cli/main/eva_sub_cli/etc/call_home_payload_schema.json";
77-
try (InputStream in = new URL(schemaURL).openStream();
78-
BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))) {
79-
schema = reader.lines().collect(Collectors.joining("\n"));
80-
}
81-
}
82-
8360
@BeforeEach
84-
public void setup() throws IOException {
61+
public void setup() {
8562
schemaDownloader.evictSchemaCache();
86-
87-
// mock tag
88-
ObjectMapper mapper = new ObjectMapper();
89-
ArrayNode arrayNode = mapper.createArrayNode();
90-
ObjectNode tagNode = mapper.createObjectNode();
91-
tagNode.put("name", mockVersion);
92-
arrayNode.add(tagNode);
93-
when(restTemplate.getForObject(eq(SchemaDownloader.TAG_URL), eq(JsonNode.class))).thenReturn(arrayNode);
9463
}
9564

9665
@Container
@@ -108,15 +77,14 @@ static void dataSourceProperties(DynamicPropertyRegistry registry) {
10877
registry.add("eva.email.server", () -> "test-email-server");
10978
registry.add("eva.email.port", () -> 1025);
11079
registry.add("eva.helpdesk.email", () -> "test-helpdesk-email");
80+
registry.add("callhome.schema.url", () -> callhomeSchemaURL);
11181
}
11282

11383

11484
@Test
11585
@Transactional
11686
public void testRegisterCallHomeEvent() throws Exception {
117-
when(restTemplate.getForObject(eq(mockSchemaUrl), eq(String.class))).thenReturn(schema);
11887
ObjectMapper mapper = new ObjectMapper();
119-
12088
ObjectNode callHomeJsonRootNode = getCallHomeJson(mapper);
12189

12290
mvc.perform(post("/v1/call-home/events")
@@ -151,9 +119,7 @@ public void testRegisterCallHomeEvent() throws Exception {
151119
@Test
152120
@Transactional
153121
public void testRegisterCallHomeEvent_BadRequestAsFieldIsMissing() throws Exception {
154-
when(restTemplate.getForObject(eq(mockSchemaUrl), eq(String.class))).thenReturn(schema);
155122
ObjectMapper mapper = new ObjectMapper();
156-
157123
ObjectNode callHomeJsonRootNode = getCallHomeJson(mapper);
158124
callHomeJsonRootNode.putNull("eventType");
159125

@@ -174,9 +140,10 @@ public void testRegisterCallHomeEvent_BadRequestAsFieldIsMissing() throws Except
174140
@Test
175141
@Transactional
176142
public void testRegisterCallHomeEvent_InternalServerError() throws Exception {
177-
when(restTemplate.getForObject(eq(mockSchemaUrl), eq(String.class))).thenReturn(null);
178-
ObjectMapper mapper = new ObjectMapper();
143+
doThrow(new RuntimeException("Exception while downloading schema"))
144+
.when(schemaDownloader).loadSchemaFromGitHub(callhomeSchemaURL);
179145

146+
ObjectMapper mapper = new ObjectMapper();
180147
ObjectNode callHomeJsonRootNode = getCallHomeJson(mapper);
181148

182149
mvc.perform(post("/v1/call-home/events")

0 commit comments

Comments
 (0)