34227587a2beb4907356dc5974f7039fa2a4eee3
[appc.git] /
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, prop);
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(prop.getProperty(ctx.getAttribute(VNF_TYPE) + "." + (REST_PROTOCOL) + "."
75                 + ctx.getAttribute(INPUT_REQUEST_ACTION) + "." + (REST_PORT))).thenReturn("8080");
76
77         when(ctx.getAttribute(INPUT_CONTEXT)).thenReturn("input-context");
78         when(ctx.getAttribute(INPUT_SUB_CONTEXT)).thenReturn("input-sub-context");
79
80         String resourceUri = resourceUriExtractor.extractResourceUri(ctx, prop);
81
82         Assert.assertEquals("http://localhost:8080/input-context/input-sub-context", resourceUri);
83     }
84
85     @Test
86     public void should_extract_url_input_if_request_action_provided() throws Exception {
87         when(ctx.getAttribute(INPUT_REQUEST_ACTION)).thenReturn("request-action");
88         when(ctx.getAttribute(INPUT_URL)).thenReturn("");
89         when(ctx.getAttribute(INPUT_HOST_IP_ADDRESS)).thenReturn("localhost");
90         when(prop.getProperty(ctx.getAttribute(VNF_TYPE) + "." + (REST_PROTOCOL) + "."
91                 + ctx.getAttribute(INPUT_REQUEST_ACTION) + "." + (REST_PORT))).thenReturn("8080");
92
93         when(prop.getProperty(ctx.getAttribute(VNF_TYPE) + "." + REST_PROTOCOL + "."
94                 + ctx.getAttribute(INPUT_REQUEST_ACTION) + "." + REST_CONTEXT_URL)).thenReturn("ra-context");
95         when(prop.getProperty("request-action.sub-context")).thenReturn("ra-sub-context");
96
97         String resourceUri = resourceUriExtractor.extractResourceUri(ctx, prop);
98
99         Assert.assertEquals("http://localhost:8080/ra-context/ra-sub-context", resourceUri);
100     }
101
102     @Test
103     public void should_throw_exception_if_missing_context() throws Exception {
104         when(ctx.getAttribute(INPUT_URL)).thenReturn("");
105         when(ctx.getAttribute(INPUT_HOST_IP_ADDRESS)).thenReturn("localhost");
106         when(prop.getProperty(ctx.getAttribute(VNF_TYPE) + "." + (REST_PROTOCOL) + "."
107                 + ctx.getAttribute(INPUT_REQUEST_ACTION) + "." + (REST_PORT))).thenReturn("8080");
108
109         expectedException.expect(Exception.class);
110         expectedException.expectMessage("Could not find the context for operation null");
111
112         resourceUriExtractor.extractResourceUri(ctx, prop);
113     }
114
115 }