Added Junit Class for ParamsHandlerActivator
[appc.git] / appc-config / appc-config-params / provider / src / test / java / org / onap / sdnc / config / TestParamsHandlerActivator.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright 2019 IBM
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 package org.onap.sdnc.config;
21
22 import static org.hamcrest.CoreMatchers.is;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertThat;
25
26 import java.lang.reflect.Field;
27 import java.util.Dictionary;
28 import java.util.List;
29
30 import org.junit.After;
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.Mockito;
36 import org.mockito.MockitoAnnotations;
37 import org.mockito.runners.MockitoJUnitRunner;
38 import org.onap.sdnc.config.params.ParamsHandlerActivator;
39 import org.onap.sdnc.config.params.parser.PropertyDefinitionNode;
40 import org.osgi.framework.BundleContext;
41 import org.osgi.framework.ServiceRegistration;
42
43 @RunWith(MockitoJUnitRunner.class)
44 public class TestParamsHandlerActivator {
45
46     ParamsHandlerActivator paramsHandlerActivator;
47     Field registrationsField;
48
49     @Mock
50     BundleContext ctx;
51
52     @Mock
53     ServiceRegistration serviceRegistration;
54
55     @Before
56     public void setUp() throws Exception {
57         this.paramsHandlerActivator = new ParamsHandlerActivator();
58         this.registrationsField = ParamsHandlerActivator.class.getDeclaredField("registrations");
59         registrationsField.setAccessible(true);
60         MockitoAnnotations.initMocks(this);
61         Mockito.when(ctx.registerService(Mockito.anyString(), Mockito.any(PropertyDefinitionNode.class),
62                 Mockito.any(Dictionary.class))).thenReturn(serviceRegistration);
63     }
64
65     @Test
66     public void start() throws Exception {
67         paramsHandlerActivator.start(ctx);
68         List<ServiceRegistration> registrations = (List<ServiceRegistration>) registrationsField
69                 .get(paramsHandlerActivator);
70         assertEquals(1, registrations.size());
71         assertThat(registrations.get(0), is(serviceRegistration));
72     }
73
74     @Test
75     public void stop() throws Exception {
76         paramsHandlerActivator.start(ctx);
77         paramsHandlerActivator.stop(ctx);
78         Mockito.verify(serviceRegistration).unregister();
79     }
80
81     @After
82     public void stopActivator() throws Exception {
83         paramsHandlerActivator.stop(ctx);
84     }
85 }