158e0c93b2616cd30983f0074cc789a559c14b47
[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
27 import java.util.Properties;
28 import org.junit.Assert;
29 import org.junit.Before;
30 import org.junit.Rule;
31 import org.junit.Test;
32 import org.junit.rules.ExpectedException;
33 import org.onap.appc.flow.controller.data.Transaction;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
35
36 public class TransactionHandlerTest {
37
38   private static final String RESOURCE_URI = "some uri";
39
40   private TransactionHandler transactionHandler;
41   private SvcLogicContext ctx;
42   private Properties prop;
43
44   @Rule
45   public ExpectedException expectedException = ExpectedException.none();
46
47   @Before
48   public void setUp() {
49     transactionHandler = new TransactionHandler();
50     ctx = mock(SvcLogicContext.class);
51     prop = mock(Properties.class);
52   }
53
54   @Test
55   public void should_throw_exception_when_input_request_action_type_missing() throws Exception {
56
57     when(ctx.getAttribute(INPUT_REQUEST_ACTION_TYPE)).thenReturn("");
58
59     expectedException.expect(Exception.class);
60     expectedException.expectMessage("Don't know REST operation for Action");
61     transactionHandler = new TransactionHandler();
62     transactionHandler.buildTransaction(ctx, prop, RESOURCE_URI);
63   }
64
65   @Test
66   public void should_throw_exception_when_input_request_action_missing() throws Exception {
67
68     when(ctx.getAttribute(INPUT_REQUEST_ACTION_TYPE)).thenReturn("foo");
69
70     expectedException.expect(Exception.class);
71     expectedException.expectMessage("Don't know request-action request-action");
72     transactionHandler.buildTransaction(ctx, prop, "some uri");
73   }
74
75   @Test
76   public void should_return_proper_transaction() throws Exception {
77
78     when(ctx.getAttribute(INPUT_REQUEST_ACTION_TYPE)).thenReturn("input-ra-type");
79     when(ctx.getAttribute(INPUT_REQUEST_ACTION)).thenReturn("input-ra");
80
81     when(prop.getProperty(ctx.getAttribute(INPUT_REQUEST_ACTION).concat(".default-rest-user")))
82         .thenReturn("rest-user");
83     when(prop.getProperty(ctx.getAttribute(INPUT_REQUEST_ACTION).concat(".default-rest-pass")))
84         .thenReturn("rest-pass");
85
86     Transaction transaction = transactionHandler.buildTransaction(ctx, prop, "some uri");
87
88     Assert.assertEquals(INPUT_REQUEST_ACTION, transaction.getAction());
89     Assert.assertEquals("input-ra-type", transaction.getExecutionRPC());
90     Assert.assertEquals("some uri", transaction.getExecutionEndPoint());
91     Assert.assertEquals("rest-user", transaction.getuId());
92     Assert.assertEquals("rest-pass", transaction.getPswd());
93   }
94
95 }