Improve coverage in flow/controller/node
[appc.git] / appc-config / appc-flow-controller / provider / src / test / java / org / onap / appc / flow / controller / node / ResourceUriExtractorTest.java
1 package org.onap.appc.flow.controller.node;
2
3 import static org.mockito.Mockito.mock;
4 import static org.mockito.Mockito.when;
5 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_CONTEXT;
6 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_HOST_IP_ADDRESS;
7 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_PORT_NUMBER;
8 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_REQUEST_ACTION;
9 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_SUB_CONTEXT;
10 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_URL;
11
12 import java.util.Properties;
13 import org.junit.Assert;
14 import org.junit.Before;
15 import org.junit.Rule;
16 import org.junit.Test;
17 import org.junit.rules.ExpectedException;
18 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
19
20 public class ResourceUriExtractorTest {
21
22   private Properties prop;
23
24   @Rule
25   public ExpectedException expectedException = ExpectedException.none();
26
27   @Before
28   public void setUp() {
29     prop = new Properties();
30   }
31
32   @Test
33   public void should_return_input_url_if_exist() throws Exception {
34     SvcLogicContext ctx = mock(SvcLogicContext.class);
35     when(ctx.getAttribute(INPUT_URL)).thenReturn("test resource uri");
36
37     String resourceUri = ResourceUriExtractor.extractResourceUri(ctx, prop);
38
39     Assert.assertEquals("test resource uri", resourceUri);
40   }
41
42   @Test
43   public void should_extract_url_input_if_context_input_provided() throws Exception {
44     SvcLogicContext ctx = mock(SvcLogicContext.class);
45
46     when(ctx.getAttribute(INPUT_URL)).thenReturn("");
47     when(ctx.getAttribute(INPUT_HOST_IP_ADDRESS)).thenReturn("localhost");
48     when(ctx.getAttribute(INPUT_PORT_NUMBER)).thenReturn("8080");
49
50     when(ctx.getAttribute(INPUT_CONTEXT)).thenReturn("input-context");
51     when(ctx.getAttribute(INPUT_SUB_CONTEXT)).thenReturn("input-sub-context");
52
53     String resourceUri = ResourceUriExtractor.extractResourceUri(ctx, prop);
54
55     Assert.assertEquals("http://localhost:8080/input-context/input-sub-context", resourceUri);
56   }
57
58   @Test
59   public void should_extract_url_input_if_request_action_provided() throws Exception {
60     SvcLogicContext ctx = mock(SvcLogicContext.class);
61
62     when(ctx.getAttribute(INPUT_URL)).thenReturn("");
63     when(ctx.getAttribute(INPUT_HOST_IP_ADDRESS)).thenReturn("localhost");
64     when(ctx.getAttribute(INPUT_PORT_NUMBER)).thenReturn("8080");
65
66     when(ctx.getAttribute(INPUT_REQUEST_ACTION)).thenReturn("request-action");
67     when(ctx.getAttribute(INPUT_REQUEST_ACTION)).thenReturn("request-action");
68
69     prop.put("request-action.context", "ra-context");
70     prop.put("request-action.sub-context", "ra-sub-context");
71
72     String resourceUri = ResourceUriExtractor.extractResourceUri(ctx, prop);
73
74     Assert.assertEquals("http://localhost:8080/ra-context/ra-sub-context", resourceUri);
75   }
76
77   @Test
78   public void should_throw_exception_if_missing_context() throws Exception {
79     SvcLogicContext ctx = mock(SvcLogicContext.class);
80
81     when(ctx.getAttribute(INPUT_URL)).thenReturn("");
82     when(ctx.getAttribute(INPUT_HOST_IP_ADDRESS)).thenReturn("localhost");
83     when(ctx.getAttribute(INPUT_PORT_NUMBER)).thenReturn("8080");
84
85     expectedException.expect(Exception.class);
86     expectedException.expectMessage("Could Not found the context for operation null");
87     ResourceUriExtractor.extractResourceUri(ctx, prop);
88   }
89
90 }