aboutsummaryrefslogtreecommitdiffstats
path: root/adapters/mso-sdnc-adapter/src/test
diff options
context:
space:
mode:
authorRamesh Parthasarathy <ramesh.parthasarathy@att.com>2020-07-11 19:30:10 +0000
committerRamesh Parthasarathy <ramesh.parthasarathy@att.com>2020-07-11 19:30:10 +0000
commit6cf18e59f1f7fc94bcae27785dc9a17008324fa8 (patch)
treeb51501cd5bb3459e6fe3eaf0813ce09ad08400bc /adapters/mso-sdnc-adapter/src/test
parent9045a32ba68f9ffb2b07857701cff186d4b938d3 (diff)
Address NullPointerException with apih healthcheck endpoint
Facing NullPointerException with apih healthcheck endpoint with elalto branch. Brittany has addressed this issue with master branch and this change brings in those changes with elalto branch This is a squashed commit cherry picked commits from master commit 4e79baed423434b42d75f0d5c26757cd6792ce79 commit b341d9cd027b0f7bec125d51b8298c3e31d7f685 commit 02b85383a1f2a7c9677db2929b30fc283ad9a00c commit 8e3e32d4c7a10ddd8ee97576e030185e273bd2c0 commit 8a1bade3296510df340d11f7c22353994cbdfa83 commit ef6d7e225d8601489f8559483e1414dc62a62e8b commit 84a201f889f635c9405ea737d814fc43d274afd7 Issue-ID: SO-3046 Signed-off-by: Ramesh Parthasarathy(rp6768)<ramesh.parthasarathy@att.com> Change-Id: Ic7579f84746dad4002a5291edd54237eaaa602cf
Diffstat (limited to 'adapters/mso-sdnc-adapter/src/test')
-rw-r--r--adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/sdncrest/BPRestCallbackUnitTest.java142
-rw-r--r--adapters/mso-sdnc-adapter/src/test/resources/BPRestCallbackRequest.json1
2 files changed, 143 insertions, 0 deletions
diff --git a/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/sdncrest/BPRestCallbackUnitTest.java b/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/sdncrest/BPRestCallbackUnitTest.java
new file mode 100644
index 0000000000..09089890ab
--- /dev/null
+++ b/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/sdncrest/BPRestCallbackUnitTest.java
@@ -0,0 +1,142 @@
+package org.onap.so.adapters.sdnc.sdncrest;
+
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.when;
+import java.io.IOException;
+import java.net.URI;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.Spy;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.onap.so.adapters.sdnc.impl.Constants;
+import org.springframework.core.env.Environment;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.client.HttpServerErrorException;
+import org.springframework.web.client.ResourceAccessException;
+import org.springframework.web.client.RestTemplate;
+import org.springframework.web.util.UriComponentsBuilder;
+
+@RunWith(MockitoJUnitRunner.class)
+public class BPRestCallbackUnitTest {
+ @Mock
+ private Environment env;
+
+ @Mock
+ private RestTemplate restTemplate;
+
+ @Spy
+ @InjectMocks
+ private BPRestCallback bpRestCallback;
+
+ private HttpEntity<String> requestEntity;
+ private String message;
+ private HttpHeaders headers;
+ private URI uri;
+
+ @Before
+ public void setUp() throws IOException {
+ headers = new HttpHeaders();
+ headers.setContentType(MediaType.APPLICATION_JSON);
+ message = input("BPRestCallbackRequest.json");
+ requestEntity = new HttpEntity<>(message, headers);
+ UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("http://localhost:8000/sdnc");
+ uri = builder.build(true).toUri();
+ }
+
+ public String input(String JsonInput) throws IOException {
+ JsonInput = "src/test/resources/" + JsonInput;
+ return new String(Files.readAllBytes(Paths.get(JsonInput)));
+ }
+
+ @Test
+ public void sendTest() throws IOException {
+ ResponseEntity<String> postResponse = new ResponseEntity<String>("response", HttpStatus.OK);
+ doReturn(restTemplate).when(bpRestCallback).setRestTemplate(60000);
+ doReturn(false).when(bpRestCallback).setAuthorizationHeader(headers);
+ when(restTemplate.postForEntity(uri, requestEntity, String.class)).thenReturn(postResponse);
+ boolean response = bpRestCallback.send("http://localhost:8000/sdnc", message);
+ assertTrue(response);
+ }
+
+ @Test
+ public void sendNoAuthHeaderTest() throws IOException {
+ doReturn(true).when(bpRestCallback).setAuthorizationHeader(headers);
+ doReturn(restTemplate).when(bpRestCallback).setRestTemplate(60000);
+ boolean response = bpRestCallback.send("http://localhost:8000/sdnc", message);
+ assertTrue(response);
+ }
+
+ @Test
+ public void sendErrorTest() throws IOException {
+ doReturn(false).when(bpRestCallback).setAuthorizationHeader(headers);
+ doReturn(restTemplate).when(bpRestCallback).setRestTemplate(60000);
+ when(restTemplate.postForEntity(uri, requestEntity, String.class))
+ .thenThrow(new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR, null, null, null));
+ boolean response = bpRestCallback.send("http://localhost:8000/sdnc", message);
+ assertTrue(response);
+ }
+
+ @Test
+ public void sendResponse3xxTest() throws IOException {
+ ResponseEntity<String> postResponse = new ResponseEntity<String>("response", HttpStatus.MULTIPLE_CHOICES);
+ doReturn(false).when(bpRestCallback).setAuthorizationHeader(headers);
+ doReturn(restTemplate).when(bpRestCallback).setRestTemplate(60000);
+ when(restTemplate.postForEntity(uri, requestEntity, String.class)).thenReturn(postResponse);
+ boolean response = bpRestCallback.send("http://localhost:8000/sdnc", message);
+ assertTrue(response);
+ }
+
+ @Test
+ public void sendResponseNullMessageTest() throws IOException {
+ HttpHeaders httpHeaders = new HttpHeaders();
+ httpHeaders.setContentType(MediaType.APPLICATION_JSON);
+ HttpEntity<String> requestEntityNoMessage = new HttpEntity<>(null, httpHeaders);
+ ResponseEntity<String> postResponse = new ResponseEntity<String>("response", HttpStatus.OK);
+ doReturn(false).when(bpRestCallback).setAuthorizationHeader(httpHeaders);
+ doReturn(restTemplate).when(bpRestCallback).setRestTemplate(60000);
+ when(restTemplate.postForEntity(uri, requestEntityNoMessage, String.class)).thenReturn(postResponse);
+ boolean response = bpRestCallback.send("http://localhost:8000/sdnc", null);
+ assertTrue(response);
+ }
+
+ @Test
+ public void postThrowsExceptionTest() throws IOException {
+ doReturn(false).when(bpRestCallback).setAuthorizationHeader(headers);
+ doReturn(restTemplate).when(bpRestCallback).setRestTemplate(60000);
+ when(restTemplate.postForEntity(uri, requestEntity, String.class))
+ .thenThrow(new ResourceAccessException("ResourceAccessException"));
+ boolean response = bpRestCallback.send("http://localhost:8000/sdnc", message);
+ assertFalse(response);
+ }
+
+ @Test
+ public void setAuthorizationHeaderTest() {
+ HttpHeaders authHeaders = new HttpHeaders();
+ when(env.getProperty(Constants.BPEL_AUTH_PROP))
+ .thenReturn("5E12ACACBD552A415E081E29F2C4772F9835792A51C766CCFDD7433DB5220B59969CB2798C");
+ when(env.getProperty(Constants.ENCRYPTION_KEY_PROP)).thenReturn("07a7159d3bf51a0e53be7a8f89699be7");
+ boolean result = bpRestCallback.setAuthorizationHeader(authHeaders);
+ assertFalse(result);
+ }
+
+ @Test
+ public void setAuthorizationHeaderErrorTest() {
+ HttpHeaders authHeaders = new HttpHeaders();
+ when(env.getProperty(Constants.BPEL_AUTH_PROP)).thenReturn("test");
+ when(env.getProperty(Constants.ENCRYPTION_KEY_PROP)).thenReturn("test");
+ boolean result = bpRestCallback.setAuthorizationHeader(authHeaders);
+ assertTrue(result);
+ }
+}
diff --git a/adapters/mso-sdnc-adapter/src/test/resources/BPRestCallbackRequest.json b/adapters/mso-sdnc-adapter/src/test/resources/BPRestCallbackRequest.json
new file mode 100644
index 0000000000..21f3dab7e0
--- /dev/null
+++ b/adapters/mso-sdnc-adapter/src/test/resources/BPRestCallbackRequest.json
@@ -0,0 +1 @@
+{"SDNCServiceResponse":{"sdncRequestId":"b5b763aa-0d8a-4438-b900-83af45d21d10","responseCode":"200","ackFinalIndicator":"N"}} \ No newline at end of file