Skip to content
Snippets Groups Projects
Commit 6817d0b3 authored by Sviatoslav Nekhaienko's avatar Sviatoslav Nekhaienko
Browse files

fix unit tests

parent 0fcb974b
Branches
Tags
1 merge request!60Indexer to read from Schema Service as well as Storage Schema
......@@ -42,18 +42,19 @@ public class SchemaToStorageFormatImplTest {
@Test
public void firstSchemaPassed() {
testSingleFile("converter/first/schema.json", "osdu:osdu:Wellbore:1.0.0");
testSingleFile("/converter/first/schema.json", "osdu:osdu:Wellbore:1.0.0");
}
@Test
public void wkeSchemaPassed() {
testSingleFile("converter/wks/slb_wke_wellbore.json", "slb:wks:wellbore:1.0.6");
testSingleFile("/converter/wks/slb_wke_wellbore.json", "slb:wks:wellbore:1.0.6");
}
@Test
public void folderPassed() throws URISyntaxException, IOException {
String folder = "converter/R3-json-schema";
Path path = Paths.get(ClassLoader.getSystemResource(folder).toURI());
String folder = "/converter/R3-json-schema";
Path path = Paths.get(this.getClass().getResource(folder).toURI());
Files.walk(path)
.filter(Files::isRegularFile)
.filter(f -> f.toString().endsWith(".json"))
......@@ -63,7 +64,6 @@ public class SchemaToStorageFormatImplTest {
private void testSingleFile(String filename, String kind) {
String json = getSchemaFromSchemaService(filename);
Map<String, Object> expected = getStorageSchema( filename + ".res");
Map<String, Object> converted = schemaToStorageFormatImpl.convertToMap(json, kind);
compareSchemas(expected, converted, filename);
......@@ -75,7 +75,7 @@ public class SchemaToStorageFormatImplTest {
= new TypeReference<Map<String, Object>>() {
};
try {
return objectMapper.readValue(ClassLoader.getSystemResource(s), typeRef);
return objectMapper.readValue(this.getClass().getResource(s), typeRef);
} catch (IOException | IllegalArgumentException e) {
fail("Failed to load schema from file:" + s);
}
......@@ -86,8 +86,8 @@ public class SchemaToStorageFormatImplTest {
private String getSchemaFromSchemaService(String s) {
try {
return new String(Files.readAllBytes(
Paths.get(ClassLoader.getSystemResource(s).toURI())), StandardCharsets.UTF_8);
} catch (IOException | URISyntaxException e) {
Paths.get(this.getClass().getResource(s).toURI())), StandardCharsets.UTF_8);
} catch (Throwable e) {
fail("Failed to read file:" + s);
}
return null;
......
......@@ -41,15 +41,15 @@ public class SchemaServiceImplTest {
private ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().build();
@Spy
private SchemaToStorageFormatImpl schemaToStorageFormat = new SchemaToStorageFormatImpl(objectMapper);
@Mock
private IUrlFetchService urlFetchService;
@Mock
private IRequestInfo requestInfo;
@Spy
private SchemaToStorageFormatImpl schemaToStorageFormat = new SchemaToStorageFormatImpl(objectMapper);
@InjectMocks
private SchemaServiceImpl sut;
......
......@@ -51,25 +51,26 @@ public class SchemaServiceTest {
@Test
public void should_returnValidResponse_givenValidKind_getSchemaByKind() throws Exception {
String validSchemaFromStorage = "{" +
" \"kind\": \"tenant:test:test:1.0.0\"," +
" \"schema\": [" +
" {" +
" \"path\": \"msg\"," +
" \"kind\": \"string\"" +
" }," +
" {" +
" \"path\": \"references.entity\"," +
" \"kind\": \"string\"" +
" }" +
" ]," +
" \"ext\": null" +
String validSchemaFromSchemaService = "{\n" +
" \"data\":{\n" +
" \"allOf\":[\n" +
" {\n" +
" \"type\":\"object\",\n" +
" \"properties\":{\n" +
" \"WellID\":{\n" +
" \"type\":\"string\",\n" +
" \"pattern\":\"^srn:<namespace>:master-data\\\\/Well:[^:]+:[0-9]*$\"\n" +
" }\n" +
" }\n" +
" }\n" +
" ]\n" +
" }\n" +
"}";
String kind = "tenant:test:test:1.0.0";
HttpResponse httpResponse = new HttpResponse();
httpResponse.setResponseCode(HttpStatus.OK.value());
httpResponse.setBody(validSchemaFromStorage);
httpResponse.setBody(validSchemaFromSchemaService);
when(this.urlFetchService.sendRequest(ArgumentMatchers.any())).thenReturn(httpResponse);
......
......@@ -14,17 +14,21 @@
package org.opengroup.osdu.indexer.service;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatchers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.opengroup.osdu.core.common.http.IUrlFetchService;
import org.opengroup.osdu.core.common.model.http.HttpResponse;
import org.opengroup.osdu.core.common.provider.interfaces.IRequestInfo;
import org.opengroup.osdu.indexer.schema.converter.SchemaToStorageFormatImpl;
import org.opengroup.osdu.indexer.service.impl.SchemaServiceImpl;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.HashMap;
......@@ -36,6 +40,11 @@ import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(SpringRunner.class)
public class SchemaServiceTest {
private ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().build();
@Spy
private SchemaToStorageFormatImpl schemaToStorageFormat = new SchemaToStorageFormatImpl(objectMapper);
@Mock
private IUrlFetchService urlFetchService;
@Mock
......@@ -51,25 +60,26 @@ public class SchemaServiceTest {
@Test
public void should_returnValidResponse_givenValidKind_getSchemaByKind() throws Exception {
String validSchemaFromStorage = "{" +
" \"kind\": \"tenant:test:test:1.0.0\"," +
" \"schema\": [" +
" {" +
" \"path\": \"msg\"," +
" \"kind\": \"string\"" +
" }," +
" {" +
" \"path\": \"references.entity\"," +
" \"kind\": \"string\"" +
" }" +
" ]," +
" \"ext\": null" +
String validSchemaFromSchemaService = "{\n" +
" \"data\":{\n" +
" \"allOf\":[\n" +
" {\n" +
" \"type\":\"object\",\n" +
" \"properties\":{\n" +
" \"WellID\":{\n" +
" \"type\":\"string\",\n" +
" \"pattern\":\"^srn:<namespace>:master-data\\\\/Well:[^:]+:[0-9]*$\"\n" +
" }\n" +
" }\n" +
" }\n" +
" ]\n" +
" }\n" +
"}";
String kind = "tenant:test:test:1.0.0";
HttpResponse httpResponse = new HttpResponse();
httpResponse.setResponseCode(HttpStatus.OK.value());
httpResponse.setBody(validSchemaFromStorage);
httpResponse.setBody(validSchemaFromSchemaService);
when(this.urlFetchService.sendRequest(ArgumentMatchers.any())).thenReturn(httpResponse);
......
......@@ -50,25 +50,26 @@ public class SchemaServiceTest {
@Test
public void should_returnValidResponse_givenValidKind_getSchemaByKind() throws Exception {
String validSchemaFromStorage = "{" +
" \"kind\": \"tenant:test:test:1.0.0\"," +
" \"schema\": [" +
" {" +
" \"path\": \"msg\"," +
" \"kind\": \"string\"" +
" }," +
" {" +
" \"path\": \"references.entity\"," +
" \"kind\": \"string\"" +
" }" +
" ]," +
" \"ext\": null" +
String validSchemaFromSchemaService = "{\n" +
" \"data\":{\n" +
" \"allOf\":[\n" +
" {\n" +
" \"type\":\"object\",\n" +
" \"properties\":{\n" +
" \"WellID\":{\n" +
" \"type\":\"string\",\n" +
" \"pattern\":\"^srn:<namespace>:master-data\\\\/Well:[^:]+:[0-9]*$\"\n" +
" }\n" +
" }\n" +
" }\n" +
" ]\n" +
" }\n" +
"}";
String kind = "tenant:test:test:1.0.0";
HttpResponse httpResponse = new HttpResponse();
httpResponse.setResponseCode(HttpStatus.OK.value());
httpResponse.setBody(validSchemaFromStorage);
httpResponse.setBody(validSchemaFromSchemaService);
when(this.urlFetchService.sendRequest(ArgumentMatchers.any())).thenReturn(httpResponse);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment