Improve coverage flow/controller/node #3
[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.RestServiceNode.REST_RESPONSE;
31 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_PARAM_RESPONSE_PREFIX;
32
33 import java.util.HashMap;
34 import java.util.Map;
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;
46
47 public class TestRestServiceNode {
48
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' }";
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 = new EnvVariables(envKey -> MOCK_ENV);
91     restServiceNode = new RestServiceNode(transactionHandlerMock, restExecutorMock, uriExtractorMock, envVariables);
92   }
93
94   @Test
95   public void should_send_request() throws Exception {
96     // given
97     params.put(INPUT_PARAM_RESPONSE_PREFIX, "some-prefix");
98
99     // when
100     restServiceNode.sendRequest(params, ctxMock);
101
102     // then
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);
110   }
111
112   @Test
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"));
117
118     expectedException.expect(SvcLogicException.class);
119     expectedException.expectMessage("resource uri exception");
120
121     restServiceNode.sendRequest(params, ctxMock);
122   }
123
124   @Test
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"));
129
130     expectedException.expect(SvcLogicException.class);
131     expectedException.expectMessage("transaction exception");
132
133     restServiceNode.sendRequest(params, ctxMock);
134   }
135
136   @Test
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"));
141
142     expectedException.expect(SvcLogicException.class);
143     expectedException.expectMessage("rest executor exception");
144
145     restServiceNode.sendRequest(params, ctxMock);
146   }
147
148   @Ignore("missing asserts")
149   @Test(expected = Exception.class)
150   public void testRestServiceNode() throws Exception {
151
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");
161
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);
167
168     for (Object key : ctx.getAttributeKeySet()) {
169       String parmName = (String) key;
170       String parmValue = ctx.getAttribute(parmName);
171     }
172   }
173
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();
183
184     FlowControlNode node = new FlowControlNode();
185     Map<String, String> flowSeq = restExe.execute(transaction, ctx);
186     String flowSequnce = flowSeq.get("restResponse");
187
188   }
189 }