1 package org.onap.so.adapters.sdnc.sdncrest;
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;
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;
31 @RunWith(MockitoJUnitRunner.class)
32 public class BPRestCallbackUnitTest {
34 private Environment env;
37 private RestTemplate restTemplate;
41 private BPRestCallback bpRestCallback;
43 private HttpEntity<String> requestEntity;
44 private String message;
45 private HttpHeaders headers;
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();
58 public String input(String JsonInput) throws IOException {
59 JsonInput = "src/test/resources/" + JsonInput;
60 return new String(Files.readAllBytes(Paths.get(JsonInput)));
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);
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);
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);
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);
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);
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);
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);
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);