diff options
author | waynedunican <wayne.dunican@est.tech> | 2024-10-18 14:41:55 +0100 |
---|---|---|
committer | waynedunican <wayne.dunican@est.tech> | 2024-10-21 13:30:04 +0100 |
commit | 85f95e93a19c778e72b6022a713f0eea55002585 (patch) | |
tree | bd3ec6444bc0f2ac59e92963ce3f7addf81ad1fd | |
parent | 5ec75bd70fa4195cef4ab7f8ba62f1587e944727 (diff) |
Improve code and branch coverage in apex-pdp
Issue-ID: POLICY-5059
Change-Id: I714863fce899754f22c2d64cc4bfbd0051d3e27e
Signed-off-by: waynedunican <wayne.dunican@est.tech>
12 files changed, 900 insertions, 1 deletions
diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/ApexEventTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/ApexEventTest.java index ae6aac660..fcf1cf411 100644 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/ApexEventTest.java +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/ApexEventTest.java @@ -22,6 +22,7 @@ package org.onap.policy.apex.service.engine.event; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.Map; import java.util.Properties; @@ -170,4 +171,10 @@ class ApexEventTest { assertThat(actual2).isNull(); assertThat(actual3).isNull(); } + + @Test + void testConstructor() throws ApexEventException { + ApexEvent apexEventNew = new ApexEvent("name", "version", "namespace", "source", "target"); + assertEquals("name", apexEventNew.getName()); + } }
\ No newline at end of file diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/ApexPluginsEventConsumerTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/ApexPluginsEventConsumerTest.java new file mode 100644 index 000000000..50029fc75 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/ApexPluginsEventConsumerTest.java @@ -0,0 +1,74 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2024 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.service.engine.event; + +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.producer.ApexFileEventProducer; +import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters; +import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode; + +class ApexPluginsEventConsumerTest { + + ApexPluginsEventConsumer consumer; + + @BeforeEach + void setUp() { + consumer = new ApexPluginsEventConsumer() { + @Override + public void run() { + // do nothing + } + + @Override + public void init(String name, EventHandlerParameters consumerParameters, + ApexEventReceiver apexEventReceiver) throws ApexEventException { + // do nothing + } + + @Override + public void stop() { + // do nothing + } + }; + } + + @Test + void testStart() { + assertThatCode(() -> consumer.start()).doesNotThrowAnyException(); + } + + @Test + void testGetPeeredReference() { + EventHandlerPeeredMode mode = EventHandlerPeeredMode.REQUESTOR; + assertNull(consumer.getPeeredReference(mode)); + + ApexPluginsEventProducer producer = new ApexFileEventProducer(); + PeeredReference reference = new PeeredReference(mode, consumer, producer); + consumer.setPeeredReference(mode, reference); + assertEquals(producer, consumer.getPeeredReference(mode).getPeeredProducer()); + assertEquals(consumer, consumer.getPeeredReference(mode).getPeeredConsumer()); + } +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/main/ApexEngineServiceHandlerTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/main/ApexEngineServiceHandlerTest.java new file mode 100644 index 000000000..f90a4311d --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/main/ApexEngineServiceHandlerTest.java @@ -0,0 +1,100 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2024 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.service.engine.main; + +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; +import org.onap.policy.apex.model.basicmodel.concepts.ApexException; +import org.onap.policy.apex.service.engine.event.ApexEvent; +import org.onap.policy.apex.service.engine.runtime.EngineService; +import org.onap.policy.apex.service.engine.runtime.EngineServiceEventInterface; + +@ExtendWith(MockitoExtension.class) +class ApexEngineServiceHandlerTest { + + @Mock + private EngineService engineService; + + @Mock + private ApexEvent apexEvent; + + @Mock + private EngineServiceEventInterface interfaceMock; + + @InjectMocks + private ApexEngineServiceHandler apexEngineServiceHandler; + + @BeforeEach + public void setup() { + Mockito.lenient().when(engineService.getEngineServiceEventInterface()).thenReturn(interfaceMock); + } + + @Test + void testForwardEvent_Success() { + Mockito.lenient().when(apexEvent.getName()).thenReturn("TestEvent"); + + apexEngineServiceHandler.forwardEvent(apexEvent); + + verify(engineService.getEngineServiceEventInterface(), times(1)).sendEvent(apexEvent); + } + + @Test + void testForwardEvent_ExceptionThrown() { + Mockito.lenient().when(apexEvent.getName()).thenReturn("TestEvent"); + + when(engineService.getEngineServiceEventInterface()).thenThrow(new RuntimeException("Simulated Exception")); + + ApexActivatorRuntimeException thrown = assertThrows(ApexActivatorRuntimeException.class, () -> { + apexEngineServiceHandler.forwardEvent(apexEvent); + }); + + assertTrue(thrown.getMessage().contains("error transferring event")); + assertTrue(thrown.getCause().getMessage().contains("Simulated Exception")); + } + + @Test + void testTerminate_Success() throws ApexException { + assertThatCode(apexEngineServiceHandler::terminate) + .doesNotThrowAnyException(); + + verify(engineService, times(1)).stop(); + verify(engineService, times(1)).clear(); + } + + @Test + void testTerminate_NullService() { + ApexEngineServiceHandler handlerWithNullService = new ApexEngineServiceHandler(null); + assertDoesNotThrow(() -> handlerWithNullService.terminate()); + } +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/main/ApexExceptionsTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/main/ApexExceptionsTest.java new file mode 100644 index 000000000..3338046cd --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/main/ApexExceptionsTest.java @@ -0,0 +1,54 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2024 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.service.engine.main; + +import static org.assertj.core.api.Assertions.assertThatCode; + +import org.junit.jupiter.api.Test; +import org.onap.policy.apex.model.basicmodel.concepts.ApexException; +import org.onap.policy.apex.model.basicmodel.concepts.ApexRuntimeException; + +class ApexExceptionsTest { + + @Test + void testExceptions() { + assertThatCode(() -> { + throw new ApexActivatorException("Test Apex Activator Exception"); + }).hasMessageContaining("Test Apex Activator Exception") + .isInstanceOf(ApexException.class); + + Exception e = new Exception(); + assertThatCode(() -> { + throw new ApexActivatorException("Test Apex Activator Exception", e); + }).hasMessageContaining("Test Apex Activator Exception") + .isInstanceOf(ApexException.class); + + assertThatCode(() -> { + throw new ApexActivatorRuntimeException("Test runtime exception"); + }).hasMessageContaining("Test runtime exception") + .isInstanceOf(ApexRuntimeException.class); + + assertThatCode(() -> { + throw new ApexActivatorRuntimeException("Test runtime exception", e); + }).hasMessageContaining("Test runtime exception") + .isInstanceOf(ApexRuntimeException.class); + } +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/parameters/carriertechnology/CarrierTechnologyParametersJsonAdapterTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/parameters/carriertechnology/CarrierTechnologyParametersJsonAdapterTest.java new file mode 100644 index 000000000..70f03c9b6 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/parameters/carriertechnology/CarrierTechnologyParametersJsonAdapterTest.java @@ -0,0 +1,120 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2024 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.service.parameters.carriertechnology; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.mockito.Mockito.mock; + +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonElement; +import com.google.gson.JsonParser; +import com.google.gson.JsonSerializationContext; +import java.lang.reflect.Type; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.onap.policy.common.parameters.ParameterRuntimeException; + +class CarrierTechnologyParametersJsonAdapterTest { + + CarrierTechnologyParametersJsonAdapter adapter; + RestPluginCarrierTechnologyParameters parameters; + JsonDeserializationContext deserializationContext; + + @BeforeEach + void setUp() { + adapter = new CarrierTechnologyParametersJsonAdapter(); + parameters = new RestPluginCarrierTechnologyParameters(); + deserializationContext = Mockito.mock(JsonDeserializationContext.class); + } + + @Test + void testSerialize() { + Type typeOf = mock(Type.class); + JsonSerializationContext context = mock(JsonSerializationContext.class); + assertThatThrownBy(() -> adapter.serialize(parameters, typeOf, context)) + .isInstanceOf(ParameterRuntimeException.class) + .hasMessageContaining("serialization of Apex carrier technology parameters to Json is not supported"); + + } + + @Test + void testDeserialize() { + String jsonString = """ + { + "parameters": {} + } + """; + JsonElement jsonElement = JsonParser.parseString(jsonString); + CarrierTechnologyParameters result = adapter.deserialize(jsonElement, null, deserializationContext); + assertNull(result); + + jsonString = """ + { + "carrierTechnology": "UNKNOWN_TECH", + "parameters": {} + } + """; + JsonElement finalJsonElement = JsonParser.parseString(jsonString); + assertThatThrownBy(() -> adapter.deserialize(finalJsonElement, null, deserializationContext)) + .isInstanceOf(ParameterRuntimeException.class) + .hasMessageContaining("carrier technology \"UNKNOWN_TECH\""); + + jsonString = """ + { + "carrierTechnology": "", + "parameters": {} + } + """; + JsonElement finalJsonElement1 = JsonParser.parseString(jsonString); + assertThatThrownBy(() -> adapter.deserialize(finalJsonElement1, null, deserializationContext)).isInstanceOf( + ParameterRuntimeException.class); + + jsonString = """ + { + "carrierTechnology": "null", + "parameters": {} + } + """; + JsonElement finalJsonElement2 = JsonParser.parseString(jsonString); + assertThatThrownBy(() -> adapter.deserialize(finalJsonElement2, null, deserializationContext)).isInstanceOf( + ParameterRuntimeException.class); + + } + + @Test + void testDeserializeWithInvalidParameterClass() { + String jsonString = """ + { + "carrierTechnology": "FILE", + "parameterClassName": "InvalidClassName", + "parameters": {} + } + """; + JsonElement jsonElement = JsonParser.parseString(jsonString); + + assertThatThrownBy(() -> adapter.deserialize(jsonElement, null, deserializationContext)) + .isInstanceOf(ParameterRuntimeException.class) + .hasMessageContaining("could not find class"); + } + +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/parameters/carriertechnology/CarrierTechnologyParametersTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/parameters/carriertechnology/CarrierTechnologyParametersTest.java new file mode 100644 index 000000000..a6370dfc7 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/parameters/carriertechnology/CarrierTechnologyParametersTest.java @@ -0,0 +1,76 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2024 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.service.parameters.carriertechnology; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class CarrierTechnologyParametersTest { + + CarrierTechnologyParameters parameters; + + @BeforeEach + void setUp() { + parameters = new CarrierTechnologyParameters() { + @Override + public String getLabel() { + return super.getLabel(); + } + }; + } + + @Test + void testSetLabel() { + parameters.setLabel("testLabel"); + assertEquals("testLabel", parameters.getLabel()); + assertEquals("testLabel", parameters.getName()); + + parameters.setLabel(null); + assertNull(parameters.getLabel()); + } + + @Test + void testSetEventProducerPluginClass() { + parameters.setEventProducerPluginClass("testClassName"); + assertEquals("testClassName", parameters.getEventProducerPluginClass()); + + parameters.setEventProducerPluginClass(null); + assertNull(parameters.getEventProducerPluginClass()); + } + + @Test + void testSetEventConsumerPluginClass() { + parameters.setEventConsumerPluginClass("testClassName"); + assertEquals("testClassName", parameters.getEventConsumerPluginClass()); + + parameters.setEventConsumerPluginClass(null); + assertNull(parameters.getEventConsumerPluginClass()); + } + + @Test + void testToString() { + assertThat(parameters.toString()).contains("CarrierTechnologyParameters"); + } +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/parameters/carriertechnology/RestPluginCarrierTechnologyParametersTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/parameters/carriertechnology/RestPluginCarrierTechnologyParametersTest.java new file mode 100644 index 000000000..05079dbb8 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/parameters/carriertechnology/RestPluginCarrierTechnologyParametersTest.java @@ -0,0 +1,159 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2024 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.service.parameters.carriertechnology; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import jakarta.ws.rs.core.MultivaluedMap; +import java.util.Set; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.onap.policy.common.parameters.BeanValidationResult; +import org.onap.policy.common.parameters.ValidationResult; +import org.onap.policy.common.parameters.ValidationStatus; + +class RestPluginCarrierTechnologyParametersTest { + + RestPluginCarrierTechnologyParameters parameters; + + @BeforeEach + void setUp() { + parameters = new RestPluginCarrierTechnologyParameters(); + } + + @Test + void testHeaders() { + String[][] headers = { + {"HeaderOne", "valueOne"}, + {"HeaderTwo", "valueTwo"} + }; + parameters.setHttpHeaders(headers); + assertTrue(parameters.checkHttpHeadersSet()); + + MultivaluedMap<String, Object> multiVHeaders = parameters.getHttpHeadersAsMultivaluedMap(); + assertNotNull(multiVHeaders); + + parameters.setHttpHeaders(null); + assertFalse(parameters.checkHttpHeadersSet()); + multiVHeaders = parameters.getHttpHeadersAsMultivaluedMap(); + assertNull(multiVHeaders); + + headers = new String[][] {}; + parameters.setHttpHeaders(headers); + assertFalse(parameters.checkHttpHeadersSet()); + } + + @Test + void testValidate() { + String[][] headers = { + {"HeaderOne", "valueOne"}, + {"HeaderTwo", "valueTwo"} + }; + parameters.setHttpHeaders(headers); + + String url = "https://testurl.com/{something}/{else}"; + parameters.setUrl(url); + + BeanValidationResult result = parameters.validate(); + assertNotNull(result.getResult()); + + parameters.setHttpHeaders(null); + result = parameters.validate(); + assertNotNull(result.getResult()); + + headers = new String[][] { + {"HeaderOne", "valueOne"}, + null, + {"HeaderTwo", "ValueTwo", "ValueThree"}, + {null, "2"}, + {"HeaderThree", null} + }; + parameters.setHttpHeaders(headers); + assertThatCode(parameters::validate).doesNotThrowAnyException(); + } + + @Test + void testUrl() { + String urlMissingEndTag = "http://www.blah.com/{par1/somethingelse"; + parameters.setUrl(urlMissingEndTag); + ValidationResult result = parameters.validateUrl(); + assertEquals(ValidationStatus.INVALID, result.getStatus()); + + String urlNestedTag = "http://www.blah.com/{par1/{some}thingelse"; + parameters.setUrl(urlNestedTag); + result = parameters.validateUrl(); + assertEquals(ValidationStatus.INVALID, result.getStatus()); + + String urlMissingStartTag = "http://www.blah.com/{par1}/some}thingelse"; + parameters.setUrl(urlMissingStartTag); + result = parameters.validateUrl(); + assertEquals(ValidationStatus.INVALID, result.getStatus()); + + String urlMissingStartTag2 = "http://www.blah.com/par1}/somethingelse"; + parameters.setUrl(urlMissingStartTag2); + result = parameters.validateUrl(); + assertEquals(ValidationStatus.INVALID, result.getStatus()); + + String urlEmptyTag = "http://www.blah.com/{}/somethingelse"; + parameters.setUrl(urlEmptyTag); + result = parameters.validateUrl(); + assertEquals(ValidationStatus.INVALID, result.getStatus()); + + String urlNull = null; + parameters.setUrl(urlNull); + result = parameters.validateUrl(); + assertNull(result); + + String urlValid = "https://testurl.com/{something}/{else}"; + parameters.setUrl(urlValid); + result = parameters.validateUrl(); + assertNull(result); + + Set<String> keys = parameters.getKeysFromUrl(); + assertNotNull(keys); + } + + @Test + void testCodeFilter() { + parameters.setHttpCodeFilter("test"); + assertNull(parameters.validateHttpCodeFilter()); + + parameters.setHttpCodeFilter(null); + assertNull(parameters.validateHttpCodeFilter()); + + parameters.setHttpCodeFilter(""); + assertEquals(ValidationStatus.INVALID, parameters.validateHttpCodeFilter().getStatus()); + + parameters.setHttpCodeFilter("(22M"); + assertEquals(ValidationStatus.INVALID, parameters.validateHttpCodeFilter().getStatus()); + } + + @Test + void testToString() { + assertThat(parameters.toString()).contains("CarrierTechnologyParameters"); + } +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/parameters/engineservice/EngineServiceParametersJsonAdapterTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/parameters/engineservice/EngineServiceParametersJsonAdapterTest.java new file mode 100644 index 000000000..c0e064e01 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/parameters/engineservice/EngineServiceParametersJsonAdapterTest.java @@ -0,0 +1,94 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2024 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.service.parameters.engineservice; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.Mockito.mock; + +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonElement; +import com.google.gson.JsonParser; +import com.google.gson.JsonSerializationContext; +import java.lang.reflect.Type; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.onap.policy.apex.core.engine.EngineParameters; +import org.onap.policy.common.parameters.ParameterRuntimeException; + +class EngineServiceParametersJsonAdapterTest { + + EngineServiceParametersJsonAdapter adapter; + Type typeOf; + JsonSerializationContext contextSer; + JsonDeserializationContext contextDeser; + + @BeforeEach + void setUp() { + adapter = new EngineServiceParametersJsonAdapter(); + typeOf = mock(Type.class); + contextSer = mock(JsonSerializationContext.class); + contextDeser = mock(JsonDeserializationContext.class); + } + + @Test + void testSerialize() { + EngineParameters parameters = new EngineParameters(); + assertThatThrownBy(() -> adapter.serialize(parameters, typeOf, contextSer)) + .isInstanceOf(ParameterRuntimeException.class) + .hasMessageContaining("serialization of Apex parameters to Json is not supported"); + } + + @Test + void testGetExecutorParametersListError() { + String jsonString = """ + { + "taskParameters": {}, + "executorParameters": { + "param1": "value1", + "param2": "value2" + } + } + """; + JsonElement jsonElement = JsonParser.parseString(jsonString); + assertThatThrownBy(() -> adapter.deserialize(jsonElement, typeOf, contextDeser)) + .isInstanceOf(ParameterRuntimeException.class) + .hasMessageContaining("entry is not a parameter JSON object"); + } + + @Test + void testDeserialize() { + String jsonString = """ + { + "taskParameters": [ + { + "test1": "value1", + "test2": "value2" + } + ], + "executorParameters": {} + } + """; + JsonElement jsonElement = JsonParser.parseString(jsonString); + EngineParameters engineParameters = adapter.deserialize(jsonElement, typeOf, contextDeser); + assertNotNull(engineParameters); + } +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/parameters/eventhandler/EventHandlerParametersTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/parameters/eventhandler/EventHandlerParametersTest.java new file mode 100644 index 000000000..e77fd4e38 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/parameters/eventhandler/EventHandlerParametersTest.java @@ -0,0 +1,150 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2024 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.service.parameters.eventhandler; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters; +import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParameters; +import org.onap.policy.common.parameters.BeanValidationResult; +import org.onap.policy.common.parameters.ValidationStatus; + +class EventHandlerParametersTest { + + private EventHandlerParameters handlerParams; + + @BeforeEach + void setUp() { + handlerParams = new EventHandlerParameters(); + } + + @Test + void testConstructor() { + assertFalse(handlerParams.isPeeredMode(EventHandlerPeeredMode.SYNCHRONOUS)); + assertFalse(handlerParams.isPeeredMode(EventHandlerPeeredMode.REQUESTOR)); + assertNull(handlerParams.getSynchronousPeer()); + assertNull(handlerParams.getRequestorPeer()); + assertEquals(0, handlerParams.getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); + assertEquals(0, handlerParams.getPeerTimeout(EventHandlerPeeredMode.REQUESTOR)); + assertNull(handlerParams.getEventName()); + assertNull(handlerParams.getEventNameFilter()); + } + + @Test + void testSetAndGetCarrierTechnologyParameters() { + CarrierTechnologyParameters carrierParams = mock(CarrierTechnologyParameters.class); + handlerParams.setCarrierTechnologyParameters(carrierParams); + assertEquals(carrierParams, handlerParams.getCarrierTechnologyParameters()); + } + + @Test + void testSetAndGetEventProtocolParameters() { + EventProtocolParameters protocolParams = mock(EventProtocolParameters.class); + handlerParams.setEventProtocolParameters(protocolParams); + assertEquals(protocolParams, handlerParams.getEventProtocolParameters()); + } + + @Test + void testCheckSetName() { + assertTrue(handlerParams.checkSetName()); + handlerParams.setName(null); + assertFalse(handlerParams.checkSetName()); + handlerParams.setName(" "); + assertFalse(handlerParams.checkSetName()); + handlerParams.setName("ValidName"); + assertTrue(handlerParams.checkSetName()); + } + + @Test + void testSetPeeredMode() { + handlerParams.setPeeredMode(EventHandlerPeeredMode.SYNCHRONOUS, true); + assertTrue(handlerParams.isPeeredMode(EventHandlerPeeredMode.SYNCHRONOUS)); + handlerParams.setPeeredMode(EventHandlerPeeredMode.REQUESTOR, true); + assertTrue(handlerParams.isPeeredMode(EventHandlerPeeredMode.REQUESTOR)); + } + + @Test + void testSetAndGetPeer() { + handlerParams.setPeer(EventHandlerPeeredMode.SYNCHRONOUS, "SyncPeer"); + assertEquals("SyncPeer", handlerParams.getPeer(EventHandlerPeeredMode.SYNCHRONOUS)); + handlerParams.setPeer(EventHandlerPeeredMode.REQUESTOR, "RequestorPeer"); + assertEquals("RequestorPeer", handlerParams.getPeer(EventHandlerPeeredMode.REQUESTOR)); + } + + @Test + void testSetAndGetPeerTimeout() { + handlerParams.setPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS, 5000); + assertEquals(5000, handlerParams.getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); + handlerParams.setPeerTimeout(EventHandlerPeeredMode.REQUESTOR, 7000); + assertEquals(7000, handlerParams.getPeerTimeout(EventHandlerPeeredMode.REQUESTOR)); + } + + @Test + void testIsSetEventName() { + assertFalse(handlerParams.isSetEventName()); + handlerParams.setEventName("TestEvent"); + assertTrue(handlerParams.isSetEventName()); + } + + @Test + void testIsSetEventNameFilter() { + assertFalse(handlerParams.isSetEventNameFilter()); + handlerParams.setEventNameFilter(".*"); + assertTrue(handlerParams.isSetEventNameFilter()); + } + + @Test + void testValidateValidFilter() { + handlerParams.setEventNameFilter(".*"); + BeanValidationResult result = handlerParams.validate(); + assertEquals(ValidationStatus.INVALID, result.getStatus()); + } + + @Test + void testValidateInvalidFilter() { + handlerParams.setEventNameFilter("[invalid_regex"); + BeanValidationResult result = handlerParams.validate(); + assertEquals(ValidationStatus.INVALID, result.getStatus()); + assertFalse(result.getMessage().contains("not a valid regular expression")); + } + + @Test + void testValidateNullFilter() { + handlerParams.setEventNameFilter(null); + BeanValidationResult result = handlerParams.validate(); + assertEquals(ValidationStatus.INVALID, result.getStatus()); + } + + @Test + void testPeeredModeDefaultCases() { + assertNull(handlerParams.getPeer(EventHandlerPeeredMode.REQUESTOR)); + handlerParams.setPeer(EventHandlerPeeredMode.REQUESTOR, "test"); + assertEquals(0, handlerParams.getPeerTimeout(EventHandlerPeeredMode.REQUESTOR)); + handlerParams.setPeerTimeout(EventHandlerPeeredMode.REQUESTOR, 1000); + handlerParams.setPeeredMode(EventHandlerPeeredMode.REQUESTOR, true); + } +} diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpUpdateListener.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpUpdateListener.java index 56889cf72..f0eb4ff4b 100644 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpUpdateListener.java +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpUpdateListener.java @@ -212,4 +212,21 @@ class TestPdpUpdateListener { "Apex engine started. But, only the following polices are running - apex_policy_name:1.0 . " + "Other policies failed execution. Please see the logs for more details.")); } + + @Test + void testPdpUpdateMessageListener_pdpUpdateMessageIntervalZero() throws CoderException { + final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT); + final ToscaPolicy toscaPolicy = + TestListenerUtils.createToscaPolicy("apex policy name", "1.0", "src/test/resources/dummyProperties.json"); + final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>(); + toscaPolicies.add(toscaPolicy); + final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies, + new LinkedList<>()); + pdpUpdateMsg.setPdpHeartbeatIntervalMs((long) -1); + pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg); + assertEquals(pdpStatus.getPdpGroup(), pdpUpdateMsg.getPdpGroup()); + assertEquals(pdpStatus.getPdpSubgroup(), pdpUpdateMsg.getPdpSubgroup()); + assertEquals(pdpStatus.getPolicies(), + new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPoliciesToBeDeployed())); + } } diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/TestToscaPolicyTypeIdentifierParameters.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/TestToscaPolicyTypeIdentifierParameters.java new file mode 100644 index 000000000..001087dbc --- /dev/null +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/TestToscaPolicyTypeIdentifierParameters.java @@ -0,0 +1,49 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2024 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.services.onappf.parameters; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +class TestToscaPolicyTypeIdentifierParameters { + + ToscaPolicyTypeIdentifierParameters parameters; + ToscaPolicyTypeIdentifierParameters parametersCopy; + + @Test + void test() { + parameters = getParametersInstance(); + parametersCopy = getParametersInstance(); + + // testing the equals method ToscaPolicyTypeIdentifierParameters + assertTrue(parameters.equals(parametersCopy)); //NOSONAR + assertEquals(parameters.hashCode(), parametersCopy.hashCode()); + } + + protected ToscaPolicyTypeIdentifierParameters getParametersInstance() { + ToscaPolicyTypeIdentifierParameters parametersTest = new ToscaPolicyTypeIdentifierParameters(); + parametersTest.setName("testName"); + parametersTest.setVersion("1.0.0"); + return parametersTest; + } +}
\ No newline at end of file diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/rest/TestHealthCheckRestControllerV1.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/rest/TestHealthCheckRestControllerV1.java index a63824a0e..2484155a0 100644 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/rest/TestHealthCheckRestControllerV1.java +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/rest/TestHealthCheckRestControllerV1.java @@ -27,7 +27,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import jakarta.ws.rs.client.Invocation; import jakarta.ws.rs.client.SyncInvoker; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.onap.policy.common.endpoints.report.HealthCheckReport; |