ODL Upgrade Method 2
[appc.git] / appc-adapters / appc-iaas-adapter / appc-iaas-adapter-bundle / src / test / java / org / onap / appc / adapter / iaas / AppcProviderAdapterActivatorTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2018 AT&T. 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  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.appc.adapter.iaas;
22
23 import static org.mockito.BDDMockito.given;
24 import static org.mockito.BDDMockito.then;
25 import static org.mockito.Matchers.eq;
26 import static org.mockito.Matchers.isA;
27 import static org.mockito.Matchers.isNull;
28 import static org.mockito.Mockito.only;
29
30 import java.util.Dictionary;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.Mock;
35 import org.mockito.runners.MockitoJUnitRunner;
36 import org.onap.appc.adapter.iaas.impl.ProviderAdapterImpl;
37 import org.osgi.framework.BundleContext;
38 import org.osgi.framework.ServiceRegistration;
39
40 @RunWith(MockitoJUnitRunner.class)
41 public class AppcProviderAdapterActivatorTest {
42
43     @Mock
44     private ServiceRegistration serviceRegistration;
45     @Mock
46     private BundleContext bundleContext;
47
48     private AppcProviderAdapterActivator appcProviderAdapterActivator = new AppcProviderAdapterActivator();
49
50     @Before
51     public void setUp() {
52         given(bundleContext.registerService(eq(ProviderAdapter.class), isA(ProviderAdapterImpl.class), isNull(
53             Dictionary.class))).willReturn(serviceRegistration);
54     }
55
56     @Test
57     public void start_shouldRegisterService_whenRegistrationOccursForTheFirstTime() throws Exception {
58         registerService();
59
60         then(bundleContext).should(only())
61             .registerService(eq(ProviderAdapter.class), isA(ProviderAdapterImpl.class), isNull(
62                 Dictionary.class));
63     }
64
65     @Test
66     public void start_shouldRegisterServiceOnlyOnce_whenServiceRegistrationIsNotNull() throws Exception {
67         // GIVEN
68         registerService();
69
70         // WHEN
71         registerService();
72
73         // THEN
74         then(bundleContext).should(only()).registerService(eq(ProviderAdapter.class), isA(ProviderAdapterImpl.class), isNull(
75             Dictionary.class));
76     }
77
78     @Test
79     public void stop_shouldUnregisterService_whenServiceRegistrationObjectIsNotNull() throws Exception {
80         // GIVEN
81         registerService();
82
83         // WHEN
84         unregisterService();
85
86         // THEN
87         then(serviceRegistration).should().unregister();
88     }
89
90     @Test
91     public void stop_shouldNotAttemptToUnregisterService_whenServiceHasAlreadyBeenUnregistered()
92         throws Exception {
93         // GIVEN
94         registerService();
95         unregisterService();
96
97         // WHEN
98         unregisterService();
99
100         // THEN
101         then(serviceRegistration).should(only()).unregister();
102     }
103
104     private void registerService() throws Exception {
105         appcProviderAdapterActivator.start(bundleContext);
106     }
107
108     private void unregisterService() throws Exception {
109         appcProviderAdapterActivator.stop(bundleContext);
110     }
111 }