Merge "Initial pass at netvirt model"
[netvirt.git] / northbound / src / test / java / org / opendaylight / ovsdb / northbound / OvsdbNorthboundV3Test.java
1 /*
2 * Copyright (C) 2014 Red Hat, Inc.
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.northbound;
11
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertSame;
16 import static org.junit.Assert.fail;
17 import static org.mockito.Matchers.anyObject;
18 import static org.mockito.Matchers.anyString;
19 import static org.mockito.Matchers.eq;
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.when;
22
23 import java.security.Principal;
24 import javax.ws.rs.core.SecurityContext;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.opendaylight.controller.northbound.commons.exception.UnauthorizedException;
28 import org.opendaylight.controller.northbound.commons.utils.NorthboundUtils;
29 import org.opendaylight.controller.sal.authorization.Privilege;
30 import org.powermock.api.mockito.PowerMockito;
31 import org.powermock.core.classloader.annotations.PrepareForTest;
32 import org.powermock.modules.junit4.PowerMockRunner;
33
34 @RunWith(PowerMockRunner.class)
35 @PrepareForTest(NorthboundUtils.class)
36 public class OvsdbNorthboundV3Test {
37     private static final String USER = "admin";
38
39     @Test
40     public void testSetSecurityContext () {
41         SecurityContext securityContext = mock(SecurityContext.class);
42         Principal principal = mock(Principal.class);
43
44         when(securityContext.getUserPrincipal()).thenReturn(null)
45                 .thenReturn(principal);
46         when(principal.getName()).thenReturn(USER);
47
48         OvsdbNorthboundV3 ovsdbNorthboundV3 = new OvsdbNorthboundV3();
49
50         // Check if SecurityContext is null
51         ovsdbNorthboundV3.setSecurityContext(null);
52         String userName = ovsdbNorthboundV3.getUserName();
53         assertNull(userName);
54
55         // Check if user has no Principal
56         ovsdbNorthboundV3.setSecurityContext(securityContext);
57         userName = ovsdbNorthboundV3.getUserName();
58         assertNull(userName);
59
60         // Success case
61         ovsdbNorthboundV3.setSecurityContext(securityContext);
62         userName = ovsdbNorthboundV3.getUserName();
63         assertEquals(USER, userName);
64     }
65
66     @Test
67     public void testGetNode () {
68         PowerMockito.mockStatic(NorthboundUtils.class);
69         when(NorthboundUtils.isAuthorized(anyString(), eq("default"), eq(Privilege.WRITE), anyObject()))
70                 .thenReturn(false)
71                 .thenReturn(true);
72
73         OvsdbNorthboundV3 ovsdbNorthboundV3 = new OvsdbNorthboundV3();
74
75         // Check for unauthorized user
76         try {
77             NodeResource nodeResource = ovsdbNorthboundV3.getNode();
78             fail("Expected an UnauthorizedException to be thrown");
79         } catch (UnauthorizedException e) {
80             assertSame(UnauthorizedException.class, e.getClass());
81         }
82
83         // Success case
84         NodeResource nodeResource = ovsdbNorthboundV3.getNode();
85         assertNotNull(nodeResource);
86     }
87 }