Make EditConfig methods static
[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.mockito.Mockito.doReturn;
12 import static org.mockito.Mockito.doThrow;
13 import static org.mockito.Mockito.mock;
14
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import org.junit.Assert;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.mockito.Mock;
21 import org.mockito.MockitoAnnotations;
22 import org.osgi.framework.BundleContext;
23 import org.osgi.framework.InvalidSyntaxException;
24 import org.osgi.framework.ServiceReference;
25 import org.osgi.service.cm.ManagedService;
26
27 public class NetconfConfigUtilTest {
28
29     @Mock
30     private ServiceReference<ManagedService> serviceRef;
31
32     @Mock
33     private ServiceReference<ManagedService> netconfConfigurationRef;
34
35     @Before
36     public void setUp() {
37         MockitoAnnotations.initMocks(this);
38     }
39
40     @Test
41     public void testGetNetconfConfigurationService() throws Exception {
42         final Collection<ServiceReference<ManagedService>> services = new ArrayList<>();
43         services.add(serviceRef);
44         services.add(netconfConfigurationRef);
45         final BundleContext context = mock(BundleContext.class);
46         doReturn(services).when(context).getServiceReferences(ManagedService.class, null);
47         final ManagedService service = mock(ManagedService.class);
48         doReturn(service).when(context).getService(serviceRef);
49         NetconfConfiguration netconfConfiguration = new NetconfConfiguration();
50         doReturn(netconfConfiguration).when(context).getService(netconfConfigurationRef);
51         final NetconfConfiguration actualNetconfConfiguration =
52                 NetconfConfigUtil.getNetconfConfigurationService(context);
53         Assert.assertEquals(netconfConfiguration, actualNetconfConfiguration);
54
55     }
56
57     @Test
58     public void testGetNetconfConfigurationServiceAbsent() throws Exception {
59         final Collection<ServiceReference<ManagedService>> services = new ArrayList<>();
60         services.add(serviceRef);
61         final BundleContext context = mock(BundleContext.class);
62         doReturn(services).when(context).getServiceReferences(ManagedService.class, null);
63         final ManagedService service = mock(ManagedService.class);
64         doReturn(service).when(context).getService(serviceRef);
65         try {
66             NetconfConfigUtil.getNetconfConfigurationService(context);
67             Assert.fail(IllegalStateException.class + "exception expected");
68         } catch (IllegalStateException e) {
69
70         }
71     }
72
73     @Test
74     public void testGetNetconfConfigurationServiceInvalidSyntax() throws Exception {
75         final BundleContext context = mock(BundleContext.class);
76         final InvalidSyntaxException exception = new InvalidSyntaxException("Invalid syntax", "filter");
77         doThrow(exception).when(context).getServiceReferences(ManagedService.class, null);
78         try {
79             NetconfConfigUtil.getNetconfConfigurationService(context);
80             Assert.fail(InvalidSyntaxException.class + "exception expected");
81         } catch (InvalidSyntaxException e) {
82             return;
83         }
84     }
85 }