Code changes to fix Exception
[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 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  *  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.appc.flow.controller.node;
23
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
26 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_REQUEST_ACTION;
27 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_REQUEST_ACTION_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 REST operation for Action");
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 request-action request-action");
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
83     when(prop.getProperty(ctx.getAttribute(INPUT_REQUEST_ACTION).concat(".default-rest-user")))
84         .thenReturn("rest-user");
85     when(prop.getProperty(ctx.getAttribute(INPUT_REQUEST_ACTION).concat(".default-rest-pass")))
86         .thenReturn("rest-pass");
87
88     Transaction transaction = transactionHandler.buildTransaction(ctx, prop, "some uri");
89
90     Assert.assertEquals(INPUT_REQUEST_ACTION, transaction.getAction());
91     Assert.assertEquals("input-ra-type", transaction.getExecutionRPC());
92     Assert.assertEquals("some uri", transaction.getExecutionEndPoint());
93     Assert.assertEquals("rest-user", transaction.getuId());
94     Assert.assertEquals("rest-pass", transaction.getPswd());
95   }
96
97 }