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
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.Matchers.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.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;
39 public class XacmlPdpStateChangeListenerTest {
40 private static final String TOPIC = "my-topic";
43 private TopicSinkClient client;
46 private XacmlState state;
49 private PdpStatus status;
52 private PdpStateChange change;
54 private XacmlPdpStateChangeListener listener;
57 * Initializes objects, including the listener.
61 MockitoAnnotations.initMocks(this);
63 listener = new XacmlPdpStateChangeListener(client, state);
65 when(state.shouldHandle(change)).thenReturn(true);
66 when(state.updateInternalState(change)).thenReturn(status);
68 when(client.send(status)).thenReturn(true);
72 public void testOnTopicEvent_Unhandled() {
73 when(state.shouldHandle(change)).thenReturn(false);
74 listener.onTopicEvent(CommInfrastructure.NOOP, TOPIC, null, change);
76 verify(state, never()).updateInternalState(any(PdpStateChange.class));
77 verify(client, never()).send(any());
81 public void testOnTopicEvent_SendFailed() {
82 when(client.send(status)).thenReturn(false);
84 listener.onTopicEvent(CommInfrastructure.NOOP, TOPIC, null, change);
86 verify(state).updateInternalState(change);
87 verify(client).send(status);
91 public void testOnTopicEvent_SendOk() {
92 listener.onTopicEvent(CommInfrastructure.NOOP, TOPIC, null, change);
94 verify(state).updateInternalState(change);
95 verify(client).send(status);