diff options
author | 2018-03-07 15:08:52 +0100 | |
---|---|---|
committer | 2018-03-09 20:40:43 +0000 | |
commit | c6f9a1aab9e6ba545d8148d4f7456235be1e67d8 (patch) | |
tree | 777e0cba8d87df922d047caaa51dfbabd0718dd3 /appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java | |
parent | 072d9eb28f13b50058cf9a50207dce435ab88369 (diff) |
ChefAdapterImpl JUnits
-Added unit tests for chefXXX methods - extracted new test class
-Moved privateKeyCheck method to new class PrivateKeyChecker + JUnits
-New Factory class ChefAdapterFactory for ChefAdapterImpl construction
-Fixed potential bug with wrong String being assigned in iniSvcLogic method
Change-Id: I27badfd01bfaa807b8ecb9b4a4c13e7f026e34af
Issue-ID: APPC-437
Signed-off-by: Michal Kabaj <michal.kabaj@nokia.com>
Diffstat (limited to 'appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java')
5 files changed, 229 insertions, 7 deletions
diff --git a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/ChefAdapterImplDataRetrieverTest.java b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/ChefAdapterImplDataRetrieverTest.java index 1f447a4ff..d4850cda2 100644 --- a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/ChefAdapterImplDataRetrieverTest.java +++ b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/ChefAdapterImplDataRetrieverTest.java @@ -41,7 +41,7 @@ public class ChefAdapterImplDataRetrieverTest { SvcLogicContext svcLogicContext = new SvcLogicContext(); // WHEN - ChefAdapter chefAdapter = new ChefAdapterImpl(); + ChefAdapter chefAdapter = new ChefAdapterFactory().create(); chefAdapter.retrieveData(params, svcLogicContext); // THEN @@ -55,7 +55,7 @@ public class ChefAdapterImplDataRetrieverTest { SvcLogicContext svcLogicContext = new SvcLogicContext(); // WHEN - ChefAdapter chefAdapter = new ChefAdapterImpl(); + ChefAdapter chefAdapter = new ChefAdapterFactory().create(); chefAdapter.retrieveData(params, svcLogicContext); // THEN @@ -69,7 +69,7 @@ public class ChefAdapterImplDataRetrieverTest { SvcLogicContext svcLogicContext = new SvcLogicContext(); // WHEN - ChefAdapter chefAdapter = new ChefAdapterImpl(); + ChefAdapter chefAdapter = new ChefAdapterFactory().create(); chefAdapter.retrieveData(params, svcLogicContext); // THEN diff --git a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/ChefAdapterImplHttpMethodTest.java b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/ChefAdapterImplHttpMethodTest.java new file mode 100644 index 000000000..82d15366b --- /dev/null +++ b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/ChefAdapterImplHttpMethodTest.java @@ -0,0 +1,181 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2018 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.onap.appc.adapter.chef.impl; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.verifyZeroInteractions; + +import com.google.common.collect.ImmutableMap; +import java.util.Map; +import java.util.function.Consumer; +import java.util.function.Supplier; +import org.apache.http.HttpStatus; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.appc.adapter.chef.ChefAdapter; +import org.onap.appc.adapter.chef.chefclient.ChefApiClientFactory; +import org.onap.appc.adapter.chef.chefclient.api.ChefApiClient; +import org.onap.appc.adapter.chef.chefclient.api.ChefResponse; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; + +@RunWith(MockitoJUnitRunner.class) +public class ChefAdapterImplHttpMethodTest { + + private static final String CLIENT_PRIVATE_KEY_PATH = "/opt/onap/appc/chef/localhost/onap/testclient.pem"; + private static final String RESULT_CODE_ATTR_KEY = "chefServerResult.code"; + private static final String RESULT_MESSAGE_ATTR_KEY = "chefServerResult.message"; + private static final String EXPECTED_RESPONSE_MSG = "chefResponseMessage"; + private static final String ACTION_PARAM = "action"; + private static final String REQUEST_BODY_DATA = "requestBodyData"; + private static final Map<String, String> PARAMS = ImmutableMap + .of("username", "testclient", + "serverAddress", "localhost", + "organizations", "onap", + "chefAction", ACTION_PARAM, + "chefRequestBody", REQUEST_BODY_DATA); + @Mock + private PrivateKeyChecker privateKeyChecker; + @Mock + private ChefApiClientFactory chefApiClientFactory; + @Mock + private ChefApiClient chefApiClient; + + private ChefAdapter chefAdapter; + private SvcLogicContext svcLogicContext; + + @Before + public void setUp() { + chefAdapter = new ChefAdapterImpl(chefApiClientFactory, privateKeyChecker); + svcLogicContext = new SvcLogicContext(); + } + + @Test + public void chefGet_shouldExecuteHttpClient_andSetChefResponseInContext_whenPrivateKeyFileExists() { + assertSuccessfulChefHttpCallFor(() -> chefApiClient.get(ACTION_PARAM), this::chefGet); + } + + @Test + public void chefGet_shouldNotExecuteHttpClient_andSetErrorResponseInContext_whenPrivateKeyFileDoesNotExist() { + assertNoChefCallOccurFor(this::chefGet); + } + + @Test + public void chefDelete_shouldExecuteHttpClient_andSetChefResponseInContext_whenPrivateKeyFileExists() { + assertSuccessfulChefHttpCallFor(() -> chefApiClient.delete(ACTION_PARAM), this::chefDelete); + } + + @Test + public void chefDelete_shouldNotExecuteHttpClient_andSetErrorResponseInContext_whenPrivateKeyFileDoesNotExist() { + assertNoChefCallOccurFor(this::chefDelete); + } + + @Test + public void chefPut_shouldExecuteHttpClient_andSetChefResponseInContext_whenPrivateKeyFileExists() { + assertSuccessfulChefHttpCallFor(() -> chefApiClient.put(ACTION_PARAM, REQUEST_BODY_DATA), this::chefPut); + } + + @Test + public void chefPut_shouldNotExecuteHttpClient_andSetErrorResponseInContext_whenPrivateKeyFileDoesNotExist() { + assertNoChefCallOccurFor(this::chefPut); + } + + @Test + public void chefPost_shouldExecuteHttpClient_andSetChefResponseInContext_whenPrivateKeyFileExists() { + assertSuccessfulChefHttpCallFor(() -> chefApiClient.post(ACTION_PARAM, REQUEST_BODY_DATA), this::chefPost); + } + + @Test + public void chefPost_shouldNotExecuteHttpClient_andSetErrorResponseInContext_whenPrivateKeyFileDoesNotExist() { + assertNoChefCallOccurFor(this::chefPost); + } + + public void assertSuccessfulChefHttpCallFor(Supplier<ChefResponse> responseSupplier, + Consumer<ChefAdapter> chefAdapterCall) { + // GIVEN + given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(true); + given(chefApiClientFactory.create("https://localhost/organizations/onap", + "onap", + "testclient", + CLIENT_PRIVATE_KEY_PATH)).willReturn(chefApiClient); + given(responseSupplier.get()).willReturn(ChefResponse.create(HttpStatus.SC_OK, EXPECTED_RESPONSE_MSG)); + + // WHEN + chefAdapterCall.accept(chefAdapter); + + // THEN + assertThat(svcLogicContext.getStatus()).isEqualTo("success"); + assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY)).isEqualTo(Integer.toString(HttpStatus.SC_OK)); + assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(EXPECTED_RESPONSE_MSG); + } + + public void assertNoChefCallOccurFor(Consumer<ChefAdapter> chefAdapterCall) { + // GIVEN + given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(false); + + // WHEN + chefAdapterCall.accept(chefAdapter); + + // THEN + verifyZeroInteractions(chefApiClient); + assertThat(svcLogicContext.getStatus()).isEqualTo("success"); + assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY)) + .isEqualTo(Integer.toString(HttpStatus.SC_INTERNAL_SERVER_ERROR)); + assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo( + "Cannot find the private key in the APPC file system, please load the private key to " + + CLIENT_PRIVATE_KEY_PATH); + } + + public void chefGet(ChefAdapter chefAdapter) { + try { + chefAdapter.chefGet(PARAMS, svcLogicContext); + } catch (SvcLogicException e) { + e.printStackTrace(); + } + } + + public void chefDelete(ChefAdapter chefAdapter) { + try { + chefAdapter.chefDelete(PARAMS, svcLogicContext); + } catch (SvcLogicException e) { + e.printStackTrace(); + } + } + + public void chefPost(ChefAdapter chefAdapter) { + try { + chefAdapter.chefPost(PARAMS, svcLogicContext); + } catch (SvcLogicException e) { + e.printStackTrace(); + } + } + + public void chefPut(ChefAdapter chefAdapter) { + try { + chefAdapter.chefPut(PARAMS, svcLogicContext); + } catch (SvcLogicException e) { + e.printStackTrace(); + } + } +}
\ No newline at end of file diff --git a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/ChefAdapterImplTest.java b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/ChefAdapterImplTest.java index df8326782..7b3546765 100644 --- a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/ChefAdapterImplTest.java +++ b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/ChefAdapterImplTest.java @@ -42,7 +42,7 @@ public class ChefAdapterImplTest { SvcLogicContext svcLogicContext = new SvcLogicContext(); // WHEN - ChefAdapter chefAdapter = new ChefAdapterImpl(); + ChefAdapter chefAdapter = new ChefAdapterFactory().create(); chefAdapter.nodeObejctBuilder(params, svcLogicContext); // THEN @@ -87,7 +87,7 @@ public class ChefAdapterImplTest { SvcLogicContext svcLogicContext = new SvcLogicContext(); // WHEN - ChefAdapter chefAdapter = new ChefAdapterImpl(); + ChefAdapter chefAdapter = new ChefAdapterFactory().create(); chefAdapter.combineStrings(params, svcLogicContext); // THEN diff --git a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/PrivateKeyCheckerTest.java b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/PrivateKeyCheckerTest.java new file mode 100644 index 000000000..b5c206753 --- /dev/null +++ b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/PrivateKeyCheckerTest.java @@ -0,0 +1,40 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2018 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.onap.appc.adapter.chef.impl; + +import static junit.framework.TestCase.assertFalse; +import static junit.framework.TestCase.assertTrue; + +import org.junit.Test; + +public class PrivateKeyCheckerTest { + + @Test + public void doesExist_shouldReturnTrue_whenFileExists() { + String pemFilePath = getClass().getResource("/testclient.pem").getPath(); + assertTrue(new PrivateKeyChecker().doesExist(pemFilePath)); + } + + @Test + public void doesExist_shouldReturnFalse_whenFileDoesNotExist() { + assertFalse(new PrivateKeyChecker().doesExist("dummyPemFile")); + } + +}
\ No newline at end of file diff --git a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/TestChefAdapterImpl.java b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/TestChefAdapterImpl.java index d5eb0af36..e7d7e77b9 100644 --- a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/TestChefAdapterImpl.java +++ b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/TestChefAdapterImpl.java @@ -33,6 +33,7 @@ import java.util.Map; import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.onap.appc.adapter.chef.ChefAdapter; import org.onap.appc.exceptions.APPCException; import com.att.cdp.exceptions.ZoneException; import org.onap.ccsdk.sli.core.sli.SvcLogicContext; @@ -41,14 +42,14 @@ import org.onap.ccsdk.sli.core.sli.SvcLogicException; public class TestChefAdapterImpl { private SvcLogicContext svcContext; - private ChefAdapterImpl adapter; + private ChefAdapter adapter; private Map<String, String> params; private String getAttribute; @Before public void setup() { - adapter = new ChefAdapterImpl(); + adapter = new ChefAdapterFactory().create(); params = new HashMap<>(); params.put("pemPath", "/src/test/resources/testclient.pem"); |