From 9358c33fd9393deb7d619d03884b19724f639a72 Mon Sep 17 00:00:00 2001 From: RehanRaza Date: Tue, 12 May 2020 13:54:48 +0200 Subject: [PATCH] Improve code coverage of A1 controller Change-Id: Ia9630e0f6d619e2bc7435845ee73e15e1fa06a1e Issue-ID: NONRTRIC-201 Signed-off-by: RehanRaza --- .../northbound/nonrt-ric-api/provider/pom.xml | 6 + .../northbound/provider/NonrtRicApiProvider.java | 3 +- .../{ => provider}/NonrtRicApiProviderTest.java | 70 ++++++++++-- .../restadapter/RestAdapterImplTest.java | 122 +++++++++++++++++++++ 4 files changed, 187 insertions(+), 14 deletions(-) rename sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/test/java/org/o_ran_sc/nonrtric/sdnc_a1/northbound/{ => provider}/NonrtRicApiProviderTest.java (66%) create mode 100644 sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/test/java/org/o_ran_sc/nonrtric/sdnc_a1/northbound/restadapter/RestAdapterImplTest.java diff --git a/sdnc-a1-controller/northbound/nonrt-ric-api/provider/pom.xml b/sdnc-a1-controller/northbound/nonrt-ric-api/provider/pom.xml index 2a586bae..e37d656f 100644 --- a/sdnc-a1-controller/northbound/nonrt-ric-api/provider/pom.xml +++ b/sdnc-a1-controller/northbound/nonrt-ric-api/provider/pom.xml @@ -167,6 +167,12 @@ 1.10.19 test + + com.squareup.okhttp3 + mockwebserver + 3.14.6 + test + org.json json diff --git a/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/main/java/org/o_ran_sc/nonrtric/sdnc_a1/northbound/provider/NonrtRicApiProvider.java b/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/main/java/org/o_ran_sc/nonrtric/sdnc_a1/northbound/provider/NonrtRicApiProvider.java index 9a625b1b..9876792a 100644 --- a/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/main/java/org/o_ran_sc/nonrtric/sdnc_a1/northbound/provider/NonrtRicApiProvider.java +++ b/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/main/java/org/o_ran_sc/nonrtric/sdnc_a1/northbound/provider/NonrtRicApiProvider.java @@ -96,7 +96,6 @@ public class NonrtRicApiProvider implements AutoCloseable, A1ADAPTERAPIService { setNotificationService(notificationPublishService); setRpcRegistry(rpcProviderRegistry); initialize(); - } public void initialize() { @@ -209,7 +208,7 @@ public class NonrtRicApiProvider implements AutoCloseable, A1ADAPTERAPIService { return Futures.immediateFuture(rpcResult); } - private GetA1PolicyOutput getA1(GetA1PolicyInput input) { + protected GetA1PolicyOutput getA1(GetA1PolicyInput input) { log.info("Start of getA1"); GetA1PolicyOutputBuilder responseBuilder = new GetA1PolicyOutputBuilder(); diff --git a/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/test/java/org/o_ran_sc/nonrtric/sdnc_a1/northbound/NonrtRicApiProviderTest.java b/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/test/java/org/o_ran_sc/nonrtric/sdnc_a1/northbound/provider/NonrtRicApiProviderTest.java similarity index 66% rename from sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/test/java/org/o_ran_sc/nonrtric/sdnc_a1/northbound/NonrtRicApiProviderTest.java rename to sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/test/java/org/o_ran_sc/nonrtric/sdnc_a1/northbound/provider/NonrtRicApiProviderTest.java index dd76a077..b33b7714 100644 --- a/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/test/java/org/o_ran_sc/nonrtric/sdnc_a1/northbound/NonrtRicApiProviderTest.java +++ b/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/test/java/org/o_ran_sc/nonrtric/sdnc_a1/northbound/provider/NonrtRicApiProviderTest.java @@ -18,13 +18,11 @@ * ============LICENSE_END========================================================= */ -package org.o_ran_sc.nonrtric.sdnc_a1.northbound; +package org.o_ran_sc.nonrtric.sdnc_a1.northbound.provider; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.when; - import java.util.concurrent.ExecutionException; - import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -32,7 +30,6 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.internal.util.reflection.Whitebox; import org.mockito.runners.MockitoJUnitRunner; -import org.o_ran_sc.nonrtric.sdnc_a1.northbound.provider.NonrtRicApiProvider; import org.o_ran_sc.nonrtric.sdnc_a1.northbound.restadapter.RestAdapter; import org.opendaylight.controller.md.sal.binding.api.DataBroker; import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService; @@ -53,6 +50,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.web.client.RestClientResponseException; /** * This class Tests all the methods in NonrtRicApiProvider @@ -82,7 +80,7 @@ public class NonrtRicApiProviderTest extends AbstractConcurrentDataBrokerTest { } @Test - public void testGetA1Policy() throws InterruptedException, ExecutionException { + public void testGetA1PolicySuccess() throws InterruptedException, ExecutionException { GetA1PolicyInputBuilder inputBuilder = new GetA1PolicyInputBuilder(); inputBuilder.setNearRtRicUrl(nearRtRicUrl); Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter); @@ -95,7 +93,7 @@ public class NonrtRicApiProviderTest extends AbstractConcurrentDataBrokerTest { } @Test - public void testGetA1PolicyType() throws InterruptedException, ExecutionException { + public void testGetA1PolicyTypeSuccess() throws InterruptedException, ExecutionException { GetA1PolicyTypeInputBuilder inputBuilder = new GetA1PolicyTypeInputBuilder(); inputBuilder.setNearRtRicUrl(nearRtRicUrl); Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter); @@ -108,7 +106,7 @@ public class NonrtRicApiProviderTest extends AbstractConcurrentDataBrokerTest { } @Test - public void testGetA1PolicyStatus() throws InterruptedException, ExecutionException { + public void testGetA1PolicyStatusSuccess() throws InterruptedException, ExecutionException { GetA1PolicyStatusInputBuilder inputBuilder = new GetA1PolicyStatusInputBuilder(); inputBuilder.setNearRtRicUrl(nearRtRicUrl); Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter); @@ -121,7 +119,21 @@ public class NonrtRicApiProviderTest extends AbstractConcurrentDataBrokerTest { } @Test - public void testPutA1Policy() throws InterruptedException, ExecutionException { + public void testGetA1Failure() throws InterruptedException, ExecutionException { + GetA1PolicyInputBuilder inputBuilder = new GetA1PolicyInputBuilder(); + inputBuilder.setNearRtRicUrl(nearRtRicUrl); + Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter); + String returnedBody = "GET failed"; + int returnedStatusCode = 404; + when(restAdapter.get(eq(nearRtRicUrl.getValue()), eq(String.class))) + .thenThrow(new RestClientResponseException(null, returnedStatusCode, null, null, returnedBody.getBytes(), null)); + GetA1PolicyOutput result = nonrtRicApiProvider.getA1(inputBuilder.build()); + Assert.assertEquals(returnedBody, result.getBody()); + Assert.assertTrue(returnedStatusCode == result.getHttpStatus()); + } + + @Test + public void testPutA1PolicySuccess() throws InterruptedException, ExecutionException { PutA1PolicyInputBuilder inputBuilder = new PutA1PolicyInputBuilder(); String testPolicy = "{}"; inputBuilder.setNearRtRicUrl(nearRtRicUrl); @@ -136,15 +148,49 @@ public class NonrtRicApiProviderTest extends AbstractConcurrentDataBrokerTest { } @Test - public void testDeleteA1() throws InterruptedException, ExecutionException { + public void testPutA1PolicyFailure() throws InterruptedException, ExecutionException { + PutA1PolicyInputBuilder inputBuilder = new PutA1PolicyInputBuilder(); + String testPolicy = "{}"; + inputBuilder.setNearRtRicUrl(nearRtRicUrl); + inputBuilder.setBody(testPolicy); + Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter); + String returnedBody = "PUT failed"; + int returnedStatusCode = 400; + when(restAdapter.put(eq(nearRtRicUrl.getValue()), eq(testPolicy), eq(String.class))) + .thenThrow(new RestClientResponseException(null, returnedStatusCode, null, null, returnedBody.getBytes(), null)); + PutA1PolicyOutput result = nonrtRicApiProvider.putA1Policy(inputBuilder.build()).get().getResult(); + Assert.assertEquals(returnedBody, result.getBody()); + Assert.assertTrue(returnedStatusCode == result.getHttpStatus()); + } + + @Test + public void testDeleteA1Success() throws InterruptedException, ExecutionException { DeleteA1PolicyInputBuilder inputBuilder = new DeleteA1PolicyInputBuilder(); inputBuilder.setNearRtRicUrl(nearRtRicUrl); Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter); + ResponseEntity getResponseNoContent = new ResponseEntity<>(HttpStatus.NO_CONTENT); + String returnedBody = "returned body"; + ResponseEntity getResponseOk = new ResponseEntity<>(returnedBody, HttpStatus.OK); + when(restAdapter.delete(nearRtRicUrl.getValue())).thenReturn(getResponseNoContent).thenReturn(getResponseOk); + DeleteA1PolicyOutput resultNoContent = nonrtRicApiProvider.deleteA1Policy(inputBuilder.build()).get().getResult(); + Assert.assertTrue(HttpStatus.NO_CONTENT.value() == resultNoContent.getHttpStatus()); + DeleteA1PolicyOutput resultOk = nonrtRicApiProvider.deleteA1Policy(inputBuilder.build()).get().getResult(); + Assert.assertEquals(returnedBody, resultOk.getBody()); + Assert.assertTrue(HttpStatus.OK.value() == resultOk.getHttpStatus()); + } - ResponseEntity getResponse = new ResponseEntity<>(HttpStatus.NO_CONTENT); - when(restAdapter.delete(nearRtRicUrl.getValue())).thenReturn(getResponse); + @Test + public void testDeleteA1Failure() throws InterruptedException, ExecutionException { + DeleteA1PolicyInputBuilder inputBuilder = new DeleteA1PolicyInputBuilder(); + inputBuilder.setNearRtRicUrl(nearRtRicUrl); + Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter); + String returnedBody = "DELETE failed"; + int returnedStatusCode = 404; + when(restAdapter.delete(nearRtRicUrl.getValue())) + .thenThrow(new RestClientResponseException(null, returnedStatusCode, null, null, returnedBody.getBytes(), null)); DeleteA1PolicyOutput result = nonrtRicApiProvider.deleteA1Policy(inputBuilder.build()).get().getResult(); - Assert.assertTrue(HttpStatus.NO_CONTENT.value() == result.getHttpStatus()); + Assert.assertEquals(returnedBody, result.getBody()); + Assert.assertTrue(returnedStatusCode == result.getHttpStatus()); } } diff --git a/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/test/java/org/o_ran_sc/nonrtric/sdnc_a1/northbound/restadapter/RestAdapterImplTest.java b/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/test/java/org/o_ran_sc/nonrtric/sdnc_a1/northbound/restadapter/RestAdapterImplTest.java new file mode 100644 index 00000000..8e1d806d --- /dev/null +++ b/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/test/java/org/o_ran_sc/nonrtric/sdnc_a1/northbound/restadapter/RestAdapterImplTest.java @@ -0,0 +1,122 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.o_ran_sc.nonrtric.sdnc_a1.northbound.restadapter; + +import java.io.IOException; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.RestClientException; +import okhttp3.mockwebserver.MockResponse; +import okhttp3.mockwebserver.MockWebServer; +import okhttp3.mockwebserver.RecordedRequest; + +public class RestAdapterImplTest { + private static MockWebServer mockWebServer; + private static RestAdapter adapterUnderTest; + + private static final String VALID_PROTOCOL = "http"; + private static final String INVALID_PROTOCOL = "ftp"; + private static final String REQUEST_URL = "/test"; + private static final String TEST_BODY = "test"; + private static final int SUCCESS_CODE = 200; + private static final int ERROR_CODE = 500; + + @Before + public void init() throws IOException { + mockWebServer = new MockWebServer(); + mockWebServer.start(); + adapterUnderTest = new RestAdapterImpl(); + } + + @After + public void tearDown() throws IOException { + mockWebServer.shutdown(); + } + + @Test + public void testInvalidUrlOrProtocol() throws InterruptedException { + ResponseEntity response = adapterUnderTest.get("://localhost:" + mockWebServer.getPort() + REQUEST_URL, + String.class); + Assert.assertTrue(HttpStatus.BAD_REQUEST.value() == response.getStatusCodeValue()); + response = adapterUnderTest.get(INVALID_PROTOCOL + "://localhost:" + mockWebServer.getPort() + REQUEST_URL, + String.class); + Assert.assertTrue(HttpStatus.BAD_REQUEST.value() == response.getStatusCodeValue()); + } + + @Test + public void testGetNoError() throws InterruptedException { + mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE).setBody(TEST_BODY)); + ResponseEntity response = adapterUnderTest.get(VALID_PROTOCOL + "://localhost:" + + mockWebServer.getPort() + REQUEST_URL, String.class); + RecordedRequest recordedRequest = mockWebServer.takeRequest(); + Assert.assertEquals(TEST_BODY, response.getBody()); + Assert.assertTrue(SUCCESS_CODE == response.getStatusCodeValue()); + Assert.assertEquals("GET", recordedRequest.getMethod()); + Assert.assertEquals(REQUEST_URL, recordedRequest.getPath()); + } + + @Test(expected = RestClientException.class) + public void testGetError() { + mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE)); + adapterUnderTest.get(VALID_PROTOCOL + "://localhost:" + mockWebServer.getPort() + REQUEST_URL, String.class); + } + + @Test + public void testPutNoError() throws InterruptedException { + mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE).setBody(TEST_BODY)); + ResponseEntity response = adapterUnderTest.put(VALID_PROTOCOL + "://localhost:" + + mockWebServer.getPort() + REQUEST_URL, TEST_BODY, String.class); + RecordedRequest recordedRequest = mockWebServer.takeRequest(); + Assert.assertEquals(TEST_BODY, response.getBody()); + Assert.assertTrue(SUCCESS_CODE == response.getStatusCodeValue()); + Assert.assertEquals("PUT", recordedRequest.getMethod()); + Assert.assertEquals(REQUEST_URL, recordedRequest.getPath()); + Assert.assertEquals(TEST_BODY, recordedRequest.getBody().readUtf8()); + } + + @Test(expected = RestClientException.class) + public void testPutError() { + mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE)); + adapterUnderTest.put(VALID_PROTOCOL + "://localhost:" + mockWebServer.getPort() + REQUEST_URL, TEST_BODY, + String.class); + } + + @Test + public void testDeleteNoError() throws InterruptedException { + mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE)); + ResponseEntity response = adapterUnderTest.delete(VALID_PROTOCOL + "://localhost:" + + mockWebServer.getPort() + REQUEST_URL); + RecordedRequest recordedRequest = mockWebServer.takeRequest(); + Assert.assertTrue(SUCCESS_CODE == response.getStatusCodeValue()); + Assert.assertEquals("DELETE", recordedRequest.getMethod()); + Assert.assertEquals(REQUEST_URL, recordedRequest.getPath()); + } + + @Test(expected = RestClientException.class) + public void testDeleteError() { + mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE)); + adapterUnderTest.delete(VALID_PROTOCOL + "://localhost:" + mockWebServer.getPort() + REQUEST_URL); + } +} -- 2.16.6