Merge "S3P Stability and Performance Tests"
[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 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.Matchers.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.mockito.Mock;
31 import org.mockito.MockitoAnnotations;
32 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
33 import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClient;
34 import org.onap.policy.models.pdp.concepts.PdpStateChange;
35 import org.onap.policy.models.pdp.concepts.PdpStatus;
36 import org.onap.policy.pdpx.main.XacmlState;
37 import org.onap.policy.pdpx.main.comm.listeners.XacmlPdpStateChangeListener;
38
39 public class XacmlPdpStateChangeListenerTest {
40     private static final String TOPIC = "my-topic";
41
42     @Mock
43     private TopicSinkClient client;
44
45     @Mock
46     private XacmlState state;
47
48     @Mock
49     private PdpStatus status;
50
51     @Mock
52     private PdpStateChange change;
53
54     private XacmlPdpStateChangeListener listener;
55
56     /**
57      * Initializes objects, including the listener.
58      */
59     @Before
60     public void setUp() {
61         MockitoAnnotations.initMocks(this);
62
63         listener = new XacmlPdpStateChangeListener(client, state);
64
65         when(state.shouldHandle(change)).thenReturn(true);
66         when(state.updateInternalState(change)).thenReturn(status);
67
68         when(client.send(status)).thenReturn(true);
69     }
70
71     @Test
72     public void testOnTopicEvent_Unhandled() {
73         when(state.shouldHandle(change)).thenReturn(false);
74         listener.onTopicEvent(CommInfrastructure.NOOP, TOPIC, null, change);
75
76         verify(state, never()).updateInternalState(any(PdpStateChange.class));
77         verify(client, never()).send(any());
78     }
79
80     @Test
81     public void testOnTopicEvent_SendFailed() {
82         when(client.send(status)).thenReturn(false);
83
84         listener.onTopicEvent(CommInfrastructure.NOOP, TOPIC, null, change);
85
86         verify(state).updateInternalState(change);
87         verify(client).send(status);
88     }
89
90     @Test
91     public void testOnTopicEvent_SendOk() {
92         listener.onTopicEvent(CommInfrastructure.NOOP, TOPIC, null, change);
93
94         verify(state).updateInternalState(change);
95         verify(client).send(status);
96     }
97
98 }