* ONAP PAP
* ================================================================================
* Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2020-2021,2023 Nordix Foundation.
+ * Modifications Copyright (C) 2020-2021, 2023 Nordix Foundation.
* Modifications Copyright (C) 2022 Bell Canada. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertSame;
-import static org.junit.Assert.assertTrue;
+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.assertSame;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
+import jakarta.ws.rs.core.Response.Status;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
+import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
-import javax.ws.rs.core.Response.Status;
import org.assertj.core.api.Assertions;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
+import org.mockito.MockitoAnnotations;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.base.PfModelRuntimeException;
import org.onap.policy.models.pdp.concepts.Pdp;
import org.onap.policy.pap.main.service.PolicyStatusService;
import org.springframework.test.util.ReflectionTestUtils;
-@RunWith(MockitoJUnitRunner.class)
-public class PdpModifyRequestMapTest extends CommonRequestBase {
+class PdpModifyRequestMapTest extends CommonRequestBase {
private static final String MY_REASON = "my reason";
private static final int EXPIRED_SECONDS = 100;
private PdpStateChange change;
private PdpStatus response;
+ AutoCloseable autoCloseable;
+
/**
* Sets up.
*
* @throws Exception if an error occurs
*/
- @Before
+ @BeforeEach
@Override
public void setUp() throws Exception {
super.setUp();
+ autoCloseable = MockitoAnnotations.openMocks(this);
response = new PdpStatus();
map = new MyMap(mapParams);
}
+ @AfterEach
+ void tearDown() throws Exception {
+ autoCloseable.close();
+ }
+
@Test
- public void testPdpModifyRequestMap() {
+ void testPdpModifyRequestMap() {
assertSame(mapParams, ReflectionTestUtils.getField(map, "params"));
assertSame(lock, ReflectionTestUtils.getField(map, "modifyLock"));
}
@Test
- public void testIsEmpty() {
+ void testIsEmpty() {
assertTrue(map.isEmpty());
map.addRequest(change);
}
@Test
- public void testStopPublishing() {
+ void testStopPublishing() {
// try with non-existent PDP
map.stopPublishing(PDP1);
}
@Test
- public void testAddRequestPdpUpdatePdpStateChange_BothNull() {
+ void testAddRequestPdpUpdatePdpStateChange_BothNull() {
// nulls should be ok
Assertions.assertThatCode(() -> map.addRequest(null, null)).doesNotThrowAnyException();
}
@Test
- public void testAddRequestPdpUpdatePdpStateChange_NullUpdate() {
+ void testAddRequestPdpUpdatePdpStateChange_NullUpdate() {
map.addRequest(null, change);
Request req = getSingletons(1).get(0);
}
@Test
- public void testAddRequestPdpUpdatePdpStateChange_NullStateChange() {
+ void testAddRequestPdpUpdatePdpStateChange_NullStateChange() {
map.addRequest(update, null);
Request req = getSingletons(1).get(0);
* message.
*/
@Test
- public void testAddRequestPdpUpdatePdpStateChange_BothProvided_Active() {
+ void testAddRequestPdpUpdatePdpStateChange_BothProvided_Active() {
change.setState(PdpState.ACTIVE);
map.addRequest(update, change);
* message.
*/
@Test
- public void testAddRequestPdpUpdatePdpStateChange_BothProvided_Passive() {
+ void testAddRequestPdpUpdatePdpStateChange_BothProvided_Passive() {
change.setState(PdpState.PASSIVE);
map.addRequest(update, change);
}
@Test
- public void testAddRequestPdpUpdatePdpStateChange() {
+ void testAddRequestPdpUpdatePdpStateChange() {
// null should be ok
map.addRequest(null, null);
// broadcast should throw an exception
change.setName(null);
assertThatIllegalArgumentException().isThrownBy(() -> map.addRequest(change))
- .withMessageStartingWith("unexpected broadcast message: PdpStateChange");
+ .withMessageStartingWith("unexpected broadcast message: PdpStateChange");
}
@Test
- public void testAddRequestPdpUpdate() {
+ void testAddRequestPdpUpdate() {
// null should be ok
map.addRequest((PdpUpdate) null);
// broadcast should throw an exception
update.setName(null);
assertThatIllegalArgumentException().isThrownBy(() -> map.addRequest(update))
- .withMessageStartingWith("unexpected broadcast message: PdpUpdate");
+ .withMessageStartingWith("unexpected broadcast message: PdpUpdate");
}
@Test
- public void testAddRequestPdpStateChange() {
+ void testAddRequestPdpStateChange() {
// null should be ok
map.addRequest((PdpStateChange) null);
// broadcast should throw an exception
change.setName(null);
assertThatIllegalArgumentException().isThrownBy(() -> map.addRequest(change))
- .withMessageStartingWith("unexpected broadcast message: PdpStateChange");
+ .withMessageStartingWith("unexpected broadcast message: PdpStateChange");
}
@Test
- public void testAddSingleton() {
+ void testAddSingleton() {
map.addRequest(change);
assertEquals(1, map.nalloc);
}
@Test
- public void testStartNextRequest_NoMore() {
+ void testStartNextRequest_NoMore() {
map.addRequest(change);
// indicate success
}
@Test
- public void testStartNextRequest_HaveMore() {
+ void testStartNextRequest_HaveMore() {
map.addRequest(update);
map.addRequest(change);
}
@Test
- public void testRemoveExpiredPdps() throws Exception {
+ void testRemoveExpiredPdps() {
PdpGroup group1 = makeGroup(MY_GROUP);
group1.setPdpSubgroups(List.of(makeSubGroup(MY_SUBGROUP, PDP1)));
}
@Test
- public void testRemoveExpiredPdps_NothingExpired() throws Exception {
+ void testRemoveExpiredPdps_NothingExpired() {
PdpGroup group1 = makeGroup(MY_GROUP);
group1.setPdpSubgroups(List.of(makeSubGroup(MY_SUBGROUP, PDP1)));
}
@Test
- public void testRemoveExpiredPdps_DaoEx() throws Exception {
+ void testRemoveExpiredPdps_DaoEx() {
when(pdpGroupService.getFilteredPdpGroups(any())).thenThrow(makeRuntimeException());
assertThatCode(map::removeExpiredPdps).doesNotThrowAnyException();
}
@Test
- public void testRemoveExpiredPdps_DaoRtEx() throws Exception {
+ void testRemoveExpiredPdps_DaoRtEx() {
when(pdpGroupService.getFilteredPdpGroups(any())).thenThrow(makeRuntimeException());
assertThatCode(map::removeExpiredPdps).doesNotThrowAnyException();
}
@Test
- public void testRemoveFromSubgroup() throws Exception {
+ void testRemoveFromSubgroup() {
PdpGroup group = makeGroup(MY_GROUP);
group.setPdpSubgroups(List.of(makeSubGroup(MY_SUBGROUP, PDP1, PDP2, PDP3)));
}
@Test
- public void testMakePdpRequests() {
+ void testMakePdpRequests() {
// this should invoke the real method without throwing an exception
PdpModifyRequestMap reqMap =
new PdpModifyRequestMap(pdpGroupService, policyStatusService, responseHandler, undeployer, notifier);
}
@Test
- public void testSingletonListenerFailure() throws Exception {
+ void testSingletonListenerFailure() throws Exception {
map.addRequest(change);
// invoke the method
- invokeFailureHandler(1);
+ invokeFailureHandler();
verify(undeployer, never()).undeploy(any(), any(), any());
verify(requests, never()).stopPublishing();
* Tests Listener.failure() when something has to be undeployed.
*/
@Test
- public void testSingletonListenerFailureUndeploy() throws Exception {
+ void testSingletonListenerFailureUndeploy() throws Exception {
ToscaConceptIdentifier ident = new ToscaConceptIdentifier("undeployed", "2.3.4");
ToscaPolicy policy = mock(ToscaPolicy.class);
when(policy.getIdentifier()).thenReturn(ident);
// add some policies to the update
- update.setPoliciesToBeDeployed(Arrays.asList(policy));
+ update.setPoliciesToBeDeployed(List.of(policy));
map.addRequest(update);
doAnswer(ans -> {
PdpUpdate update2 = new PdpUpdate(update);
update2.setPoliciesToBeDeployed(Collections.emptyList());
- update2.setPoliciesToBeUndeployed(Arrays.asList(policy.getIdentifier()));
+ update2.setPoliciesToBeUndeployed(List.of(policy.getIdentifier()));
assertTrue(req.reconfigure(update2));
throw makeException();
}).when(undeployer).undeploy(any(), any(), any());
req.checkResponse(response);
// invoke the method
- invokeFailureHandler(1);
+ invokeFailureHandler();
verify(undeployer).undeploy(eq(MY_GROUP), eq(MY_SUBGROUP), undeployCaptor.capture());
- assertEquals(Arrays.asList(ident).toString(), undeployCaptor.getValue().toString());
+ assertEquals(List.of(ident).toString(), undeployCaptor.getValue().toString());
// no effect on the map
map.addRequest(update);
* remains unchanged.
*/
@Test
- public void testSingletonListenerFailureUndeployMessageUnchanged() throws Exception {
+ void testSingletonListenerFailureUndeployMessageUnchanged() throws Exception {
ToscaConceptIdentifier ident = new ToscaConceptIdentifier("msg-unchanged", "8.7.6");
ToscaPolicy policy = mock(ToscaPolicy.class);
when(policy.getIdentifier()).thenReturn(ident);
// add some policies to the update
- update.setPoliciesToBeDeployed(Arrays.asList(policy));
+ update.setPoliciesToBeDeployed(List.of(policy));
map.addRequest(update);
req.checkResponse(response);
// invoke the method
- invokeFailureHandler(1);
+ invokeFailureHandler();
verify(undeployer).undeploy(eq(MY_GROUP), eq(MY_SUBGROUP), undeployCaptor.capture());
- assertEquals(Arrays.asList(ident).toString(), undeployCaptor.getValue().toString());
+ assertEquals(List.of(ident).toString(), undeployCaptor.getValue().toString());
// requests should have been removed from the map so this should allocate another
map.addRequest(update);
}
@Test
- public void testSingletonListenerSuccess() throws Exception {
+ void testSingletonListenerSuccess() {
map.addRequest(change);
// invoke the method
- invokeSuccessHandler(1);
+ invokeSuccessHandler();
verify(requests, never()).stopPublishing();
}
@Test
- public void testRequestCompleted_LastRequest() throws Exception {
+ void testRequestCompleted_LastRequest() {
map.addRequest(change);
// invoke the method
- invokeSuccessHandler(1);
+ invokeSuccessHandler();
verify(requests, never()).stopPublishing();
}
@Test
- public void testRequestCompleted_NameMismatch() throws Exception {
+ void testRequestCompleted_NameMismatch() {
// use a different name
when(requests.getPdpName()).thenReturn(DIFFERENT);
// put the PDP in a group
PdpGroup group = makeGroup(MY_GROUP);
- group.setPdpSubgroups(Arrays.asList(makeSubGroup(MY_SUBGROUP, PDP1, DIFFERENT)));
+ group.setPdpSubgroups(List.of(makeSubGroup(MY_SUBGROUP, PDP1, DIFFERENT)));
// invoke the method - with a different name (i.e., PDP1 instead of DIFFERENT)
- invokeSuccessHandler(1);
+ invokeSuccessHandler();
verify(requests, never()).stopPublishing();
}
@Test
- public void testRequestCompleted_AlreadyStopped() throws Exception {
+ void testRequestCompleted_AlreadyStopped() {
map.addRequest(change);
map.stopPublishing(PDP1);
// invoke the method
- invokeSuccessHandler(1);
+ invokeSuccessHandler();
// should have called this a second time
verify(requests, times(2)).stopPublishing();
}
@Test
- public void testRequestCompleted_NotFirstInQueue() throws Exception {
+ void testRequestCompleted_NotFirstInQueue() {
map.addRequest(change);
when(requests.isFirstInQueue(any())).thenReturn(false);
// invoke the method
- invokeSuccessHandler(1);
+ invokeSuccessHandler();
// should not have called this
verify(requests, never()).stopPublishing();
}
@Test
- public void testSingletonListenerRetryCountExhausted() throws Exception {
+ void testSingletonListenerRetryCountExhausted() {
final var request = map.addRequest(change);
// invoke the method
- invokeLastRetryHandler(1, request);
+ invokeLastRetryHandler(request);
verify(requests).stopPublishing();
}
/**
* Invokes the first request's listener.success() method.
- *
- * @param count expected number of requests
*/
- private void invokeSuccessHandler(int count) {
- getListener(getSingletons(count).get(0)).success(PDP1, response);
+ private void invokeSuccessHandler() {
+ getListener(getSingletons(1).get(0)).success(PDP1, response);
}
/**
* Invokes the first request's listener.failure() method.
- *
- * @param count expected number of requests
*/
- private void invokeFailureHandler(int count) {
- getListener(getSingletons(count).get(0)).failure(PDP1, MY_REASON);
+ private void invokeFailureHandler() {
+ getListener(getSingletons(1).get(0)).failure(PDP1, MY_REASON);
}
/**
* Invokes the first request's listener.retryCountExhausted() method.
*
- * @param count expected number of requests
* @param request request whose count was exhausted
*/
- private void invokeLastRetryHandler(int count, Request request) {
- getListener(getSingletons(count).get(0)).retryCountExhausted(request);
+ private void invokeLastRetryHandler(Request request) {
+ getListener(getSingletons(1).get(0)).retryCountExhausted(request);
}
/**
subgroup.setPdpType(pdpType);
subgroup.setCurrentInstanceCount(pdpNames.length);
- subgroup.setPdpInstances(Arrays.asList(pdpNames).stream().map(this::makePdp).collect(Collectors.toList()));
+ subgroup.setPdpInstances(Arrays.stream(pdpNames).map(this::makePdp).collect(Collectors.toList()));
return subgroup;
}
* Gets the input to the method.
*
* @return the input that was passed to the dao.updatePdpGroups() method
- * @throws Exception if an error occurred
*/
- private List<PdpGroup> getGroupUpdates() throws Exception {
+ private List<PdpGroup> getGroupUpdates() {
verify(pdpGroupService).updatePdpGroups(updateCaptor.capture());
return copyList(updateCaptor.getValue());
*/
private List<PdpGroup> copyList(List<PdpGroup> source) {
List<PdpGroup> newlst = new ArrayList<>(source);
- Collections.sort(newlst, (left, right) -> left.getName().compareTo(right.getName()));
+ newlst.sort(Comparator.comparing(PdpGroup::getName));
return newlst;
}
public MyMap(PdpModifyRequestMapParams params) {
super(pdpGroupService, policyStatusService, responseHandler, undeployer, notifier);
- super.initialize(params);;
+ super.initialize(params);
+ ;
}
@Override