aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/test/java/org/onap/policy/pap/main/comm/PdpTrackerTest.java
blob: 2b1e8f1ef0ea120b0ca0bbaf8a5d43b846e48adb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*-
 * ============LICENSE_START=======================================================
 * ONAP PAP
 * ================================================================================
 * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * 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.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.pap.main.comm;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.pdp.concepts.PdpGroup;
import org.onap.policy.models.pdp.concepts.PdpGroups;
import org.onap.policy.models.provider.PolicyModelsProvider;
import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper;
import org.onap.policy.pap.main.PolicyPapRuntimeException;
import org.onap.policy.pap.main.comm.TimerManager.Timer;

public class PdpTrackerTest {
    private static final String PDP1 = "pdp1";
    private static final String PDP2 = "pdp2";

    private PdpTracker tracker;
    private PdpTracker.PdpTrackerBuilder builder;

    private Object modifyLock;

    @Captor
    private ArgumentCaptor<Consumer<String>> handlerCaptor;

    @Mock
    private PdpModifyRequestMap requestMap;

    @Mock
    private TimerManager timers;

    @Mock
    private PolicyModelsProviderFactoryWrapper daoFactory;

    @Mock
    private PolicyModelsProvider dao;

    @Mock
    private Timer timer1;

    @Mock
    private Timer timer2;

    /**
     * Sets up.
     *
     * @throws Exception if an error occurs
     */
    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);

        modifyLock = new Object();

        builder = PdpTracker.builder().daoFactory(daoFactory).modifyLock(modifyLock).requestMap(requestMap)
                        .timers(timers);

        when(daoFactory.create()).thenReturn(dao);

        when(dao.getPdpGroups(null)).thenReturn(Collections.emptyList());

        when(timers.register(eq(PDP1), any())).thenReturn(timer1);
        when(timers.register(eq(PDP2), any())).thenReturn(timer2);

        tracker = builder.build();
    }

    @Test
    public void testPdpTracker() throws Exception {
        // verify that PDPs were loaded
        verify(dao).getPdpGroups(null);
    }

    @Test
    public void testBuilderToString() throws Exception {
        assertNotNull(builder.toString());
    }

    @Test
    public void testPdpTracker_MissingRequestMap() throws Exception {
        assertThatThrownBy(() -> builder.requestMap(null)).isInstanceOf(NullPointerException.class);
    }

    @Test
    public void testPdpTracker_MissingModifyLock() throws Exception {
        assertThatThrownBy(() -> builder.modifyLock(null)).isInstanceOf(NullPointerException.class);
    }

    @Test
    public void testPdpTracker_MissingTimers() throws Exception {
        assertThatThrownBy(() -> builder.timers(null)).isInstanceOf(NullPointerException.class);
    }

    @Test
    public void testPdpTracker_MissingDaoFactory() throws Exception {
        assertThatThrownBy(() -> builder.daoFactory(null)).isInstanceOf(NullPointerException.class);
    }

    @Test
    public void testLoadPdps_testLoadPdpsFromGroup() throws Exception {
        // arrange for DAO to return a couple of groups
        String groupsJson = ResourceUtils.getResourceAsString("comm/PdpTracker.json");
        List<PdpGroup> groups = new StandardCoder().decode(groupsJson, PdpGroups.class).getGroups();
        when(dao.getPdpGroups(null)).thenReturn(groups);

        tracker = builder.build();

        // verify that all PDPs were registered
        verify(timers).register(eq("pdp-A"), any());
        verify(timers).register(eq("pdp-B"), any());
        verify(timers).register(eq("pdp-C"), any());
        verify(timers).register(eq("pdp-D"), any());
    }

    @Test
    public void testLoadPdps_DaoException() throws Exception {
        // arrange for DAO to throw an exception
        PfModelException ex = mock(PfModelException.class);
        when(daoFactory.create()).thenThrow(ex);

        assertThatThrownBy(() -> builder.build()).isInstanceOf(PolicyPapRuntimeException.class).hasCause(ex);
    }

    @Test
    public void testAdd() {
        tracker.add(PDP1);
        verify(timers).register(eq(PDP1), any());
        verify(timer1, never()).cancel();

        tracker.add(PDP2);
        verify(timers).register(eq(PDP2), any());
        verify(timer1, never()).cancel();
        verify(timer2, never()).cancel();

        // re-add PDP1 - old timer should be canceled and a new timer added
        Timer timer3 = mock(Timer.class);
        when(timers.register(eq(PDP1), any())).thenReturn(timer3);
        tracker.add(PDP1);
        verify(timer1).cancel();
        verify(timer2, never()).cancel();
        verify(timer3, never()).cancel();
    }

    @Test
    public void testHandleTimeout() throws Exception {
        tracker.add(PDP1);
        tracker.add(PDP2);

        verify(timers).register(eq(PDP1), handlerCaptor.capture());

        handlerCaptor.getValue().accept(PDP1);

        verify(requestMap).removeFromGroups(PDP1);

        // now we'll re-add PDP1 - the original timer should not be canceled
        Timer timer3 = mock(Timer.class);
        when(timers.register(eq(PDP1), any())).thenReturn(timer3);
        tracker.add(PDP1);
        verify(timer1, never()).cancel();
    }

    @Test
    public void testHandleTimeout_MapException() throws Exception {
        tracker.add(PDP1);

        verify(timers).register(eq(PDP1), handlerCaptor.capture());

        // arrange for request map to throw an exception
        PfModelException ex = mock(PfModelException.class);
        when(requestMap.removeFromGroups(PDP1)).thenThrow(ex);

        // exception should be caught, but not re-thrown
        handlerCaptor.getValue().accept(PDP1);
    }
}