From be8c2967de20d71203f07049d4071c7521d27026 Mon Sep 17 00:00:00 2001 From: "arkadiusz.adamski" Date: Tue, 13 Apr 2021 11:23:28 +0100 Subject: Query deployed policies by regex - Query deployed policies by regex on the name, for a given policy type Issue-ID: POLICY-2535 Signed-off-by: arkadiusz.adamski Change-Id: Ia2be683d044b22e4104ae14e2ce301882091c8ea --- .../main/rest/TestPolicyStatusControllerV1.java | 43 +++++++++++++++++++++- .../pap/main/rest/TestPolicyStatusProvider.java | 33 +++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) (limited to 'main/src/test') diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/TestPolicyStatusControllerV1.java b/main/src/test/java/org/onap/policy/pap/main/rest/TestPolicyStatusControllerV1.java index 1d0c7aa7..fc86fef1 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/TestPolicyStatusControllerV1.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestPolicyStatusControllerV1.java @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019,2021 Nordix Foundation. * Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. * Modifications Copyright (C) 2021 Bell Canada. All rights reserved. * ================================================================================ @@ -22,6 +22,7 @@ package org.onap.policy.pap.main.rest; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertEquals; import javax.ws.rs.client.Invocation; @@ -54,6 +55,14 @@ public class TestPolicyStatusControllerV1 extends CommonPapRestServer { // verify it fails when no authorization info is included checkUnauthRequest(uri, req -> req.get()); + checkRequest(POLICY_STATUS_ENDPOINT); + } + + @Test + public void testQueryAllDeployedPoliciesWithRegex() throws Exception { + checkRequest(POLICY_STATUS_ENDPOINT + "?regex=my.(1)name"); + checkEmptyRegexRequest(POLICY_STATUS_ENDPOINT + "?regex="); + checkInvalidRegexRequest(POLICY_STATUS_ENDPOINT + "?regex=my-(name"); } @Test @@ -77,6 +86,16 @@ public class TestPolicyStatusControllerV1 extends CommonPapRestServer { checkRequest(POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/my-group-name/my-name/1.2.3"); } + @Test + public void testGetStatusOfPoliciesWithRegex() throws Exception { + checkRequest(POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/my-group-name?regex=my-%3F%5Bmn%5Da.%7B1%7De"); + checkRequest(POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/my-group-name?regex=my.(1)name"); + // my-?[mna.{1}e + checkInvalidRegexRequest(POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/my-group-name?regex=my-%3F%5Bmna.%7B1%7De"); + checkInvalidRegexRequest(POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/my-group-name?regex=my.(1name"); + checkEmptyRegexRequest(POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/my-group-name?regex="); + } + private void checkRequest(String uri) throws Exception { Invocation.Builder invocationBuilder = sendRequest(uri); Response rawresp = invocationBuilder.get(); @@ -85,4 +104,26 @@ public class TestPolicyStatusControllerV1 extends CommonPapRestServer { // verify it fails when no authorization info is included checkUnauthRequest(uri, req -> req.get()); } + + private void checkInvalidRegexRequest(String uri) throws Exception { + Invocation.Builder invocationBuilder = sendRequest(uri); + Response rawresp = invocationBuilder.get(); + assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus()); + final String entity = rawresp.readEntity(String.class); + assertThat(entity).contains("error parsing regexp"); + + // verify it fails when no authorization info is included + checkUnauthRequest(uri, req -> req.get()); + } + + private void checkEmptyRegexRequest(String uri) throws Exception { + Invocation.Builder invocationBuilder = sendRequest(uri); + Response rawresp = invocationBuilder.get(); + assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus()); + final String entity = rawresp.readEntity(String.class); + assertThat(entity).contains("empty string passed as a regex"); + + // verify it fails when no authorization info is included + checkUnauthRequest(uri, req -> req.get()); + } } diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/TestPolicyStatusProvider.java b/main/src/test/java/org/onap/policy/pap/main/rest/TestPolicyStatusProvider.java index 81ed6805..d3ff4ea6 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/TestPolicyStatusProvider.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestPolicyStatusProvider.java @@ -4,6 +4,7 @@ * ================================================================================ * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2021 Bell Canada. All rights reserved. + * Modifications Copyright (C) 2021 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,6 +28,7 @@ import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.when; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.Iterator; import java.util.List; @@ -197,6 +199,37 @@ public class TestPolicyStatusProvider extends ProviderSuper { assertThat(status.getState()).isEqualTo(State.FAILURE); } + @Test + public void testGetPolicyStatusByRegexNoMatch() throws PfModelException { + buildPolicyStatusToReturn1(); + final String pattern = "Hello"; + + final Collection actual = prov.getByRegex(pattern); + assertThat(actual).isEmpty(); + } + + @Test + public void testGetPolicyStatusOneMatch() throws PfModelException { + buildPolicyStatusToReturn1(); + final String pattern = "My(We|Po)[li]{0,3}c.A"; + + final Collection actual = prov.getByRegex(pattern); + assertThat(actual).hasSize(1); + + final String actualName = actual.iterator().next().getPolicy().getName(); + assertThat(actualName).isEqualTo("MyPolicyA"); + } + + @Test + public void testGetPolicyStatusAllMatch() throws PfModelException { + buildPolicyStatusToReturn1(); + final String pattern = "My(We|Po)[li]{0,3}c.{2}0*"; + + final Collection actual = prov.getByRegex(pattern); + + assertThat(actual).hasSize(3); + } + private void buildPolicyStatusToReturn1() throws PfModelException { PdpPolicyStatusBuilder builder = PdpPolicyStatus.builder().pdpGroup(MY_GROUP).pdpType(MY_PDP_TYPE) -- cgit 1.2.3-korg