increasing test coverage in common-app-api common catalog 96/94196/6
authorBartosz Gardziejewski <bartosz.gardziejewski@nokia.com>
Fri, 23 Aug 2019 10:42:11 +0000 (12:42 +0200)
committerTomasz Golabek <tomasz.golabek@nokia.com>
Fri, 23 Aug 2019 13:53:32 +0000 (13:53 +0000)
Issue-ID: SDC-2326
Signed-off-by: Bartosz Gardziejewski <bartosz.gardziejewski@nokia.com>
Change-Id: Ida905fc6f0f28d75522c865d33abf636c1fb7fdb

common-app-api/src/test/java/org/openecomp/sdc/common/http/client/api/HttpClientConfigImmutableTest.java [new file with mode: 0644]
common-app-api/src/test/java/org/openecomp/sdc/common/http/client/api/HttpClientFactoryTest.java [new file with mode: 0644]
common-app-api/src/test/java/org/openecomp/sdc/common/http/client/api/HttpConnectionMngFactoryTest.java [new file with mode: 0644]
common-app-api/src/test/java/org/openecomp/sdc/common/http/client/api/HttpExecuteExceptionTest.java [new file with mode: 0644]
common-app-api/src/test/java/org/openecomp/sdc/common/http/client/api/HttpResponseTest.java [new file with mode: 0644]
common-app-api/src/test/java/org/openecomp/sdc/common/http/client/api/ResponsesTest.java [new file with mode: 0644]
common-app-api/src/test/java/org/openecomp/sdc/common/http/client/api/RestUtilsTest.java

