2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pap.main.notification;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.Matchers.any;
27 import static org.mockito.Mockito.never;
28 import static org.mockito.Mockito.times;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.Collections;
35 import java.util.List;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.Mock;
39 import org.mockito.MockitoAnnotations;
40 import org.onap.policy.models.pap.concepts.PolicyStatus;
42 public class PolicyUndeployTrackerTest extends PolicyCommonSupport {
45 private PolicyTrackerData data;
47 private PolicyUndeployTracker tracker;
50 * Creates various objects, including {@link #tracker}.
54 MockitoAnnotations.initMocks(this);
58 tracker = new PolicyUndeployTracker();
63 tracker.addData(makeData(policy1, PDP1, PDP2));
65 // indicate that PDP2 has been undeployed
66 tracker.processResponse(PDP2, Collections.emptyList(), new ArrayList<>(0));
68 // indicate that PDP1 has been undeployed
69 List<PolicyStatus> statusList = new ArrayList<>();
70 tracker.processResponse(PDP1, Collections.emptyList(), statusList);
72 assertEquals(1, statusList.size());
73 assertEquals(policy1, statusList.get(0).getPolicy());
74 assertEquals(type, statusList.get(0).getPolicyType());
75 assertEquals("[2, 0, 0]", getCounts(statusList.get(0)).toString());
77 // indicate that PDP1 has been re-deployed - should not get a notification,
81 tracker.processResponse(PDP1, Arrays.asList(policy1), statusList);
82 assertTrue(statusList.isEmpty());
86 public void testUpdateData() {
87 // when success returns false
88 assertFalse(tracker.updateData(PDP1, data, false));
89 verify(data).success(PDP1);
90 verify(data, never()).fail(any());
93 assertFalse(tracker.updateData(PDP1, data, true));
94 verify(data).success(PDP1);
95 verify(data).fail(any());
97 // when success & fail return true
98 when(data.success(PDP1)).thenReturn(true);
99 when(data.fail(PDP1)).thenReturn(true);
100 assertTrue(tracker.updateData(PDP1, data, false));
101 verify(data, times(2)).success(PDP1);
102 verify(data, times(1)).fail(PDP1);
105 assertTrue(tracker.updateData(PDP1, data, true));
106 verify(data, times(2)).success(PDP1);
107 verify(data, times(2)).fail(PDP1);
111 public void testShouldRemove() {
112 // when data is not complete
113 assertFalse(tracker.shouldRemove(data));
115 // when data is complete
116 when(data.isComplete()).thenReturn(true);
117 assertTrue(tracker.shouldRemove(data));