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=fd50425084de68aa74df677ff8696ab543c03e3f;hb=f725c8b2b708a4d8b09b1a65151705af471e4405;hp=e463cae91d14d91198516917c93dddd346b16173;hpb=f700867fa65c7172cee7fca229eb10f2ecdf77dd;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 e463cae9..fd504250 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 @@ -30,6 +30,7 @@ import com.google.gson.Gson; import java.util.Arrays; import java.util.List; +import java.util.Optional; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -37,8 +38,10 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.stubbing.OngoingStubbing; import org.oransc.policyagent.clients.A1Client.A1ProtocolType; +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.web.reactive.function.client.WebClientResponseException; @@ -64,12 +67,22 @@ public class SdncOscA1ClientTest { AsyncRestClient asyncRestClientMock; + private ControllerConfig controllerConfig() { + return ImmutableControllerConfig.builder() // + .name("name") // + .baseUrl("baseUrl") // + .password(CONTROLLER_PASSWORD) // + .userName(CONTROLLER_USERNAME) // + .build(); + } + @BeforeEach public void init() { asyncRestClientMock = mock(AsyncRestClient.class); Ric ric = A1ClientHelper.createRic(RIC_1_URL); - clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_STD_V1_1, ric.getConfig(), CONTROLLER_USERNAME, - CONTROLLER_PASSWORD, asyncRestClientMock); + + clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_STD_V1_1, ric.getConfig(), controllerConfig(), + asyncRestClientMock); } @Test @@ -83,7 +96,7 @@ public class SdncOscA1ClientTest { public void testGetPolicyTypeIdentities_OSC() { clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_OSC_V1, // A1ClientHelper.createRic(RIC_1_URL).getConfig(), // - CONTROLLER_USERNAME, CONTROLLER_PASSWORD, asyncRestClientMock); + controllerConfig(), asyncRestClientMock); String response = createResponse(Arrays.asList(POLICY_TYPE_1_ID)); whenAsyncPostThenReturn(Mono.just(response)); @@ -93,10 +106,10 @@ public class SdncOscA1ClientTest { 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 = A1ClientHelper.createInputJsonString(expectedParams); + String expInput = SdncJsonHelper.createInputJsonString(expectedParams); verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_URL, expInput, CONTROLLER_USERNAME, CONTROLLER_PASSWORD); @@ -110,13 +123,12 @@ public class SdncOscA1ClientTest { return SdncOscA1Client.gson; } - private String createResponse(Object obj) { - AdapterResponse output = ImmutableAdapterResponse.builder() // - .body(gson().toJson(obj)) // + private String createResponse(Object body) { + AdapterOutput output = ImmutableAdapterOutput.builder() // + .body(gson().toJson(body)) // .httpStatus(200) // .build(); - - return gson().toJson(output); + return SdncJsonHelper.createOutputJsonString(output); } @Test @@ -128,10 +140,10 @@ public class SdncOscA1ClientTest { List returned = clientUnderTest.getPolicyIdentities().block(); assertEquals(2, returned.size(), ""); - AdapterRequest expectedParams = ImmutableAdapterRequest.builder() // + ImmutableAdapterRequest expectedParams = ImmutableAdapterRequest.builder() // .nearRtRicUrl(policiesUrl()) // .build(); - String expInput = A1ClientHelper.createInputJsonString(expectedParams); + String expInput = SdncJsonHelper.createInputJsonString(expectedParams); verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_URL, expInput, CONTROLLER_USERNAME, CONTROLLER_PASSWORD); @@ -156,7 +168,7 @@ public class SdncOscA1ClientTest { .nearRtRicUrl(expUrl) // .body(POLICY_JSON_VALID) // .build(); - String expInput = A1ClientHelper.createInputJsonString(expectedInputParams); + String expInput = SdncJsonHelper.createInputJsonString(expectedInputParams); verify(asyncRestClientMock).postWithAuthHeader(PUT_A1_URL, expInput, CONTROLLER_USERNAME, CONTROLLER_PASSWORD); } @@ -164,23 +176,27 @@ public class SdncOscA1ClientTest { @Test public void testPutPolicyRejected() { final String policyJson = "{}"; - AdapterResponse adapterResponse = ImmutableAdapterResponse.builder() // + AdapterOutput adapterOutput = ImmutableAdapterOutput.builder() // .body("NOK") // .httpStatus(400) // 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() // .nearRtRicUrl(expUrl) // .body(policyJson) // .build(); - String expRequest = A1ClientHelper.createInputJsonString(expRequestParams); + String expRequest = SdncJsonHelper.createInputJsonString(expRequestParams); verify(asyncRestClientMock).postWithAuthHeader(PUT_A1_URL, expRequest, CONTROLLER_USERNAME, CONTROLLER_PASSWORD); StepVerifier.create(returnedMono) @@ -199,7 +215,7 @@ public class SdncOscA1ClientTest { AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() // .nearRtRicUrl(expUrl) // .build(); - String expInput = A1ClientHelper.createInputJsonString(expectedInputParams); + String expInput = SdncJsonHelper.createInputJsonString(expectedInputParams); verify(asyncRestClientMock).postWithAuthHeader(DELETE_A1_URL, expInput, CONTROLLER_USERNAME, CONTROLLER_PASSWORD); @@ -213,25 +229,46 @@ public class SdncOscA1ClientTest { String returnedStatus = clientUnderTest.getPolicyStatus(policy).block(); - assertEquals("OK", returnedStatus, "unexpeted status"); + assertEquals("OK", returnedStatus, "unexpected status"); final String expUrl = policiesUrl() + "/" + POLICY_1_ID + "/status"; AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() // .nearRtRicUrl(expUrl) // .build(); - String expInput = A1ClientHelper.createInputJsonString(expectedInputParams); + String expInput = SdncJsonHelper.createInputJsonString(expectedInputParams); verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_STATUS_URL, expInput, CONTROLLER_USERNAME, CONTROLLER_PASSWORD); } + @Test + public void testGetVersion() { + 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, ""); + } + private void whenPostReturnOkResponse() { - AdapterResponse adapterResponse = ImmutableAdapterResponse.builder() // + AdapterOutput adapterOutput = ImmutableAdapterOutput.builder() // .body("OK") // .httpStatus(200) // .build(); - String resp = gson().toJson(adapterResponse); + String resp = SdncJsonHelper.createOutputJsonString(adapterOutput); + whenAsyncPostThenReturn(Mono.just(resp)); + } + + private void whenPostReturnOkResponseNoBody() { + AdapterOutput adapterOutput = ImmutableAdapterOutput.builder() // + .httpStatus(200) // + .body(Optional.empty()) // + .build(); + + String resp = SdncJsonHelper.createOutputJsonString(adapterOutput); whenAsyncPostThenReturn(Mono.just(resp)); }