diff --git a/common-app-api/src/test/java/org/openecomp/sdc/common/http/client/api/HttpClientConfigImmutableTest.java b/common-app-api/src/test/java/org/openecomp/sdc/common/http/client/api/HttpClientConfigImmutableTest.java
new file mode 100644 (file)
index 0000000..eccd251
--- /dev/null
@@ -0,0 +1,157 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * SDC
+ * ================================================================================
+ * Copyright (C) 2019 Nokia. All rights reserved.
+ * ================================================================================
+ * 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
+ *
+ *      http://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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.sdc.common.http.client.api;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.openecomp.sdc.common.http.config.BasicAuthorization;
+import org.openecomp.sdc.common.http.config.ClientCertificate;
+import org.openecomp.sdc.common.http.config.HttpClientConfig;
+import org.openecomp.sdc.common.http.config.Timeouts;
+import org.openecomp.sdc.security.SecurityUtil;
+
+import java.util.Collections;
+import java.util.Map;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+@RunWith(MockitoJUnitRunner.class)
+public class HttpClientConfigImmutableTest {
+
+    @Mock
+    private ComparableHttpRequestRetryHandler testRetryHandler;
+
+    private ClientCertificate testClientCertificate;
+
+    private Map<String, String> testHeaders;
+
+    private BasicAuthorization testBasicAuthorization;
+
+
+    private Timeouts testTimeouts;
+
+
+    private int testNumOfRetries;
+
+    @Test
+    public void validateArgConstructorCreatesValidImmutableConfig() {
+        HttpClientConfigImmutable httpClientConfigImmutable = new HttpClientConfigImmutable(prepareTestClientConfig());
+        validateAllVieldsInImmutableConfig(httpClientConfigImmutable);
+    }
+
+    @Test
+    public void validateStaticMethodCreatesValidImmutableConfig() {
+        HttpClientConfigImmutable httpClientConfigImmutable = HttpClientConfigImmutable.getOrCreate(prepareTestClientConfig());
+        validateAllVieldsInImmutableConfig(httpClientConfigImmutable);
+    }
+
+    @Test
+    public void validateToString() {
+        HttpClientConfigImmutable httpClientConfigImmutable = HttpClientConfigImmutable.getOrCreate(prepareTestClientConfig());
+        final String result = httpClientConfigImmutable.toString();
+
+        assertTrue(result.contains(testBasicAuthorization.toString()));
+        assertTrue(result.contains(testClientCertificate.toString()));
+        assertTrue(result.contains(testRetryHandler.toString()));
+        assertTrue(result.contains(testTimeouts.toString()));
+        assertTrue(result.contains(testHeaders.toString()));
+        assertTrue(result.contains(Integer.toString(testNumOfRetries)));
+    }
+
+    @Test
+    public void validateHashCode() {
+        HttpClientConfigImmutable httpClientConfigImmutable01 = new HttpClientConfigImmutable(prepareTestClientConfig());
+        HttpClientConfigImmutable httpClientConfigImmutable02 = HttpClientConfigImmutable.getOrCreate(prepareTestClientConfig());
+
+        assertEquals(
+                httpClientConfigImmutable01.hashCode(),
+                httpClientConfigImmutable02.hashCode()
+        );
+    }
+
+    private void validateAllVieldsInImmutableConfig(HttpClientConfigImmutable httpClientConfigImmutable) {
+        assertEquals(
+                httpClientConfigImmutable.getNumOfRetries(),
+                testNumOfRetries);
+        assertEquals(
+                httpClientConfigImmutable.getReadTimeoutMs(),
+                testTimeouts.getReadTimeoutMs());
+        assertEquals(
+                httpClientConfigImmutable.getConnectTimeoutMs(),
+                testTimeouts.getConnectTimeoutMs());
+        assertEquals(
+                httpClientConfigImmutable.getConnectPoolTimeoutMs(),
+                testTimeouts.getConnectPoolTimeoutMs());
+        assertEquals(
+                httpClientConfigImmutable.getBasicAuthUserName(),
+                testBasicAuthorization.getUserName());
+        assertEquals(
+                httpClientConfigImmutable.getBasicAuthPassword(),
+                testBasicAuthorization.getPassword());
+        assertEquals(
+                httpClientConfigImmutable.getClientCertificate().getClass(),
+                testClientCertificate.getClass());
+        assertEquals(
+                httpClientConfigImmutable.getClientCertKeyStore(),
+                testClientCertificate.getKeyStore());
+        assertEquals(
+                httpClientConfigImmutable.getClientCertKeyPassword(),
+                testClientCertificate.getKeyStorePassword());
+        assertEquals(
+                httpClientConfigImmutable.getRetryHandler(),
+                testRetryHandler);
+        assertEquals(
+                httpClientConfigImmutable.getHeaders(),
+                testHeaders);
+    }
+
+    private HttpClientConfig prepareTestClientConfig() {
+        final String testUserName = "testUser";
+        final String testUserPassword = SecurityUtil.INSTANCE.encrypt("testPassword").left().value();
+        final int timeouts = 10;
+        final String testKeyStore = "testKeyStore";
+        final String testKeyStorePassword = SecurityUtil.INSTANCE.encrypt("testKeyStorePassword").left().value();
+
+        testNumOfRetries = 10;
+        testHeaders = Collections.emptyMap();
+        testTimeouts = new Timeouts(timeouts,timeouts);
+        testTimeouts.setConnectPoolTimeoutMs(timeouts);
+        testBasicAuthorization = new BasicAuthorization();
+        testBasicAuthorization.setUserName(testUserName);
+        testBasicAuthorization.setPassword(testUserPassword);
+        testClientCertificate = new ClientCertificate();
+        testClientCertificate.setKeyStore(testKeyStore);
+        testClientCertificate.setKeyStorePassword(testKeyStorePassword);
+
+        HttpClientConfig httpClientConfig = new HttpClientConfig();
+        httpClientConfig.setNumOfRetries(testNumOfRetries);
+        httpClientConfig.setTimeouts(testTimeouts);
+        httpClientConfig.setBasicAuthorization(testBasicAuthorization);
+        httpClientConfig.setClientCertificate(testClientCertificate);
+        httpClientConfig.setRetryHandler(testRetryHandler);
+        httpClientConfig.setHeaders(testHeaders);
+
+        return httpClientConfig;
+    }
+}
diff --git a/common-app-api/src/test/java/org/openecomp/sdc/common/http/client/api/HttpClientFactoryTest.java b/common-app-api/src/test/java/org/openecomp/sdc/common/http/client/api/HttpClientFactoryTest.java
new file mode 100644 (file)
index 0000000..ddfed06
--- /dev/null
@@ -0,0 +1,81 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * SDC
+ * ================================================================================
+ * Copyright (C) 2019 Nokia. All rights reserved.
+ * ================================================================================
+ * 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
+ *
+ *      http://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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.sdc.common.http.client.api;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.openecomp.sdc.common.http.config.BasicAuthorization;
+import org.openecomp.sdc.common.http.config.ClientCertificate;
+import org.openecomp.sdc.common.http.config.HttpClientConfig;
+import org.openecomp.sdc.common.http.config.Timeouts;
+import org.openecomp.sdc.security.SecurityUtil;
+
+import java.util.Collections;
+import java.util.Map;
+
+import static junit.framework.TestCase.assertNotNull;
+
+@RunWith(MockitoJUnitRunner.class)
+public class HttpClientFactoryTest {
+
+    @Mock
+    HttpConnectionMngFactory httpConnectionMngFactory;
+
+    @Test
+    public void validateNewClientCreationReturnsValidClient() throws HttpExecuteException {
+        HttpClient httpClient = new HttpClientFactory(httpConnectionMngFactory).createClient("Http",prepareTestClientConfigImmutable());
+        assertNotNull(httpClient);
+        httpClient.close();
+    }
+
+    private HttpClientConfigImmutable prepareTestClientConfigImmutable() {
+        final String testUserName = "testUser";
+        final String testUserPassword = SecurityUtil.INSTANCE.encrypt("testPassword").left().value();
+        final int timeouts = 10;
+        final String testKeyStore = "testKeyStore";
+        final String testKeyStorePassword = SecurityUtil.INSTANCE.encrypt("testKeyStorePassword").left().value();
+
+        int testNumOfRetries = 10;
+        ComparableHttpRequestRetryHandler testRetryHandler = Mockito.mock(ComparableHttpRequestRetryHandler.class);
+        Map<String, String> testHeaders = Collections.emptyMap();
+        Timeouts testTimeouts = new Timeouts(timeouts, timeouts);
+        testTimeouts.setConnectPoolTimeoutMs(timeouts);
+        BasicAuthorization testBasicAuthorization = new BasicAuthorization();
+        testBasicAuthorization.setUserName(testUserName);
+        testBasicAuthorization.setPassword(testUserPassword);
+        ClientCertificate testClientCertificate = new ClientCertificate();
+        testClientCertificate.setKeyStore(testKeyStore);
+        testClientCertificate.setKeyStorePassword(testKeyStorePassword);
+
+        HttpClientConfig httpClientConfig = new HttpClientConfig();
+        httpClientConfig.setNumOfRetries(testNumOfRetries);
+        httpClientConfig.setTimeouts(testTimeouts);
+        httpClientConfig.setBasicAuthorization(testBasicAuthorization);
+        httpClientConfig.setClientCertificate(testClientCertificate);
+        httpClientConfig.setRetryHandler(testRetryHandler);
+        httpClientConfig.setHeaders(testHeaders);
+
+        return new HttpClientConfigImmutable(httpClientConfig);
+    }
+}
diff --git a/common-app-api/src/test/java/org/openecomp/sdc/common/http/client/api/HttpConnectionMngFactoryTest.java b/common-app-api/src/test/java/org/openecomp/sdc/common/http/client/api/HttpConnectionMngFactoryTest.java
new file mode 100644 (file)
index 0000000..eb32637
--- /dev/null
@@ -0,0 +1,47 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * SDC
+ * ================================================================================
+ * Copyright (C) 2019 Nokia. All rights reserved.
+ * ================================================================================
+ * 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
+ *
+ *      http://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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.sdc.common.http.client.api;
+
+import org.apache.http.conn.HttpClientConnectionManager;
+import org.junit.Test;
+import org.openecomp.sdc.common.http.config.ClientCertificate;
+import org.openecomp.sdc.security.SecurityUtil;
+
+import static org.junit.Assert.assertNotNull;
+
+public class HttpConnectionMngFactoryTest {
+
+    @Test
+    public void validateFactoryCreatesValidHttpClientConnectionManager() {
+        final String testKeyStore = "testKeyStore ";
+        final String testKeyStorePassword = SecurityUtil.INSTANCE.encrypt("testKeyStorePassword").left().value();
+
+        ClientCertificate clientCertificate = new ClientCertificate();
+        clientCertificate.setKeyStore(testKeyStore);
+        clientCertificate.setKeyStorePassword(testKeyStorePassword);
+        HttpClientConnectionManager httpClientConnectionManager = new HttpConnectionMngFactory().getOrCreate(clientCertificate);
+
+        assertNotNull(httpClientConnectionManager);
+
+        httpClientConnectionManager.shutdown();
+    }
+
+}
diff --git a/common-app-api/src/test/java/org/openecomp/sdc/common/http/client/api/HttpExecuteExceptionTest.java b/common-app-api/src/test/java/org/openecomp/sdc/common/http/client/api/HttpExecuteExceptionTest.java
new file mode 100644 (file)
index 0000000..281e730
--- /dev/null
@@ -0,0 +1,64 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * SDC
+ * ================================================================================
+ * Copyright (C) 2019 Nokia. All rights reserved.
+ * ================================================================================
+ * 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
+ *
+ *      http://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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.sdc.common.http.client.api;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertEquals;
+
+public class HttpExecuteExceptionTest {
+
+    @Rule
+    public ExpectedException expectedException = ExpectedException.none();
+
+    @Test
+    public void validateOneArgConstructorWithMessage() throws Exception{
+        final String testMessage = "test error message";
+        expectedException.expect(HttpExecuteException.class);
+        expectedException.expectMessage(testMessage);
+        throw new HttpExecuteException(testMessage);
+    }
+
+    @Test
+    public void validateOneArgConstructorWithThrowable() throws Exception{
+        final String testThrowableMessage = "test throwable error message";
+        final Throwable testThrowable = new Throwable(testThrowableMessage);
+        expectedException.expect(HttpExecuteException.class);
+        expectedException.expectCause(is(testThrowable));
+
+        throw new HttpExecuteException(testThrowable);
+    }
+
+    @Test
+    public void validateAllArgConstructor() throws Exception{
+        final String testMessage = "test error message";
+        final String testThrowableMessage = "test throwable error message";
+        final Throwable testThrowable = new Throwable(testThrowableMessage);
+        expectedException.expect(HttpExecuteException.class);
+        expectedException.expectMessage(testMessage);
+        expectedException.expectCause(is(testThrowable));
+
+        throw new HttpExecuteException(testMessage, testThrowable);
+    }
+}
diff --git a/common-app-api/src/test/java/org/openecomp/sdc/common/http/client/api/HttpResponseTest.java b/common-app-api/src/test/java/org/openecomp/sdc/common/http/client/api/HttpResponseTest.java
new file mode 100644 (file)
index 0000000..d96f071
--- /dev/null
@@ -0,0 +1,65 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * SDC
+ * ================================================================================
+ * Copyright (C) 2019 Nokia. All rights reserved.
+ * ================================================================================
+ * 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
+ *
+ *      http://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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.sdc.common.http.client.api;
+
+import org.apache.http.HttpStatus;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class HttpResponseTest {
+
+    @Test
+    public void validateNoDescriptionConstructor() {
+        final String testResponse = "test response";
+
+        HttpResponse<String> httpResponseTest = new HttpResponse<>(testResponse, HttpStatus.SC_OK);
+
+        assertEquals(httpResponseTest.getStatusCode(),HttpStatus.SC_OK);
+        assertEquals(httpResponseTest.getResponse(),testResponse);
+        assertEquals(httpResponseTest.getDescription(),"");
+    }
+
+    @Test
+    public void validateAllArgsConstructor() {
+        final String testResponse = "test response";
+        final String testDescription = "test description";
+
+        HttpResponse<String> httpResponseTest = new HttpResponse<>(testResponse, HttpStatus.SC_OK, testDescription);
+
+        assertEquals(httpResponseTest.getStatusCode(),HttpStatus.SC_OK);
+        assertEquals(httpResponseTest.getResponse(),testResponse);
+        assertEquals(httpResponseTest.getDescription(),testDescription);
+    }
+
+    @Test
+    public void validateToStringConstructor() {
+        final String testResponse = "test response";
+        final String testDescription = "test description";
+
+        HttpResponse<String> httpResponseTest = new HttpResponse<>(testResponse, HttpStatus.SC_OK, testDescription);
+
+        assertTrue(httpResponseTest.toString().contains(Integer.toString(HttpStatus.SC_OK)));
+        assertTrue(httpResponseTest.toString().contains(testResponse));
+        assertTrue(httpResponseTest.toString().contains(testDescription));
+    }
+}
diff --git a/common-app-api/src/test/java/org/openecomp/sdc/common/http/client/api/ResponsesTest.java b/common-app-api/src/test/java/org/openecomp/sdc/common/http/client/api/ResponsesTest.java
new file mode 100644 (file)
index 0000000..3d45033
--- /dev/null
@@ -0,0 +1,34 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * SDC
+ * ================================================================================
+ * Copyright (C) 2019 Nokia. All rights reserved.
+ * ================================================================================
+ * 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
+ *
+ *      http://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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.sdc.common.http.client.api;
+
+import org.apache.http.HttpStatus;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class ResponsesTest {
+
+    @Test
+    public void validateStaticFields() {
+        assertEquals(Responses.INTERNAL_SERVER_ERROR.getStatusCode(), HttpStatus.SC_INTERNAL_SERVER_ERROR);
+    }
+}
index 2a3ec16..7a0bb9a 100644 (file)
@@ -23,12 +23,10 @@ package org.openecomp.sdc.common.http.client.api;
 import org.apache.http.HttpHeaders;
 import org.junit.Test;
 
-import static org.junit.Assert.*;
-
-import org.openecomp.sdc.common.http.client.api.*;
-
 import java.util.Properties;
 
+import static org.junit.Assert.assertEquals;
+
 public class RestUtilsTest {
 
     @Test