From 99913f91b06abedd84e3837f836461cf8a14c0d7 Mon Sep 17 00:00:00 2001 From: "ning.xi" Date: Thu, 11 Jul 2019 07:31:31 +0000 Subject: Make URL configurable in REST Requestor/Client Change-Id: Ifb9870636996d1af786f05fcef7aa811d6f57532 Issue-ID: POLICY-1743 Signed-off-by: ning.xi --- .../restclient/ApexRestClientProducerTest.java | 59 +++++++++++++++++++++- .../RestClientCarrierTechnologyParametersTest.java | 58 +++++++++++++++++++-- .../restclient/SupportApexEventReceiver.java | 1 + 3 files changed, 112 insertions(+), 6 deletions(-) (limited to 'plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test') diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientProducerTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientProducerTest.java index 8ef0ec95c..d57bdd769 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientProducerTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientProducerTest.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -41,9 +42,13 @@ import org.onap.policy.apex.service.engine.event.SynchronousEventCache; import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.ApexFileEventConsumer; import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters; import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode; +import org.onap.policy.common.parameters.GroupValidationResult; +import org.onap.policy.common.parameters.ParameterException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Properties; + /** * Test the ApexRestClientProducer class. * @@ -174,7 +179,7 @@ public class ApexRestClientProducerTest { } @Test - public void testApexRestClientProducerPostEvent() { + public void testApexRestClientProducerPostEventFail() { MockitoAnnotations.initMocks(this); ApexRestClientProducer arcp = new ApexRestClientProducer(); @@ -202,10 +207,60 @@ public class ApexRestClientProducerTest { Mockito.doReturn(targetMock).when(httpClientMock).target(rcctp.getUrl()); arcp.setClient(httpClientMock); + //test property not found + rcctp.setUrl("http://some.place2.that.{key}.not/{tag}and.again.{tag}"); + Properties properties = new Properties(); + properties.put("tag", "exist"); try { - arcp.sendEvent(123, null, "EventName", "This is an Event"); + arcp.sendEvent(123, properties, "EventName", "This is an Event"); arcp.stop(); + fail("test should throw an exception"); } catch (Exception e) { + assertEquals( + "key\"key\"specified on url " + + "\"http://some.place2.that.{key}.not/{tag}and.again.{tag}\"not found " + + "in execution properties passed by the current policy", + e.getMessage()); + } + } + + @Test + public void testApexRestClientProducerPostEventOK() { + MockitoAnnotations.initMocks(this); + + ApexRestClientProducer arcp = new ApexRestClientProducer(); + assertNotNull(arcp); + + EventHandlerParameters producerParameters = new EventHandlerParameters(); + RestClientCarrierTechnologyParameters rcctp = new RestClientCarrierTechnologyParameters(); + producerParameters.setCarrierTechnologyParameters(rcctp); + + rcctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.PUT); + try { + arcp.init("RestClientConsumer", producerParameters); + assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.PUT, rcctp.getHttpMethod()); + + assertEquals("RestClientConsumer", arcp.getName()); + } catch (ApexEventException e) { + fail("test should not throw an exception"); + } + + System.out.println("fail test"); + rcctp.setUrl("http://some.place.{key}.does.not/{tag}"); + Properties properties = new Properties(); + properties.put("tag", "exist"); + properties.put("key", "that"); + Mockito.doReturn(Response.Status.OK.getStatusCode()).when(responseMock).getStatus(); + Mockito.doReturn(responseMock).when(builderMock).put(Mockito.any()); + Mockito.doReturn(builderMock).when(targetMock).request("application/json"); + Mockito.doReturn(builderMock).when(builderMock).headers(Mockito.any()); + Mockito.doReturn(targetMock).when(httpClientMock).target("http://some.place.that.does.not/exist"); + arcp.setClient(httpClientMock); + + try { + arcp.sendEvent(123, properties, "EventName", "This is an Event"); + arcp.stop(); + } catch (Exception ex) { fail("test should not throw an exception"); } } diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/RestClientCarrierTechnologyParametersTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/RestClientCarrierTechnologyParametersTest.java index 3d0c9b73f..f982059f7 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/RestClientCarrierTechnologyParametersTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/RestClientCarrierTechnologyParametersTest.java @@ -1,19 +1,20 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * 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========================================================= */ @@ -31,6 +32,8 @@ import org.onap.policy.apex.service.parameters.ApexParameterHandler; import org.onap.policy.apex.service.parameters.ApexParameters; import org.onap.policy.common.parameters.ParameterException; +import java.util.Set; + /** * Test REST Requestor carrier technology parameters. */ @@ -102,7 +105,7 @@ public class RestClientCarrierTechnologyParametersTest { assertEquals("bbb", rrctp1.getHttpHeadersAsMultivaluedMap().get("aaa").get(0)); assertEquals("ddd", rrctp1.getHttpHeadersAsMultivaluedMap().get("ccc").get(0)); assertEquals("fff", rrctp1.getHttpHeadersAsMultivaluedMap().get("eee").get(0)); - + rrctp1.setHttpHeaders(null); assertEquals(null, rrctp1.getHttpHeadersAsMultivaluedMap()); } catch (ParameterException pe) { @@ -146,4 +149,51 @@ public class RestClientCarrierTechnologyParametersTest { + "[url=http://some.where, httpMethod=DELETE, httpHeaders=[[aaa, bbb], [ccc, ddd]]]", rrctp.toString()); } + + @Test + public void testUrlValidation() { + RestClientCarrierTechnologyParameters rrctp = + new RestClientCarrierTechnologyParameters(); + + rrctp.setUrl("http://some.where.no.tag.in.url"); + assertEquals("http://some.where.no.tag.in.url", rrctp.getUrl()); + + String[][] httpHeaders = new String[2][2]; + httpHeaders[0][0] = "aaa"; + httpHeaders[0][1] = "bbb"; + httpHeaders[1][0] = "ccc"; + httpHeaders[1][1] = "ddd"; + + rrctp.setHttpHeaders(httpHeaders); + assertEquals("aaa", rrctp.getHttpHeaders()[0][0]); + assertEquals("bbb", rrctp.getHttpHeaders()[0][1]); + assertEquals("ccc", rrctp.getHttpHeaders()[1][0]); + assertEquals("ddd", rrctp.getHttpHeaders()[1][1]); + + assertEquals(true, rrctp.validateTagInUrl()); + + rrctp.setUrl("http://{place}.{that}/is{that}.{one}"); + assertEquals(true, rrctp.validateTagInUrl()); + + Set keymap = rrctp.getKeysFromUrl(); + assertEquals(true, keymap.contains("place")); + assertEquals(true, keymap.contains("that")); + assertEquals(true, keymap.contains("one")); + + rrctp.setUrl("http://{place.{that}/{is}.{not}/{what}.{exist}"); + assertEquals(false, rrctp.validateTagInUrl()); + rrctp.setUrl("http://{place}.{that}/{is}.{not}/{what}.{exist"); + assertEquals(false, rrctp.validateTagInUrl()); + rrctp.setUrl("http://place.that/is.not/what.{exist"); + assertEquals(false, rrctp.validateTagInUrl()); + rrctp.setUrl("http://place}.{that}/{is}.{not}/{what}.{exist}"); + assertEquals(false, rrctp.validateTagInUrl()); + rrctp.setUrl("http://{place}.{that}/is}.{not}/{what}.{exist}"); + assertEquals(false, rrctp.validateTagInUrl()); + rrctp.setUrl("http://{place}.{that}/{}.{not}/{what}.{exist}"); + assertEquals(false, rrctp.validateTagInUrl()); + rrctp.setUrl("http://{place}.{that}/{ }.{not}/{what}.{exist}"); + assertEquals(false, rrctp.validateTagInUrl()); + + } } diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/SupportApexEventReceiver.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/SupportApexEventReceiver.java index 739a6660a..717d1e9cb 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/SupportApexEventReceiver.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/SupportApexEventReceiver.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. -- cgit 1.2.3-korg