2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.rest.util;
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
28 import org.junit.Test;
29 import org.mockito.Mockito;
30 import org.onap.policy.rest.util.PolicyContainer.ItemSetChangeEvent;
31 import org.onap.policy.rest.util.PolicyContainer.ItemSetChangeListener;
33 public class PolicyItemSetChangeNotifierTest {
35 public void testNotifier() {
38 PolicyItemSetChangeNotifier notifier = new PolicyItemSetChangeNotifier();
39 assertNotNull(notifier);
41 assertEquals(null, notifier.getItemSetChangeListeners());
43 ItemSetChangeListener listener1 = Mockito.mock(ItemSetChangeListener.class);
44 notifier.addItemSetChangeListener(listener1);
45 assertEquals(1, notifier.getItemSetChangeListeners().size());
46 ItemSetChangeListener listener2 = Mockito.mock(ItemSetChangeListener.class);
47 notifier.addItemSetChangeListener(listener2);
48 assertEquals(2, notifier.getItemSetChangeListeners().size());
50 notifier.removeItemSetChangeListener(listener1);
51 assertEquals(1, notifier.getItemSetChangeListeners().size());
52 notifier.addItemSetChangeListener(listener1);
53 assertEquals(2, notifier.getItemSetChangeListeners().size());
55 ItemSetChangeEvent event = Mockito.mock(ItemSetChangeEvent.class);
56 notifier.fireItemSetChange(event);
58 notifier.removeItemSetChangeListener(listener1);
59 assertEquals(1, notifier.getItemSetChangeListeners().size());
60 notifier.removeItemSetChangeListener(listener2);
61 assertEquals(0, notifier.getItemSetChangeListeners().size());
62 notifier.removeItemSetChangeListener(listener2);
63 assertEquals(0, notifier.getItemSetChangeListeners().size());
65 notifier.setItemSetChangeListeners(null);
66 notifier.removeItemSetChangeListener(listener2);
67 assertEquals(null, notifier.getItemSetChangeListeners());
69 assertThatCode(() -> notifier.fireItemSetChange(event)).doesNotThrowAnyException();
71 notifier.setContainer(new DummyPolicyContainer());
72 assertThatCode(() -> notifier.fireItemSetChange()).doesNotThrowAnyException();
74 PolicyContainer dummySource = new DummyPolicyContainer();
75 assertEquals(dummySource, new PolicyItemSetChangeNotifier.BaseItemSetChangeEvent(dummySource).getContainer());