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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pdpx.main.comm.listeners;
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;
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;
39 @RunWith(MockitoJUnitRunner.class)
40 public class XacmlPdpStateChangeListenerTest {
41 private static final String TOPIC = "my-topic";
44 private TopicSinkClient client;
47 private XacmlState state;
50 private PdpStatus status;
53 private PdpStateChange change;
55 private XacmlPdpStateChangeListener listener;
58 * Initializes objects, including the listener.
62 listener = new XacmlPdpStateChangeListener(client, state);
64 when(state.shouldHandle(change)).thenReturn(true);
65 when(state.updateInternalState(change)).thenReturn(status);
67 when(client.send(status)).thenReturn(true);
71 public void testOnTopicEvent_Unhandled() {
72 when(state.shouldHandle(change)).thenReturn(false);
73 listener.onTopicEvent(CommInfrastructure.NOOP, TOPIC, null, change);
75 verify(state, never()).updateInternalState(any(PdpStateChange.class));
76 verify(client, never()).send(any());
80 public void testOnTopicEvent_SendFailed() {
81 when(client.send(status)).thenReturn(false);
83 listener.onTopicEvent(CommInfrastructure.NOOP, TOPIC, null, change);
85 verify(state).updateInternalState(change);
86 verify(client).send(status);
90 public void testOnTopicEvent_SendOk() {
91 listener.onTopicEvent(CommInfrastructure.NOOP, TOPIC, null, change);
93 verify(state).updateInternalState(change);
94 verify(client).send(status);