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=885db0f63a2f73d4a14cb6db153c8eba4573dd49;hpb=deb5bb7b792ba3ac90e4c622cd804c83e4647b92;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 885db0f6..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,246 +20,351 @@ 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.json.JSONException; 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.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.Flux; 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"; - private static final String POLICY_TYPES_IDENTITIES_URL = "/A1-ADAPTER-API:getPolicyTypeIdentities"; - private static final String POLICY_IDENTITIES_URL = "/A1-ADAPTER-API:getPolicyIdentities"; - private static final String POLICY_TYPES_URL = "/A1-ADAPTER-API:getPolicyType"; - private static final String PUT_POLICY_URL = "/A1-ADAPTER-API:putPolicy"; - private static final String DELETE_POLICY_URL = "/A1-ADAPTER-API:deletePolicy"; - + private static final String GET_A1_POLICY_URL = "/A1-ADAPTER-API:getA1Policy"; + private static final String PUT_A1_URL = "/A1-ADAPTER-API:putA1Policy"; + private static final String DELETE_A1_URL = "/A1-ADAPTER-API:deleteA1Policy"; + private static final String GET_A1_POLICY_STATUS_URL = "/A1-ADAPTER-API:getA1PolicyStatus"; private static final String POLICY_TYPE_1_ID = "type1"; - private static final String POLICY_TYPE_2_ID = "type2"; - private static final String POLICY_TYPE_SCHEMA_VALID = "{\"type\":\"type1\"}"; - private static final String POLICY_TYPE_SCHEMA_INVALID = "\"type\":\"type1\"}"; private static final String POLICY_1_ID = "policy1"; private static final String POLICY_2_ID = "policy2"; private static final String POLICY_JSON_VALID = "{\"scope\":{\"ueId\":\"ue1\"}}"; - private static final String POLICY_JSON_INVALID = "\"scope\":{\"ueId\":\"ue1\"}}"; SdncOscA1Client clientUnderTest; + @Mock 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); - clientUnderTest = new SdncOscA1Client(A1ClientHelper.createRic(RIC_1_URL).getConfig(), CONTROLLER_USERNAME, - CONTROLLER_PASSWORD, asyncRestClientMock); + void init() { + Ric ric = A1ClientHelper.createRic(RIC_1_URL); + + clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_STD_V1_1, ric.getConfig(), controllerConfig(), + asyncRestClientMock); } @Test - public void testGetPolicyTypeIdentities() { - SdncOscAdapterInput inputParams = ImmutableSdncOscAdapterInput.builder() // - .nearRtRicUrl(RIC_1_URL) // - .build(); - String inputJsonString = A1ClientHelper.createInputJsonString(inputParams); + 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 + void getPolicyTypeIdentities_OSC() { + clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_OSC_V1, // + A1ClientHelper.createRic(RIC_1_URL).getConfig(), // + controllerConfig(), asyncRestClientMock); + + String response = createOkResponseWithBody(Arrays.asList(POLICY_TYPE_1_ID)); + whenAsyncPostThenReturn(Mono.just(response)); - List policyTypeIds = Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID); - Mono policyTypeIdsResp = - A1ClientHelper.createOutputJsonResponse("policy-type-id-list", policyTypeIds.toString()); - whenAsyncPostThenReturn(policyTypeIdsResp); + List policyTypeIds = clientUnderTest.getPolicyTypeIdentities().block(); - Mono> returnedMono = clientUnderTest.getPolicyTypeIdentities(); + assertEquals(1, policyTypeIds.size()); + assertEquals(POLICY_TYPE_1_ID, policyTypeIds.get(0)); - verify(asyncRestClientMock).postWithAuthHeader(POLICY_TYPES_IDENTITIES_URL, inputJsonString, - CONTROLLER_USERNAME, CONTROLLER_PASSWORD); - StepVerifier.create(returnedMono).expectNext(policyTypeIds).expectComplete().verify(); + String expUrl = RIC_1_URL + "/a1-p/policytypes"; + ImmutableAdapterRequest expectedParams = ImmutableAdapterRequest.builder() // + .nearRtRicUrl(expUrl) // + .build(); + String expInput = SdncJsonHelper.createInputJsonString(expectedParams); + verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_URL, expInput, CONTROLLER_USERNAME, + CONTROLLER_PASSWORD); } @Test - public void testGetPolicyIdentities() { - SdncOscAdapterInput inputParams = ImmutableSdncOscAdapterInput.builder() // - .nearRtRicUrl(RIC_1_URL) // - .build(); - String inputJsonString = A1ClientHelper.createInputJsonString(inputParams); + void getTypeSchema_STD() { + String policyType = clientUnderTest.getPolicyTypeSchema("").block(); + + assertEquals("{}", policyType); + } - List policyIds = Arrays.asList(POLICY_1_ID, POLICY_2_ID); - Mono policyIdsResp = A1ClientHelper.createOutputJsonResponse("policy-id-list", policyIds.toString()); - whenAsyncPostThenReturn(policyIdsResp); + @Test + void getTypeSchema_OSC() throws IOException { + clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_OSC_V1, // + A1ClientHelper.createRic(RIC_1_URL).getConfig(), // + controllerConfig(), asyncRestClientMock); - Mono> returnedMono = clientUnderTest.getPolicyIdentities(); + String ricResponse = loadFile("test_osc_get_schema_response.json"); + JsonElement elem = gson().fromJson(ricResponse, JsonElement.class); + String responseFromController = createOkResponseWithBody(elem); + whenAsyncPostThenReturn(Mono.just(responseFromController)); - verify(asyncRestClientMock).postWithAuthHeader(POLICY_IDENTITIES_URL, inputJsonString, CONTROLLER_USERNAME, - CONTROLLER_PASSWORD); - StepVerifier.create(returnedMono).expectNext(policyIds).expectComplete().verify(); + 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 testGetValidPolicyType() { - SdncOscAdapterInput inputParams = ImmutableSdncOscAdapterInput.builder() // - .nearRtRicUrl(RIC_1_URL) // - .policyTypeId(POLICY_TYPE_1_ID) // - .build(); - String inputJsonString = A1ClientHelper.createInputJsonString(inputParams); + 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)); + } + + @Test + void getPolicyIdentities_STD() { + + String policyIdsResp = createOkResponseWithBody(Arrays.asList(POLICY_1_ID, POLICY_2_ID)); + whenAsyncPostThenReturn(Mono.just(policyIdsResp)); - String policyType = "{\"policySchema\": " + POLICY_TYPE_SCHEMA_VALID + ", \"statusSchema\": {} }"; - Mono policyTypeResp = A1ClientHelper.createOutputJsonResponse("policy-type", policyType); - whenAsyncPostThenReturn(policyTypeResp); + List returned = clientUnderTest.getPolicyIdentities().block(); - Mono returnedMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID); + assertEquals(2, returned.size()); - verify(asyncRestClientMock).postWithAuthHeader(POLICY_TYPES_URL, inputJsonString, CONTROLLER_USERNAME, + ImmutableAdapterRequest expectedParams = ImmutableAdapterRequest.builder() // + .nearRtRicUrl(policiesUrl()) // + .build(); + String expInput = SdncJsonHelper.createInputJsonString(expectedParams); + verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_URL, expInput, CONTROLLER_USERNAME, CONTROLLER_PASSWORD); - StepVerifier.create(returnedMono).expectNext(POLICY_TYPE_SCHEMA_VALID).expectComplete().verify(); + } @Test - public void testGetInvalidPolicyType() { - SdncOscAdapterInput inputParams = ImmutableSdncOscAdapterInput.builder() // - .nearRtRicUrl(RIC_1_URL) // - .policyTypeId(POLICY_TYPE_1_ID) // - .build(); - String inputJsonString = A1ClientHelper.createInputJsonString(inputParams); + void getPolicyIdentities_OSC() { + clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_OSC_V1, // + A1ClientHelper.createRic(RIC_1_URL).getConfig(), // + controllerConfig(), asyncRestClientMock); - String policyType = "{\"policySchema\": " + POLICY_TYPE_SCHEMA_INVALID + ", \"statusSchema\": {} }"; - Mono policyTypeResp = A1ClientHelper.createOutputJsonResponse("policy-type", policyType); - whenAsyncPostThenReturn(policyTypeResp); + 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)); - Mono returnedMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID); + List returned = clientUnderTest.getPolicyIdentities().block(); - verify(asyncRestClientMock).postWithAuthHeader(POLICY_TYPES_URL, inputJsonString, CONTROLLER_USERNAME, + 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); - StepVerifier.create(returnedMono).expectErrorMatches(throwable -> throwable instanceof JSONException).verify(); } @Test - public void testPutPolicyValidResponse() { - SdncOscAdapterInput inputParams = ImmutableSdncOscAdapterInput.builder() // - .nearRtRicUrl(RIC_1_URL) // - .policyTypeId(POLICY_TYPE_1_ID) // - .policyId(POLICY_1_ID) // - .policy(POLICY_JSON_VALID) // + 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); + final String expUrl = policiesUrl() + "/" + POLICY_1_ID; + AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() // + .nearRtRicUrl(expUrl) // + .body(POLICY_JSON_VALID) // .build(); - String inputJsonString = A1ClientHelper.createInputJsonString(inputParams); + String expInput = SdncJsonHelper.createInputJsonString(expectedInputParams); - Mono policyResp = A1ClientHelper.createOutputJsonResponse("returned-policy", POLICY_JSON_VALID); - whenAsyncPostThenReturn(policyResp); + verify(asyncRestClientMock).postWithAuthHeader(PUT_A1_URL, expInput, CONTROLLER_USERNAME, CONTROLLER_PASSWORD); + } - Mono returnedMono = clientUnderTest - .putPolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID)); + @Test + void putPolicyRejected() { + final String policyJson = "{}"; + AdapterOutput adapterOutput = ImmutableAdapterOutput.builder() // + .body("NOK") // + .httpStatus(HttpStatus.BAD_REQUEST.value()) // ERROR + .build(); - verify(asyncRestClientMock).postWithAuthHeader(PUT_POLICY_URL, inputJsonString, CONTROLLER_USERNAME, + 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 = SdncJsonHelper.createInputJsonString(expRequestParams); + verify(asyncRestClientMock).postWithAuthHeader(PUT_A1_URL, expRequest, CONTROLLER_USERNAME, CONTROLLER_PASSWORD); - StepVerifier.create(returnedMono).expectNext(POLICY_JSON_VALID).expectComplete().verify(); + StepVerifier.create(returnedMono) + .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify(); } @Test - public void testPutPolicyInvalidResponse() { - SdncOscAdapterInput inputParams = ImmutableSdncOscAdapterInput.builder() // - .nearRtRicUrl(RIC_1_URL) // - .policyTypeId(POLICY_TYPE_1_ID) // - .policyId(POLICY_1_ID) // - .policy(POLICY_JSON_VALID) // - .build(); - String inputJsonString = A1ClientHelper.createInputJsonString(inputParams); + void deletePolicy() { + whenPostReturnOkResponse(); - Mono policyResp = A1ClientHelper.createOutputJsonResponse("returned-policy", POLICY_JSON_INVALID); - whenAsyncPostThenReturn(policyResp); + String returned = clientUnderTest + .deletePolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID)) + .block(); - Mono returnedMono = clientUnderTest - .putPolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID)); + assertEquals("OK", returned); + final String expUrl = policiesUrl() + "/" + POLICY_1_ID; + AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() // + .nearRtRicUrl(expUrl) // + .build(); + String expInput = SdncJsonHelper.createInputJsonString(expectedInputParams); - verify(asyncRestClientMock).postWithAuthHeader(PUT_POLICY_URL, inputJsonString, CONTROLLER_USERNAME, + verify(asyncRestClientMock).postWithAuthHeader(DELETE_A1_URL, expInput, CONTROLLER_USERNAME, CONTROLLER_PASSWORD); - StepVerifier.create(returnedMono).expectErrorMatches(throwable -> throwable instanceof JSONException).verify(); } @Test - public void testDeletePolicy() { - SdncOscAdapterInput inputParams = ImmutableSdncOscAdapterInput.builder() // - .nearRtRicUrl(RIC_1_URL) // - .policyId(POLICY_1_ID) // - .build(); - String inputJsonString = A1ClientHelper.createInputJsonString(inputParams); + void getStatus() { + whenPostReturnOkResponse(); - whenAsyncPostThenReturn(Mono.empty()); + Policy policy = A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID); - Mono returnedMono = clientUnderTest - .deletePolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID)); + String returnedStatus = clientUnderTest.getPolicyStatus(policy).block(); + + assertEquals("OK", returnedStatus, "unexpected status"); - verify(asyncRestClientMock).postWithAuthHeader(DELETE_POLICY_URL, inputJsonString, CONTROLLER_USERNAME, + final String expUrl = policiesUrl() + "/" + POLICY_1_ID + "/status"; + AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() // + .nearRtRicUrl(expUrl) // + .build(); + String expInput = SdncJsonHelper.createInputJsonString(expectedInputParams); + + verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_STATUS_URL, expInput, CONTROLLER_USERNAME, CONTROLLER_PASSWORD); - StepVerifier.create(returnedMono).expectComplete().verify(); } @Test - public void testDeleteAllPolicies() { - SdncOscAdapterInput inputParams = ImmutableSdncOscAdapterInput.builder() // - .nearRtRicUrl(RIC_1_URL) // - .build(); - String inputJsonStringGetIds = A1ClientHelper.createInputJsonString(inputParams); - inputParams = ImmutableSdncOscAdapterInput.builder() // - .nearRtRicUrl(RIC_1_URL) // - .policyId(POLICY_1_ID) // - .build(); - String inputJsonStringDeletePolicy1 = A1ClientHelper.createInputJsonString(inputParams); - inputParams = ImmutableSdncOscAdapterInput.builder() // - .nearRtRicUrl(RIC_1_URL) // - .policyId(POLICY_2_ID) // - .build(); - String inputJsonStringDeletePolicy2 = A1ClientHelper.createInputJsonString(inputParams); + void getVersion_STD() { + whenPostReturnOkResponse(); + + A1ProtocolType returnedVersion = clientUnderTest.getProtocolVersion().block(); + + assertEquals(A1ProtocolType.SDNC_OSC_STD_V1_1, returnedVersion); - List policyIds = Arrays.asList(POLICY_1_ID, POLICY_2_ID); - Mono policyIdsResp = A1ClientHelper.createOutputJsonResponse("policy-id-list", policyIds.toString()); - whenAsyncPostThenReturn(policyIdsResp).thenReturn(Mono.empty()); + whenPostReturnOkResponseNoBody(); - Flux returnedFlux = clientUnderTest.deleteAllPolicies(); + returnedVersion = clientUnderTest.getProtocolVersion().block(); - StepVerifier.create(returnedFlux).expectComplete().verify(); - verify(asyncRestClientMock).postWithAuthHeader(POLICY_IDENTITIES_URL, inputJsonStringGetIds, - CONTROLLER_USERNAME, CONTROLLER_PASSWORD); - verify(asyncRestClientMock).postWithAuthHeader(DELETE_POLICY_URL, inputJsonStringDeletePolicy1, - CONTROLLER_USERNAME, CONTROLLER_PASSWORD); - verify(asyncRestClientMock).postWithAuthHeader(DELETE_POLICY_URL, inputJsonStringDeletePolicy2, - CONTROLLER_USERNAME, CONTROLLER_PASSWORD); + assertEquals(A1ProtocolType.SDNC_OSC_STD_V1_1, returnedVersion); } @Test - public void testGetStatus() { - SdncOscAdapterInput inputParams = ImmutableSdncOscAdapterInput.builder() // - .nearRtRicUrl(RIC_1_URL) // - .policyId(POLICY_1_ID) // - .build(); - String inputJsonString = A1ClientHelper.createInputJsonString(inputParams); + void getVersion_OSC() { + clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_OSC_V1, // + A1ClientHelper.createRic(RIC_1_URL).getConfig(), // + controllerConfig(), asyncRestClientMock); - String status = "STATUS"; - Mono policyStatusResp = A1ClientHelper.createOutputJsonResponse("policy-status", status); - whenAsyncPostThenReturn(policyStatusResp); + whenAsyncPostThenReturn(Mono.error(new Exception("Error"))).thenReturn(Mono.just(createOkResponseString(true))); - Policy policy = A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID); + A1ProtocolType returnedVersion = clientUnderTest.getProtocolVersion().block(); - String returnedStatus = clientUnderTest.getPolicyStatus(policy).block(); + assertEquals(A1ProtocolType.SDNC_OSC_OSC_V1, returnedVersion); + } - assertEquals(status, returnedStatus, "unexpexted status"); + private String policiesUrl() { + return RIC_1_URL + "/A1-P/v1/policies"; + } - final String expectedUrl = "/A1-ADAPTER-API:getPolicyStatus"; - verify(asyncRestClientMock).postWithAuthHeader(expectedUrl, inputJsonString, CONTROLLER_USERNAME, - CONTROLLER_PASSWORD); + 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() { + 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); + } + + 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) {