Improve coverage flow/controller/node #3
[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   private ResourceUriExtractor resourceUriExtractor;
28
29   @Before
30   public void setUp() {
31     ctx = mock(SvcLogicContext.class);
32     prop = mock(Properties.class);
33     resourceUriExtractor = new ResourceUriExtractor();
34   }
35
36   @Test
37   public void should_return_input_url_if_exist() throws Exception {
38     ctx = mock(SvcLogicContext.class);
39     when(ctx.getAttribute(INPUT_URL)).thenReturn("http://localhost:8080");
40
41     resourceUriExtractor = new ResourceUriExtractor();
42     String resourceUri = resourceUriExtractor.extractResourceUri(ctx, prop);
43
44     Assert.assertEquals("http://localhost:8080", resourceUri);
45   }
46
47   @Test
48   public void should_extract_url_input_if_context_input_provided() throws Exception {
49     when(ctx.getAttribute(INPUT_URL)).thenReturn("");
50     when(ctx.getAttribute(INPUT_HOST_IP_ADDRESS)).thenReturn("localhost");
51     when(ctx.getAttribute(INPUT_PORT_NUMBER)).thenReturn("8080");
52
53     when(ctx.getAttribute(INPUT_CONTEXT)).thenReturn("input-context");
54     when(ctx.getAttribute(INPUT_SUB_CONTEXT)).thenReturn("input-sub-context");
55
56     String resourceUri = resourceUriExtractor.extractResourceUri(ctx, prop);
57
58     Assert.assertEquals("http://localhost:8080/input-context/input-sub-context", resourceUri);
59   }
60
61   @Test
62   public void should_extract_url_input_if_request_action_provided() throws Exception {
63
64     when(ctx.getAttribute(INPUT_URL)).thenReturn("");
65     when(ctx.getAttribute(INPUT_HOST_IP_ADDRESS)).thenReturn("localhost");
66     when(ctx.getAttribute(INPUT_PORT_NUMBER)).thenReturn("8080");
67
68     when(ctx.getAttribute(INPUT_REQUEST_ACTION)).thenReturn("request-action");
69     when(ctx.getAttribute(INPUT_REQUEST_ACTION)).thenReturn("request-action");
70
71     when(prop.getProperty("request-action.context")).thenReturn("ra-context");
72     when(prop.getProperty("request-action.sub-context")).thenReturn("ra-sub-context");
73
74     String resourceUri = resourceUriExtractor.extractResourceUri(ctx, prop);
75
76     Assert.assertEquals("http://localhost:8080/ra-context/ra-sub-context", resourceUri);
77   }
78
79   @Test
80   public void should_throw_exception_if_missing_context() throws Exception {
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
88     resourceUriExtractor.extractResourceUri(ctx, prop);
89   }
90
91 }