From 9e12f7d41fb0a3e87efca12c113429716565d230 Mon Sep 17 00:00:00 2001
From: Sviatoslav Nekhaienko <snekhaienko@slb.com>
Date: Wed, 9 Dec 2020 13:34:13 +0200
Subject: [PATCH] added more unit tests

---
 .../service/impl/SchemaServiceImpl.java       |  6 +-
 .../azure/service/SchemaServiceTest.java      | 60 +++++++++++++++++--
 2 files changed, 58 insertions(+), 8 deletions(-)

diff --git a/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/impl/SchemaServiceImpl.java b/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/impl/SchemaServiceImpl.java
index 0bd1ed4e9..469bf2043 100644
--- a/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/impl/SchemaServiceImpl.java
+++ b/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/impl/SchemaServiceImpl.java
@@ -68,7 +68,7 @@ public class SchemaServiceImpl implements SchemaService {
         return getFromSchemaService(kind);
     }
 
-    private String getFromSchemaService(String kind) throws UnsupportedEncodingException, URISyntaxException {
+    public String getFromSchemaService(String kind) throws UnsupportedEncodingException, URISyntaxException {
         HttpResponse response = getSchemaServiceResponse(kind);
 
         if (response.getResponseCode() == HttpStatus.SC_NOT_FOUND) {
@@ -80,7 +80,7 @@ public class SchemaServiceImpl implements SchemaService {
                 schemaToStorageFormat.convertToString(response.getBody(), kind);
     }
 
-    private String getFromStorageService(String kind) throws URISyntaxException, UnsupportedEncodingException {
+    public String getFromStorageService(String kind) throws URISyntaxException, UnsupportedEncodingException {
         String schemaFromStorageService = storageService.getStorageSchema(kind);
 
         if (schemaFromStorageService != null) {
@@ -92,7 +92,7 @@ public class SchemaServiceImpl implements SchemaService {
         return null;
     }
 
-    private HttpResponse getSchemaServiceResponse(String kind) throws UnsupportedEncodingException, URISyntaxException {
+    protected HttpResponse getSchemaServiceResponse(String kind) throws UnsupportedEncodingException, URISyntaxException {
         String url = String.format("%s/%s", SCHEMA_HOST, URLEncoder.encode(kind, StandardCharsets.UTF_8.toString()));
         FetchServiceHttpRequest request = FetchServiceHttpRequest.builder()
                 .httpMethod(HttpMethods.GET)
diff --git a/provider/indexer-azure/src/test/java/org/opengroup/osdu/indexer/azure/service/SchemaServiceTest.java b/provider/indexer-azure/src/test/java/org/opengroup/osdu/indexer/azure/service/SchemaServiceTest.java
index 6c74d6be9..6f9011461 100644
--- a/provider/indexer-azure/src/test/java/org/opengroup/osdu/indexer/azure/service/SchemaServiceTest.java
+++ b/provider/indexer-azure/src/test/java/org/opengroup/osdu/indexer/azure/service/SchemaServiceTest.java
@@ -14,6 +14,7 @@
 
 package org.opengroup.osdu.indexer.azure.service;
 
+import com.fasterxml.jackson.core.type.TypeReference;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import org.junit.Before;
 import org.junit.Test;
@@ -32,9 +33,11 @@ import org.springframework.test.context.junit4.SpringRunner;
 
 import javax.inject.Inject;
 import java.util.HashMap;
+import java.util.Map;
 
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
+import static org.junit.Assert.*;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.*;
 import static org.powermock.api.mockito.PowerMockito.when;
 
 @RunWith(SpringRunner.class)
@@ -51,6 +54,7 @@ public class SchemaServiceTest {
     private IRequestInfo requestInfo;
     @Mock
     private StorageService storageService;
+
     @InjectMocks
     private SchemaServiceImpl sut;
 
@@ -60,9 +64,10 @@ public class SchemaServiceTest {
     }
 
     @Test
-    public void should_returnValidResponse_givenValidKind_getSchemaByKind() throws Exception {
+    public void should_returnValidResponse_givenValidSchema() throws Exception {
 
         String validSchemaFromSchemaService = "{\n" +
+                "\"properties\": {" +
                 "   \"data\":{\n" +
                 "      \"allOf\":[\n" +
                 "         {\n" +
@@ -76,6 +81,7 @@ public class SchemaServiceTest {
                 "         }\n" +
                 "      ]\n" +
                 "   }\n" +
+                "   }\n" +
                 "}";
         String kind = "tenant:test:test:1.0.0";
 
@@ -83,10 +89,16 @@ public class SchemaServiceTest {
         httpResponse.setResponseCode(HttpStatus.OK.value());
         httpResponse.setBody(validSchemaFromSchemaService);
 
-        when(this.urlFetchService.sendRequest(ArgumentMatchers.any())).thenReturn(httpResponse);
+        when(this.urlFetchService.sendRequest(any())).thenReturn(httpResponse);
 
         String recordSchemaResponse = this.sut.getSchema(kind);
 
+        Map<String, Object> result = objectMapper.readValue(recordSchemaResponse,
+                new TypeReference<Map<String,Object>>(){});
+        assertEquals("Schema must have two root items", 2, result.size());
+        assertEquals("Wrong kind", "tenant:test:test:1.0.0", result.get("kind"));
+        assertEquals("Wrong schema attributes", "[{path=WellID, kind=link}]", result.get("schema").toString());
+
         assertNotNull(recordSchemaResponse);
     }
 
@@ -98,11 +110,49 @@ public class SchemaServiceTest {
         HttpResponse httpResponse = new HttpResponse();
         httpResponse.setResponseCode(HttpStatus.NOT_FOUND.value());
 
-        when(this.urlFetchService.sendRequest(ArgumentMatchers.any())).thenReturn(httpResponse);
+        when(this.urlFetchService.sendRequest(any())).thenReturn(httpResponse);
 
         String recordSchemaResponse = this.sut.getSchema(kind);
 
         assertNull(recordSchemaResponse);
     }
 
+    @Test
+    public void should_call_Storage_then_Schema() throws Exception {
+        String kind = "tenant:test:test:1.0.0";
+
+        SchemaServiceImpl schemaService = Mockito.mock(SchemaServiceImpl.class);
+        when(schemaService.getSchema(any())).thenCallRealMethod();
+
+        InOrder inOrder = inOrder(schemaService);
+
+        String recordSchemaResponse = schemaService.getSchema(kind);
+        assertNull(recordSchemaResponse);
+
+        inOrder.verify(schemaService).getSchema(any());
+        inOrder.verify(schemaService).getFromStorageService(any());
+        inOrder.verify(schemaService).getFromSchemaService(any());
+        verify(schemaService, times(1)).getFromStorageService(any());
+        verify(schemaService, times(1)).getFromSchemaService(any());
+    }
+
+    @Test
+    public void should_call_only_Storage_if_it_returns_result() throws Exception {
+        String kind = "tenant:test:test:1.0.0";
+
+        SchemaServiceImpl schemaService = Mockito.mock(SchemaServiceImpl.class);
+        when(schemaService.getSchema(any())).thenCallRealMethod();
+        String someSchema = "some schema";
+        when(schemaService.getFromStorageService(any())).thenReturn(someSchema);
+
+        InOrder inOrder = inOrder(schemaService);
+
+        String recordSchemaResponse = schemaService.getSchema(kind);
+        assertEquals(recordSchemaResponse, someSchema);
+
+        inOrder.verify(schemaService).getSchema(any());
+        inOrder.verify(schemaService).getFromStorageService(any());
+        verify(schemaService, times(0)).getFromSchemaService(any());
+        verify(schemaService, times(1)).getFromStorageService(any());
+    }
 }
-- 
GitLab