2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * =============================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
20 * ============LICENSE_END=========================================================
22 package org.onap.appc.flow.controller.node;
24 import static org.mockito.Matchers.any;
25 import static org.mockito.Matchers.eq;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.verifyNoMoreInteractions;
29 import static org.mockito.Mockito.when;
30 import static org.onap.appc.flow.controller.node.RestServiceNode.REST_RESPONSE;
31 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_PARAM_RESPONSE_PREFIX;
33 import java.util.HashMap;
35 import java.util.Properties;
36 import org.junit.Before;
37 import org.junit.Ignore;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.rules.ExpectedException;
41 import org.onap.appc.flow.controller.data.Transaction;
42 import org.onap.appc.flow.controller.executorImpl.RestExecutor;
43 import org.onap.appc.flow.controller.utils.FlowControllerConstants;
44 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
45 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
47 public class TestRestServiceNode {
49 private static final String RESOURCE_URI = "resource-uri";
50 private static final String MOCK_ENV = "src/test/resources";
51 private static final String REST_BODY_RESPONSE = "{ 'state' : 'TEST' }";
53 private RestServiceNode restServiceNode;
55 private ResourceUriExtractor uriExtractorMock;
56 private TransactionHandler transactionHandlerMock;
57 private RestExecutor restExecutorMock;
59 private SvcLogicContext ctxMock;
60 private Transaction transaction;
61 private Map<String, String> params;
64 public ExpectedException expectedException = ExpectedException.none();
67 public void setUp() throws Exception {
69 uriExtractorMock = mock(ResourceUriExtractor.class);
70 transactionHandlerMock = mock(TransactionHandler.class);
71 restExecutorMock = mock(RestExecutor.class);
72 ctxMock = mock(SvcLogicContext.class);
73 transaction = mock(Transaction.class);
76 params = new HashMap<>();
78 HashMap<String, String> restResponseMap = new HashMap<>();
79 restResponseMap.put(REST_RESPONSE, REST_BODY_RESPONSE.replaceAll("'", "\""));
82 .extractResourceUri(any(SvcLogicContext.class), any(Properties.class)))
83 .thenReturn(RESOURCE_URI);
84 when(transactionHandlerMock
85 .buildTransaction(any(SvcLogicContext.class), any(Properties.class), eq(RESOURCE_URI)))
86 .thenReturn(transaction);
87 when(restExecutorMock.execute(eq(transaction), any(SvcLogicContext.class)))
88 .thenReturn(restResponseMap);
90 EnvVariables envVariables = new EnvVariables(envKey -> MOCK_ENV);
91 restServiceNode = new RestServiceNode(transactionHandlerMock, restExecutorMock, uriExtractorMock, envVariables);
95 public void should_send_request() throws Exception {
97 params.put(INPUT_PARAM_RESPONSE_PREFIX, "some-prefix");
100 restServiceNode.sendRequest(params, ctxMock);
103 verify(uriExtractorMock)
104 .extractResourceUri(eq(ctxMock), any(Properties.class));
105 verify(transactionHandlerMock)
106 .buildTransaction(eq(ctxMock), any(Properties.class), eq(RESOURCE_URI));
107 verify(restExecutorMock)
108 .execute(transaction, ctxMock);
109 verifyNoMoreInteractions(uriExtractorMock, transactionHandlerMock, restExecutorMock);
113 public void should_rethrow_exception_from_uri_extractor() throws Exception {
114 when(uriExtractorMock
115 .extractResourceUri(eq(ctxMock), any(Properties.class)))
116 .thenThrow(new Exception("resource uri exception"));
118 expectedException.expect(SvcLogicException.class);
119 expectedException.expectMessage("resource uri exception");
121 restServiceNode.sendRequest(params, ctxMock);
125 public void should_rethrow_exception_from_transaction_handler() throws Exception {
126 when(transactionHandlerMock
127 .buildTransaction(eq(ctxMock), any(Properties.class), eq(RESOURCE_URI)))
128 .thenThrow(new Exception("transaction exception"));
130 expectedException.expect(SvcLogicException.class);
131 expectedException.expectMessage("transaction exception");
133 restServiceNode.sendRequest(params, ctxMock);
137 public void should_rethrow_exception_from_rest_executor() throws Exception {
138 when(restExecutorMock
139 .execute(transaction, ctxMock))
140 .thenThrow(new Exception("rest executor exception"));
142 expectedException.expect(SvcLogicException.class);
143 expectedException.expectMessage("rest executor exception");
145 restServiceNode.sendRequest(params, ctxMock);
148 @Ignore("missing asserts")
149 @Test(expected = Exception.class)
150 public void testRestServiceNode() throws Exception {
152 SvcLogicContext ctx = new SvcLogicContext();
153 ctx.setAttribute(FlowControllerConstants.VNF_TYPE, "vUSP - vDBE-IPX HUB");
154 ctx.setAttribute(FlowControllerConstants.REQUEST_ACTION, "healthcheck");
155 ctx.setAttribute(FlowControllerConstants.VNFC_TYPE, "TESTVNFC-CF");
156 ctx.setAttribute(FlowControllerConstants.REQUEST_ID, "TESTCOMMONFRMWK");
157 ctx.setAttribute("host-ip-address", "127.0.0.1");
158 ctx.setAttribute("port-number", "8888");
159 ctx.setAttribute("request-action-type", "GET");
160 ctx.setAttribute("context", "loader/restconf/operations/appc-provider-lcm:health-check");
162 HashMap<String, String> inParams = new HashMap<String, String>();
163 RestServiceNode rsn = new RestServiceNode();
164 inParams.put("output-state", "state");
165 inParams.put("responsePrefix", "healthcheck");
166 rsn.sendRequest(inParams, ctx);
168 for (Object key : ctx.getAttributeKeySet()) {
169 String parmName = (String) key;
170 String parmValue = ctx.getAttribute(parmName);
174 @Ignore("missing asserts")
175 @Test(expected = Exception.class)
176 public void testInputParamsRestServiceNode() throws Exception {
177 SvcLogicContext ctx = new SvcLogicContext();
178 ctx.setAttribute("vnf-id", "test");
179 ctx.setAttribute("tmp.vnfInfo.vm-count", "1");
180 ctx.setAttribute("tmp.vnfInfo.vm[0].vnfc-count", "1");
181 RestExecutor restExe = new RestExecutor();
182 Transaction transaction = new Transaction();
184 FlowControlNode node = new FlowControlNode();
185 Map<String, String> flowSeq = restExe.execute(transaction, ctx);
186 String flowSequnce = flowSeq.get("restResponse");