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.mockito.Mockito.doReturn;
12 import static org.mockito.Mockito.doThrow;
13 import static org.mockito.Mockito.mock;
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;
27 public class NetconfConfigUtilTest {
30 private ServiceReference<ManagedService> serviceRef;
33 private ServiceReference<ManagedService> netconfConfigurationRef;
37 MockitoAnnotations.initMocks(this);
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);
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);
66 NetconfConfigUtil.getNetconfConfigurationService(context);
67 Assert.fail(IllegalStateException.class + "exception expected");
68 } catch (IllegalStateException e) {
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);
79 NetconfConfigUtil.getNetconfConfigurationService(context);
80 Assert.fail(InvalidSyntaxException.class + "exception expected");
81 } catch (InvalidSyntaxException e) {