Changes needed for MultiStep Actions
[appc.git] / appc-config / appc-flow-controller / provider / src / test / java / org / onap / appc / flow / controller / node / ResourceUriExtractorTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2018 Nokia. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
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_CONTEXT;
27 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_HOST_IP_ADDRESS;
28 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_REQUEST_ACTION;
29 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_SUB_CONTEXT;
30 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_URL;
31 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.REST_CONTEXT_URL;
32 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.REST_PORT;
33 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.REST_PROTOCOL;
34 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VNF_TYPE;
35 import java.util.Properties;
36 import org.junit.Assert;
37 import org.junit.Before;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.rules.ExpectedException;
41 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
42
43 public class ResourceUriExtractorTest {
44
45     private Properties prop;
46     private SvcLogicContext ctx;
47
48     @Rule
49     public ExpectedException expectedException = ExpectedException.none();
50     private ResourceUriExtractor resourceUriExtractor;
51
52     @Before
53     public void setUp() {
54         ctx = mock(SvcLogicContext.class);
55         prop = mock(Properties.class);
56         resourceUriExtractor = new ResourceUriExtractor();
57     }
58
59     @Test
60     public void should_return_input_url_if_exist() throws Exception {
61         ctx = mock(SvcLogicContext.class);
62         when(ctx.getAttribute(INPUT_URL)).thenReturn("http://localhost:8080");
63
64         resourceUriExtractor = new ResourceUriExtractor();
65         String resourceUri = resourceUriExtractor.extractResourceUri(ctx);
66
67         Assert.assertEquals("http://localhost:8080", resourceUri);
68     }
69
70     @Test
71     public void should_extract_url_input_if_context_input_provided() throws Exception {
72         when(ctx.getAttribute(INPUT_URL)).thenReturn("");
73         when(ctx.getAttribute(INPUT_HOST_IP_ADDRESS)).thenReturn("localhost");
74         when(ctx.getAttribute(REST_PORT)).thenReturn("8080");
75
76         when(ctx.getAttribute(INPUT_CONTEXT)).thenReturn("input-context");
77
78         String resourceUri = resourceUriExtractor.extractResourceUri(ctx);
79
80         Assert.assertEquals("http://localhost:8080/input-context", resourceUri);
81     }
82
83     @Test
84     public void should_extract_url_input_if_request_action_provided() throws Exception {
85         when(ctx.getAttribute(INPUT_REQUEST_ACTION)).thenReturn("request-action");
86         when(ctx.getAttribute(INPUT_URL)).thenReturn("");
87         when(ctx.getAttribute(INPUT_HOST_IP_ADDRESS)).thenReturn("localhost");
88         when(ctx.getAttribute(REST_PORT)).thenReturn("8080");
89         when(ctx.getAttribute(INPUT_CONTEXT)).thenReturn("ra-context");
90         when(prop.getProperty("request-action.sub-context")).thenReturn("ra-sub-context");
91
92         String resourceUri = resourceUriExtractor.extractResourceUri(ctx);
93
94         Assert.assertEquals("http://localhost:8080/ra-context", resourceUri);
95     }
96
97     @Test
98     public void should_throw_exception_if_missing_context() throws Exception {
99         when(ctx.getAttribute(INPUT_URL)).thenReturn("");
100         when(ctx.getAttribute(INPUT_HOST_IP_ADDRESS)).thenReturn("localhost");
101         when(ctx.getAttribute(INPUT_HOST_IP_ADDRESS)).thenReturn("localhost");
102         when(prop.getProperty(ctx.getAttribute(VNF_TYPE)+"."+(REST_PROTOCOL)+"."+ctx.getAttribute(INPUT_REQUEST_ACTION)
103             +"."+(REST_PORT))).thenReturn("8080");
104
105         expectedException.expect(Exception.class);
106         expectedException.expectMessage("Could not find the context for operation null");
107
108         resourceUriExtractor.extractResourceUri(ctx);
109     }
110
111 }