2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.appc.flow.controller.node;
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;
43 public class ResourceUriExtractorTest {
45 private Properties prop;
46 private SvcLogicContext ctx;
49 public ExpectedException expectedException = ExpectedException.none();
50 private ResourceUriExtractor resourceUriExtractor;
54 ctx = mock(SvcLogicContext.class);
55 prop = mock(Properties.class);
56 resourceUriExtractor = new ResourceUriExtractor();
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");
64 resourceUriExtractor = new ResourceUriExtractor();
65 String resourceUri = resourceUriExtractor.extractResourceUri(ctx, prop);
67 Assert.assertEquals("http://localhost:8080", resourceUri);
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");
77 when(ctx.getAttribute(INPUT_CONTEXT)).thenReturn("input-context");
78 when(ctx.getAttribute(INPUT_SUB_CONTEXT)).thenReturn("input-sub-context");
80 String resourceUri = resourceUriExtractor.extractResourceUri(ctx, prop);
82 Assert.assertEquals("http://localhost:8080/input-context/input-sub-context", resourceUri);
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");
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");
97 String resourceUri = resourceUriExtractor.extractResourceUri(ctx, prop);
99 Assert.assertEquals("http://localhost:8080/ra-context/ra-sub-context", resourceUri);
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");
109 expectedException.expect(Exception.class);
110 expectedException.expectMessage("Could not find the context for operation null");
112 resourceUriExtractor.extractResourceUri(ctx, prop);