Unit/SONAR/Checkstyle in ONAP-REST
[policy/engine.git] / ONAP-REST / src / test / java / org / onap / policy / rest / util / PolicyItemSetChangeNotifierTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.rest.util;
23
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27
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;
32
33 public class PolicyItemSetChangeNotifierTest {
34     @Test
35     public void testNotifier() {
36
37         // Test constructor
38         PolicyItemSetChangeNotifier notifier = new PolicyItemSetChangeNotifier();
39         assertNotNull(notifier);
40
41         assertEquals(null, notifier.getItemSetChangeListeners());
42
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());
49
50         notifier.removeItemSetChangeListener(listener1);
51         assertEquals(1, notifier.getItemSetChangeListeners().size());
52         notifier.addItemSetChangeListener(listener1);
53         assertEquals(2, notifier.getItemSetChangeListeners().size());
54
55         ItemSetChangeEvent event = Mockito.mock(ItemSetChangeEvent.class);
56         notifier.fireItemSetChange(event);
57
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());
64
65         notifier.setItemSetChangeListeners(null);
66         notifier.removeItemSetChangeListener(listener2);
67         assertEquals(null, notifier.getItemSetChangeListeners());
68
69         assertThatCode(() -> notifier.fireItemSetChange(event)).doesNotThrowAnyException();
70
71         notifier.setContainer(new DummyPolicyContainer());
72         assertThatCode(() -> notifier.fireItemSetChange()).doesNotThrowAnyException();
73
74         PolicyContainer dummySource = new DummyPolicyContainer();
75         assertEquals(dummySource, new PolicyItemSetChangeNotifier.BaseItemSetChangeEvent(dummySource).getContainer());
76     }
77 }