diff options
author | 2019-03-12 11:53:22 -0400 | |
---|---|---|
committer | 2019-03-13 16:26:27 -0400 | |
commit | 4a3cfdff6285a516f1e05d4cebd748ea623177e5 (patch) | |
tree | 326e5d97e6e731d93ef355cc441295a9cf67ba16 /main/src/test/java/org/onap | |
parent | d8e7fa11c3bab410f41a4161f9f736eab5b2e3ec (diff) |
Add PDP Group Deploy and Delete REST API stubs
Note: this will not build until the models-pap code has been added.
Added PDP group Delete methods, with and without version.
Added checks for OK status codes.
Added topic configuration.
Still does not have the "simplified" PDP Group deploy/undeploy.
Still won't build without "Move PDP Group classes to concepts subdirectory".
Will add junit tests for code changes in another review.
Updated comments about unlocking after updates.
Made ServiceManager final.
Included topic start/stop actions.
Fixed parameter comment.
Removed unneeded parameters from json files.
Fixed argument test in shell script.
Updated licenses.
Change-Id: I6176d51918ae758e04fb68562dc9ca70534137d4
Issue-ID: POLICY-1542
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'main/src/test/java/org/onap')
13 files changed, 386 insertions, 269 deletions
diff --git a/main/src/test/java/org/onap/policy/pap/main/comm/PdpClientExceptionTest.java b/main/src/test/java/org/onap/policy/pap/main/comm/PdpClientExceptionTest.java deleted file mode 100644 index aa927665..00000000 --- a/main/src/test/java/org/onap/policy/pap/main/comm/PdpClientExceptionTest.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP PAP - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.pap.main.comm; - -import static org.junit.Assert.assertEquals; - -import org.junit.Test; -import org.onap.policy.common.utils.test.ExceptionsTester; -import org.onap.policy.pap.main.comm.PdpClientException; - -public class PdpClientExceptionTest { - - @Test - public void test() { - assertEquals(5, new ExceptionsTester().test(PdpClientException.class)); - } -} diff --git a/main/src/test/java/org/onap/policy/pap/main/comm/PdpClientTest.java b/main/src/test/java/org/onap/policy/pap/main/comm/PdpClientTest.java deleted file mode 100644 index 22823bfb..00000000 --- a/main/src/test/java/org/onap/policy/pap/main/comm/PdpClientTest.java +++ /dev/null @@ -1,141 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP PAP - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.pap.main.comm; - -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertSame; -import static org.mockito.Matchers.anyString; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import java.util.Arrays; -import java.util.LinkedList; -import java.util.List; -import java.util.Properties; -import java.util.concurrent.atomic.AtomicReference; -import org.junit.Before; -import org.junit.Test; -import org.mockito.internal.util.reflection.Whitebox; -import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; -import org.onap.policy.common.endpoints.event.comm.TopicEndpoint; -import org.onap.policy.common.endpoints.event.comm.TopicListener; -import org.onap.policy.common.endpoints.event.comm.TopicSink; - -public class PdpClientTest { - private static final String SINK_FIELD_NAME = "sink"; - private static final String TOPIC = "my-topic"; - - private PdpClient client; - private TopicSink sink; - private List<TopicSink> sinks; - - /** - * Creates mocks and an initial client object. - * - * @throws Exception if an error occurs - */ - @Before - public void setUp() throws Exception { - sink = mock(TopicSink.class); - when(sink.send(anyString())).thenReturn(true); - - sinks = Arrays.asList(sink, null); - - client = new PdpClient2(TOPIC); - } - - /** - * Uses a real NO-OP topic sink. - */ - @Test - public void testGetTopicSinks() throws Exception { - // clear all topics and then configure one topic - TopicEndpoint.manager.shutdown(); - - Properties props = new Properties(); - props.setProperty("noop.sink.topics", TOPIC); - TopicEndpoint.manager.addTopicSinks(props); - - sink = TopicEndpoint.manager.getNoopTopicSink(TOPIC); - assertNotNull(sink); - - AtomicReference<String> evref = new AtomicReference<>(null); - - sink.register(new TopicListener() { - @Override - public void onTopicEvent(CommInfrastructure infra, String topic, String event) { - evref.set(event); - } - }); - - sink.start(); - - client = new PdpClient(TOPIC); - client.send(100); - - assertEquals("100", evref.get()); - } - - @Test - public void testPdpClient_testGetTopic() { - assertEquals(TOPIC, client.getTopic()); - assertSame(sink, Whitebox.getInternalState(client, SINK_FIELD_NAME)); - - // unknown topic -> should throw exception - sinks = new LinkedList<>(); - assertThatThrownBy(() -> new PdpClient2(TOPIC)).isInstanceOf(PdpClientException.class) - .hasMessage("no sinks for topic: my-topic"); - } - - @Test - public void testSend() throws Exception { - client.send(Arrays.asList("abc", "def")); - verify(sink).send("['abc','def']".replace('\'', '"')); - - // sink send fails - when(sink.send(anyString())).thenReturn(false); - assertFalse(client.send("ghi")); - - // sink send throws an exception - RuntimeException ex = new RuntimeException("expected exception"); - when(sink.send(anyString())).thenThrow(ex); - assertFalse(client.send("jkl")); - } - - /** - * PdpClient with some overrides. - */ - private class PdpClient2 extends PdpClient { - - public PdpClient2(String topic) throws PdpClientException { - super(topic); - } - - @Override - protected List<TopicSink> getTopicSinks(String topic) { - return sinks; - } - } -} diff --git a/main/src/test/java/org/onap/policy/pap/main/parameters/CommonTestData.java b/main/src/test/java/org/onap/policy/pap/main/parameters/CommonTestData.java index 27d8ac2a..8054194b 100644 --- a/main/src/test/java/org/onap/policy/pap/main/parameters/CommonTestData.java +++ b/main/src/test/java/org/onap/policy/pap/main/parameters/CommonTestData.java @@ -21,6 +21,13 @@ package org.onap.policy.pap.main.parameters; +import java.util.Map; +import java.util.TreeMap; +import org.onap.policy.common.parameters.ParameterGroup; +import org.onap.policy.common.utils.coder.Coder; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; + /** * Class to hold/create all parameters for test cases. * @@ -36,21 +43,70 @@ public class CommonTestData { private static final boolean REST_SERVER_AAF = false; public static final String PAP_GROUP_NAME = "PapGroup"; + private static final Coder coder = new StandardCoder(); + + /** + * Converts the contents of a map to a parameter class. + * + * @param source property map + * @param clazz class of object to be created from the map + * @return a new object represented by the map + */ + public <T extends ParameterGroup> T toObject(Map<String, Object> source, Class<T> clazz) { + try { + return coder.decode(coder.encode(source), clazz); + + } catch (CoderException e) { + throw new RuntimeException("cannot create " + clazz.getName() + " from map", e); + } + } + /** - * Returns an instance of RestServerParameters for test cases. + * Returns a property map for a PapParameterGroup map for test cases. + * @param name name of the parameters + * + * @return a property map suitable for constructing an object + */ + public Map<String, Object> getPapParameterGroupMap(String name) { + Map<String,Object> map = new TreeMap<>(); + + map.put("name", name); + map.put("restServerParameters", getRestServerParametersMap(false)); + map.put("pdpGroupDeploymentParameters", getPdpGroupDeploymentParametersMap()); + + return map; + } + + /** + * Returns a property map for a RestServerParameters map for test cases. * * @param isEmpty boolean value to represent that object created should be empty or not - * @return the restServerParameters object + * @return a property map suitable for constructing an object */ - public RestServerParameters getRestServerParameters(final boolean isEmpty) { - final RestServerParameters restServerParameters; + public Map<String,Object> getRestServerParametersMap(final boolean isEmpty) { + Map<String,Object> map = new TreeMap<>(); + map.put("https", REST_SERVER_HTTPS); + map.put("aaf", REST_SERVER_AAF); + if (!isEmpty) { - restServerParameters = new RestServerParameters(REST_SERVER_HOST, REST_SERVER_PORT, REST_SERVER_USER, - REST_SERVER_PASSWORD, REST_SERVER_HTTPS, REST_SERVER_AAF); - } else { - restServerParameters = new RestServerParameters(null, 0, null, null, REST_SERVER_HTTPS, REST_SERVER_AAF); + map.put("host", REST_SERVER_HOST); + map.put("port", REST_SERVER_PORT); + map.put("userName", REST_SERVER_USER); + map.put("password", REST_SERVER_PASSWORD); } - return restServerParameters; + + return map; } + /** + * Returns a property map for a PdpGroupDeploymentParameters map for test cases. + * + * @return a property map suitable for constructing an object + */ + public Map<String,Object> getPdpGroupDeploymentParametersMap() { + Map<String,Object> map = new TreeMap<>(); + map.put("waitResponseMs", "1"); + + return map; + } } diff --git a/main/src/test/java/org/onap/policy/pap/main/parameters/TestPapParameterGroup.java b/main/src/test/java/org/onap/policy/pap/main/parameters/TestPapParameterGroup.java index 4759f646..2450a750 100644 --- a/main/src/test/java/org/onap/policy/pap/main/parameters/TestPapParameterGroup.java +++ b/main/src/test/java/org/onap/policy/pap/main/parameters/TestPapParameterGroup.java @@ -25,6 +25,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import java.util.Map; import org.junit.Test; import org.onap.policy.common.parameters.GroupValidationResult; @@ -37,10 +38,16 @@ public class TestPapParameterGroup { CommonTestData commonTestData = new CommonTestData(); @Test + public void testPapParameterGroup_Named() { + final PapParameterGroup papParameters = new PapParameterGroup("my-name"); + assertEquals("my-name", papParameters.getName()); + } + + @Test public void testPapParameterGroup() { - final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false); - final PapParameterGroup papParameters = - new PapParameterGroup(CommonTestData.PAP_GROUP_NAME, restServerParameters); + final PapParameterGroup papParameters = commonTestData.toObject( + commonTestData.getPapParameterGroupMap(CommonTestData.PAP_GROUP_NAME), PapParameterGroup.class); + final RestServerParameters restServerParameters = papParameters.getRestServerParameters(); final GroupValidationResult validationResult = papParameters.validate(); assertTrue(validationResult.isValid()); assertEquals(CommonTestData.PAP_GROUP_NAME, papParameters.getName()); @@ -54,19 +61,18 @@ public class TestPapParameterGroup { @Test public void testPapParameterGroup_NullName() { - final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false); - final PapParameterGroup papParameters = new PapParameterGroup(null, restServerParameters); + final PapParameterGroup papParameters = commonTestData.toObject( + commonTestData.getPapParameterGroupMap(null), PapParameterGroup.class); final GroupValidationResult validationResult = papParameters.validate(); assertFalse(validationResult.isValid()); assertEquals(null, papParameters.getName()); - assertTrue(validationResult.getResult().contains( - "field \"name\" type \"java.lang.String\" value \"null\" INVALID, " + "must be a non-blank string")); + assertTrue(validationResult.getResult().contains("is null")); } @Test public void testPapParameterGroup_EmptyName() { - final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false); - final PapParameterGroup papParameters = new PapParameterGroup("", restServerParameters); + final PapParameterGroup papParameters = commonTestData.toObject( + commonTestData.getPapParameterGroupMap(""), PapParameterGroup.class); final GroupValidationResult validationResult = papParameters.validate(); assertFalse(validationResult.isValid()); assertEquals("", papParameters.getName()); @@ -76,9 +82,8 @@ public class TestPapParameterGroup { @Test public void testPapParameterGroup_SetName() { - final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false); - final PapParameterGroup papParameters = - new PapParameterGroup(CommonTestData.PAP_GROUP_NAME, restServerParameters); + final PapParameterGroup papParameters = commonTestData.toObject( + commonTestData.getPapParameterGroupMap(CommonTestData.PAP_GROUP_NAME), PapParameterGroup.class); papParameters.setName("PapNewGroup"); final GroupValidationResult validationResult = papParameters.validate(); assertTrue(validationResult.isValid()); @@ -87,10 +92,11 @@ public class TestPapParameterGroup { @Test public void testApiParameterGroup_EmptyRestServerParameters() { - final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(true); + Map<String, Object> map = commonTestData.getPapParameterGroupMap(CommonTestData.PAP_GROUP_NAME); + map.put("restServerParameters", commonTestData.getRestServerParametersMap(true)); - final PapParameterGroup papParameters = - new PapParameterGroup(CommonTestData.PAP_GROUP_NAME, restServerParameters); + final PapParameterGroup papParameters = commonTestData.toObject( + map, PapParameterGroup.class); final GroupValidationResult validationResult = papParameters.validate(); assertFalse(validationResult.isValid()); assertTrue(validationResult.getResult() diff --git a/main/src/test/java/org/onap/policy/pap/main/parameters/TestPapParameterHandler.java b/main/src/test/java/org/onap/policy/pap/main/parameters/TestPapParameterHandler.java index b0f9521d..363a130a 100644 --- a/main/src/test/java/org/onap/policy/pap/main/parameters/TestPapParameterHandler.java +++ b/main/src/test/java/org/onap/policy/pap/main/parameters/TestPapParameterHandler.java @@ -21,6 +21,7 @@ package org.onap.policy.pap.main.parameters; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -93,13 +94,7 @@ public class TestPapParameterHandler { final PapCommandLineArguments noArguments = new PapCommandLineArguments(); noArguments.parse(noArgumentString); - try { - new PapParameterHandler().getParameters(noArguments); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().contains( - "field \"name\" type \"java.lang.String\" value \"null\" INVALID, must be a non-blank string")); - } + assertThatThrownBy(() -> new PapParameterHandler().getParameters(noArguments)).hasMessageContaining("is null"); } @Test diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/CommonPapRestServer.java b/main/src/test/java/org/onap/policy/pap/main/rest/CommonPapRestServer.java index 8f522740..25ab1c37 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/CommonPapRestServer.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/CommonPapRestServer.java @@ -21,25 +21,22 @@ package org.onap.policy.pap.main.rest; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import java.io.File; -import java.io.IOException; -import java.net.InetSocketAddress; -import java.net.ServerSocket; import java.security.SecureRandom; -import java.security.cert.X509Certificate; import java.util.HashMap; import java.util.Map; import java.util.Properties; +import java.util.function.Function; import javax.net.ssl.SSLContext; -import javax.net.ssl.TrustManager; -import javax.net.ssl.X509TrustManager; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.Invocation; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; import org.junit.After; import org.junit.AfterClass; @@ -88,7 +85,7 @@ public class CommonPapRestServer { */ @BeforeClass public static void setUpBeforeClass() throws Exception { - allocPort(); + port = NetworkUtil.allocPort(); httpsPrefix = "https://localhost:" + port + "/"; @@ -140,41 +137,32 @@ public class CommonPapRestServer { * @throws Exception if an error occurs */ protected void testSwagger(final String endpoint) throws Exception { - final Invocation.Builder invocationBuilder = sendFqeRequest(httpsPrefix + "swagger.yaml"); + final Invocation.Builder invocationBuilder = sendFqeRequest(httpsPrefix + "swagger.yaml", true); final String resp = invocationBuilder.get(String.class); assertTrue(resp.contains(ENDPOINT_PREFIX + endpoint + ":")); } /** - * Allocates a port for the server. - * - * @throws IOException if an error occurs - */ - private static void allocPort() throws IOException { - ServerSocket socket = new ServerSocket(); - socket.bind(new InetSocketAddress("localhost", 0)); - - port = socket.getLocalPort(); - socket.close(); - } - - /** * Makes a parameter configuration file. * * @throws Exception if an error occurs */ private static void makeConfigFile() throws Exception { - Map<String, Object> params = new HashMap<>(); - params.put("host", "0.0.0.0"); - params.put("port", port); - params.put("userName", "healthcheck"); - params.put("password", "zb!XztG34"); - params.put("https", true); + Map<String, Object> restParams = new HashMap<>(); + restParams.put("host", "0.0.0.0"); + restParams.put("port", port); + restParams.put("userName", "healthcheck"); + restParams.put("password", "zb!XztG34"); + restParams.put("https", true); + + Map<String, Object> pdpGroupDeploy = new HashMap<>(); + pdpGroupDeploy.put("waitResponseMs", "0"); Map<String, Object> config = new HashMap<>(); config.put("name", "PapGroup"); - config.put("restServerParameters", params); + config.put("restServerParameters", restParams); + config.put("pdpGroupDeploymentParameters", pdpGroupDeploy); File file = new File("src/test/resources/parameters/TestConfigParams.json"); file.deleteOnExit(); @@ -198,9 +186,12 @@ public class CommonPapRestServer { systemProps.put("javax.net.ssl.keyStorePassword", "Pol1cy_0nap"); System.setProperties(systemProps); - final String[] papConfigParameters = new String[2]; - papConfigParameters[0] = "-c"; - papConfigParameters[1] = "src/test/resources/parameters/TestConfigParams.json"; + // @formatter:off + final String[] papConfigParameters = { + "-c", "src/test/resources/parameters/TestConfigParams.json", + "-p", "src/test/resources/parameters/topic.properties" + }; + // @formatter:on main = new Main(papConfigParameters); @@ -231,6 +222,19 @@ public class CommonPapRestServer { } /** + * Verifies that unauthorized requests fail. + * + * @param endpoint the target end point + * @param sender function that sends the requests to the target + * @throws Exception if an error occurs + */ + protected void checkUnauthRequest(final String endpoint, Function<Invocation.Builder, Response> sender) + throws Exception { + assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), + sender.apply(sendNoAuthRequest(endpoint)).getStatus()); + } + + /** * Sends a request to an endpoint. * * @param endpoint the target endpoint @@ -238,45 +242,40 @@ public class CommonPapRestServer { * @throws Exception if an error occurs */ protected Invocation.Builder sendRequest(final String endpoint) throws Exception { - return sendFqeRequest(httpsPrefix + ENDPOINT_PREFIX + endpoint); + return sendFqeRequest(httpsPrefix + ENDPOINT_PREFIX + endpoint, true); } /** - * Sends a request to a fully qualified endpoint. + * Sends a request to an endpoint, without any authorization header. * - * @param endpoint the fully qualified target endpoint + * @param endpoint the target endpoint * @return a request builder * @throws Exception if an error occurs */ - protected Invocation.Builder sendFqeRequest(final String fullyQualifiedEndpoint) throws Exception { - - // @formatter:off - final TrustManager[] noopTrustManager = new TrustManager[] { - new X509TrustManager() { - - @Override - public X509Certificate[] getAcceptedIssuers() { - return new X509Certificate[0]; - } - - @Override - public void checkClientTrusted(final java.security.cert.X509Certificate[] certs, - final String authType) {} - - @Override - public void checkServerTrusted(final java.security.cert.X509Certificate[] certs, - final String authType) {} - } - }; - // @formatter:on + protected Invocation.Builder sendNoAuthRequest(final String endpoint) throws Exception { + return sendFqeRequest(httpsPrefix + ENDPOINT_PREFIX + endpoint, false); + } + /** + * Sends a request to a fully qualified endpoint. + * + * @param fullyQualifiedEndpoint the fully qualified target endpoint + * @param includeAuth if authorization header should be included + * @return a request builder + * @throws Exception if an error occurs + */ + protected Invocation.Builder sendFqeRequest(final String fullyQualifiedEndpoint, boolean includeAuth) + throws Exception { final SSLContext sc = SSLContext.getInstance("TLSv1.2"); - sc.init(null, noopTrustManager, new SecureRandom()); + sc.init(null, NetworkUtil.getAlwaysTrustingManager(), new SecureRandom()); final ClientBuilder clientBuilder = ClientBuilder.newBuilder().sslContext(sc).hostnameVerifier((host, session) -> true); final Client client = clientBuilder.build(); - final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34"); - client.register(feature); + + if (includeAuth) { + final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34"); + client.register(feature); + } final WebTarget webTarget = client.target(fullyQualifiedEndpoint); diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/PapRestControllerV1Test.java b/main/src/test/java/org/onap/policy/pap/main/rest/PapRestControllerV1Test.java new file mode 100644 index 00000000..317ab1c1 --- /dev/null +++ b/main/src/test/java/org/onap/policy/pap/main/rest/PapRestControllerV1Test.java @@ -0,0 +1,63 @@ +/* + * ============LICENSE_START======================================================= + * ONAP PAP + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.pap.main.rest; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.util.UUID; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.ResponseBuilder; +import org.junit.Before; +import org.junit.Test; + +public class PapRestControllerV1Test { + + private PapRestControllerV1 ctlr; + private ResponseBuilder bldr; + + @Before + public void setUp() { + ctlr = new PapRestControllerV1(); + bldr = Response.status(Response.Status.OK); + } + + @Test + public void testAddVersionControlHeaders() { + Response resp = ctlr.addVersionControlHeaders(bldr).build(); + assertEquals("0", resp.getHeaderString(PapRestControllerV1.VERSION_MINOR_NAME)); + assertEquals("0", resp.getHeaderString(PapRestControllerV1.VERSION_PATCH_NAME)); + assertEquals("1.0.0", resp.getHeaderString(PapRestControllerV1.VERSION_LATEST_NAME)); + } + + @Test + public void testAddLoggingHeaders_Null() { + Response resp = ctlr.addLoggingHeaders(bldr, null).build(); + assertNotNull(resp.getHeaderString(PapRestControllerV1.REQUEST_ID_NAME)); + } + + @Test + public void testAddLoggingHeaders_NonNull() { + UUID uuid = UUID.randomUUID(); + Response resp = ctlr.addLoggingHeaders(bldr, uuid).build(); + assertEquals(uuid.toString(), resp.getHeaderString(PapRestControllerV1.REQUEST_ID_NAME)); + } +} diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/TestHealthCheckRestControllerV1.java b/main/src/test/java/org/onap/policy/pap/main/rest/TestHealthCheckRestControllerV1.java index 0ddfb3b5..6dd7103b 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/TestHealthCheckRestControllerV1.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestHealthCheckRestControllerV1.java @@ -46,6 +46,9 @@ public class TestHealthCheckRestControllerV1 extends CommonPapRestServer { final Invocation.Builder invocationBuilder = sendRequest(HEALTHCHECK_ENDPOINT); final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class); validateHealthCheckReport(NAME, SELF, true, 200, ALIVE, report); + + // verify it fails when no authorization info is included + checkUnauthRequest(HEALTHCHECK_ENDPOINT, req -> req.get()); } @Test diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeleteControllerV1.java b/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeleteControllerV1.java new file mode 100644 index 00000000..107c46ad --- /dev/null +++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeleteControllerV1.java @@ -0,0 +1,79 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019 AT&T Intellectual Property. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.pap.main.rest; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import javax.ws.rs.client.Invocation; +import javax.ws.rs.core.Response; +import org.junit.Test; +import org.onap.policy.models.pap.concepts.PdpGroupDeleteResponse; + +public class TestPdpGroupDeleteControllerV1 extends CommonPapRestServer { + + private static final String DELETE_ENDPOINT = "pdps/groups"; + + @Test + public void testSwagger() throws Exception { + super.testSwagger(DELETE_ENDPOINT + "/{name}"); + super.testSwagger(DELETE_ENDPOINT + "/{name}/versions/{version}"); + } + + @Test + public void testDeleteGroup() throws Exception { + String uri = DELETE_ENDPOINT + "/my-name"; + + Invocation.Builder invocationBuilder = sendRequest(uri); + Response rawresp = invocationBuilder.delete(); + PdpGroupDeleteResponse resp = rawresp.readEntity(PdpGroupDeleteResponse.class); + assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); + assertNull(resp.getErrorDetails()); + + rawresp = invocationBuilder.delete(); + resp = rawresp.readEntity(PdpGroupDeleteResponse.class); + assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); + assertNull(resp.getErrorDetails()); + + // verify it fails when no authorization info is included + checkUnauthRequest(uri, req -> req.delete()); + } + + @Test + public void testDeleteGroupVersion() throws Exception { + String uri = DELETE_ENDPOINT + "/my-name/versions/1.2.3"; + + Invocation.Builder invocationBuilder = sendRequest(uri); + Response rawresp = invocationBuilder.delete(); + PdpGroupDeleteResponse resp = rawresp.readEntity(PdpGroupDeleteResponse.class); + assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); + assertNull(resp.getErrorDetails()); + + rawresp = invocationBuilder.delete(); + resp = rawresp.readEntity(PdpGroupDeleteResponse.class); + assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); + assertNull(resp.getErrorDetails()); + + // verify it fails when no authorization info is included + checkUnauthRequest(uri, req -> req.delete()); + } +} diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeployControllerV1.java b/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeployControllerV1.java new file mode 100644 index 00000000..d49f00cc --- /dev/null +++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeployControllerV1.java @@ -0,0 +1,78 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019 AT&T Intellectual Property. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.pap.main.rest; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import java.util.Arrays; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.Invocation; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import org.junit.Test; +import org.onap.policy.models.pap.concepts.PdpGroup; +import org.onap.policy.models.pap.concepts.PdpGroupDeployResponse; +import org.onap.policy.models.pap.concepts.PdpSubGroup; + +public class TestPdpGroupDeployControllerV1 extends CommonPapRestServer { + + private static final String DEPLOY_ENDPOINT = "pdps"; + + @Test + public void testSwagger() throws Exception { + super.testSwagger(DEPLOY_ENDPOINT); + } + + @Test + public void testDeploy() throws Exception { + Entity<PdpGroup> entgrp = makePdpGroupEntity(); + + Invocation.Builder invocationBuilder = sendRequest(DEPLOY_ENDPOINT); + Response rawresp = invocationBuilder.post(entgrp); + PdpGroupDeployResponse resp = rawresp.readEntity(PdpGroupDeployResponse.class); + assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); + assertNull(resp.getErrorDetails()); + + rawresp = invocationBuilder.post(entgrp); + resp = rawresp.readEntity(PdpGroupDeployResponse.class); + assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); + assertNull(resp.getErrorDetails()); + + // verify it fails when no authorization info is included + checkUnauthRequest(DEPLOY_ENDPOINT, req -> req.post(entgrp)); + } + + private Entity<PdpGroup> makePdpGroupEntity() { + PdpSubGroup subgrp = new PdpSubGroup(); + subgrp.setPdpType("drools"); + + PdpGroup group = new PdpGroup(); + group.setName("drools-group"); + group.setDescription("my description"); + group.setVersion("my-version"); + group.setPdpSubgroups(Arrays.asList(subgrp)); + + Entity<PdpGroup> entgrp = Entity.entity(group, MediaType.APPLICATION_JSON); + return entgrp; + } +} diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/TestStatisticsRestControllerV1.java b/main/src/test/java/org/onap/policy/pap/main/rest/TestStatisticsRestControllerV1.java index de142b51..9874389e 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/TestStatisticsRestControllerV1.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestStatisticsRestControllerV1.java @@ -66,6 +66,9 @@ public class TestStatisticsRestControllerV1 extends CommonPapRestServer { invocationBuilder = sendRequest(STATISTICS_ENDPOINT); report = invocationBuilder.get(StatisticsReport.class); validateStatisticsReport(report, 1, 200); + + // verify it fails when no authorization info is included + checkUnauthRequest(STATISTICS_ENDPOINT, req -> req.get()); } @Test diff --git a/main/src/test/java/org/onap/policy/pap/main/startstop/TestMain.java b/main/src/test/java/org/onap/policy/pap/main/startstop/TestMain.java index 1f6b2baf..37da7e9b 100644 --- a/main/src/test/java/org/onap/policy/pap/main/startstop/TestMain.java +++ b/main/src/test/java/org/onap/policy/pap/main/startstop/TestMain.java @@ -52,7 +52,8 @@ public class TestMain { @Test public void testMain() throws PolicyPapException { - final String[] papConfigParameters = {"-c", "parameters/PapConfigParameters.json"}; + final String[] papConfigParameters = + {"-c", "parameters/PapConfigParameters.json", "-p", "parameters/topic.properties"}; main = new Main(papConfigParameters); assertTrue(main.getParameters().isValid()); assertEquals(CommonTestData.PAP_GROUP_NAME, main.getParameters().getName()); diff --git a/main/src/test/java/org/onap/policy/pap/main/startstop/TestPapActivator.java b/main/src/test/java/org/onap/policy/pap/main/startstop/TestPapActivator.java index 5e611673..059ea0b4 100644 --- a/main/src/test/java/org/onap/policy/pap/main/startstop/TestPapActivator.java +++ b/main/src/test/java/org/onap/policy/pap/main/startstop/TestPapActivator.java @@ -27,6 +27,8 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; +import java.io.FileInputStream; +import java.util.Properties; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -47,14 +49,23 @@ public class TestPapActivator { /** * Initializes an activator. + * * @throws Exception if an error occurs */ @Before public void setUp() throws Exception { - final String[] papConfigParameters = { "-c", "parameters/PapConfigParameters.json" }; + final String[] papConfigParameters = + {"-c", "parameters/PapConfigParameters.json", "-p", "parameters/topic.properties"}; final PapCommandLineArguments arguments = new PapCommandLineArguments(papConfigParameters); final PapParameterGroup parGroup = new PapParameterHandler().getParameters(arguments); - activator = new PapActivator(parGroup); + + Properties props = new Properties(); + String propFile = arguments.getFullConfigurationFilePath(); + try (FileInputStream stream = new FileInputStream(propFile)) { + props.load(stream); + } + + activator = new PapActivator(parGroup, props); } /** @@ -84,7 +95,6 @@ public class TestPapActivator { @Test public void testGetCurrent_testSetCurrent() { - PapActivator.setCurrent(activator); assertSame(activator, PapActivator.getCurrent()); } |