X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=policy-agent%2Fsrc%2Ftest%2Fjava%2Forg%2Foransc%2Fpolicyagent%2Fclients%2FSdncOscA1ClientTest.java;h=a6f6187ff90c44c9bc45149ef4ba0502e3aabe9f;hb=6a39814272307d0207222c9229b0d765ac062bf0;hp=18ef0979c09dd2a3da2f15d9587a7fb27f5e1c18;hpb=222a0318c0551eaa4b827a19c7eb5e380a68faf6;p=nonrtric.git diff --git a/policy-agent/src/test/java/org/oransc/policyagent/clients/SdncOscA1ClientTest.java b/policy-agent/src/test/java/org/oransc/policyagent/clients/SdncOscA1ClientTest.java index 18ef0979..a6f6187f 100644 --- a/policy-agent/src/test/java/org/oransc/policyagent/clients/SdncOscA1ClientTest.java +++ b/policy-agent/src/test/java/org/oransc/policyagent/clients/SdncOscA1ClientTest.java @@ -20,36 +20,45 @@ package org.oransc.policyagent.clients; +import static org.junit.Assert.fail; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import com.google.gson.Gson; +import com.google.gson.JsonElement; +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.nio.file.Files; import java.util.Arrays; import java.util.List; +import java.util.Optional; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.stubbing.OngoingStubbing; import org.oransc.policyagent.clients.A1Client.A1ProtocolType; +import org.oransc.policyagent.clients.ImmutableAdapterOutput.Builder; +import org.oransc.policyagent.clients.SdncOscA1Client.AdapterOutput; import org.oransc.policyagent.clients.SdncOscA1Client.AdapterRequest; -import org.oransc.policyagent.clients.SdncOscA1Client.AdapterResponse; import org.oransc.policyagent.configuration.ControllerConfig; import org.oransc.policyagent.configuration.ImmutableControllerConfig; import org.oransc.policyagent.repository.Policy; import org.oransc.policyagent.repository.Ric; +import org.springframework.http.HttpStatus; import org.springframework.web.reactive.function.client.WebClientResponseException; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; @ExtendWith(MockitoExtension.class) -public class SdncOscA1ClientTest { +class SdncOscA1ClientTest { private static final String CONTROLLER_USERNAME = "username"; private static final String CONTROLLER_PASSWORD = "password"; private static final String RIC_1_URL = "RicUrl"; @@ -64,6 +73,7 @@ public class SdncOscA1ClientTest { SdncOscA1Client clientUnderTest; + @Mock AsyncRestClient asyncRestClientMock; private ControllerConfig controllerConfig() { @@ -76,8 +86,7 @@ public class SdncOscA1ClientTest { } @BeforeEach - public void init() { - asyncRestClientMock = mock(AsyncRestClient.class); + void init() { Ric ric = A1ClientHelper.createRic(RIC_1_URL); clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_STD_V1_1, ric.getConfig(), controllerConfig(), @@ -85,62 +94,92 @@ public class SdncOscA1ClientTest { } @Test - public void testGetPolicyTypeIdentities_STD() { + void createClientWithWrongProtocol_thenErrorIsThrown() { + try { + new SdncOscA1Client(A1ProtocolType.STD_V1_1, null, null, new AsyncRestClient("", null)); + fail("Should have thrown exception."); + } catch (IllegalArgumentException e) { + return; + } + } + + @Test + void getPolicyTypeIdentities_STD() { List policyTypeIds = clientUnderTest.getPolicyTypeIdentities().block(); assertEquals(1, policyTypeIds.size(), "should hardcoded to one"); assertEquals("", policyTypeIds.get(0), "should hardcoded to empty"); } @Test - public void testGetPolicyTypeIdentities_OSC() { + void getPolicyTypeIdentities_OSC() { clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_OSC_V1, // A1ClientHelper.createRic(RIC_1_URL).getConfig(), // controllerConfig(), asyncRestClientMock); - String response = createResponse(Arrays.asList(POLICY_TYPE_1_ID)); + String response = createOkResponseWithBody(Arrays.asList(POLICY_TYPE_1_ID)); whenAsyncPostThenReturn(Mono.just(response)); List policyTypeIds = clientUnderTest.getPolicyTypeIdentities().block(); - assertEquals(1, policyTypeIds.size(), ""); - assertEquals(POLICY_TYPE_1_ID, policyTypeIds.get(0), ""); + + assertEquals(1, policyTypeIds.size()); + assertEquals(POLICY_TYPE_1_ID, policyTypeIds.get(0)); String expUrl = RIC_1_URL + "/a1-p/policytypes"; - AdapterRequest expectedParams = ImmutableAdapterRequest.builder() // + ImmutableAdapterRequest expectedParams = ImmutableAdapterRequest.builder() // .nearRtRicUrl(expUrl) // .build(); String expInput = SdncJsonHelper.createInputJsonString(expectedParams); verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_URL, expInput, CONTROLLER_USERNAME, CONTROLLER_PASSWORD); - } - private String policiesUrl() { - return RIC_1_URL + "/A1-P/v1/policies"; - } + @Test + void getTypeSchema_STD() { + String policyType = clientUnderTest.getPolicyTypeSchema("").block(); - private Gson gson() { - return SdncOscA1Client.gson; + assertEquals("{}", policyType); } - private String createResponse(Object obj) { - AdapterResponse output = ImmutableAdapterResponse.builder() // - .body(gson().toJson(obj)) // - .httpStatus(200) // - .build(); + @Test + void getTypeSchema_OSC() throws IOException { + clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_OSC_V1, // + A1ClientHelper.createRic(RIC_1_URL).getConfig(), // + controllerConfig(), asyncRestClientMock); + + String ricResponse = loadFile("test_osc_get_schema_response.json"); + JsonElement elem = gson().fromJson(ricResponse, JsonElement.class); + String responseFromController = createOkResponseWithBody(elem); + whenAsyncPostThenReturn(Mono.just(responseFromController)); - return gson().toJson(output); + String response = clientUnderTest.getPolicyTypeSchema("policyTypeId").block(); + + JsonElement respJson = gson().fromJson(response, JsonElement.class); + assertEquals("policyTypeId", respJson.getAsJsonObject().get("title").getAsString(), + "title should be updated to contain policyType ID"); } @Test - public void testGetPolicyIdentities() { + void parseJsonArrayOfString() { + // One integer and one string + String inputString = "[1, \"1\" ]"; + + List result = SdncJsonHelper.parseJsonArrayOfString(inputString).collectList().block(); + assertEquals(2, result.size()); + assertEquals("1", result.get(0)); + assertEquals("1", result.get(1)); + } - String policyIdsResp = createResponse(Arrays.asList(POLICY_1_ID, POLICY_2_ID)); + @Test + void getPolicyIdentities_STD() { + + String policyIdsResp = createOkResponseWithBody(Arrays.asList(POLICY_1_ID, POLICY_2_ID)); whenAsyncPostThenReturn(Mono.just(policyIdsResp)); List returned = clientUnderTest.getPolicyIdentities().block(); - assertEquals(2, returned.size(), ""); - AdapterRequest expectedParams = ImmutableAdapterRequest.builder() // + assertEquals(2, returned.size()); + + ImmutableAdapterRequest expectedParams = ImmutableAdapterRequest.builder() // .nearRtRicUrl(policiesUrl()) // .build(); String expInput = SdncJsonHelper.createInputJsonString(expectedParams); @@ -150,19 +189,36 @@ public class SdncOscA1ClientTest { } @Test - public void testGetValidPolicyType() { - String policyType = clientUnderTest.getPolicyTypeSchema("").block(); - assertEquals("{}", policyType, ""); + void getPolicyIdentities_OSC() { + clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_OSC_V1, // + A1ClientHelper.createRic(RIC_1_URL).getConfig(), // + controllerConfig(), asyncRestClientMock); + + String policytypeIdsResp = createOkResponseWithBody(Arrays.asList(POLICY_TYPE_1_ID)); + String policyIdsResp = createOkResponseWithBody(Arrays.asList(POLICY_1_ID, POLICY_2_ID)); + whenAsyncPostThenReturn(Mono.just(policytypeIdsResp)).thenReturn(Mono.just(policyIdsResp)); + + List returned = clientUnderTest.getPolicyIdentities().block(); + + assertEquals(2, returned.size()); + + ImmutableAdapterRequest expectedParams = ImmutableAdapterRequest.builder() // + .nearRtRicUrl(RIC_1_URL + "/a1-p/policytypes/type1/policies") // + .build(); + String expInput = SdncJsonHelper.createInputJsonString(expectedParams); + verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_URL, expInput, CONTROLLER_USERNAME, + CONTROLLER_PASSWORD); } @Test - public void testPutPolicyValidResponse() { + void putPolicyValidResponse() { whenPostReturnOkResponse(); String returned = clientUnderTest .putPolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID)) .block(); - assertEquals("OK", returned, ""); + + assertEquals("OK", returned); final String expUrl = policiesUrl() + "/" + POLICY_1_ID; AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() // .nearRtRicUrl(expUrl) // @@ -174,18 +230,22 @@ public class SdncOscA1ClientTest { } @Test - public void testPutPolicyRejected() { + void putPolicyRejected() { final String policyJson = "{}"; - AdapterResponse adapterResponse = ImmutableAdapterResponse.builder() // + AdapterOutput adapterOutput = ImmutableAdapterOutput.builder() // .body("NOK") // - .httpStatus(400) // ERROR + .httpStatus(HttpStatus.BAD_REQUEST.value()) // ERROR .build(); - String resp = gson().toJson(adapterResponse); + String resp = SdncJsonHelper.createOutputJsonString(adapterOutput); whenAsyncPostThenReturn(Mono.just(resp)); Mono returnedMono = clientUnderTest .putPolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, policyJson, POLICY_TYPE_1_ID)); + StepVerifier.create(returnedMono) // + .expectSubscription() // + .expectErrorMatches(t -> t instanceof WebClientResponseException) // + .verify(); final String expUrl = policiesUrl() + "/" + POLICY_1_ID; AdapterRequest expRequestParams = ImmutableAdapterRequest.builder() // @@ -200,13 +260,14 @@ public class SdncOscA1ClientTest { } @Test - public void testDeletePolicy() { + void deletePolicy() { whenPostReturnOkResponse(); String returned = clientUnderTest .deletePolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID)) .block(); - assertEquals("OK", returned, ""); + + assertEquals("OK", returned); final String expUrl = policiesUrl() + "/" + POLICY_1_ID; AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() // .nearRtRicUrl(expUrl) // @@ -218,7 +279,7 @@ public class SdncOscA1ClientTest { } @Test - public void testGetStatus() { + void getStatus() { whenPostReturnOkResponse(); Policy policy = A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID); @@ -238,20 +299,72 @@ public class SdncOscA1ClientTest { } @Test - public void testGetVersion() { + void getVersion_STD() { whenPostReturnOkResponse(); + + A1ProtocolType returnedVersion = clientUnderTest.getProtocolVersion().block(); + + assertEquals(A1ProtocolType.SDNC_OSC_STD_V1_1, returnedVersion); + + whenPostReturnOkResponseNoBody(); + + returnedVersion = clientUnderTest.getProtocolVersion().block(); + + assertEquals(A1ProtocolType.SDNC_OSC_STD_V1_1, returnedVersion); + } + + @Test + void getVersion_OSC() { + clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_OSC_V1, // + A1ClientHelper.createRic(RIC_1_URL).getConfig(), // + controllerConfig(), asyncRestClientMock); + + whenAsyncPostThenReturn(Mono.error(new Exception("Error"))).thenReturn(Mono.just(createOkResponseString(true))); + A1ProtocolType returnedVersion = clientUnderTest.getProtocolVersion().block(); - assertEquals(A1ProtocolType.SDNC_OSC_STD_V1_1, returnedVersion, ""); + + assertEquals(A1ProtocolType.SDNC_OSC_OSC_V1, returnedVersion); + } + + private String policiesUrl() { + return RIC_1_URL + "/A1-P/v1/policies"; + } + + private Gson gson() { + return SdncOscA1Client.gson; + } + + private String loadFile(String fileName) throws IOException { + ClassLoader loader = Thread.currentThread().getContextClassLoader(); + URL url = loader.getResource(fileName); + File file = new File(url.getFile()); + return new String(Files.readAllBytes(file.toPath())); } private void whenPostReturnOkResponse() { - AdapterResponse adapterResponse = ImmutableAdapterResponse.builder() // - .body("OK") // - .httpStatus(200) // + whenAsyncPostThenReturn(Mono.just(createOkResponseString(true))); + } + + private void whenPostReturnOkResponseNoBody() { + whenAsyncPostThenReturn(Mono.just(createOkResponseString(false))); + } + + private String createOkResponseWithBody(Object body) { + AdapterOutput output = ImmutableAdapterOutput.builder() // + .body(gson().toJson(body)) // + .httpStatus(HttpStatus.OK.value()) // .build(); + return SdncJsonHelper.createOutputJsonString(output); + } - String resp = gson().toJson(adapterResponse); - whenAsyncPostThenReturn(Mono.just(resp)); + private String createOkResponseString(boolean withBody) { + Builder responseBuilder = ImmutableAdapterOutput.builder().httpStatus(HttpStatus.OK.value()); + if (withBody) { + responseBuilder.body(HttpStatus.OK.name()); + } else { + responseBuilder.body(Optional.empty()); + } + return SdncJsonHelper.createOutputJsonString(responseBuilder.build()); } private OngoingStubbing> whenAsyncPostThenReturn(Mono response) {