Split sequence generation to classess
[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 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  *
19  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  * ============LICENSE_END=========================================================
21  */
22 package org.onap.appc.flow.controller.node;
23
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.InputParamsCollector.SDNC_CONFIG_DIR_VAR;
31 import static org.onap.appc.flow.controller.node.RestServiceNode.REST_RESPONSE;
32 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_PARAM_RESPONSE_PREFIX;
33
34 import java.util.HashMap;
35 import java.util.Map;
36 import java.util.Properties;
37 import org.junit.Before;
38 import org.junit.Ignore;
39 import org.junit.Rule;
40 import org.junit.Test;
41 import org.junit.rules.ExpectedException;
42 import org.onap.appc.flow.controller.data.Transaction;
43 import org.onap.appc.flow.controller.executorImpl.RestExecutor;
44 import org.onap.appc.flow.controller.utils.FlowControllerConstants;
45 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
46 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
47
48 public class TestRestServiceNode {
49
50   private static final String RESOURCE_URI = "resource-uri";
51   private static final String REST_BODY_RESPONSE = "{ 'state' : 'TEST' }";
52
53   private RestServiceNode restServiceNode;
54
55   private ResourceUriExtractor uriExtractorMock;
56   private TransactionHandler transactionHandlerMock;
57   private RestExecutor restExecutorMock;
58
59   private SvcLogicContext ctxMock;
60   private Transaction transaction;
61   private Map<String, String> params;
62
63   @Rule
64   public ExpectedException expectedException = ExpectedException.none();
65
66   @Before
67   public void setUp() throws Exception {
68
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);
74
75     // given
76     params = new HashMap<>();
77
78     HashMap<String, String> restResponseMap = new HashMap<>();
79     restResponseMap.put(REST_RESPONSE, REST_BODY_RESPONSE.replaceAll("'", "\""));
80
81     when(uriExtractorMock
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);
89
90     EnvVariables envVariables = mock(EnvVariables.class);
91     when(envVariables.getenv(SDNC_CONFIG_DIR_VAR)).thenReturn("src/test/resources");
92     restServiceNode = new RestServiceNode(transactionHandlerMock, restExecutorMock, uriExtractorMock, envVariables);
93   }
94
95   @Test
96   public void should_send_request() throws Exception {
97     // given
98     params.put(INPUT_PARAM_RESPONSE_PREFIX, "some-prefix");
99
100     // when
101     restServiceNode.sendRequest(params, ctxMock);
102
103     // then
104     verify(uriExtractorMock)
105         .extractResourceUri(eq(ctxMock), any(Properties.class));
106     verify(transactionHandlerMock)
107         .buildTransaction(eq(ctxMock), any(Properties.class), eq(RESOURCE_URI));
108     verify(restExecutorMock)
109         .execute(transaction, ctxMock);
110     verifyNoMoreInteractions(uriExtractorMock, transactionHandlerMock, restExecutorMock);
111   }
112
113   @Test
114   public void should_rethrow_exception_from_uri_extractor() throws Exception {
115     when(uriExtractorMock
116         .extractResourceUri(eq(ctxMock), any(Properties.class)))
117         .thenThrow(new Exception("resource uri exception"));
118
119     expectedException.expect(SvcLogicException.class);
120     expectedException.expectMessage("resource uri exception");
121
122     restServiceNode.sendRequest(params, ctxMock);
123   }
124
125   @Test
126   public void should_rethrow_exception_from_transaction_handler() throws Exception {
127     when(transactionHandlerMock
128         .buildTransaction(eq(ctxMock), any(Properties.class), eq(RESOURCE_URI)))
129         .thenThrow(new Exception("transaction exception"));
130
131     expectedException.expect(SvcLogicException.class);
132     expectedException.expectMessage("transaction exception");
133
134     restServiceNode.sendRequest(params, ctxMock);
135   }
136
137   @Test
138   public void should_rethrow_exception_from_rest_executor() throws Exception {
139     when(restExecutorMock
140         .execute(transaction, ctxMock))
141         .thenThrow(new Exception("rest executor exception"));
142
143     expectedException.expect(SvcLogicException.class);
144     expectedException.expectMessage("rest executor exception");
145
146     restServiceNode.sendRequest(params, ctxMock);
147   }
148
149   @Ignore("missing asserts")
150   @Test(expected = Exception.class)
151   public void testRestServiceNode() throws Exception {
152
153     SvcLogicContext ctx = new SvcLogicContext();
154     ctx.setAttribute(FlowControllerConstants.VNF_TYPE, "vUSP - vDBE-IPX HUB");
155     ctx.setAttribute(FlowControllerConstants.REQUEST_ACTION, "healthcheck");
156     ctx.setAttribute(FlowControllerConstants.VNFC_TYPE, "TESTVNFC-CF");
157     ctx.setAttribute(FlowControllerConstants.REQUEST_ID, "TESTCOMMONFRMWK");
158     ctx.setAttribute("host-ip-address", "127.0.0.1");
159     ctx.setAttribute("port-number", "8888");
160     ctx.setAttribute("request-action-type", "GET");
161     ctx.setAttribute("context", "loader/restconf/operations/appc-provider-lcm:health-check");
162
163     HashMap<String, String> inParams = new HashMap<String, String>();
164     RestServiceNode rsn = new RestServiceNode();
165     inParams.put("output-state", "state");
166     inParams.put("responsePrefix", "healthcheck");
167     rsn.sendRequest(inParams, ctx);
168
169     for (Object key : ctx.getAttributeKeySet()) {
170       String parmName = (String) key;
171       String parmValue = ctx.getAttribute(parmName);
172     }
173   }
174
175   @Ignore("missing asserts")
176   @Test(expected = Exception.class)
177   public void testInputParamsRestServiceNode() throws Exception {
178     SvcLogicContext ctx = new SvcLogicContext();
179     ctx.setAttribute("vnf-id", "test");
180     ctx.setAttribute("tmp.vnfInfo.vm-count", "1");
181     ctx.setAttribute("tmp.vnfInfo.vm[0].vnfc-count", "1");
182     RestExecutor restExe = new RestExecutor();
183     Transaction transaction = new Transaction();
184
185     FlowControlNode node = new FlowControlNode();
186     Map<String, String> flowSeq = restExe.execute(transaction, ctx);
187     String flowSequnce = flowSeq.get("restResponse");
188
189   }
190 }