Improve coverage flow/controller/node #2
[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   private SvcLogicContext ctx;
24
25   @Rule
26   public ExpectedException expectedException = ExpectedException.none();
27
28   @Before
29   public void setUp() {
30     ctx = mock(SvcLogicContext.class);
31     prop = mock(Properties.class);
32   }
33
34   @Test
35   public void should_return_input_url_if_exist() throws Exception {
36     ctx = mock(SvcLogicContext.class);
37     when(ctx.getAttribute(INPUT_URL)).thenReturn("test resource uri");
38
39     String resourceUri = ResourceUriExtractor.extractResourceUri(ctx, prop);
40
41     Assert.assertEquals("test resource uri", resourceUri);
42   }
43
44   @Test
45   public void should_extract_url_input_if_context_input_provided() throws Exception {
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
61     when(ctx.getAttribute(INPUT_URL)).thenReturn("");
62     when(ctx.getAttribute(INPUT_HOST_IP_ADDRESS)).thenReturn("localhost");
63     when(ctx.getAttribute(INPUT_PORT_NUMBER)).thenReturn("8080");
64
65     when(ctx.getAttribute(INPUT_REQUEST_ACTION)).thenReturn("request-action");
66     when(ctx.getAttribute(INPUT_REQUEST_ACTION)).thenReturn("request-action");
67
68     when(prop.getProperty("request-action.context")).thenReturn("ra-context");
69     when(prop.getProperty("request-action.sub-context")).thenReturn("ra-sub-context");
70
71     String resourceUri = ResourceUriExtractor.extractResourceUri(ctx, prop);
72
73     Assert.assertEquals("http://localhost:8080/ra-context/ra-sub-context", resourceUri);
74   }
75
76   @Test
77   public void should_throw_exception_if_missing_context() throws Exception {
78     when(ctx.getAttribute(INPUT_URL)).thenReturn("");
79     when(ctx.getAttribute(INPUT_HOST_IP_ADDRESS)).thenReturn("localhost");
80     when(ctx.getAttribute(INPUT_PORT_NUMBER)).thenReturn("8080");
81
82     expectedException.expect(Exception.class);
83     expectedException.expectMessage("Could Not found the context for operation null");
84
85     ResourceUriExtractor.extractResourceUri(ctx, prop);
86   }
87
88 }