diff options
author | raviteja.karumuri <raviteja.karumuri@est.tech> | 2024-04-26 16:14:43 +0100 |
---|---|---|
committer | raviteja.karumuri <raviteja.karumuri@est.tech> | 2024-05-29 18:01:36 +0100 |
commit | a5eea6c8b67ede9db9b741935398e246c6095671 (patch) | |
tree | 58a0419377f93ba81c901f3285b37f952136ab0a | |
parent | d3aad19502465a7ff60d54cedbb06af301c99915 (diff) |
Unit Test cases for the V3Controller Endpoints
Issue-ID: CCSDK-4007
Change-Id: Ibc55cc660406b2129a56c0c0e565ab857cb4629b
Signed-off-by: Raviteja Karumuri <raviteja.karumuri@est.tech>
5 files changed, 312 insertions, 63 deletions
diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerV3.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerV3.java index 83d3ecc8..edc3be12 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerV3.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerV3.java @@ -27,8 +27,7 @@ import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.PolicyContro import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.PolicyInformation; import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.PolicyObjectInformation; import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.PolicyTypeInformation; -import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyType; -import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric; +import org.onap.ccsdk.oran.a1policymanagementservice.service.v3.ErrorHandlingService; import org.onap.ccsdk.oran.a1policymanagementservice.service.v3.PolicyService; import org.onap.ccsdk.oran.a1policymanagementservice.util.v3.Helper; import org.springframework.beans.factory.annotation.Autowired; @@ -53,9 +52,9 @@ public class PolicyControllerV3 implements A1PolicyManagementApi { @Autowired private Helper helper; - private PolicyType policyType; + @Autowired + private ErrorHandlingService errorHandlingService; - private Ric ric; @Override public Mono<ResponseEntity<PolicyObjectInformation>> createPolicy(Mono<PolicyObjectInformation> policyObjectInformation, ServerWebExchange exchange) { return policyObjectInformation.flatMap(policyObjectInfo -> { @@ -90,6 +89,7 @@ public class PolicyControllerV3 implements A1PolicyManagementApi { @Override public Mono<ResponseEntity<Object>> putPolicy(String policyId, Mono<Object> body, ServerWebExchange exchange) throws Exception { - return body.flatMap(payload -> policyService.putPolicyService(policyId, payload, exchange)); + return body.flatMap(payload -> policyService.putPolicyService(policyId, payload, exchange)) + .doOnError(error -> errorHandlingService.handleError(error)); } } diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/service/v3/PolicyService.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/service/v3/PolicyService.java index 66b5ae0c..6cd9b8d3 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/service/v3/PolicyService.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/service/v3/PolicyService.java @@ -112,17 +112,19 @@ public class PolicyService { } public Mono<ResponseEntity<Object>> putPolicyService(String policyId, Object body, ServerWebExchange exchange) { - try { Policy existingPolicy = policies.getPolicy(policyId); PolicyObjectInformation pos = new PolicyObjectInformation(existingPolicy.getRic().getConfig().getRicId(), body, existingPolicy.getType().getId()); Policy updatedPolicy = helper.buildPolicy(pos, existingPolicy.getType(), existingPolicy.getRic(), policyId); Ric ric = existingPolicy.getRic(); - ric.getLock().lock(Lock.LockType.SHARED, "updatePolicy") + return authorizationService.authCheck(exchange, updatedPolicy, AccessType.WRITE) + .doOnError(error -> errorHandlingService.handleError(error)) + .flatMap(policy -> ric.getLock().lock(Lock.LockType.SHARED, "updatePolicy")) + .doOnError(error -> errorHandlingService.handleError(error)) .flatMap(grant -> postPolicy(updatedPolicy, grant)) + .map(header -> new ResponseEntity<Object>(policies.get(updatedPolicy.getId()).getJson(), HttpStatus.OK)) .doOnError(error -> errorHandlingService.handleError(error)); - return Mono.just(new ResponseEntity<>(policies.getPolicy(policyId), HttpStatus.OK)); } catch(Exception ex) { return Mono.error(ex); } @@ -198,12 +200,12 @@ public class PolicyService { } private Mono<ResponseEntity<Void>> deletePolicy(Policy policy, Lock.Grant grant) { - System.out.println(); return helper.checkRicStateIdle(policy.getRic()) .doOnError(error -> errorHandlingService.handleError(error)) .flatMap(ric -> helper.checkSupportedType(ric, policy.getType())) .doOnError(error -> errorHandlingService.handleError(error)) .flatMap(ric -> a1ClientFactory.createA1Client(ric)) + .doOnError(error -> errorHandlingService.handleError(error)) .flatMap(a1Client -> a1Client.deletePolicy(policy)) .doOnError(error -> errorHandlingService.handleError(error)) .doOnNext(policyString -> policies.remove(policy)) diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerTest.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerTest.java index 1ec85109..d92097bd 100644 --- a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerTest.java +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerTest.java @@ -20,12 +20,14 @@ package org.onap.ccsdk.oran.a1policymanagementservice.controllers.v3; +import com.google.gson.Gson; import org.junit.jupiter.api.*; import org.onap.ccsdk.oran.a1policymanagementservice.clients.SecurityContext; import org.onap.ccsdk.oran.a1policymanagementservice.config.TestConfig; import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig; import org.onap.ccsdk.oran.a1policymanagementservice.controllers.OpenPolicyAgentSimulatorController; import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.RappSimulatorController; +import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.PolicyObjectInformation; import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies; import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes; import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics; @@ -48,6 +50,7 @@ import reactor.core.publisher.Mono; import java.lang.invoke.MethodHandles; import java.nio.file.Path; +import java.util.Objects; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; @@ -96,6 +99,9 @@ public class PolicyControllerTest { @Autowired private OpenPolicyAgentSimulatorController openPolicyAgentSimulatorController; + @Autowired + private Gson gson; + @LocalServerPort private int port; @@ -139,7 +145,25 @@ public class PolicyControllerTest { testHelper.addPolicyType(policyTypeName, nonRtRicId); String policyBody = testHelper.postPolicyBody(nonRtRicId, policyTypeName); Mono<ResponseEntity<String>> responseMono = testHelper.restClientV3().postForEntity(url, policyBody); - testHelper.testSuccessResponse(responseMono, HttpStatus.CREATED, "{\"scope\":{\"ueId\":\"ue5100\",\"qosId\":\"qos5100\"},\"qosObjectives\":{\"priorityLevel\":5100.0}}"); + testHelper.testSuccessResponse(responseMono, HttpStatus.CREATED, responseBody -> + responseBody.contains("{\"scope\":{\"ueId\":\"ue5100\",\"qosId\":\"qos5100\"},\"qosObjectives\":{\"priorityLevel\":5100.0}}")); + testHelper.testSuccessHeader(responseMono, "location", headerValue -> headerValue.contains("https://localhost:" + port + "/a1policymanagement/v1/policies/")); + } + + @Test + @DisplayName("test delete Policy") + void testDeletePolicy() throws Exception { + String nonRtRicId = "ric.1"; + String policyTypeName = "type1_1.2.3"; + String url = "/policies"; + testHelper.addPolicyType(policyTypeName, nonRtRicId); + String policyBody = testHelper.postPolicyBody(nonRtRicId, policyTypeName); + Mono<ResponseEntity<String>> responseMono = testHelper.restClientV3().postForEntity(url, policyBody); + String []locationHeader = Objects.requireNonNull(Objects.requireNonNull(responseMono.block()).getHeaders() + .get("location")).get(0).split("/"); + String policyID = locationHeader[(locationHeader.length) - 1]; + Mono<ResponseEntity<String>> responseMonoDelete = testHelper.restClientV3().deleteForEntity(url+"/" +policyID); + testHelper.testSuccessResponse(responseMonoDelete, HttpStatus.NO_CONTENT, responseBody -> true); } @Test @@ -179,4 +203,30 @@ public class PolicyControllerTest { Mono<ResponseEntity<String>> responseMono = testHelper.restClientV3().postForEntity(url, policyBody); testHelper.testErrorCode(responseMono, HttpStatus.NOT_FOUND, "Could not find type: noPolicyType"); } + + @Test + public void testGetPolicyTypesNoRicFound() throws Exception{ + String policyTypeName = "type1_1.2.3"; + String nonRtRicId = "ricOne"; + testHelper.addPolicyType(policyTypeName, nonRtRicId); + Mono<ResponseEntity<String>> responseMono = testHelper.restClientV3().getForEntity("/policyTypes" + "?nearRtRicId=\"noRic\""); + testHelper.testErrorCode(responseMono, HttpStatus.NOT_FOUND, "Near-RT RIC not Found using ID:"); + } + + @Test + @DisplayName("test get Policy") + void testGetPolicy() throws Exception { + String nonRtRicId = "ric.1"; + String policyTypeName = "type1_1.2.3"; + String url = "/policies"; + testHelper.addPolicyType(policyTypeName, nonRtRicId); + String policyBody = testHelper.postPolicyBody(nonRtRicId, policyTypeName); + Mono<ResponseEntity<String>> responseMono = testHelper.restClientV3().postForEntity(url, policyBody); + String []locationHeader = Objects.requireNonNull(Objects.requireNonNull(responseMono.block()).getHeaders() + .get("location")).get(0).split("/"); + String policyID = locationHeader[(locationHeader.length) - 1]; + Mono<ResponseEntity<String>> responseMonoGet = testHelper.restClientV3().getForEntity(url+"/" +policyID); + testHelper.testSuccessResponse(responseMonoGet, HttpStatus.OK, responseBody -> + responseBody.contains("{\"scope\":{\"ueId\":\"ue5100\",\"qosId\":\"qos5100\"},\"qosObjectives\":{\"priorityLevel\":5100.0}}")); + } } diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/service/v3/PolicyServiceTest.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/service/v3/PolicyServiceTest.java index ee0073a4..7b550e3a 100644 --- a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/service/v3/PolicyServiceTest.java +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/service/v3/PolicyServiceTest.java @@ -20,16 +20,22 @@ package org.onap.ccsdk.oran.a1policymanagementservice.service.v3; +import com.google.gson.Gson; +import com.google.gson.JsonParser; import org.junit.jupiter.api.*; import org.mockito.Mockito; -import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1ClientFactory; +import org.onap.ccsdk.oran.a1policymanagementservice.config.TestConfig; +import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.EntityNotFoundException; import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException; +import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.PolicyInformation; import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.PolicyObjectInformation; +import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.PolicyTypeInformation; import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies; import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy; -import org.onap.ccsdk.oran.a1policymanagementservice.service.v3.AuthorizationService; -import org.onap.ccsdk.oran.a1policymanagementservice.service.v3.PolicyService; +import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyType; +import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics; import org.onap.ccsdk.oran.a1policymanagementservice.util.v3.Helper; +import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory; import org.onap.ccsdk.oran.a1policymanagementservice.utils.v3.TestHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -38,35 +44,41 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestPropertySource; import org.springframework.util.FileSystemUtils; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; +import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import java.io.IOException; import java.lang.invoke.MethodHandles; import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Map; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; @TestMethodOrder(MethodOrderer.MethodName.class) -@SpringBootTest() +@SpringBootTest +@ContextConfiguration(classes = TestConfig.class) @TestPropertySource(properties = { // "app.vardata-directory=/tmp/pmstestv3", // }) public class PolicyServiceTest { private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); - @Autowired - private Policies policies; @Autowired - private A1ClientFactory a1ClientFactory; + private Policies policies; @Autowired - private ErrorHandlingService errorHandlingService; + private Rics rics; @Autowired private PolicyService policyService; @@ -80,9 +92,16 @@ public class PolicyServiceTest { @MockBean private AuthorizationService authorizationService; + @Autowired + private MockA1ClientFactory a1ClientFactory; + + @Autowired + private Gson gson; + @AfterEach public void clear() { policies.clear(); + rics.clear(); } @AfterAll @@ -123,4 +142,211 @@ public class PolicyServiceTest { Mono<ResponseEntity<PolicyObjectInformation>> responseMono = policyService.createPolicyService(testHelper.policyObjectInfo(nonRtRicId, policyTypeName), serverWebExchange); testHelper.verifyMockError(responseMono, "Not authorized"); } + + @Test + public void testDeletePolicySuccess() throws Exception { + + String policyTypeName = "uri_type_123"; + String nonRtRicId = "Ric_347"; + testHelper.addPolicyType(policyTypeName, nonRtRicId); + Policy policy = testHelper.buidTestPolicy(testHelper.policyObjectInfo(nonRtRicId, policyTypeName), "122344-5674"); + policies.put(policy); + ServerWebExchange serverWebExchange = Mockito.mock(DefaultServerWebExchange.class); + when(helper.checkRicStateIdle(any())).thenReturn(Mono.just(policy.getRic())); + when(helper.checkSupportedType(any(), any())).thenReturn(Mono.just(policy.getRic())); + when(authorizationService.authCheck(any(), any(), any())).thenReturn(Mono.just(policy)); + Mono<ResponseEntity<Void>> responseMonoDelete = policyService.deletePolicyService(policy.getId(), serverWebExchange); + assert(policies.size() == 1); + testHelper.testSuccessResponse(responseMonoDelete, HttpStatus.NO_CONTENT, responseBody -> policies.size() == 0); + } + + @Test + public void testDeletePolicyThrowsException() throws Exception { + + ServerWebExchange serverWebExchange = Mockito.mock(DefaultServerWebExchange.class); + assertThrows(EntityNotFoundException.class, () -> policyService.deletePolicyService("dummyPolicyID", serverWebExchange)); + } + + @Test + public void testPutPolicy() throws Exception { + + String policyTypeName = "uri_type_123"; + String nonRtRicId = "Ric_347"; + testHelper.addPolicyType(policyTypeName, nonRtRicId); + Policy policy = testHelper.buidTestPolicy(testHelper.policyObjectInfo(nonRtRicId, policyTypeName), "122344-5674"); + policies.put(policy); + ServerWebExchange serverWebExchange = Mockito.mock(DefaultServerWebExchange.class); + PolicyObjectInformation updatedPolicyObjectInfo = testHelper.policyObjectInfo(nonRtRicId, policyTypeName); + updatedPolicyObjectInfo.setPolicyObject(gson.fromJson(JsonParser.parseString("{\n" + + " \"scope\": {\n" + + " \"ueId\": \"ue6100\",\n" + + " \"qosId\": \"qos6100\"\n" + + " },\n" + + " \"qosObjectives\": {\n" + + " \"priorityLevel\": 6100.0\n" + + " }\n" + + " }").getAsJsonObject().toString(), Map.class)); + Policy updatedPolicy = testHelper.buidTestPolicy(updatedPolicyObjectInfo, "122344-5674"); + when(helper.buildPolicy(any(),any(), any(), any())).thenReturn(updatedPolicy); + when(helper.checkRicStateIdle(any())).thenReturn(Mono.just(updatedPolicy.getRic())); + when(helper.checkSupportedType(any(), any())).thenReturn(Mono.just(updatedPolicy.getRic())); + when(authorizationService.authCheck(any(), any(), any())).thenReturn(Mono.just(updatedPolicy)); + Mono<ResponseEntity<Object>> responseMono = policyService.putPolicyService(policy.getId(), updatedPolicyObjectInfo.getPolicyObject(), serverWebExchange); + testHelper.testSuccessResponse(responseMono, HttpStatus.OK, responseBody -> { + if (responseBody instanceof String returnPolicy) + return returnPolicy.contains(updatedPolicy.getJson()); + return false; + }); + } + + @Test + public void testGetPolicyTypes() throws Exception { + + String policyTypeName = "uri_type_123"; + String nonRtRicId = "Ric_347"; + testHelper.addPolicyType(policyTypeName, nonRtRicId); + ServerWebExchange serverWebExchange = Mockito.mock(DefaultServerWebExchange.class); + when(helper.toPolicyTypeInfoCollection(any(), any())).thenCallRealMethod(); + Mono<ResponseEntity<Flux<PolicyTypeInformation>>> responseEntityMono = policyService.getPolicyTypesService(null, null,null, serverWebExchange); + testHelper.testSuccessResponse(responseEntityMono, HttpStatus.OK, responseBody -> responseBody.toStream().count() == 1); + } + + @Test + public void testGetPolicyTypesEmpty() throws Exception { + ServerWebExchange serverWebExchange = Mockito.mock(DefaultServerWebExchange.class); + when(helper.toPolicyTypeInfoCollection(any(), any())).thenCallRealMethod(); + Mono<ResponseEntity<Flux<PolicyTypeInformation>>> responseEntityMono = policyService.getPolicyTypesService(null, null, null, serverWebExchange); + testHelper.testSuccessResponse(responseEntityMono, HttpStatus.OK, responseBody -> responseBody.toStream().count() == 0); + } + + @Test + public void testGetPolicyTypesNoRic() { + ServerWebExchange serverWebExchange = Mockito.mock(DefaultServerWebExchange.class); + assertThrows(EntityNotFoundException.class, () -> policyService.getPolicyTypesService("NoRic", "","", serverWebExchange)); + } + + @Test + public void testGetPolicyTypesNoMatchedTypeName() throws Exception { + String policyTypeName = "uri_type_123"; + String nonRtRicId = "Ric_347"; + testHelper.addPolicyType(policyTypeName, nonRtRicId); + when(helper.toPolicyTypeInfoCollection(any(), any())).thenCallRealMethod(); + ServerWebExchange serverWebExchange = Mockito.mock(DefaultServerWebExchange.class); + Mono<ResponseEntity<Flux<PolicyTypeInformation>>> responseEntityMono = policyService.getPolicyTypesService("", "noTypeName", null, serverWebExchange); + testHelper.testSuccessResponse(responseEntityMono, HttpStatus.OK, responseBody -> responseBody.toStream().findAny().isEmpty()); + } + + @Test + public void testGetPolicyTypesNoMatchedTypeNameWithRic() throws Exception { + String policyTypeName = "uri_type_123"; + String nonRtRicId = "Ric_347"; + testHelper.addPolicyType(policyTypeName, nonRtRicId); + when(helper.toPolicyTypeInfoCollection(any(), any())).thenCallRealMethod(); + ServerWebExchange serverWebExchange = Mockito.mock(DefaultServerWebExchange.class); + Mono<ResponseEntity<Flux<PolicyTypeInformation>>> responseEntityMono = policyService.getPolicyTypesService("Ric_347", "noTypeName", null, serverWebExchange); + testHelper.testSuccessResponse(responseEntityMono, HttpStatus.OK, responseBody -> responseBody.toStream().findAny().isEmpty()); + } + + @Test + public void testGetPolicyTypesMatchedTypeName() throws Exception { + String policyTypeName = "uri_type_123"; + String nonRtRicId = "Ric_347"; + testHelper.addPolicyType(policyTypeName, nonRtRicId); + when(helper.toPolicyTypeInfoCollection(any(), any())).thenCallRealMethod(); + ServerWebExchange serverWebExchange = Mockito.mock(DefaultServerWebExchange.class); + Mono<ResponseEntity<Flux<PolicyTypeInformation>>> responseEntityMono = policyService.getPolicyTypesService(null, "uri_type", null, serverWebExchange); + testHelper.testSuccessResponse(responseEntityMono, HttpStatus.OK, responseBody -> responseBody.toStream().count() == 1); + } + + @Test + public void testGetPolicyTypesMatchedTypeNameWithRic() throws Exception { + String policyTypeName = "uri_type_123"; + String nonRtRicId = "Ric_347"; + testHelper.addPolicyType(policyTypeName, nonRtRicId); + ServerWebExchange serverWebExchange = Mockito.mock(DefaultServerWebExchange.class); + when(helper.toPolicyTypeInfoCollection(any(), any())).thenCallRealMethod(); + Mono<ResponseEntity<Flux<PolicyTypeInformation>>> responseEntityMono = policyService + .getPolicyTypesService("Ric_347", "uri_type", null, serverWebExchange); + testHelper.testSuccessResponse(responseEntityMono, HttpStatus.OK, responseBody -> responseBody.toStream().count() == 1); + } + + @Test + public void testGetPolicyIds() throws Exception { + String policyTypeName = "uri_type_123"; + String nonRtRicId = "Ric_347"; + testHelper.addPolicyType(policyTypeName, nonRtRicId); + Policy policy = testHelper.buidTestPolicy(testHelper.policyObjectInfo(nonRtRicId, policyTypeName), "122344-5674"); + policies.put(policy); + when(authorizationService.authCheck(any(), any(), any())).thenReturn(Mono.just(policy)); + ServerWebExchange serverWebExchange = Mockito.mock(DefaultServerWebExchange.class); + Policy singlePolicy = policies.filterPolicies(null, null, null, null).iterator().next(); + Collection<PolicyInformation> mockPolicyInfoCollection = new ArrayList<>(); + mockPolicyInfoCollection.add(new PolicyInformation(singlePolicy.getId(), singlePolicy.getRic().getConfig().getRicId())); + when(helper.toFluxPolicyInformation(any())).thenReturn(Flux.fromIterable(mockPolicyInfoCollection)); + Mono<ResponseEntity<Flux<PolicyInformation>>> responseEntityMono = policyService + .getPolicyIdsService(null, null, null, null, serverWebExchange); + testHelper.testSuccessResponse(responseEntityMono, HttpStatus.OK, responseBody -> responseBody.toStream().count() == 1); + } + + @Test + public void testGetPolicyIdsNoRic() throws Exception { + testHelper.addPolicyType("uri_type_123", "Ric_347"); + ServerWebExchange serverWebExchange = Mockito.mock(DefaultServerWebExchange.class); + EntityNotFoundException exception = assertThrows(EntityNotFoundException.class, () -> policyService + .getPolicyIdsService("uri_type_123", "noRic", "", "", serverWebExchange)); + assertEquals("Near-RT RIC not found using ID: noRic", exception.getMessage()); + } + + @Test + public void testGetPolicyIdsNoPolicyType() { + ServerWebExchange serverWebExchange = Mockito.mock(DefaultServerWebExchange.class); + EntityNotFoundException exception = assertThrows(EntityNotFoundException.class, () -> policyService + .getPolicyIdsService("noPolicyType", "noRic", "", "", serverWebExchange)); + assertEquals("Policy type not found using ID: noPolicyType", exception.getMessage()); + } + + @Test + public void testGetPolicyService() throws Exception { + String policyTypeName = "uri_type_123"; + String nonRtRicId = "Ric_347"; + testHelper.addPolicyType(policyTypeName, nonRtRicId); + Policy policy = testHelper.buidTestPolicy(testHelper.policyObjectInfo(nonRtRicId, policyTypeName), "122344-5674"); + policies.put(policy); + when(authorizationService.authCheck(any(), any(), any())).thenReturn(Mono.just(policy)); + ServerWebExchange serverWebExchange = Mockito.mock(DefaultServerWebExchange.class); + Mono<ResponseEntity<Object>> responseEntityMono = policyService.getPolicyService(policy.getId(), serverWebExchange); + testHelper.testSuccessResponse(responseEntityMono, HttpStatus.OK, responseBody -> { + if (responseBody instanceof String returnPolicy) + return returnPolicy.contains(policy.getJson()); + return false; + }); + } + + @Test + public void testGetPolicyServiceNoPolicy() throws Exception { + ServerWebExchange serverWebExchange = Mockito.mock(DefaultServerWebExchange.class); + EntityNotFoundException exception = assertThrows(EntityNotFoundException.class, () -> policyService + .getPolicyService("NoPolicy", serverWebExchange)); + assertEquals("Could not find policy: NoPolicy", exception.getMessage()); + } + + @Test + public void testGetPolicyTypeService() throws Exception { + String policyTypeName = "uri_type_123"; + String nonRtRicId = "Ric_347"; + PolicyType addedPolicyType = testHelper.addPolicyType(policyTypeName, nonRtRicId); + Mono<ResponseEntity<Object>> responseEntityMono = policyService.getPolicyTypeDefinitionService(policyTypeName); + testHelper.testSuccessResponse(responseEntityMono, HttpStatus.OK, responseBody -> { + if (responseBody instanceof String returnPolicyType) + return returnPolicyType.contains(addedPolicyType.getSchema()); + return false; + }); + } + + @Test + public void testGetPolicyTypeServiceNoPolicyType() { + EntityNotFoundException exception = assertThrows(EntityNotFoundException.class, () -> policyService + .getPolicyTypeDefinitionService("NoPolicyType")); + assertEquals("PolicyType not found with ID: NoPolicyType", exception.getMessage()); + } } diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/utils/v3/TestHelper.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/utils/v3/TestHelper.java index f6b1ce82..bfec3f6b 100644 --- a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/utils/v3/TestHelper.java +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/utils/v3/TestHelper.java @@ -20,7 +20,6 @@ package org.onap.ccsdk.oran.a1policymanagementservice.utils.v3; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.io.CharStreams; import com.google.gson.Gson; @@ -33,7 +32,6 @@ import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationCo import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig; import org.onap.ccsdk.oran.a1policymanagementservice.configuration.WebClientConfig; import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.Consts; -import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.RappSimulatorController; import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.PolicyObjectInformation; import org.onap.ccsdk.oran.a1policymanagementservice.repository.*; import org.springframework.beans.factory.annotation.Autowired; @@ -46,7 +44,6 @@ import org.springframework.stereotype.Component; import org.springframework.web.reactive.function.client.WebClientResponseException; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; -import reactor.util.annotation.Nullable; import java.io.IOException; import java.io.InputStream; @@ -55,9 +52,11 @@ import java.nio.charset.StandardCharsets; import java.time.Instant; import java.util.ArrayList; import java.util.List; +import java.util.Objects; +import java.util.function.Predicate; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertTrue; @Component public class TestHelper { @@ -89,10 +88,6 @@ public class TestHelper { return restClientV3(false); } - public AsyncRestClient restClient() { - return restClient(false); - } - public AsyncRestClient restClient(String baseUrl, boolean useTrustValidation) { WebClientConfig config = this.applicationConfig.getWebClientConfig(); config = WebClientConfig.builder() @@ -123,20 +118,6 @@ public class TestHelper { return restClient(baseUrl() + Consts.V2_API_ROOT, useTrustValidation); } - public void putService(String name) throws JsonProcessingException { - putService(name, 0, null); - } - - public void putService(String name, long keepAliveIntervalSeconds, @Nullable HttpStatus expectedStatus) throws JsonProcessingException { - String url = "/services"; - String body = createServiceJson(name, keepAliveIntervalSeconds); - ResponseEntity<String> resp = restClient().putForEntity(url, body).block(); - if (expectedStatus != null) { - assertNotNull(resp); - assertEquals(expectedStatus, resp.getStatusCode(), ""); - } - } - public PolicyType createPolicyType(String policyTypeName, String filePath) throws IOException { InputStream in = getClass().getResourceAsStream(filePath); assert in != null; @@ -209,39 +190,29 @@ public class TestHelper { .id(id).build(); } - public String createServiceJson(String name, long keepAliveIntervalSeconds) throws JsonProcessingException { - String callbackUrl = baseUrl() + RappSimulatorController.SERVICE_CALLBACK_URL; - return createServiceJson(name, keepAliveIntervalSeconds, callbackUrl); - } - - public String createServiceJson(String name, long keepAliveIntervalSeconds, String url) throws JsonProcessingException { - org.onap.ccsdk.oran.a1policymanagementservice.models.v2.ServiceRegistrationInfo service = new org.onap.ccsdk.oran.a1policymanagementservice.models.v2.ServiceRegistrationInfo(name) - .keepAliveIntervalSeconds(keepAliveIntervalSeconds) - .callbackUrl(url); - - return objectMapper.writeValueAsString(service); - } - - public void testSuccessResponse(Mono<ResponseEntity<String>> responseEntityMono, HttpStatus httpStatusCode, - String responseContains) { + public <T> void testSuccessResponse(Mono<ResponseEntity<T>> responseEntityMono, HttpStatus httpStatusCode, + Predicate<T> responsePredicate) { StepVerifier.create(responseEntityMono) .expectNextMatches(responseEntity -> { // Assert status code HttpStatusCode status = responseEntity.getStatusCode(); - String res = responseEntity.getBody(); - assertThat(res).contains(responseContains); + T responseBody = responseEntity.getBody(); + assert responsePredicate.test(responseBody); return status.value() == httpStatusCode.value(); }) .expectComplete() .verify(); } - public void testErrorCode(Mono<?> request, HttpStatus expStatus) { - testErrorCode(request, expStatus, "", true); - } - - public void testErrorCode(Mono<?> request, HttpStatus expStatus, boolean expectApplicationProblemJsonMediaType) { - testErrorCode(request, expStatus, "", expectApplicationProblemJsonMediaType); + public void testSuccessHeader(Mono<ResponseEntity<String>> responseEntityMono, String headerKey, + Predicate<String> responsePredicate) { + StepVerifier.create(responseEntityMono) + .expectNextMatches(responseEntity -> { + String headerValue = Objects.requireNonNull(responseEntity.getHeaders().get(headerKey)).get(0); + return responsePredicate.test(headerValue); + }) + .expectComplete() + .verify(); } public void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) { |