2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.netconf.util.osgi;
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;
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;
28 public class NetconfConfigUtilTest {
31 private ServiceReference<ManagedService> serviceRef;
34 private ServiceReference<ManagedService> netconfConfigurationRef;
38 MockitoAnnotations.initMocks(this);
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);
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);
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"));
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);
80 NetconfConfigUtil.getNetconfConfigurationService(context);
81 Assert.fail(InvalidSyntaxException.class + "exception expected");
82 } catch (InvalidSyntaxException e) {