Healthcheck bug fixes
[appc.git] / appc-config / appc-flow-controller / provider / src / test / java / org / onap / appc / flow / controller / node / TransactionHandlerTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
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.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;
28
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;
37
38 public class TransactionHandlerTest {
39
40   private static final String RESOURCE_URI = "some uri";
41
42   private TransactionHandler transactionHandler;
43   private SvcLogicContext ctx;
44   private Properties prop;
45
46   @Rule
47   public ExpectedException expectedException = ExpectedException.none();
48
49   @Before
50   public void setUp() {
51     transactionHandler = new TransactionHandler();
52     ctx = mock(SvcLogicContext.class);
53     prop = mock(Properties.class);
54   }
55
56   @Test
57   public void should_throw_exception_when_input_request_action_type_missing() throws Exception {
58
59     when(ctx.getAttribute(INPUT_REQUEST_ACTION_TYPE)).thenReturn("");
60
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);
65   }
66
67   @Test
68   public void should_throw_exception_when_input_request_action_missing() throws Exception {
69
70     when(ctx.getAttribute(INPUT_REQUEST_ACTION_TYPE)).thenReturn("foo");
71
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");
75   }
76
77   @Test
78   public void should_return_proper_transaction() throws Exception {
79
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");
83     
84     String userKey = ctx.getAttribute(VNF_TYPE) + "." + REST_PROTOCOL + "." + ctx.getAttribute(INPUT_REQUEST_ACTION) 
85                      + ".user";
86     String passwordKey = ctx.getAttribute(VNF_TYPE) + "." + REST_PROTOCOL + "." + ctx.getAttribute(INPUT_REQUEST_ACTION) 
87                      + ".password";
88
89     when(prop.getProperty(userKey))
90         .thenReturn("rest-user");
91     when(prop.getProperty(passwordKey))
92         .thenReturn("rest-pass");
93
94     Transaction transaction = transactionHandler.buildTransaction(ctx, prop, "some uri");
95
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());
101   }
102
103 }