Commit c8c7ac91 authored by harshit aggarwal's avatar harshit aggarwal
Browse files

Merge branch 'haaggarw-ut' into 'master'

Adding Unit Tests for Core Module

See merge request !187
parents 461da0fe d591d0cf
Pipeline #106726 failed with stages
in 4 minutes and 22 seconds
......@@ -205,6 +205,33 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.3</version>
<configuration>
<excludes>
<exclude>**/*org/opengroup/osdu/dataset/model/**</exclude>
<exclude>**/*org/opengroup/osdu/dataset/di/**</exclude>
<exclude>**/*org/opengroup/osdu/dataset/swagger/**</exclude>
<exclude>**/*org/opengroup/osdu/dataset/DatasetApplication.class</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
......@@ -65,7 +65,7 @@ public class DatasetRegistryServiceImpl implements DatasetRegistryService {
*/
final String DATASET_KIND_REGEX = "^[\\w\\-\\.]+:[\\w\\-\\.]+:dataset--+[\\w\\-\\.]+:[0-9]+.[0-9]+.[0-9]+$";
private final HttpResponseBodyMapper bodyMapper;
private HttpResponseBodyMapper bodyMapper;
@Inject
private DpsHeaders headers;
......
/*
* Copyright 2021 Google LLC
* Copyright 2021 EPAM Systems, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.opengroup.osdu.dataset.dms;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.dataset.di.DatasetConfig;
@RunWith(MockitoJUnitRunner.class)
public class DmsFactoryTest {
@Mock
private DatasetConfig datasetConfig;
@Mock
private DpsHeaders headers;
@Mock
private DmsServiceProperties dmsServiceProperties;
@InjectMocks
private DmsFactory dmsFactory;
@Test
public void create_success_return_DmsRestService_Object() {
when(datasetConfig.isUseRestDms()).thenReturn(true);
IDmsProvider dmsRestService = dmsFactory.create(headers, dmsServiceProperties);
assertNotNull(dmsRestService);
assertTrue(dmsRestService instanceof DmsRestService);
verify(datasetConfig, times(1)).isUseRestDms();
}
@Test
public void create_success_return_DmsService_Object() {
when(datasetConfig.isUseRestDms()).thenReturn(false);
IDmsProvider dmsRestService = dmsFactory.create(headers, dmsServiceProperties);
assertNotNull(dmsRestService);
assertTrue(dmsRestService instanceof DmsService);
verify(datasetConfig, times(1)).isUseRestDms();
}
@Test
public void create_failure_NullPointerException() {
try {
dmsFactory.create(null, dmsServiceProperties);
}
catch (Exception exception) {
assertNotNull(exception);
assertTrue(exception instanceof NullPointerException);
assertEquals("headers cannot be null", exception.getMessage());
}
verify(datasetConfig, times(0)).isUseRestDms();
}
}
/*
* Copyright 2021 Google LLC
* Copyright 2021 EPAM Systems, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.opengroup.osdu.dataset.dms;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.opengroup.osdu.core.common.dms.model.CopyDmsRequest;
import org.opengroup.osdu.core.common.dms.model.CopyDmsResponse;
import org.opengroup.osdu.core.common.dms.model.RetrievalInstructionsResponse;
import org.opengroup.osdu.core.common.http.HttpRequest;
import org.opengroup.osdu.core.common.http.HttpResponse;
import org.opengroup.osdu.core.common.http.IHttpClient;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.dataset.model.request.GetDatasetRegistryRequest;
import org.opengroup.osdu.dataset.model.response.GetDatasetStorageInstructionsResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@RunWith(MockitoJUnitRunner.class)
public class DmsRestServiceTest {
private final String URL = "https://contoso.com";
private final String INVALID_URL = "http://finance.yahoo.com/q/h?s=^IXIC";
private final String GetDatasetStorageInstructionsResponse_body = "{\"providerKey\":\"dummy-key\",\"storageLocation\":{\"key1\":{},\"key2\":{}}}";
private final String RetrievalInstructionsResponse_body = "{\"providerKey\":\"dummy-key\",\"datasets\":[{\"datasetRegistryId\":\"dummyid\",\"retrievalProperties\":{\"key1\":{}}}]}";
private final String CopyDmsResponse_body = "[{\"success\": true, \"datasetBlobStoragePath\": \"string\"}]";
@Mock
private DmsServiceProperties dmsServiceProperties;
@Mock
private IHttpClient httpClient;
@Mock
private Map<String, String> headersMap;
@Mock
private HttpResponse response;
@Mock
private DpsHeaders headers;
@InjectMocks
private DmsRestService dmsRestService;
@Before
public void init() {
when(headers.getHeaders()).thenReturn(headersMap);
when(httpClient.send(any())).thenReturn(response);
}
@Test
public void getStorageInstructions_success() {
when(response.getBody()).thenReturn(GetDatasetStorageInstructionsResponse_body);
when(dmsServiceProperties.getDmsServiceBaseUrl()).thenReturn(URL);
GetDatasetStorageInstructionsResponse body = dmsRestService.getStorageInstructions();
assertNotNull(body);
verify(headers, times(1)).getHeaders();
verify(httpClient, times(1)).send(any(HttpRequest.class));
verify(dmsServiceProperties, times(1)).getDmsServiceBaseUrl();
}
@Test
public void getStorageInstructions_URISyntaxException() {
when(dmsServiceProperties.getDmsServiceBaseUrl()).thenReturn(INVALID_URL);
try {
dmsRestService.getStorageInstructions();
}
catch (Exception e) {
assertNotNull(e);
assertTrue(e instanceof AppException);
assertEquals("Invalid URL", ((AppException) e).getError().getReason());
assertEquals(500, ((AppException) e).getError().getCode());
}
verify(dmsServiceProperties, times(1)).getDmsServiceBaseUrl();
verify(httpClient, times(0)).send(any(HttpRequest.class));
}
@Test
public void getStorageInstructions_JsonProcessingException() {
when(response.getBody()).thenReturn("");
when(dmsServiceProperties.getDmsServiceBaseUrl()).thenReturn(URL);
try {
dmsRestService.getStorageInstructions();
}
catch (Exception e) {
assertNotNull(e);
assertTrue(e instanceof AppException);
assertEquals("Internal Server Error", ((AppException) e).getError().getReason());
assertEquals(500, ((AppException) e).getError().getCode());
}
verify(dmsServiceProperties, times(1)).getDmsServiceBaseUrl();
verify(httpClient, times(1)).send(any(HttpRequest.class));
verify(headers, times(1)).getHeaders();
}
@Test
public void getRetrievalInstructions_success() {
when(response.getBody()).thenReturn(RetrievalInstructionsResponse_body);
when(dmsServiceProperties.getDmsServiceBaseUrl()).thenReturn(URL);
GetDatasetRegistryRequest getDatasetRegistryRequest = new GetDatasetRegistryRequest();
getDatasetRegistryRequest.datasetRegistryIds = Arrays.asList("sup1", "sup2", "sup3");
RetrievalInstructionsResponse body = dmsRestService.getRetrievalInstructions(getDatasetRegistryRequest);
assertNotNull(body);
verify(headers, times(1)).getHeaders();
verify(httpClient, times(1)).send(any(HttpRequest.class));
verify(dmsServiceProperties, times(1)).getDmsServiceBaseUrl();
}
@Test
public void getRetrievalInstructions_URISyntaxException() {
when(dmsServiceProperties.getDmsServiceBaseUrl()).thenReturn(INVALID_URL);
GetDatasetRegistryRequest getDatasetRegistryRequest = new GetDatasetRegistryRequest();
getDatasetRegistryRequest.datasetRegistryIds = Arrays.asList("sup1", "sup2", "sup3");
try {
dmsRestService.getRetrievalInstructions(getDatasetRegistryRequest);
}
catch (Exception e) {
assertNotNull(e);
assertTrue(e instanceof AppException);
assertEquals("Invalid URL", ((AppException) e).getError().getReason());
assertEquals(500, ((AppException) e).getError().getCode());
}
verify(dmsServiceProperties, times(1)).getDmsServiceBaseUrl();
verify(httpClient, times(0)).send(any(HttpRequest.class));
}
@Test
public void getRetrievalInstructions_JsonProcessingException() {
when(response.getBody()).thenReturn("");
when(dmsServiceProperties.getDmsServiceBaseUrl()).thenReturn(URL);
GetDatasetRegistryRequest getDatasetRegistryRequest = new GetDatasetRegistryRequest();
getDatasetRegistryRequest.datasetRegistryIds = Arrays.asList("sup1", "sup2", "sup3");
try {
dmsRestService.getRetrievalInstructions(getDatasetRegistryRequest);
}
catch (Exception e) {
assertNotNull(e);
assertTrue(e instanceof AppException);
assertEquals("Internal Server Error", ((AppException) e).getError().getReason());
assertEquals(500, ((AppException) e).getError().getCode());
}
verify(dmsServiceProperties, times(1)).getDmsServiceBaseUrl();
verify(httpClient, times(1)).send(any(HttpRequest.class));
verify(headers, times(1)).getHeaders();
}
@Test
public void copyDmsToPersistentStorage_success() {
when(response.getBody()).thenReturn(CopyDmsResponse_body);
when(dmsServiceProperties.getDmsServiceBaseUrl()).thenReturn(URL);
List<CopyDmsResponse> data = dmsRestService.copyDmsToPersistentStorage(new CopyDmsRequest());
assertNotNull(data);
verify(headers, times(1)).getHeaders();
verify(httpClient, times(1)).send(any(HttpRequest.class));
verify(dmsServiceProperties, times(1)).getDmsServiceBaseUrl();
}
@Test
public void copyDmsToPersistentStorage_URISyntaxException() {
when(dmsServiceProperties.getDmsServiceBaseUrl()).thenReturn(INVALID_URL);
try {
dmsRestService.copyDmsToPersistentStorage(new CopyDmsRequest());
}
catch (Exception e) {
assertNotNull(e);
assertTrue(e instanceof AppException);
assertEquals("Invalid URL", ((AppException) e).getError().getReason());
assertEquals(500, ((AppException) e).getError().getCode());
}
verify(dmsServiceProperties, times(1)).getDmsServiceBaseUrl();
verify(httpClient, times(0)).send(any(HttpRequest.class));
}
@Test
public void copyDmsToPersistentStorage_JsonProcessingException() {
when(response.getBody()).thenReturn("");
when(dmsServiceProperties.getDmsServiceBaseUrl()).thenReturn(URL);
try {
dmsRestService.copyDmsToPersistentStorage(new CopyDmsRequest());
}
catch (Exception e) {
assertNotNull(e);
assertTrue(e instanceof AppException);
assertEquals("Internal Server Error", ((AppException) e).getError().getReason());
assertEquals(500, ((AppException) e).getError().getCode());
}
verify(dmsServiceProperties, times(1)).getDmsServiceBaseUrl();
verify(httpClient, times(1)).send(any(HttpRequest.class));
verify(headers, times(1)).getHeaders();
}
}
/*
* Copyright 2021 Google LLC
* Copyright 2021 EPAM Systems, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.opengroup.osdu.dataset.dms;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class DmsServicePropertiesTest {
private final String dmsServiceBaseUrl = "url";
private final boolean allowStorage = true;
private final String apiKey = "key";
private final boolean stagingLocationSupported = true;
@Test
public void should_successfully_create_four_args_constructor() {
DmsServiceProperties properties = new DmsServiceProperties(dmsServiceBaseUrl, allowStorage, apiKey, stagingLocationSupported);
assertNotNull(properties);
assertEquals(dmsServiceBaseUrl, properties.getDmsServiceBaseUrl());
assertEquals(allowStorage, properties.isAllowStorage());
assertEquals(apiKey, properties.getApiKey());
assertEquals(stagingLocationSupported, properties.isStagingLocationSupported());
}
@Test
public void should_successfully_create_one_args_constructor() {
DmsServiceProperties properties = new DmsServiceProperties(dmsServiceBaseUrl);
assertNotNull(properties);
assertEquals(dmsServiceBaseUrl, properties.getDmsServiceBaseUrl());
assertTrue(allowStorage);
assertNull(properties.getApiKey());
assertFalse(properties.isStagingLocationSupported());
}
@Test
public void should_successfully_create_two_args_constructor() {
DmsServiceProperties properties = new DmsServiceProperties(dmsServiceBaseUrl, allowStorage);
assertNotNull(properties);
assertEquals(dmsServiceBaseUrl, properties.getDmsServiceBaseUrl());
assertTrue(allowStorage);
assertNull(properties.getApiKey());
assertFalse(properties.isStagingLocationSupported());
}
@Test
public void should_successfully_create_no_args_constructor() {
DmsServiceProperties properties = new DmsServiceProperties();
properties.setAllowStorage(allowStorage);
properties.setApiKey(apiKey);
properties.setDmsServiceBaseUrl(dmsServiceBaseUrl);
properties.setStagingLocationSupported(stagingLocationSupported);
properties.equals(new DmsServiceProperties());
assertNotNull(properties);
assertEquals(dmsServiceBaseUrl, properties.getDmsServiceBaseUrl());
assertEquals(allowStorage, properties.isAllowStorage());
assertEquals(apiKey, properties.getApiKey());
assertEquals(stagingLocationSupported, properties.isStagingLocationSupported());
}
}
/*
* Copyright 2021 Google LLC
* Copyright 2021 EPAM Systems, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.opengroup.osdu.dataset.dms;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.opengroup.osdu.core.common.http.HttpRequest;
import org.opengroup.osdu.core.common.http.HttpResponse;
import org.opengroup.osdu.core.common.http.IHttpClient;
import org.opengroup.osdu.core.common.http.json.HttpResponseBodyParsingException;
import org.opengroup.osdu.core.common.model.http.DpsException;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.dataset.model.request.GetDatasetRegistryRequest;
import org.opengroup.osdu.dataset.model.response.GetDatasetRetrievalInstructionsResponse;
import org.opengroup.osdu.dataset.model.response.GetDatasetStorageInstructionsResponse;
import java.util.Map;
@RunWith(MockitoJUnitRunner.class)
public class DmsServiceTest {
private final String API_KEY = "key";
private final String GetDatasetStorageInstructionsResponse_body = "{\"providerKey\":\"dummy-key\",\"storageLocation\":{\"key1\":{},\"key2\":{}}}";
private final String RetrievalInstructionsResponse_body = "{\"providerKey\":\"dummy-key\",\"datasets\":[{\"datasetRegistryId\":\"dummyid\",\"retrievalProperties\":{\"key1\":{}}}]}";
@Mock
private DmsServiceProperties dmsServiceProperties;
@Mock
private IHttpClient httpClient;
@Mock
private Map<String, String> headersMap;
@Mock
private HttpResponse response;
@Mock
private DpsHeaders headers;
@Before
public void init() {
when(headers.getHeaders()).thenReturn(headersMap);
when(httpClient.send(any())).thenReturn(response);
when(dmsServiceProperties.getApiKey()).thenReturn(API_KEY);
}
@Test
public void getStorageInstructions_success() throws DmsException {
DmsService dmsService = new DmsService(dmsServiceProperties, httpClient, headers);
when(response.isSuccessCode()).thenReturn(true);
when(response.getBody()).thenReturn(GetDatasetStorageInstructionsResponse_body);
GetDatasetStorageInstructionsResponse result = dmsService.getStorageInstructions();
assertNotNull(result);
assertEquals(result.getProviderKey(), "dummy-key");
verify(response, times(1)).isSuccessCode();
verify(headers, times(1)).getHeaders();
verify(dmsServiceProperties, times(2)).getApiKey();
verify(httpClient, times(1)).send(any(HttpRequest.class));
}
@Test
public void getStorageInstructions_DmsException() {
DmsService dmsService = new DmsService(dmsServiceProperties, httpClient, headers);
when(response.isSuccessCode()).thenReturn(true);
when(httpClient.send(any())).thenReturn(response);
when(response.getBody()).thenReturn("{\"key\": \"value\"");
try {
dmsService.getStorageInstructions();
}
catch (Exception exception) {
assertTrue(exception instanceof DmsException);
assertEquals(exception.getMessage(), "Error parsing response. Check the inner HttpResponse for more info.");
}
verify(response, times(1)).isSuccessCode();
verify(headers, times(1)).getHeaders();
verify(dmsServiceProperties, times(2)).getApiKey();
verify(httpClient, times(1)).send(any(HttpRequest.class));
}
@Test
public void getStorageInstructions_generateException() throws DpsException, HttpResponseBodyParsingException {
DmsService dmsService = new DmsService(dmsServiceProperties, httpClient, headers);
when(response.isSuccessCode()).thenReturn(false);
when(httpClient.send(any())).thenReturn(response);
try {
dmsService.getStorageInstructions();
}
catch (Exception exception) {