09089890ab7eeadc512315880e5ee593bb213973
[so.git] /
1 package org.onap.so.adapters.sdnc.sdncrest;
2
3
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertTrue;
6 import static org.mockito.Mockito.doReturn;
7 import static org.mockito.Mockito.when;
8 import java.io.IOException;
9 import java.net.URI;
10 import java.nio.file.Files;
11 import java.nio.file.Paths;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.junit.runner.RunWith;
15 import org.mockito.InjectMocks;
16 import org.mockito.Mock;
17 import org.mockito.Spy;
18 import org.mockito.junit.MockitoJUnitRunner;
19 import org.onap.so.adapters.sdnc.impl.Constants;
20 import org.springframework.core.env.Environment;
21 import org.springframework.http.HttpEntity;
22 import org.springframework.http.HttpHeaders;
23 import org.springframework.http.HttpStatus;
24 import org.springframework.http.MediaType;
25 import org.springframework.http.ResponseEntity;
26 import org.springframework.web.client.HttpServerErrorException;
27 import org.springframework.web.client.ResourceAccessException;
28 import org.springframework.web.client.RestTemplate;
29 import org.springframework.web.util.UriComponentsBuilder;
30
31 @RunWith(MockitoJUnitRunner.class)
32 public class BPRestCallbackUnitTest {
33     @Mock
34     private Environment env;
35
36     @Mock
37     private RestTemplate restTemplate;
38
39     @Spy
40     @InjectMocks
41     private BPRestCallback bpRestCallback;
42
43     private HttpEntity<String> requestEntity;
44     private String message;
45     private HttpHeaders headers;
46     private URI uri;
47
48     @Before
49     public void setUp() throws IOException {
50         headers = new HttpHeaders();
51         headers.setContentType(MediaType.APPLICATION_JSON);
52         message = input("BPRestCallbackRequest.json");
53         requestEntity = new HttpEntity<>(message, headers);
54         UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("http://localhost:8000/sdnc");
55         uri = builder.build(true).toUri();
56     }
57
58     public String input(String JsonInput) throws IOException {
59         JsonInput = "src/test/resources/" + JsonInput;
60         return new String(Files.readAllBytes(Paths.get(JsonInput)));
61     }
62
63     @Test
64     public void sendTest() throws IOException {
65         ResponseEntity<String> postResponse = new ResponseEntity<String>("response", HttpStatus.OK);
66         doReturn(restTemplate).when(bpRestCallback).setRestTemplate(60000);
67         doReturn(false).when(bpRestCallback).setAuthorizationHeader(headers);
68         when(restTemplate.postForEntity(uri, requestEntity, String.class)).thenReturn(postResponse);
69         boolean response = bpRestCallback.send("http://localhost:8000/sdnc", message);
70         assertTrue(response);
71     }
72
73     @Test
74     public void sendNoAuthHeaderTest() throws IOException {
75         doReturn(true).when(bpRestCallback).setAuthorizationHeader(headers);
76         doReturn(restTemplate).when(bpRestCallback).setRestTemplate(60000);
77         boolean response = bpRestCallback.send("http://localhost:8000/sdnc", message);
78         assertTrue(response);
79     }
80
81     @Test
82     public void sendErrorTest() throws IOException {
83         doReturn(false).when(bpRestCallback).setAuthorizationHeader(headers);
84         doReturn(restTemplate).when(bpRestCallback).setRestTemplate(60000);
85         when(restTemplate.postForEntity(uri, requestEntity, String.class))
86                 .thenThrow(new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR, null, null, null));
87         boolean response = bpRestCallback.send("http://localhost:8000/sdnc", message);
88         assertTrue(response);
89     }
90
91     @Test
92     public void sendResponse3xxTest() throws IOException {
93         ResponseEntity<String> postResponse = new ResponseEntity<String>("response", HttpStatus.MULTIPLE_CHOICES);
94         doReturn(false).when(bpRestCallback).setAuthorizationHeader(headers);
95         doReturn(restTemplate).when(bpRestCallback).setRestTemplate(60000);
96         when(restTemplate.postForEntity(uri, requestEntity, String.class)).thenReturn(postResponse);
97         boolean response = bpRestCallback.send("http://localhost:8000/sdnc", message);
98         assertTrue(response);
99     }
100
101     @Test
102     public void sendResponseNullMessageTest() throws IOException {
103         HttpHeaders httpHeaders = new HttpHeaders();
104         httpHeaders.setContentType(MediaType.APPLICATION_JSON);
105         HttpEntity<String> requestEntityNoMessage = new HttpEntity<>(null, httpHeaders);
106         ResponseEntity<String> postResponse = new ResponseEntity<String>("response", HttpStatus.OK);
107         doReturn(false).when(bpRestCallback).setAuthorizationHeader(httpHeaders);
108         doReturn(restTemplate).when(bpRestCallback).setRestTemplate(60000);
109         when(restTemplate.postForEntity(uri, requestEntityNoMessage, String.class)).thenReturn(postResponse);
110         boolean response = bpRestCallback.send("http://localhost:8000/sdnc", null);
111         assertTrue(response);
112     }
113
114     @Test
115     public void postThrowsExceptionTest() throws IOException {
116         doReturn(false).when(bpRestCallback).setAuthorizationHeader(headers);
117         doReturn(restTemplate).when(bpRestCallback).setRestTemplate(60000);
118         when(restTemplate.postForEntity(uri, requestEntity, String.class))
119                 .thenThrow(new ResourceAccessException("ResourceAccessException"));
120         boolean response = bpRestCallback.send("http://localhost:8000/sdnc", message);
121         assertFalse(response);
122     }
123
124     @Test
125     public void setAuthorizationHeaderTest() {
126         HttpHeaders authHeaders = new HttpHeaders();
127         when(env.getProperty(Constants.BPEL_AUTH_PROP))
128                 .thenReturn("5E12ACACBD552A415E081E29F2C4772F9835792A51C766CCFDD7433DB5220B59969CB2798C");
129         when(env.getProperty(Constants.ENCRYPTION_KEY_PROP)).thenReturn("07a7159d3bf51a0e53be7a8f89699be7");
130         boolean result = bpRestCallback.setAuthorizationHeader(authHeaders);
131         assertFalse(result);
132     }
133
134     @Test
135     public void setAuthorizationHeaderErrorTest() {
136         HttpHeaders authHeaders = new HttpHeaders();
137         when(env.getProperty(Constants.BPEL_AUTH_PROP)).thenReturn("test");
138         when(env.getProperty(Constants.ENCRYPTION_KEY_PROP)).thenReturn("test");
139         boolean result = bpRestCallback.setAuthorizationHeader(authHeaders);
140         assertTrue(result);
141     }
142 }