Merge "Unit test for ovsdb.southbound.ovsdb.transact"
[netvirt.git] / utils / servicehelper / src / test / java / org / opendaylight / ovsdb / utils / servicehelper / ServiceHelperTest.java
1 /*
2  *  Copyright (C) 2015 Red Hat, 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  *  Authors : Sam Hague
9  */
10 package org.opendaylight.ovsdb.utils.servicehelper;
11
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.mockito.Matchers.any;
15
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.osgi.framework.Bundle;
19 import org.osgi.framework.FrameworkUtil;
20 import org.powermock.api.mockito.PowerMockito;
21 import org.powermock.core.classloader.annotations.PrepareForTest;
22 import org.powermock.modules.junit4.PowerMockRunner;
23 import org.springframework.osgi.mock.MockBundle;
24
25 /**
26  * JUnit test for
27  * {@link ServiceHelper}
28  */
29 @RunWith(PowerMockRunner.class)
30 @PrepareForTest(FrameworkUtil.class)
31 public class ServiceHelperTest {
32     @Test
33     /**
34      * Test method for
35      * {@link ServiceHelper#getGlobalInstance(Class, Object)}
36      */
37     public void getGlobalInstanceTest () {
38         Bundle bundle = new MockBundle();
39
40         PowerMockito.mockStatic(FrameworkUtil.class);
41
42         PowerMockito.when(FrameworkUtil.getBundle(any(Class.class)))
43                 .thenReturn(null);
44         Object object = ServiceHelper.getGlobalInstance(Test.class, this);
45         assertNull("Service should be null", object);
46
47         PowerMockito.when(FrameworkUtil.getBundle(any(Class.class)))
48                 .thenReturn(bundle);
49         object = ServiceHelper.getGlobalInstance(Test.class, this);
50         assertNotNull("Service should not be null", object);
51     }
52 }