2  * ============LICENSE_START=======================================================
 
   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
 
  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.
 
  18  * * ============LICENSE_END=========================================================
 
  20 package org.onap.appc.flow.controller.node;
 
  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.APPC_CONFIG_DIR_VAR;
 
  29 import static org.onap.appc.flow.controller.node.RestServiceNode.REST_RESPONSE;
 
  30 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_PARAM_RESPONSE_PREFIX;
 
  31 import java.util.HashMap;
 
  33 import java.util.Properties;
 
  34 import org.junit.Before;
 
  35 import org.junit.Ignore;
 
  36 import org.junit.Rule;
 
  37 import org.junit.Test;
 
  38 import org.junit.rules.ExpectedException;
 
  39 import org.onap.appc.flow.controller.data.Transaction;
 
  40 import org.onap.appc.flow.controller.executorImpl.RestExecutor;
 
  41 import org.onap.appc.flow.controller.utils.FlowControllerConstants;
 
  42 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 
  43 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
 
  45 public class TestRestServiceNode {
 
  47     private static final String RESOURCE_URI = "resource-uri";
 
  48     private static final String REST_BODY_RESPONSE = "{ 'state' : 'TEST' }";
 
  50     private RestServiceNode restServiceNode;
 
  52     private ResourceUriExtractor uriExtractorMock;
 
  53     private TransactionHandler transactionHandlerMock;
 
  54     private RestExecutor restExecutorMock;
 
  56     private SvcLogicContext ctxMock;
 
  57     private Transaction transaction;
 
  58     private Map<String, String> params;
 
  61     public ExpectedException expectedException = ExpectedException.none();
 
  64     public void setUp() throws Exception {
 
  66         uriExtractorMock = mock(ResourceUriExtractor.class);
 
  67         transactionHandlerMock = mock(TransactionHandler.class);
 
  68         restExecutorMock = mock(RestExecutor.class);
 
  69         ctxMock = mock(SvcLogicContext.class);
 
  70         transaction = mock(Transaction.class);
 
  73         params = new HashMap<>();
 
  75         HashMap<String, String> restResponseMap = new HashMap<>();
 
  76         restResponseMap.put(REST_RESPONSE, REST_BODY_RESPONSE.replaceAll("'", "\""));
 
  78         when(uriExtractorMock.extractResourceUri(any(SvcLogicContext.class), any(Properties.class)))
 
  79                 .thenReturn(RESOURCE_URI);
 
  80         when(transactionHandlerMock.buildTransaction(any(SvcLogicContext.class), any(Properties.class),
 
  81                 eq(RESOURCE_URI))).thenReturn(transaction);
 
  82         when(restExecutorMock.execute(eq(transaction), any(SvcLogicContext.class))).thenReturn(restResponseMap);
 
  84         EnvVariables envVariables = mock(EnvVariables.class);
 
  85         when(envVariables.getenv(APPC_CONFIG_DIR_VAR)).thenReturn("src/test/resources");
 
  86         restServiceNode = new RestServiceNode(transactionHandlerMock, restExecutorMock, uriExtractorMock, envVariables);
 
  90     public void should_send_request() throws Exception {
 
  92         params.put(INPUT_PARAM_RESPONSE_PREFIX, "some-prefix");
 
  95         restServiceNode.sendRequest(params, ctxMock);
 
  98         verify(uriExtractorMock).extractResourceUri(eq(ctxMock), any(Properties.class));
 
  99         verify(transactionHandlerMock).buildTransaction(eq(ctxMock), any(Properties.class), eq(RESOURCE_URI));
 
 100         verify(restExecutorMock).execute(transaction, ctxMock);
 
 101         verifyNoMoreInteractions(uriExtractorMock, transactionHandlerMock, restExecutorMock);
 
 105     public void should_rethrow_exception_from_uri_extractor() throws Exception {
 
 106         when(uriExtractorMock.extractResourceUri(eq(ctxMock), any(Properties.class)))
 
 107                 .thenThrow(new Exception("resource uri exception"));
 
 109         expectedException.expect(SvcLogicException.class);
 
 110         expectedException.expectMessage("resource uri exception");
 
 112         restServiceNode.sendRequest(params, ctxMock);
 
 116     public void should_rethrow_exception_from_transaction_handler() throws Exception {
 
 117         when(transactionHandlerMock.buildTransaction(eq(ctxMock), any(Properties.class), eq(RESOURCE_URI)))
 
 118                 .thenThrow(new Exception("transaction exception"));
 
 120         expectedException.expect(SvcLogicException.class);
 
 121         expectedException.expectMessage("transaction exception");
 
 123         restServiceNode.sendRequest(params, ctxMock);
 
 127     public void should_rethrow_exception_from_rest_executor() throws Exception {
 
 128         when(restExecutorMock.execute(transaction, ctxMock)).thenThrow(new Exception("rest executor exception"));
 
 130         expectedException.expect(SvcLogicException.class);
 
 131         expectedException.expectMessage("rest executor exception");
 
 133         restServiceNode.sendRequest(params, ctxMock);
 
 136     @Ignore("missing asserts")
 
 137     @Test(expected = Exception.class)
 
 138     public void testRestServiceNode() throws Exception {
 
 140         SvcLogicContext ctx = new SvcLogicContext();
 
 141         ctx.setAttribute(FlowControllerConstants.VNF_TYPE, "vUSP - vDBE-IPX HUB");
 
 142         ctx.setAttribute(FlowControllerConstants.REQUEST_ACTION, "healthcheck");
 
 143         ctx.setAttribute(FlowControllerConstants.VNFC_TYPE, "TESTVNFC-CF");
 
 144         ctx.setAttribute(FlowControllerConstants.REQUEST_ID, "TESTCOMMONFRMWK");
 
 145         ctx.setAttribute("host-ip-address", "127.0.0.1");
 
 146         ctx.setAttribute("port-number", "8888");
 
 147         ctx.setAttribute("request-action-type", "GET");
 
 148         ctx.setAttribute("context", "loader/restconf/operations/appc-provider-lcm:health-check");
 
 150         HashMap<String, String> inParams = new HashMap<String, String>();
 
 151         RestServiceNode rsn = new RestServiceNode();
 
 152         inParams.put("output-state", "state");
 
 153         inParams.put("responsePrefix", "healthcheck");
 
 154         rsn.sendRequest(inParams, ctx);
 
 156         for (Object key : ctx.getAttributeKeySet()) {
 
 157             String parmName = (String) key;
 
 158             String parmValue = ctx.getAttribute(parmName);
 
 162     @Ignore("missing asserts")
 
 163     @Test(expected = Exception.class)
 
 164     public void testInputParamsRestServiceNode() throws Exception {
 
 165         SvcLogicContext ctx = new SvcLogicContext();
 
 166         ctx.setAttribute("vnf-id", "test");
 
 167         ctx.setAttribute("tmp.vnfInfo.vm-count", "1");
 
 168         ctx.setAttribute("tmp.vnfInfo.vm[0].vnfc-count", "1");
 
 169         RestExecutor restExe = new RestExecutor();
 
 170         Transaction transaction = new Transaction();
 
 172         FlowControlNode node = new FlowControlNode();
 
 173         Map<String, String> flowSeq = restExe.execute(transaction, ctx);
 
 174         String flowSequnce = flowSeq.get("restResponse");