diff options
author | kurczews <krzysztof.kurczewski@nokia.com> | 2018-02-23 14:40:08 +0100 |
---|---|---|
committer | Patrick Brady <pb071s@att.com> | 2018-02-26 19:09:59 +0000 |
commit | 49561fcacc76c5fe6f431d926702bf24b0c7beb3 (patch) | |
tree | 5abe815bc8f6ae44d444965a11603c85e3b0db1f /appc-config/appc-flow-controller/provider/src/test | |
parent | f9f214d530f5d18ca1e36d5c0e12e6f048bdd24d (diff) |
Improve coverage flow/controller/node #3
Issue-ID: APPC-440
Change-Id: I3ffea4577747f5de3e4eb74a91d4485442474441
Signed-off-by: kurczews <krzysztof.kurczewski@nokia.com>
Diffstat (limited to 'appc-config/appc-flow-controller/provider/src/test')
5 files changed, 223 insertions, 44 deletions
diff --git a/appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/JsonValidatorTest.java b/appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/JsonValidatorTest.java new file mode 100644 index 000000000..fd6f92018 --- /dev/null +++ b/appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/JsonValidatorTest.java @@ -0,0 +1,36 @@ +package org.onap.appc.flow.controller.node; + +import com.fasterxml.jackson.databind.JsonNode; +import java.io.IOException; +import org.junit.Assert; +import org.junit.Test; + +public class JsonValidatorTest { + + @Test + public void should_return_json_node_on_valid_json() throws IOException { + String json = "{'test': 'OK'}".replaceAll("'", "\""); + JsonNode result = JsonValidator.validate(json); + + Assert.assertNotNull(result); + Assert.assertTrue(result.has("test")); + Assert.assertEquals("OK", result.get("test").asText()); + } + + @Test + public void should_return_null_on_empty_input() throws IOException { + String json = ""; + JsonNode result = JsonValidator.validate(json); + + Assert.assertNull(result); + } + + @Test + public void should_return_null_on_invalid_input() throws IOException { + String json = "{'test': 'OK'".replaceAll("'", "\""); + JsonNode result = JsonValidator.validate(json); + + Assert.assertNull(result); + } + +}
\ No newline at end of file diff --git a/appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/ResourceUriExtractorTest.java b/appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/ResourceUriExtractorTest.java index 06f86c56f..28a202c2d 100644 --- a/appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/ResourceUriExtractorTest.java +++ b/appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/ResourceUriExtractorTest.java @@ -24,21 +24,24 @@ public class ResourceUriExtractorTest { @Rule public ExpectedException expectedException = ExpectedException.none(); + private ResourceUriExtractor resourceUriExtractor; @Before public void setUp() { ctx = mock(SvcLogicContext.class); prop = mock(Properties.class); + resourceUriExtractor = new ResourceUriExtractor(); } @Test public void should_return_input_url_if_exist() throws Exception { ctx = mock(SvcLogicContext.class); - when(ctx.getAttribute(INPUT_URL)).thenReturn("test resource uri"); + when(ctx.getAttribute(INPUT_URL)).thenReturn("http://localhost:8080"); - String resourceUri = ResourceUriExtractor.extractResourceUri(ctx, prop); + resourceUriExtractor = new ResourceUriExtractor(); + String resourceUri = resourceUriExtractor.extractResourceUri(ctx, prop); - Assert.assertEquals("test resource uri", resourceUri); + Assert.assertEquals("http://localhost:8080", resourceUri); } @Test @@ -50,7 +53,7 @@ public class ResourceUriExtractorTest { when(ctx.getAttribute(INPUT_CONTEXT)).thenReturn("input-context"); when(ctx.getAttribute(INPUT_SUB_CONTEXT)).thenReturn("input-sub-context"); - String resourceUri = ResourceUriExtractor.extractResourceUri(ctx, prop); + String resourceUri = resourceUriExtractor.extractResourceUri(ctx, prop); Assert.assertEquals("http://localhost:8080/input-context/input-sub-context", resourceUri); } @@ -68,7 +71,7 @@ public class ResourceUriExtractorTest { when(prop.getProperty("request-action.context")).thenReturn("ra-context"); when(prop.getProperty("request-action.sub-context")).thenReturn("ra-sub-context"); - String resourceUri = ResourceUriExtractor.extractResourceUri(ctx, prop); + String resourceUri = resourceUriExtractor.extractResourceUri(ctx, prop); Assert.assertEquals("http://localhost:8080/ra-context/ra-sub-context", resourceUri); } @@ -82,7 +85,7 @@ public class ResourceUriExtractorTest { expectedException.expect(Exception.class); expectedException.expectMessage("Could Not found the context for operation null"); - ResourceUriExtractor.extractResourceUri(ctx, prop); + resourceUriExtractor.extractResourceUri(ctx, prop); } }
\ No newline at end of file diff --git a/appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/TestRestServiceNode.java b/appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/TestRestServiceNode.java index a6a207db7..20a1fd3f9 100644 --- a/appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/TestRestServiceNode.java +++ b/appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/TestRestServiceNode.java @@ -1,4 +1,3 @@ - /*- * ============LICENSE_START======================================================= * ONAP : APPC @@ -22,56 +21,169 @@ */ package org.onap.appc.flow.controller.node; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; +import static org.onap.appc.flow.controller.node.RestServiceNode.REST_RESPONSE; +import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_PARAM_RESPONSE_PREFIX; + import java.util.HashMap; import java.util.Map; +import java.util.Properties; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.onap.appc.flow.controller.data.Transaction; import org.onap.appc.flow.controller.executorImpl.RestExecutor; import org.onap.appc.flow.controller.utils.FlowControllerConstants; import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; public class TestRestServiceNode { - @Test(expected=Exception.class) - public void testRestServiceNode() throws Exception { + private static final String RESOURCE_URI = "resource-uri"; + private static final String MOCK_ENV = "src/test/resources"; + private static final String REST_BODY_RESPONSE = "{ 'state' : 'TEST' }"; - SvcLogicContext ctx = new SvcLogicContext(); - ctx.setAttribute(FlowControllerConstants.VNF_TYPE, "vUSP - vDBE-IPX HUB"); - ctx.setAttribute(FlowControllerConstants.REQUEST_ACTION, "healthcheck"); - ctx.setAttribute(FlowControllerConstants.VNFC_TYPE, "TESTVNFC-CF"); - ctx.setAttribute(FlowControllerConstants.REQUEST_ID,"TESTCOMMONFRMWK"); - ctx.setAttribute("host-ip-address","127.0.0.1"); - ctx.setAttribute("port-number","8888"); - ctx.setAttribute("request-action-type","GET"); - ctx.setAttribute("context", "loader/restconf/operations/appc-provider-lcm:health-check"); + private RestServiceNode restServiceNode; - HashMap<String, String> inParams = new HashMap<String, String>(); - RestServiceNode rsn = new RestServiceNode(); - inParams.put("output-state", "state"); - inParams.put("responsePrefix", "healthcheck"); - rsn.sendRequest(inParams, ctx); + private ResourceUriExtractor uriExtractorMock; + private TransactionHandler transactionHandlerMock; + private RestExecutor restExecutorMock; - for (Object key : ctx.getAttributeKeySet()) { - String parmName = (String) key; - String parmValue = ctx.getAttribute(parmName); - } + private SvcLogicContext ctxMock; + private Transaction transaction; + private Map<String, String> params; + @Rule + public ExpectedException expectedException = ExpectedException.none(); - } + @Before + public void setUp() throws Exception { + + uriExtractorMock = mock(ResourceUriExtractor.class); + transactionHandlerMock = mock(TransactionHandler.class); + restExecutorMock = mock(RestExecutor.class); + ctxMock = mock(SvcLogicContext.class); + transaction = mock(Transaction.class); + + // given + params = new HashMap<>(); + + HashMap<String, String> restResponseMap = new HashMap<>(); + restResponseMap.put(REST_RESPONSE, REST_BODY_RESPONSE.replaceAll("'", "\"")); + + when(uriExtractorMock + .extractResourceUri(any(SvcLogicContext.class), any(Properties.class))) + .thenReturn(RESOURCE_URI); + when(transactionHandlerMock + .buildTransaction(any(SvcLogicContext.class), any(Properties.class), eq(RESOURCE_URI))) + .thenReturn(transaction); + when(restExecutorMock.execute(eq(transaction), any(SvcLogicContext.class))) + .thenReturn(restResponseMap); + + EnvVariables envVariables = new EnvVariables(envKey -> MOCK_ENV); + restServiceNode = new RestServiceNode(transactionHandlerMock, restExecutorMock, uriExtractorMock, envVariables); + } + + @Test + public void should_send_request() throws Exception { + // given + params.put(INPUT_PARAM_RESPONSE_PREFIX, "some-prefix"); + + // when + restServiceNode.sendRequest(params, ctxMock); + + // then + verify(uriExtractorMock) + .extractResourceUri(eq(ctxMock), any(Properties.class)); + verify(transactionHandlerMock) + .buildTransaction(eq(ctxMock), any(Properties.class), eq(RESOURCE_URI)); + verify(restExecutorMock) + .execute(transaction, ctxMock); + verifyNoMoreInteractions(uriExtractorMock, transactionHandlerMock, restExecutorMock); + } + + @Test + public void should_rethrow_exception_from_uri_extractor() throws Exception { + when(uriExtractorMock + .extractResourceUri(eq(ctxMock), any(Properties.class))) + .thenThrow(new Exception("resource uri exception")); + expectedException.expect(SvcLogicException.class); + expectedException.expectMessage("resource uri exception"); - @Test(expected=Exception.class) - public void testInputParamsRestServiceNode() throws Exception { - SvcLogicContext ctx = new SvcLogicContext(); - ctx.setAttribute("vnf-id", "test"); - ctx.setAttribute("tmp.vnfInfo.vm-count", "1"); - ctx.setAttribute("tmp.vnfInfo.vm[0].vnfc-count", "1"); - RestExecutor restExe = new RestExecutor(); - Transaction transaction = new Transaction(); + restServiceNode.sendRequest(params, ctxMock); + } - FlowControlNode node = new FlowControlNode(); - Map<String,String> flowSeq= restExe.execute(transaction, ctx); - String flowSequnce=flowSeq.get("restResponse"); + @Test + public void should_rethrow_exception_from_transaction_handler() throws Exception { + when(transactionHandlerMock + .buildTransaction(eq(ctxMock), any(Properties.class), eq(RESOURCE_URI))) + .thenThrow(new Exception("transaction exception")); + expectedException.expect(SvcLogicException.class); + expectedException.expectMessage("transaction exception"); + + restServiceNode.sendRequest(params, ctxMock); + } + + @Test + public void should_rethrow_exception_from_rest_executor() throws Exception { + when(restExecutorMock + .execute(transaction, ctxMock)) + .thenThrow(new Exception("rest executor exception")); + + expectedException.expect(SvcLogicException.class); + expectedException.expectMessage("rest executor exception"); + + restServiceNode.sendRequest(params, ctxMock); + } + + @Ignore("missing asserts") + @Test(expected = Exception.class) + public void testRestServiceNode() throws Exception { + + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute(FlowControllerConstants.VNF_TYPE, "vUSP - vDBE-IPX HUB"); + ctx.setAttribute(FlowControllerConstants.REQUEST_ACTION, "healthcheck"); + ctx.setAttribute(FlowControllerConstants.VNFC_TYPE, "TESTVNFC-CF"); + ctx.setAttribute(FlowControllerConstants.REQUEST_ID, "TESTCOMMONFRMWK"); + ctx.setAttribute("host-ip-address", "127.0.0.1"); + ctx.setAttribute("port-number", "8888"); + ctx.setAttribute("request-action-type", "GET"); + ctx.setAttribute("context", "loader/restconf/operations/appc-provider-lcm:health-check"); + + HashMap<String, String> inParams = new HashMap<String, String>(); + RestServiceNode rsn = new RestServiceNode(); + inParams.put("output-state", "state"); + inParams.put("responsePrefix", "healthcheck"); + rsn.sendRequest(inParams, ctx); + + for (Object key : ctx.getAttributeKeySet()) { + String parmName = (String) key; + String parmValue = ctx.getAttribute(parmName); } + } + + @Ignore("missing asserts") + @Test(expected = Exception.class) + public void testInputParamsRestServiceNode() throws Exception { + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("vnf-id", "test"); + ctx.setAttribute("tmp.vnfInfo.vm-count", "1"); + ctx.setAttribute("tmp.vnfInfo.vm[0].vnfc-count", "1"); + RestExecutor restExe = new RestExecutor(); + Transaction transaction = new Transaction(); + + FlowControlNode node = new FlowControlNode(); + Map<String, String> flowSeq = restExe.execute(transaction, ctx); + String flowSequnce = flowSeq.get("restResponse"); + + } } diff --git a/appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/TransactionHandlerTest.java b/appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/TransactionHandlerTest.java index 5cdb062f4..f16dd65cc 100644 --- a/appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/TransactionHandlerTest.java +++ b/appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/TransactionHandlerTest.java @@ -18,6 +18,7 @@ public class TransactionHandlerTest { private static final String RESOURCE_URI = "some uri"; + private TransactionHandler transactionHandler; private SvcLogicContext ctx; private Properties prop; @@ -26,6 +27,7 @@ public class TransactionHandlerTest { @Before public void setUp() { + transactionHandler = new TransactionHandler(); ctx = mock(SvcLogicContext.class); prop = mock(Properties.class); } @@ -35,9 +37,10 @@ public class TransactionHandlerTest { when(ctx.getAttribute(INPUT_REQUEST_ACTION_TYPE)).thenReturn(""); - expectedException.expect(IllegalArgumentException.class); + expectedException.expect(Exception.class); expectedException.expectMessage("Don't know REST operation for Action"); - TransactionHandler.buildTransaction(ctx, prop, RESOURCE_URI); + transactionHandler = new TransactionHandler(); + transactionHandler.buildTransaction(ctx, prop, RESOURCE_URI); } @Test @@ -45,9 +48,9 @@ public class TransactionHandlerTest { when(ctx.getAttribute(INPUT_REQUEST_ACTION_TYPE)).thenReturn("foo"); - expectedException.expect(IllegalArgumentException.class); + expectedException.expect(Exception.class); expectedException.expectMessage("Don't know request-action request-action"); - TransactionHandler.buildTransaction(ctx, prop, "some uri"); + transactionHandler.buildTransaction(ctx, prop, "some uri"); } @Test @@ -61,7 +64,7 @@ public class TransactionHandlerTest { when(prop.getProperty(ctx.getAttribute(INPUT_REQUEST_ACTION).concat(".default-rest-pass"))) .thenReturn("rest-pass"); - Transaction transaction = TransactionHandler.buildTransaction(ctx, prop, "some uri"); + Transaction transaction = transactionHandler.buildTransaction(ctx, prop, "some uri"); Assert.assertEquals(INPUT_REQUEST_ACTION, transaction.getAction()); Assert.assertEquals("input-ra-type", transaction.getExecutionRPC()); diff --git a/appc-config/appc-flow-controller/provider/src/test/resources/appc-flow-controller.properties b/appc-config/appc-flow-controller/provider/src/test/resources/appc-flow-controller.properties new file mode 100644 index 000000000..0bbe7f207 --- /dev/null +++ b/appc-config/appc-flow-controller/provider/src/test/resources/appc-flow-controller.properties @@ -0,0 +1,25 @@ +### +# ============LICENSE_START======================================================= +# ONAP : APP-C +# ================================================================================ +# Copyright (C) 2017 AT&T 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========================================================= +### + +# test properties used by RestServiceNode + +healthcheck.mock=false +healthcheck.default-rest-user=User +healthcheck.default-rest-pass=@#asd723%^ |