Changes needed for MultiStep Actions
[appc.git] / appc-config / appc-flow-controller / provider / src / test / java / org / onap / appc / flow / controller / node / TestRestServiceNode.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  * * ============LICENSE_END=========================================================
19  */
20 package org.onap.appc.flow.controller.node;
21
22 import static org.mockito.Matchers.any;
23 import static org.mockito.Matchers.eq;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.verifyNoMoreInteractions;
27 import static org.mockito.Mockito.when;
28 import static org.onap.appc.flow.controller.node.RestServiceNode.REST_RESPONSE;
29 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_PARAM_RESPONSE_PREFIX;
30 import java.util.HashMap;
31 import java.util.Map;
32 import org.junit.Before;
33 import org.junit.Ignore;
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.junit.rules.ExpectedException;
37 import org.onap.appc.flow.controller.data.Transaction;
38 import org.onap.appc.flow.controller.executorImpl.RestExecutor;
39 import org.onap.appc.flow.controller.utils.FlowControllerConstants;
40 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
41 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
42
43 public class TestRestServiceNode {
44
45     private static final String RESOURCE_URI = "resource-uri";
46     private static final String REST_BODY_RESPONSE = "{ 'state' : 'TEST' }";
47
48     private RestServiceNode restServiceNode;
49
50     private ResourceUriExtractor uriExtractorMock;
51     private TransactionHandler transactionHandlerMock;
52     private RestExecutor restExecutorMock;
53
54     private SvcLogicContext ctxMock;
55     private Transaction transaction;
56     private Map<String, String> params;
57
58     @Rule
59     public ExpectedException expectedException = ExpectedException.none();
60
61     @Before
62     public void setUp() throws Exception {
63
64         uriExtractorMock = mock(ResourceUriExtractor.class);
65         transactionHandlerMock = mock(TransactionHandler.class);
66         restExecutorMock = mock(RestExecutor.class);
67         ctxMock = mock(SvcLogicContext.class);
68         transaction = mock(Transaction.class);
69
70         // given
71         params = new HashMap<>();
72
73         HashMap<String, String> restResponseMap = new HashMap<>();
74         restResponseMap.put(REST_RESPONSE, REST_BODY_RESPONSE.replaceAll("'", "\""));
75
76         when(uriExtractorMock.extractResourceUri(any(SvcLogicContext.class)))
77                 .thenReturn(RESOURCE_URI);
78         when(transactionHandlerMock.buildTransaction(any(SvcLogicContext.class),
79                 eq(RESOURCE_URI))).thenReturn(transaction);
80         when(restExecutorMock.execute(eq(transaction), any(SvcLogicContext.class))).thenReturn(restResponseMap);
81
82         EnvVariables envVariables = mock(EnvVariables.class);
83         restServiceNode = new RestServiceNode(transactionHandlerMock, restExecutorMock, uriExtractorMock, envVariables);
84     }
85
86     @Test
87     public void should_send_request() throws Exception {
88         // given
89         params.put(INPUT_PARAM_RESPONSE_PREFIX, "some-prefix");
90
91         // when
92         restServiceNode.sendRequest(params, ctxMock);
93
94         // then
95         verify(uriExtractorMock).extractResourceUri(eq(ctxMock));
96         verify(transactionHandlerMock).buildTransaction(eq(ctxMock), eq(RESOURCE_URI));
97         verify(restExecutorMock).execute(transaction, ctxMock);
98         verifyNoMoreInteractions(uriExtractorMock, transactionHandlerMock, restExecutorMock);
99     }
100
101     @Test
102     public void should_rethrow_exception_from_uri_extractor() throws Exception {
103         when(uriExtractorMock.extractResourceUri(eq(ctxMock)))
104                 .thenThrow(new Exception("resource uri exception"));
105
106         expectedException.expect(SvcLogicException.class);
107         expectedException.expectMessage("resource uri exception");
108
109         restServiceNode.sendRequest(params, ctxMock);
110     }
111
112     @Test
113     public void should_rethrow_exception_from_transaction_handler() throws Exception {
114         when(transactionHandlerMock.buildTransaction(eq(ctxMock), eq(RESOURCE_URI)))
115                 .thenThrow(new Exception("transaction exception"));
116
117         expectedException.expect(SvcLogicException.class);
118         expectedException.expectMessage("transaction exception");
119
120         restServiceNode.sendRequest(params, ctxMock);
121     }
122
123     @Test
124     public void should_rethrow_exception_from_rest_executor() throws Exception {
125         when(restExecutorMock.execute(transaction, ctxMock)).thenThrow(new Exception("rest executor exception"));
126
127         expectedException.expect(SvcLogicException.class);
128         expectedException.expectMessage("rest executor exception");
129
130         restServiceNode.sendRequest(params, ctxMock);
131     }
132
133     @Ignore("missing asserts")
134     @Test(expected = Exception.class)
135     public void testRestServiceNode() throws Exception {
136
137         SvcLogicContext ctx = new SvcLogicContext();
138         ctx.setAttribute(FlowControllerConstants.VNF_TYPE, "vUSP - vDBE-IPX HUB");
139         ctx.setAttribute(FlowControllerConstants.REQUEST_ACTION, "healthcheck");
140         ctx.setAttribute(FlowControllerConstants.VNFC_TYPE, "TESTVNFC-CF");
141         ctx.setAttribute(FlowControllerConstants.REQUEST_ID, "TESTCOMMONFRMWK");
142         ctx.setAttribute("host-ip-address", "127.0.0.1");
143         ctx.setAttribute("port-number", "8888");
144         ctx.setAttribute("request-action-type", "GET");
145         ctx.setAttribute("context", "loader/restconf/operations/appc-provider-lcm:health-check");
146
147         HashMap<String, String> inParams = new HashMap<String, String>();
148         RestServiceNode rsn = new RestServiceNode();
149         inParams.put("output-state", "state");
150         inParams.put("responsePrefix", "healthcheck");
151         rsn.sendRequest(inParams, ctx);
152
153         for (Object key : ctx.getAttributeKeySet()) {
154             String parmName = (String) key;
155             String parmValue = ctx.getAttribute(parmName);
156         }
157     }
158
159     @Ignore("missing asserts")
160     @Test(expected = Exception.class)
161     public void testInputParamsRestServiceNode() throws Exception {
162         SvcLogicContext ctx = new SvcLogicContext();
163         ctx.setAttribute("vnf-id", "test");
164         ctx.setAttribute("tmp.vnfInfo.vm-count", "1");
165         ctx.setAttribute("tmp.vnfInfo.vm[0].vnfc-count", "1");
166         RestExecutor restExe = new RestExecutor();
167         Transaction transaction = new Transaction();
168
169         FlowControlNode node = new FlowControlNode();
170         Map<String, String> flowSeq = restExe.execute(transaction, ctx);
171         String flowSequnce = flowSeq.get("restResponse");
172
173     }
174 }