2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2018 Nokia. 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.Mockito.mock;
 
  23 import static org.mockito.Mockito.when;
 
  24 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_REQUEST_ACTION;
 
  25 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_REQUEST_ACTION_TYPE;
 
  26 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.REST_PROTOCOL;
 
  27 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VNF_TYPE;
 
  29 import java.util.Properties;
 
  30 import org.junit.Assert;
 
  31 import org.junit.Before;
 
  32 import org.junit.Rule;
 
  33 import org.junit.Test;
 
  34 import org.junit.rules.ExpectedException;
 
  35 import org.onap.appc.flow.controller.data.Transaction;
 
  36 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 
  38 public class TransactionHandlerTest {
 
  40   private static final String RESOURCE_URI = "some uri";
 
  42   private TransactionHandler transactionHandler;
 
  43   private SvcLogicContext ctx;
 
  44   private Properties prop;
 
  47   public ExpectedException expectedException = ExpectedException.none();
 
  51     transactionHandler = new TransactionHandler();
 
  52     ctx = mock(SvcLogicContext.class);
 
  53     prop = mock(Properties.class);
 
  57   public void should_throw_exception_when_input_request_action_type_missing() throws Exception {
 
  59     when(ctx.getAttribute(INPUT_REQUEST_ACTION_TYPE)).thenReturn("");
 
  61     expectedException.expect(Exception.class);
 
  62     expectedException.expectMessage("Don't know vnf type to send REST request for  request-action - null");
 
  63     transactionHandler = new TransactionHandler();
 
  64     transactionHandler.buildTransaction(ctx, prop, RESOURCE_URI);
 
  68   public void should_throw_exception_when_input_request_action_missing() throws Exception {
 
  70     when(ctx.getAttribute(INPUT_REQUEST_ACTION_TYPE)).thenReturn("foo");
 
  72     expectedException.expect(Exception.class);
 
  73     expectedException.expectMessage("Don't know vnf type to send REST request for  request-action - null");
 
  74     transactionHandler.buildTransaction(ctx, prop, "some uri");
 
  78   public void should_return_proper_transaction() throws Exception {
 
  80     when(ctx.getAttribute(INPUT_REQUEST_ACTION_TYPE)).thenReturn("input-ra-type");
 
  81     when(ctx.getAttribute(INPUT_REQUEST_ACTION)).thenReturn("input-ra");
 
  82     when(ctx.getAttribute(VNF_TYPE)).thenReturn("vnf");
 
  84     String userKey = ctx.getAttribute(VNF_TYPE) + "." + REST_PROTOCOL + "." + ctx.getAttribute(INPUT_REQUEST_ACTION) 
 
  86     String passwordKey = ctx.getAttribute(VNF_TYPE) + "." + REST_PROTOCOL + "." + ctx.getAttribute(INPUT_REQUEST_ACTION) 
 
  89     when(prop.getProperty(userKey))
 
  90         .thenReturn("rest-user");
 
  91     when(prop.getProperty(passwordKey))
 
  92         .thenReturn("rest-pass");
 
  94     Transaction transaction = transactionHandler.buildTransaction(ctx, prop, "some uri");
 
  96     Assert.assertEquals(INPUT_REQUEST_ACTION, transaction.getAction());
 
  97     Assert.assertEquals("input-ra-type", transaction.getExecutionRPC());
 
  98     Assert.assertEquals("some uri", transaction.getExecutionEndPoint());
 
  99     Assert.assertEquals("rest-user", transaction.getuId());
 
 100     Assert.assertEquals("rest-pass", transaction.getPswd());