Unit Test for ansibleActivator
[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 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.appc.adapter.ansible;
24
25 import static org.mockito.BDDMockito.given;
26 import static org.mockito.BDDMockito.then;
27 import static org.mockito.Matchers.eq;
28 import static org.mockito.Matchers.isA;
29 import static org.mockito.Matchers.isNull;
30 import static org.mockito.Mockito.only;
31
32 import java.util.Dictionary;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.runners.MockitoJUnitRunner;
38 import org.onap.appc.adapter.ansible.impl.AnsibleAdapterImpl;
39 import org.osgi.framework.BundleContext;
40 import org.osgi.framework.ServiceRegistration;
41
42 @RunWith(MockitoJUnitRunner.class)
43
44 public class AnsibleActivatorTest {
45
46     @Mock
47     private ServiceRegistration<AnsibleAdapter> serviceRegistration;
48     @Mock
49     private BundleContext bundleContext;
50
51     private AnsibleActivator ansibleActivator = new AnsibleActivator();
52
53     @Before
54     public void setUp() {
55         given(bundleContext.registerService(eq(AnsibleAdapter.class), isA(AnsibleAdapterImpl.class), isNull(
56             Dictionary.class))).willReturn(serviceRegistration);
57     }
58
59     @Test
60     public void start_shouldRegisterService_whenRegistrationOccursForTheFirstTime() throws Exception {
61         registerService();
62
63         then(bundleContext).should(only())
64             .registerService(eq(AnsibleAdapter.class), isA(AnsibleAdapterImpl.class), isNull(
65                 Dictionary.class));
66     }
67
68     @Test
69     public void start_shouldRegisterServiceOnlyOnce_whenServiceRegistrationIsNotNull() throws Exception {
70         // GIVEN
71         registerService();
72
73         // WHEN
74         registerService();
75
76         // THEN
77         then(bundleContext).should(only()).registerService(eq(AnsibleAdapter.class), isA(AnsibleAdapterImpl.class), isNull(
78             Dictionary.class));
79     }
80
81     @Test
82     public void stop_shouldUnregisterService_whenServiceRegistrationObjectIsNotNull() throws Exception {
83         // GIVEN
84         registerService();
85
86         // WHEN
87         unregisterService();
88
89         // THEN
90         then(serviceRegistration).should().unregister();
91     }
92
93     @Test
94     public void stop_shouldNotAttemptToUnregisterService_whenServiceHasAlreadyBeenUnregistered()
95         throws Exception {
96         // GIVEN
97         registerService();
98         unregisterService();
99
100         // WHEN
101         unregisterService();
102
103         // THEN
104         then(serviceRegistration).should(only()).unregister();
105     }
106
107     private void registerService() throws Exception {
108         ansibleActivator.start(bundleContext);
109     }
110
111     private void unregisterService() throws Exception {
112         ansibleActivator.stop(bundleContext);
113     }
114 }