Fix sonars from dependency upgrade
[policy/xacml-pdp.git] / main / src / test / java / org / onap / policy / pdpx / main / comm / listeners / XacmlPdpStateChangeListenerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pdpx.main.comm.listeners;
22
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.Mockito.never;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Mock;
32 import org.mockito.junit.MockitoJUnitRunner;
33 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
34 import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClient;
35 import org.onap.policy.models.pdp.concepts.PdpStateChange;
36 import org.onap.policy.models.pdp.concepts.PdpStatus;
37 import org.onap.policy.pdpx.main.XacmlState;
38
39 @RunWith(MockitoJUnitRunner.class)
40 public class XacmlPdpStateChangeListenerTest {
41     private static final String TOPIC = "my-topic";
42
43     @Mock
44     private TopicSinkClient client;
45
46     @Mock
47     private XacmlState state;
48
49     @Mock
50     private PdpStatus status;
51
52     @Mock
53     private PdpStateChange change;
54
55     private XacmlPdpStateChangeListener listener;
56
57     /**
58      * Initializes objects, including the listener.
59      */
60     @Before
61     public void setUp() {
62         listener = new XacmlPdpStateChangeListener(client, state);
63
64         when(state.shouldHandle(change)).thenReturn(true);
65         when(state.updateInternalState(change)).thenReturn(status);
66
67         when(client.send(status)).thenReturn(true);
68     }
69
70     @Test
71     public void testOnTopicEvent_Unhandled() {
72         when(state.shouldHandle(change)).thenReturn(false);
73         listener.onTopicEvent(CommInfrastructure.NOOP, TOPIC, null, change);
74
75         verify(state, never()).updateInternalState(any(PdpStateChange.class));
76         verify(client, never()).send(any());
77     }
78
79     @Test
80     public void testOnTopicEvent_SendFailed() {
81         when(client.send(status)).thenReturn(false);
82
83         listener.onTopicEvent(CommInfrastructure.NOOP, TOPIC, null, change);
84
85         verify(state).updateInternalState(change);
86         verify(client).send(status);
87     }
88
89     @Test
90     public void testOnTopicEvent_SendOk() {
91         listener.onTopicEvent(CommInfrastructure.NOOP, TOPIC, null, change);
92
93         verify(state).updateInternalState(change);
94         verify(client).send(status);
95     }
96
97 }