diff options
author | k.kazak <k.kazak@samsung.com> | 2019-02-15 17:27:08 +0100 |
---|---|---|
committer | k.kazak <k.kazak@samsung.com> | 2019-02-15 17:27:08 +0100 |
commit | c9a28a8316cf3a19fcaa2f78cf92798a99139781 (patch) | |
tree | 8bcc15b809538cae7244a551e0cfa55c64906af6 /adapters/mso-adapter-utils/src/test/java | |
parent | b7f55f47a964351e7dc46d05af7f53f7da1c9eba (diff) |
fix sonar critical bug
MsoHeatUtils: remove unnecessary heatClient check for null
1. sonar assumes that heatClient is nullable and NullPointer may be thrown in line 309
2. heatClient is never null: method creates a new object or throws Exception
MsoHeatUtilsTest: added tests for getHeatClient and createStack methods
BaseTest: made CloudIdentity and CloudSite getters protected for other tests to use them
StubOpenStack: added stubs for getStack and unsuccessful authorization
removed unused imports
Change-Id: Ie62c89f55d1894df48d9cc4d14760d36e96c4cb2
Issue-ID: SO-1516
Signed-off-by: k.kazak <k.kazak@samsung.com>
Diffstat (limited to 'adapters/mso-adapter-utils/src/test/java')
3 files changed, 80 insertions, 16 deletions
diff --git a/adapters/mso-adapter-utils/src/test/java/org/onap/so/BaseTest.java b/adapters/mso-adapter-utils/src/test/java/org/onap/so/BaseTest.java index 087ac6f17b..9cfdf53094 100644 --- a/adapters/mso-adapter-utils/src/test/java/org/onap/so/BaseTest.java +++ b/adapters/mso-adapter-utils/src/test/java/org/onap/so/BaseTest.java @@ -4,6 +4,8 @@ * ================================================================================ * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ + * Modifications Copyright (c) 2019 Samsung + * ================================================================================ * 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 @@ -86,7 +88,7 @@ public abstract class BaseTest extends TestDataSetup { .withStatus(HttpStatus.SC_OK))); } - private CloudIdentity getCloudIdentity() { + protected CloudIdentity getCloudIdentity() { CloudIdentity identity = new CloudIdentity(); identity.setId("mtn13"); identity.setMsoId("m93945"); @@ -100,7 +102,7 @@ public abstract class BaseTest extends TestDataSetup { return identity; } - private CloudSite getCloudSite(CloudIdentity identity) { + protected CloudSite getCloudSite(CloudIdentity identity) { CloudSite cloudSite = new CloudSite(); cloudSite.setId("MTN13"); cloudSite.setCloudVersion("3.0"); @@ -123,4 +125,4 @@ public abstract class BaseTest extends TestDataSetup { return sb.toString(); } } -}
\ No newline at end of file +} diff --git a/adapters/mso-adapter-utils/src/test/java/org/onap/so/StubOpenStack.java b/adapters/mso-adapter-utils/src/test/java/org/onap/so/StubOpenStack.java index f5867befc0..0820076c66 100644 --- a/adapters/mso-adapter-utils/src/test/java/org/onap/so/StubOpenStack.java +++ b/adapters/mso-adapter-utils/src/test/java/org/onap/so/StubOpenStack.java @@ -4,6 +4,8 @@ * ================================================================================ * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ + * Modifications Copyright (c) 2019 Samsung + * ================================================================================ * 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 @@ -20,12 +22,6 @@ package org.onap.so; -import org.apache.http.HttpStatus; - -import java.io.BufferedReader; -import java.io.FileReader; -import java.io.IOException; - import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.delete; import static com.github.tomakehurst.wiremock.client.WireMock.get; @@ -35,6 +31,11 @@ import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching; import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import org.apache.http.HttpStatus; + public class StubOpenStack { public static void mockOpenStackResponseAccess(int port) throws IOException { @@ -43,11 +44,27 @@ public class StubOpenStack { .withStatus(HttpStatus.SC_OK))); } + public static void mockOpenStackResponseUnauthorized(int port) throws IOException { + stubFor( + post(urlPathEqualTo("/v2.0/tokens")) + .willReturn(aResponse().withHeader("Content-Type", "application/json") + .withBody(getBodyFromFile("OpenstackResponse_Access.json", port, "/mockPublicUrl")) + .withStatus(HttpStatus.SC_UNAUTHORIZED))); + } + public static void mockOpenStackDelete(String id) { stubFor(delete(urlMatching("/mockPublicUrl/stacks/" + id)).willReturn(aResponse() .withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_OK))); } + public static void mockOpenStackGet(String id) { + stubFor( + get(urlPathEqualTo("/mockPublicUrl/stacks/" + id)) + .willReturn(aResponse().withHeader("Content-Type", "application/json") + .withBodyFile("OpenstackResponse_Stack_Created.json") + .withStatus(HttpStatus.SC_OK))); + } + public static void mockOpenStackPostStack_200(String filename) { stubFor(post(urlPathEqualTo("/mockPublicUrl/stacks")).willReturn(aResponse() diff --git a/adapters/mso-adapter-utils/src/test/java/org/onap/so/openstack/utils/MsoHeatUtilsTest.java b/adapters/mso-adapter-utils/src/test/java/org/onap/so/openstack/utils/MsoHeatUtilsTest.java index 6517122475..f9fc9284fe 100644 --- a/adapters/mso-adapter-utils/src/test/java/org/onap/so/openstack/utils/MsoHeatUtilsTest.java +++ b/adapters/mso-adapter-utils/src/test/java/org/onap/so/openstack/utils/MsoHeatUtilsTest.java @@ -4,6 +4,8 @@ * ================================================================================ * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ + * Modifications Copyright (c) 2019 Samsung + * ================================================================================ * 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 @@ -26,17 +28,19 @@ import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; import static com.shazam.shazamcrest.MatcherAssert.assertThat; import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs; +import static org.junit.Assert.assertNotNull; +import com.woorea.openstack.heat.Heat; +import com.woorea.openstack.heat.model.CreateStackParam; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; - -import com.woorea.openstack.heat.model.CreateStackParam; import org.apache.http.HttpStatus; import org.junit.Assert; import org.junit.Test; +import org.onap.so.BaseTest; import org.onap.so.StubOpenStack; import org.onap.so.adapters.vdu.CloudInfo; import org.onap.so.adapters.vdu.PluginAction; @@ -46,13 +50,13 @@ import org.onap.so.adapters.vdu.VduInstance; import org.onap.so.adapters.vdu.VduModelInfo; import org.onap.so.adapters.vdu.VduStateType; import org.onap.so.adapters.vdu.VduStatus; -import org.onap.so.cloud.CloudConfig; -import org.onap.so.BaseTest; +import org.onap.so.db.catalog.beans.CloudIdentity; import org.onap.so.db.catalog.beans.CloudSite; -import org.onap.so.openstack.beans.HeatStatus; import org.onap.so.openstack.beans.StackInfo; +import org.onap.so.openstack.exceptions.MsoAdapterException; import org.onap.so.openstack.exceptions.MsoException; - +import org.onap.so.openstack.exceptions.MsoIOException; +import org.onap.so.openstack.exceptions.MsoOpenstackException; import org.springframework.beans.factory.annotation.Autowired; public class MsoHeatUtilsTest extends BaseTest{ @@ -187,6 +191,47 @@ public class MsoHeatUtilsTest extends BaseTest{ heatUtils.copyBaseOutputsToInputs(inputs, otherStackOutputs, null, aliases); Assert.assertEquals("str",otherStackOutputs.get("str")); } - + @Test + public final void getHeatClientSuccessTest() throws MsoException, IOException { + CloudSite cloudSite = getCloudSite(getCloudIdentity()); + StubOpenStack.mockOpenStackResponseAccess(wireMockPort); + Heat heatClient = heatUtils.getHeatClient(cloudSite, "TEST-tenant"); + assertNotNull(heatClient); + } + + @Test(expected = MsoOpenstackException.class) + public final void getHeatClientOpenStackResponseException404Test() throws MsoException, IOException { + CloudSite cloudSite = getCloudSite(getCloudIdentity()); + // mo mocks setup will cause 404 response from wiremock + heatUtils.getHeatClient(cloudSite, "TEST-tenant"); + } + + @Test(expected = MsoAdapterException.class) + public final void getHeatClientOpenStackResponseException401Test() throws MsoException, IOException { + CloudSite cloudSite = getCloudSite(getCloudIdentity()); + StubOpenStack.mockOpenStackResponseUnauthorized(wireMockPort); + heatUtils.getHeatClient(cloudSite, "TEST-tenant"); + } + + @Test(expected = MsoIOException.class) + public final void getHeatClientOpenStackConnectExceptionTest() throws MsoException, IOException { + CloudIdentity identity = getCloudIdentity(); + identity.setIdentityUrl("http://unreachable"); + CloudSite cloudSite = getCloudSite(identity); + // mo mocks setup will cause 404 response from wiremock + heatUtils.getHeatClient(cloudSite, "TEST-tenant"); + } + + @Test + public final void createStackSuccessTest() throws MsoException, IOException { + CloudSite cloudSite = getCloudSite(getCloudIdentity()); + StubOpenStack.mockOpenStackResponseAccess(wireMockPort); + StubOpenStack.mockOpenStackPostStack_200("OpenstackResponse_Stack_Created.json"); + StubOpenStack.mockOpenStackGet("TEST-stack/stackId"); + StackInfo stackInfo = heatUtils.createStack(cloudSite.getId(), "tenantId", "TEST-stack", + "TEST-heat", new HashMap<>(), false, 1, "TEST-env", + new HashMap<>(), new HashMap<>(), false); + assertNotNull(stackInfo); + } } |