-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Increase apex-pdp line and branch coverage
Issue-ID: POLICY-5059 Change-Id: Ifa1472dd914ec0ef25372fa057514571a7ea27ac Signed-off-by: waynedunican <[email protected]>
- Loading branch information
1 parent
bc949ba
commit f906185
Showing
7 changed files
with
297 additions
and
60 deletions.
There are no files selected for viewing
97 changes: 97 additions & 0 deletions
97
...java/org/onap/policy/apex/plugins/context/locking/hazelcast/HazelcastLockManagerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* ============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.plugins.context.locking.hazelcast; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.mockStatic; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.hazelcast.core.Hazelcast; | ||
import com.hazelcast.core.HazelcastInstance; | ||
import com.hazelcast.core.LifecycleService; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.MockedStatic; | ||
import org.onap.policy.apex.context.ContextException; | ||
import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; | ||
|
||
class HazelcastLockManagerTest { | ||
|
||
private HazelcastLockManager lockManager; | ||
private HazelcastInstance mockHazelcastInstance; | ||
private LifecycleService mockLifecycleService; | ||
private MockedStatic<Hazelcast> mockedHazelcast; | ||
|
||
@BeforeEach | ||
void setUp() throws ContextException { | ||
mockHazelcastInstance = mock(HazelcastInstance.class); | ||
mockLifecycleService = mock(LifecycleService.class); | ||
|
||
mockedHazelcast = mockStatic(Hazelcast.class); | ||
mockedHazelcast.when(Hazelcast::newHazelcastInstance).thenReturn(mockHazelcastInstance); | ||
|
||
when(mockHazelcastInstance.getLifecycleService()).thenReturn(mockLifecycleService); | ||
when(mockLifecycleService.isRunning()).thenReturn(true); | ||
|
||
lockManager = new HazelcastLockManager(); | ||
} | ||
|
||
@AfterEach | ||
void tearDown() { | ||
mockedHazelcast.close(); | ||
} | ||
|
||
@Test | ||
void testInit() { | ||
AxArtifactKey testKey = new AxArtifactKey("TestKey", "1.0"); | ||
|
||
assertDoesNotThrow(() -> lockManager.init(testKey)); | ||
|
||
mockedHazelcast.verify(Hazelcast::newHazelcastInstance); | ||
} | ||
|
||
@Test | ||
void testGetReentrantReadWriteLockWhenHazelcastNotRunning() { | ||
when(mockLifecycleService.isRunning()).thenReturn(false); | ||
|
||
String lockId = "testLock"; | ||
assertThrows(ContextException.class, () -> lockManager.getReentrantReadWriteLock(lockId)); | ||
} | ||
|
||
@Test | ||
void testShutdown() throws ContextException { | ||
lockManager.init(new AxArtifactKey("TestKey", "1.0")); | ||
assertDoesNotThrow(() -> lockManager.shutdown()); | ||
|
||
verify(mockHazelcastInstance).shutdown(); | ||
} | ||
|
||
@Test | ||
void testShutdownWithoutInit() { | ||
assertDoesNotThrow(() -> lockManager.shutdown()); | ||
verify(mockHazelcastInstance, never()).shutdown(); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
...c/test/java/org/onap/policy/apex/plugins/context/locking/hazelcast/HazelcastLockTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* ============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.plugins.context.locking.hazelcast; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.hazelcast.core.HazelcastInstance; | ||
import com.hazelcast.cp.CPSubsystem; | ||
import com.hazelcast.cp.lock.FencedLock; | ||
import java.util.concurrent.locks.Lock; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class HazelcastLockTest { | ||
|
||
private HazelcastInstance hazelcastInstance; | ||
private CPSubsystem cpSubsystem; | ||
private FencedLock mockReadLock; | ||
private FencedLock mockWriteLock; | ||
|
||
private HazelcastLock hazelcastLock; | ||
private final String lockId = "testLock"; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
hazelcastInstance = mock(HazelcastInstance.class); | ||
cpSubsystem = mock(CPSubsystem.class); | ||
mockReadLock = mock(FencedLock.class); | ||
mockWriteLock = mock(FencedLock.class); | ||
|
||
when(hazelcastInstance.getCPSubsystem()).thenReturn(cpSubsystem); | ||
when(cpSubsystem.getLock(lockId + "_READ")).thenReturn(mockReadLock); | ||
when(cpSubsystem.getLock(lockId + "_WRITE")).thenReturn(mockWriteLock); | ||
|
||
hazelcastLock = new HazelcastLock(hazelcastInstance, lockId); | ||
} | ||
|
||
@Test | ||
void testConstructor() { | ||
assertEquals(lockId, hazelcastLock.getLockId()); | ||
|
||
verify(cpSubsystem).getLock(lockId + "_READ"); | ||
verify(cpSubsystem).getLock(lockId + "_WRITE"); | ||
} | ||
|
||
@Test | ||
void testReadLock() { | ||
Lock readLock = hazelcastLock.readLock(); | ||
assertEquals(mockReadLock, readLock); | ||
} | ||
|
||
@Test | ||
void testWriteLock() { | ||
Lock writeLock = hazelcastLock.writeLock(); | ||
assertEquals(mockWriteLock, writeLock); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
105 changes: 105 additions & 0 deletions
105
...c/test/java/org/onap/policy/apex/service/engine/runtime/impl/EnEventListenerImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
Oops, something went wrong.