Split sequence generation to classess
[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  * 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_CONTEXT;
25 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_HOST_IP_ADDRESS;
26 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_PORT_NUMBER;
27 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_REQUEST_ACTION;
28 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_SUB_CONTEXT;
29 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_URL;
30
31 import java.util.Properties;
32 import org.junit.Assert;
33 import org.junit.Before;
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.junit.rules.ExpectedException;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
38
39 public class ResourceUriExtractorTest {
40
41   private Properties prop;
42   private SvcLogicContext ctx;
43
44   @Rule
45   public ExpectedException expectedException = ExpectedException.none();
46   private ResourceUriExtractor resourceUriExtractor;
47
48   @Before
49   public void setUp() {
50     ctx = mock(SvcLogicContext.class);
51     prop = mock(Properties.class);
52     resourceUriExtractor = new ResourceUriExtractor();
53   }
54
55   @Test
56   public void should_return_input_url_if_exist() throws Exception {
57     ctx = mock(SvcLogicContext.class);
58     when(ctx.getAttribute(INPUT_URL)).thenReturn("http://localhost:8080");
59
60     resourceUriExtractor = new ResourceUriExtractor();
61     String resourceUri = resourceUriExtractor.extractResourceUri(ctx, prop);
62
63     Assert.assertEquals("http://localhost:8080", resourceUri);
64   }
65
66   @Test
67   public void should_extract_url_input_if_context_input_provided() throws Exception {
68     when(ctx.getAttribute(INPUT_URL)).thenReturn("");
69     when(ctx.getAttribute(INPUT_HOST_IP_ADDRESS)).thenReturn("localhost");
70     when(ctx.getAttribute(INPUT_PORT_NUMBER)).thenReturn("8080");
71
72     when(ctx.getAttribute(INPUT_CONTEXT)).thenReturn("input-context");
73     when(ctx.getAttribute(INPUT_SUB_CONTEXT)).thenReturn("input-sub-context");
74
75     String resourceUri = resourceUriExtractor.extractResourceUri(ctx, prop);
76
77     Assert.assertEquals("http://localhost:8080/input-context/input-sub-context", resourceUri);
78   }
79
80   @Test
81   public void should_extract_url_input_if_request_action_provided() throws Exception {
82
83     when(ctx.getAttribute(INPUT_URL)).thenReturn("");
84     when(ctx.getAttribute(INPUT_HOST_IP_ADDRESS)).thenReturn("localhost");
85     when(ctx.getAttribute(INPUT_PORT_NUMBER)).thenReturn("8080");
86
87     when(ctx.getAttribute(INPUT_REQUEST_ACTION)).thenReturn("request-action");
88     when(ctx.getAttribute(INPUT_REQUEST_ACTION)).thenReturn("request-action");
89
90     when(prop.getProperty("request-action.context")).thenReturn("ra-context");
91     when(prop.getProperty("request-action.sub-context")).thenReturn("ra-sub-context");
92
93     String resourceUri = resourceUriExtractor.extractResourceUri(ctx, prop);
94
95     Assert.assertEquals("http://localhost:8080/ra-context/ra-sub-context", resourceUri);
96   }
97
98   @Test
99   public void should_throw_exception_if_missing_context() throws Exception {
100     when(ctx.getAttribute(INPUT_URL)).thenReturn("");
101     when(ctx.getAttribute(INPUT_HOST_IP_ADDRESS)).thenReturn("localhost");
102     when(ctx.getAttribute(INPUT_PORT_NUMBER)).thenReturn("8080");
103
104     expectedException.expect(Exception.class);
105     expectedException.expectMessage("Could Not found the context for operation null");
106
107     resourceUriExtractor.extractResourceUri(ctx, prop);
108   }
109
110 }