diff options
author | k.kazak <k.kazak@samsung.com> | 2019-02-22 09:00:16 +0100 |
---|---|---|
committer | k.kazak <k.kazak@samsung.com> | 2019-02-22 09:00:16 +0100 |
commit | edc2b5329e4dde7b5dcb01b8a8bccf51a8064131 (patch) | |
tree | 8b32df089e0976af75c73e3d02af99fedfa1584e /adapters/mso-adapter-utils/src/test/java | |
parent | f8842dbd4065e0b90d4ed0d4af9e4cc72ca218dd (diff) |
fix potential nullpointer from sonar
MsoMulticloudUtils: multicloudClient may be null
throw MsoException if it is null (instead of NullPointer)
removed unnecessary StackInfo object creation
using format specifiers for a logger instead of String concatenation
removed else block to make the code easier to read
Added tests for the method
Change-Id: I80d2e0ba2ef2d20c488cdfcf103d9cfa5784ab65
Issue-ID: SO-1516
Signed-off-by: k.kazak <k.kazak@samsung.com>
Diffstat (limited to 'adapters/mso-adapter-utils/src/test/java')
-rw-r--r-- | adapters/mso-adapter-utils/src/test/java/org/onap/so/openstack/utils/MsoMulticloudUtilsTest.java | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/adapters/mso-adapter-utils/src/test/java/org/onap/so/openstack/utils/MsoMulticloudUtilsTest.java b/adapters/mso-adapter-utils/src/test/java/org/onap/so/openstack/utils/MsoMulticloudUtilsTest.java new file mode 100644 index 0000000000..fcb651e4dd --- /dev/null +++ b/adapters/mso-adapter-utils/src/test/java/org/onap/so/openstack/utils/MsoMulticloudUtilsTest.java @@ -0,0 +1,119 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2019 Samsung Intellectual Property. 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.onap.so.openstack.utils; + +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.post; +import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; +import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.when; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Optional; +import org.apache.http.HttpStatus; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.onap.so.BaseTest; +import org.onap.so.cloud.CloudConfig; +import org.onap.so.db.catalog.beans.CloudIdentity; +import org.onap.so.db.catalog.beans.CloudSite; +import org.onap.so.openstack.beans.StackInfo; +import org.onap.so.openstack.exceptions.MsoException; +import org.springframework.beans.factory.annotation.Autowired; + +public class MsoMulticloudUtilsTest extends BaseTest { + + @Autowired + private MsoMulticloudUtils multicloudUtils; + + @InjectMocks + private MsoMulticloudUtils multicloudUtilsMock; + + @Mock + private CloudConfig cloudConfigMock; + + private static final String CREATE_STACK_RESPONSE = "{\"template_type\": \"TEST-template\", \"workload_id\": " + + "\"TEST-workload\", \"template_response\": {\"stack\": {\"id\": \"TEST-stack\", \"links\": []}}}"; + + @Test + public void createStackSuccess() throws MsoException, IOException { + stubFor(post(urlPathEqualTo("/v2.0")) + .willReturn(aResponse().withHeader("Content-Type", "application/json") + .withBody(CREATE_STACK_RESPONSE) + .withStatus(HttpStatus.SC_CREATED))); + StackInfo result = multicloudUtils.createStack("MTN13", "TEST-tenant", "TEST-stack", + "TEST-heat", new HashMap<>(), false, 200, "TEST-env", + new HashMap<>(), new HashMap<>()); + assertNotNull(result); + assertEquals("TEST-stack", result.getName()); + } + + @Test + public void createStackMulticloudClientIsNull() { + try { + multicloudUtilsMock.cloudConfig = cloudConfigMock; + CloudSite cloudSite = new CloudSite(); + cloudSite.setIdentityService(new CloudIdentity()); + when(cloudConfigMock.getCloudSite("MTN13")). + thenReturn(Optional.of(cloudSite)); + multicloudUtilsMock.createStack("MTN13", "TEST-tenant", "TEST-stack", + "TEST-heat", new HashMap<>(), false, 200, "TEST-env", + new HashMap<>(), new HashMap<>()); + } catch (MsoException e) { + assertEquals("0 : Multicloud client could not be initialized", e.toString()); + return; + } + fail("MsoOpenstackException expected!"); + } + + @Test + public void createStackBadRequest() { + try { + stubFor(post(urlPathEqualTo("/v2.0")) + .willReturn(aResponse().withHeader("Content-Type", "application/json") + .withStatus(HttpStatus.SC_BAD_REQUEST))); + multicloudUtils.createStack("MTN13", "TEST-tenant", "TEST-stack", + "TEST-heat", new HashMap<>(), false, 200, "TEST-env", + new HashMap<>(), new HashMap<>()); + } catch (MsoException e) { + assertEquals("0 : Bad Request", e.toString()); + return; + } + fail("MsoOpenstackException expected!"); + } + + @Test + public void createStackEmptyResponseEntity() throws MsoException { + stubFor(post(urlPathEqualTo("/v2.0")) + .willReturn(aResponse().withHeader("Content-Type", "application/json") + .withStatus(HttpStatus.SC_CREATED))); + StackInfo result = multicloudUtils.createStack("MTN13", "TEST-tenant", "TEST-stack", + "TEST-heat", new HashMap<>(), false, 200, "TEST-env", + new HashMap<>(), new HashMap<>()); + assertNotNull(result); + assertEquals("TEST-stack/", result.getName()); + } +} |