Changes for new odl version
[appc.git] / appc-adapters / appc-ansible-adapter / appc-ansible-adapter-bundle / src / test / java / org / onap / appc / adapter / ansible / AnsibleActivatorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved.
6  * =============================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.appc.adapter.ansible;
23
24 import static org.mockito.BDDMockito.given;
25 import static org.mockito.BDDMockito.then;
26 import static org.mockito.Matchers.eq;
27 import static org.mockito.Matchers.isA;
28 import static org.mockito.Matchers.isNull;
29 import static org.mockito.Mockito.only;
30
31 import java.util.Dictionary;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.mockito.runners.MockitoJUnitRunner;
37 import org.onap.appc.adapter.ansible.impl.AnsibleAdapterImpl;
38 import org.osgi.framework.BundleContext;
39 import org.osgi.framework.ServiceRegistration;
40
41 @RunWith(MockitoJUnitRunner.class)
42
43 public class AnsibleActivatorTest {
44
45     @Mock
46     private ServiceRegistration serviceRegistration;
47     @Mock
48     private BundleContext bundleContext;
49
50     private AnsibleActivator ansibleActivator = new AnsibleActivator();
51
52     @Before
53     public void setUp() {
54         given(bundleContext.registerService(eq(AnsibleAdapter.class), isA(AnsibleAdapterImpl.class), isNull(
55             Dictionary.class))).willReturn(serviceRegistration);
56     }
57
58     @Test
59     public void start_shouldRegisterService_whenRegistrationOccursForTheFirstTime() throws Exception {
60         registerService();
61
62         then(bundleContext).should(only())
63             .registerService(eq(AnsibleAdapter.class), isA(AnsibleAdapterImpl.class), isNull(
64                 Dictionary.class));
65     }
66
67     @Test
68     public void start_shouldRegisterServiceOnlyOnce_whenServiceRegistrationIsNotNull() throws Exception {
69         // GIVEN
70         registerService();
71
72         // WHEN
73         registerService();
74
75         // THEN
76         then(bundleContext).should(only()).registerService(eq(AnsibleAdapter.class), isA(AnsibleAdapterImpl.class), isNull(
77             Dictionary.class));
78     }
79
80     @Test
81     public void stop_shouldUnregisterService_whenServiceRegistrationObjectIsNotNull() throws Exception {
82         // GIVEN
83         registerService();
84
85         // WHEN
86         unregisterService();
87
88         // THEN
89         then(serviceRegistration).should().unregister();
90     }
91
92     @Test
93     public void stop_shouldNotAttemptToUnregisterService_whenServiceHasAlreadyBeenUnregistered()
94         throws Exception {
95         // GIVEN
96         registerService();
97         unregisterService();
98
99         // WHEN
100         unregisterService();
101
102         // THEN
103         then(serviceRegistration).should(only()).unregister();
104     }
105
106     private void registerService() throws Exception {
107         ansibleActivator.start(bundleContext);
108     }
109
110     private void unregisterService() throws Exception {
111         ansibleActivator.stop(bundleContext);
112     }
113 }