832fddc9903bc834444baec1c38db4ad41333b00
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / notification / PolicyDeployTrackerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.pap.main.notification;
22
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;
31
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;
41
42 public class PolicyDeployTrackerTest extends PolicyCommonSupport {
43
44     @Mock
45     private PolicyTrackerData data;
46
47     private PolicyDeployTracker tracker;
48
49     /**
50      * Creates various objects, including {@link #tracker}.
51      */
52     @Before
53     public void setUp() {
54         MockitoAnnotations.initMocks(this);
55
56         super.setUp();
57
58         tracker = new PolicyDeployTracker();
59     }
60
61     @Test
62     public void test() {
63         tracker.addData(makeData(policy1, PDP1, PDP2));
64
65         // indicate that PDP2 has succeeded
66         tracker.processResponse(PDP2, Arrays.asList(policy1), new ArrayList<>(0));
67
68         // indicate that PDP1 has succeeded
69         List<PolicyStatus> statusList = new ArrayList<>();
70         tracker.processResponse(PDP1, Arrays.asList(policy1), statusList);
71
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());
76
77         // indicate that PDP1 has failed - should get a notification, if still in the map
78         statusList.clear();
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());
84     }
85
86     @Test
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());
92
93         // when inactive
94         assertFalse(tracker.updateData(PDP1, data, false));
95         verify(data).success(PDP1);
96         verify(data).fail(any());
97
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);
104
105         // when inactive
106         assertTrue(tracker.updateData(PDP1, data, false));
107         verify(data, times(2)).success(PDP1);
108         verify(data, times(2)).fail(PDP1);
109     }
110
111     @Test
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));
117
118         // when data is empty
119         when(data.isEmpty()).thenReturn(true);
120         assertTrue(tracker.shouldRemove(data));
121     }
122 }