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 PolicyDeployTrackerTest extends PolicyCommonSupport {
45 private PolicyTrackerData data;
47 private PolicyDeployTracker tracker;
50 * Creates various objects, including {@link #tracker}.
54 MockitoAnnotations.initMocks(this);
58 tracker = new PolicyDeployTracker();
63 tracker.addData(makeData(policy1, PDP1, PDP2));
65 // indicate that PDP2 has succeeded
66 tracker.processResponse(PDP2, Arrays.asList(policy1), new ArrayList<>(0));
68 // indicate that PDP1 has succeeded
69 List<PolicyStatus> statusList = new ArrayList<>();
70 tracker.processResponse(PDP1, Arrays.asList(policy1), 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 failed - should get a notification, if still in the map
79 tracker.processResponse(PDP1, Collections.emptyList(), statusList);
80 assertEquals(1, statusList.size());
81 assertEquals(policy1, statusList.get(0).getPolicy());
82 assertEquals(type, statusList.get(0).getPolicyType());
83 assertEquals("[1, 1, 0]", getCounts(statusList.get(0)).toString());
87 public void testUpdateData() {
88 // when success returns false
89 assertFalse(tracker.updateData(PDP1, data, true));
90 verify(data).success(PDP1);
91 verify(data, never()).fail(any());
94 assertFalse(tracker.updateData(PDP1, data, false));
95 verify(data).success(PDP1);
96 verify(data).fail(any());
98 // when success & fail return true
99 when(data.success(PDP1)).thenReturn(true);
100 when(data.fail(PDP1)).thenReturn(true);
101 assertTrue(tracker.updateData(PDP1, data, true));
102 verify(data, times(2)).success(PDP1);
103 verify(data, times(1)).fail(PDP1);
106 assertTrue(tracker.updateData(PDP1, data, false));
107 verify(data, times(2)).success(PDP1);
108 verify(data, times(2)).fail(PDP1);
112 public void testShouldRemove() {
113 // when data is complete, but not empty
114 when(data.isComplete()).thenReturn(true);
115 when(data.isEmpty()).thenReturn(false);
116 assertFalse(tracker.shouldRemove(data));
118 // when data is empty
119 when(data.isEmpty()).thenReturn(true);
120 assertTrue(tracker.shouldRemove(data));