diff options
author | a.sreekumar <ajith.sreekumar@est.tech> | 2020-03-01 19:38:50 +0000 |
---|---|---|
committer | a.sreekumar <ajith.sreekumar@est.tech> | 2020-03-03 15:47:23 +0000 |
commit | 3db2a659f2d75a7303b39235dd81f4d55d3b87e2 (patch) | |
tree | b631079782ff64f49494807acd78678f8ccf45f2 /plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-grpc/src/test | |
parent | 5922128637471bfa1897b8808561000988da733d (diff) |
GRPC support for APEX-CDS interaction
Change-Id: I586153244dbd97a41e9b9d616ee9a84327b7c2da
Issue-ID: POLICY-1656
Signed-off-by: a.sreekumar <ajith.sreekumar@est.tech>
Diffstat (limited to 'plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-grpc/src/test')
4 files changed, 303 insertions, 0 deletions
diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-grpc/src/test/java/org/onap/policy/apex/plugins/event/carrier/grpc/ApexGrpcConsumerTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-grpc/src/test/java/org/onap/policy/apex/plugins/event/carrier/grpc/ApexGrpcConsumerTest.java new file mode 100644 index 000000000..dc5cc3809 --- /dev/null +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-grpc/src/test/java/org/onap/policy/apex/plugins/event/carrier/grpc/ApexGrpcConsumerTest.java @@ -0,0 +1,93 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020 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========================================================= + */ + +package org.onap.policy.apex.plugins.event.carrier.grpc; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; + +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.service.engine.event.ApexEventException; +import org.onap.policy.apex.service.engine.event.ApexEventReceiver; +import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters; +import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode; + +public class ApexGrpcConsumerTest { + ApexGrpcConsumer grpcConsumer = null; + EventHandlerParameters consumerParameters = null; + ApexEventReceiver incomingEventReceiver = null; + + private static final String GRPC_CONSUMER_ERROR_MSG = + "A gRPC Consumer may not be specified. Only sending events is possible using gRPC"; + + /** + * Set up testing. + * + * @throws ApexEventException on test set up errors. + */ + @Before + public void setUp() throws ApexEventException { + grpcConsumer = new ApexGrpcConsumer(); + consumerParameters = new EventHandlerParameters(); + consumerParameters.setCarrierTechnologyParameters(new GrpcCarrierTechnologyParameters() {}); + } + + @Test + public void testInit() { + assertThatThrownBy(() -> { + grpcConsumer.init("TestApexGrpcConsumer", consumerParameters, incomingEventReceiver); + }).hasMessage(GRPC_CONSUMER_ERROR_MSG); + } + + @Test + public void testStart() { + assertThatThrownBy(() -> { + grpcConsumer.start(); + }).hasMessage(GRPC_CONSUMER_ERROR_MSG); + } + + @Test + public void testGetName() { + assertEquals(null, new ApexGrpcConsumer().getName()); + } + + @Test + public void testGetPeeredReference() { + assertThatThrownBy(() -> { + grpcConsumer.getPeeredReference(EventHandlerPeeredMode.REQUESTOR); + }).hasMessage(GRPC_CONSUMER_ERROR_MSG); + } + + @Test + public void testSetPeeredReference() { + assertThatThrownBy(() -> { + grpcConsumer.setPeeredReference(null, null); + }).hasMessage(GRPC_CONSUMER_ERROR_MSG); + } + + @Test() + public void testStop() { + assertThatThrownBy(() -> { + new ApexGrpcConsumer().stop(); + }).hasMessage(GRPC_CONSUMER_ERROR_MSG); + } + +} diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-grpc/src/test/java/org/onap/policy/apex/plugins/event/carrier/grpc/ApexGrpcProducerTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-grpc/src/test/java/org/onap/policy/apex/plugins/event/carrier/grpc/ApexGrpcProducerTest.java new file mode 100644 index 000000000..53d191e14 --- /dev/null +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-grpc/src/test/java/org/onap/policy/apex/plugins/event/carrier/grpc/ApexGrpcProducerTest.java @@ -0,0 +1,98 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020 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========================================================= + */ + +package org.onap.policy.apex.plugins.event.carrier.grpc; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.Mockito.spy; + +import java.nio.file.Files; +import java.nio.file.Paths; +import org.assertj.core.api.Assertions; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.policy.apex.service.engine.event.ApexEventException; +import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters; +import org.onap.policy.cds.client.CdsProcessorGrpcClient; + +@RunWith(MockitoJUnitRunner.class) +public class ApexGrpcProducerTest { + private static final String HOST = "localhost"; + @Mock + private CdsProcessorGrpcClient grpcClient; + private ApexGrpcProducer apexGrpcProducer = spy(new ApexGrpcProducer()); + @Mock + private EventHandlerParameters eventHandlerParameters; + + /** + * Set up testing. + * + * @throws ApexEventException on test set up errors. + */ + @Before + public void setUp() throws ApexEventException { + populateEventHandlerParameters(HOST, 5); + } + + @Test(expected = ApexEventException.class) + public void testInit_fail() throws ApexEventException { + apexGrpcProducer.init("TestApexGrpcProducer", new EventHandlerParameters()); + } + + @Test + public void testInit_pass() { + // should not throw an exception + Assertions.assertThatCode(() -> apexGrpcProducer.init("TestApexGrpcProducer", eventHandlerParameters)) + .doesNotThrowAnyException(); + } + + @Test + public void testStop() throws ApexEventException { + apexGrpcProducer.init("TestApexGrpcProducer", eventHandlerParameters); + // should not throw an exception + Assertions.assertThatCode(() -> apexGrpcProducer.stop()).doesNotThrowAnyException(); + } + + @Test + public void testSendEvent() throws ApexEventException { + apexGrpcProducer.init("TestApexGrpcProducer", eventHandlerParameters); + assertThatThrownBy(() -> { + apexGrpcProducer.sendEvent(123, null, "grpcEvent", + Files.readString(Paths.get("src/test/resources/executionServiceInputEvent.json"))); + }).hasMessageContaining("UNAVAILABLE: io exception"); + } + + private void populateEventHandlerParameters(String host, int timeout) { + eventHandlerParameters = new EventHandlerParameters(); + GrpcCarrierTechnologyParameters params = new GrpcCarrierTechnologyParameters(); + params.setLabel("GRPC"); + params.setEventProducerPluginClass(ApexGrpcProducer.class.getName()); + params.setEventConsumerPluginClass(ApexGrpcConsumer.class.getName()); + params.setHost(host); + params.setPort(3214); + params.setUsername("dummyUser"); + params.setPassword("dummyPassword"); + params.setTimeout(timeout); + eventHandlerParameters.setCarrierTechnologyParameters(params); + } +} diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-grpc/src/test/java/org/onap/policy/apex/plugins/event/carrier/grpc/GrpcCarrierTechnologyParametersTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-grpc/src/test/java/org/onap/policy/apex/plugins/event/carrier/grpc/GrpcCarrierTechnologyParametersTest.java new file mode 100644 index 000000000..a3994c29e --- /dev/null +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-grpc/src/test/java/org/onap/policy/apex/plugins/event/carrier/grpc/GrpcCarrierTechnologyParametersTest.java @@ -0,0 +1,87 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020 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========================================================= + */ + +package org.onap.policy.apex.plugins.event.carrier.grpc; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.parameters.GroupValidationResult; + +public class GrpcCarrierTechnologyParametersTest { + + private static final String USERNAME = "username"; + private static final String PASSWORD = "password"; + private static final String HOST = "localhost"; + + private GrpcCarrierTechnologyParameters params; + + @Before + public void setUp() { + params = new GrpcCarrierTechnologyParameters(); + } + + @Test + public void testGrpcCarrierTechnologyParameters_invalid() { + GroupValidationResult result = params.validate(); + assertFalse(result.isValid()); + assertTrue(result.getResult().contains("field \"timeout\" type \"int\" value \"0\" INVALID, must be >= 1")); + assertTrue(result.getResult().contains("field \"port\" type \"int\" value \"0\" INVALID, must be >= 1024")); + assertTrue( + result.getResult().contains("field \"host\" type \"java.lang.String\" value \"null\" INVALID, is null")); + assertTrue(result.getResult() + .contains("field \"username\" type \"java.lang.String\" value \"null\" INVALID, is null")); + assertTrue(result.getResult() + .contains("field \"password\" type \"java.lang.String\" value \"null\" INVALID, is null")); + assertTrue(result.getResult().contains("")); + assertTrue(result.getResult().contains("")); + } + + @Test + public void testGrpcCarrierTechnologyParameters_valid() { + assertEquals("GRPC", params.getName()); + assertEquals(ApexGrpcConsumer.class.getName(), params.getEventConsumerPluginClass()); + assertEquals(ApexGrpcProducer.class.getName(), params.getEventProducerPluginClass()); + + params.setHost(HOST); + params.setPassword(PASSWORD); + params.setPort(2233); + params.setTimeout(1000); + params.setUsername(USERNAME); + GroupValidationResult result = params.validate(); + assertTrue(result.isValid()); + } + + @Test + public void testGrpcCarrierTechnologyParameters_invalid_values() { + params.setHost(HOST); + params.setPassword(PASSWORD); + params.setTimeout(1000); + params.setUsername(USERNAME); + + params.setPort(23); // invalid value + GroupValidationResult result = params.validate(); + assertFalse(result.isValid()); + assertTrue(result.getResult().contains("field \"port\" type \"int\" value \"23\" INVALID, must be >= 1024")); + } +} diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-grpc/src/test/resources/executionServiceInputEvent.json b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-grpc/src/test/resources/executionServiceInputEvent.json new file mode 100644 index 000000000..1054af14a --- /dev/null +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-grpc/src/test/resources/executionServiceInputEvent.json @@ -0,0 +1,25 @@ +{ + "actionIdentifiers": { + "actionName": "actionName", + "blueprintName": "bluePrintName", + "blueprintVersion": "1.0.0", + "mode": "sync" + }, + "commonHeader": { + "originatorId": "sdnc", + "requestId": "1234567", + "subRequestId": "subReqId" + }, + "payload": { + "config-assign-request": { + "resolution-key": "RES-KEY", + "config-assign-properties": { + "service-instance-id": "sid12", + "pnf-id": "pnf-id", + "pnf-address": "1.2.3.4", + "service-uuid": "service-uuid", + "customization-uuid": "customization-uuid" + } + } + } +} |