64bf74fec5387cb2f2a7a1cb1e6413765a09c21f
[netconf.git] / netconf / netconf-util / src / test / java / org / opendaylight / netconf / util / osgi / NetconfConfigUtilTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.netconf.util.osgi;
10
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.doThrow;
14 import static org.mockito.Mockito.mock;
15
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import org.junit.Assert;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.Mock;
22 import org.mockito.MockitoAnnotations;
23 import org.osgi.framework.BundleContext;
24 import org.osgi.framework.InvalidSyntaxException;
25 import org.osgi.framework.ServiceReference;
26 import org.osgi.service.cm.ManagedService;
27
28 public class NetconfConfigUtilTest {
29
30     @Mock
31     private ServiceReference<ManagedService> serviceRef;
32
33     @Mock
34     private ServiceReference<ManagedService> netconfConfigurationRef;
35
36     @Before
37     public void setUp() {
38         MockitoAnnotations.initMocks(this);
39     }
40
41     @Test
42     public void testGetNetconfConfigurationService() throws Exception {
43         final Collection<ServiceReference<ManagedService>> services = new ArrayList<>();
44         services.add(serviceRef);
45         services.add(netconfConfigurationRef);
46         final BundleContext context = mock(BundleContext.class);
47         doReturn(services).when(context).getServiceReferences(ManagedService.class, null);
48         final ManagedService service = mock(ManagedService.class);
49         doReturn(service).when(context).getService(serviceRef);
50         NetconfConfiguration netconfConfiguration = new NetconfConfiguration();
51         doReturn(netconfConfiguration).when(context).getService(netconfConfigurationRef);
52         final NetconfConfiguration actualNetconfConfiguration =
53                 NetconfConfigUtil.getNetconfConfigurationService(context);
54         Assert.assertEquals(netconfConfiguration, actualNetconfConfiguration);
55
56     }
57
58     @Test
59     public void testGetNetconfConfigurationServiceAbsent() throws Exception {
60         final Collection<ServiceReference<ManagedService>> services = new ArrayList<>();
61         services.add(serviceRef);
62         final BundleContext context = mock(BundleContext.class);
63         doReturn(services).when(context).getServiceReferences(ManagedService.class, null);
64         final ManagedService service = mock(ManagedService.class);
65         doReturn(service).when(context).getService(serviceRef);
66         try {
67             NetconfConfigUtil.getNetconfConfigurationService(context);
68             Assert.fail(IllegalStateException.class + "exception expected");
69         } catch (IllegalStateException e) {
70             assertTrue(e.getMessage().startsWith("Netconf configuration service not found"));
71         }
72     }
73
74     @Test
75     public void testGetNetconfConfigurationServiceInvalidSyntax() throws Exception {
76         final BundleContext context = mock(BundleContext.class);
77         final InvalidSyntaxException exception = new InvalidSyntaxException("Invalid syntax", "filter");
78         doThrow(exception).when(context).getServiceReferences(ManagedService.class, null);
79         try {
80             NetconfConfigUtil.getNetconfConfigurationService(context);
81             Assert.fail(InvalidSyntaxException.class + "exception expected");
82         } catch (InvalidSyntaxException e) {
83             return;
84         }
85     }
86 }