diff options
author | waynedunican <wayne.dunican@est.tech> | 2024-10-25 09:57:33 +0100 |
---|---|---|
committer | waynedunican <wayne.dunican@est.tech> | 2024-10-25 12:26:16 +0100 |
commit | f906185ce1424f707d290d3e4fc3e4b8a3e184f9 (patch) | |
tree | 10cd462d53f0d846517c182e35d854c2cd4c215f /services | |
parent | bc949bab9233b43224d9d9fab5243f7b62b7751e (diff) |
Increase apex-pdp line and branch coverage
Issue-ID: POLICY-5059
Change-Id: Ifa1472dd914ec0ef25372fa057514571a7ea27ac
Signed-off-by: waynedunican <wayne.dunican@est.tech>
Diffstat (limited to 'services')
-rw-r--r-- | services/services-engine/src/test/java/org/onap/policy/apex/service/engine/runtime/impl/EnEventListenerImplTest.java | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/runtime/impl/EnEventListenerImplTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/runtime/impl/EnEventListenerImplTest.java new file mode 100644 index 000000000..920901909 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/runtime/impl/EnEventListenerImplTest.java @@ -0,0 +1,105 @@ +/*- + * ============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.runtime.impl; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.policy.apex.core.engine.event.EnEvent; +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.event.impl.enevent.ApexEvent2EnEventConverter; +import org.onap.policy.apex.service.engine.runtime.ApexEventListener; + +class EnEventListenerImplTest { + + @Mock + private ApexEventListener mockApexEventListener; + + @Mock + private ApexEvent2EnEventConverter mockApexEnEventConverter; + + @Mock + private EnEvent mockEnEvent; + + @Mock + private ApexEvent mockApexEvent; + + @InjectMocks + private EnEventListenerImpl enEventListenerImpl; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + void testOnEnEvent_success() throws ApexException { + String eventName = "testEvent"; + when(mockEnEvent.getName()).thenReturn(eventName); + List<ApexEvent> apexEvents = Arrays.asList(mockApexEvent); + when(mockApexEnEventConverter.toApexEvent(eventName, mockEnEvent)).thenReturn(apexEvents); + + assertDoesNotThrow(() -> enEventListenerImpl.onEnEvent(mockEnEvent)); + + verify(mockApexEventListener, times(1)).onApexEvent(mockApexEvent); + } + + @Test + void testOnEnEvent_apexExceptionThrown() throws ApexException { + String eventName = "testEvent"; + when(mockEnEvent.getName()).thenReturn(eventName); + when(mockApexEnEventConverter.toApexEvent(eventName, mockEnEvent)) + .thenThrow(new ApexException("Conversion error")); + + assertThrows(ApexException.class, () -> enEventListenerImpl.onEnEvent(mockEnEvent)); + + verify(mockApexEventListener, never()).onApexEvent(any(ApexEvent.class)); + } + + @Test + void testOnEnEvent_multipleApexEvents() throws ApexException { + String eventName = "testEvent"; + when(mockEnEvent.getName()).thenReturn(eventName); + ApexEvent apexEvent1 = mock(ApexEvent.class); + ApexEvent apexEvent2 = mock(ApexEvent.class); + List<ApexEvent> apexEvents = Arrays.asList(apexEvent1, apexEvent2); + when(mockApexEnEventConverter.toApexEvent(eventName, mockEnEvent)).thenReturn(apexEvents); + + assertDoesNotThrow(() -> enEventListenerImpl.onEnEvent(mockEnEvent)); + + verify(mockApexEventListener, times(1)).onApexEvent(apexEvent1); + verify(mockApexEventListener, times(1)).onApexEvent(apexEvent2); + } +} |