2 * Copyright (C) 2014 Red Hat, Inc.
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
10 package org.opendaylight.ovsdb.northbound;
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;
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;
34 @RunWith(PowerMockRunner.class)
35 @PrepareForTest(NorthboundUtils.class)
36 public class OvsdbNorthboundV3Test {
37 private static final String USER = "admin";
40 public void testSetSecurityContext () {
41 SecurityContext securityContext = mock(SecurityContext.class);
42 Principal principal = mock(Principal.class);
44 when(securityContext.getUserPrincipal()).thenReturn(null)
45 .thenReturn(principal);
46 when(principal.getName()).thenReturn(USER);
48 OvsdbNorthboundV3 ovsdbNorthboundV3 = new OvsdbNorthboundV3();
50 // Check if SecurityContext is null
51 ovsdbNorthboundV3.setSecurityContext(null);
52 String userName = ovsdbNorthboundV3.getUserName();
55 // Check if user has no Principal
56 ovsdbNorthboundV3.setSecurityContext(securityContext);
57 userName = ovsdbNorthboundV3.getUserName();
61 ovsdbNorthboundV3.setSecurityContext(securityContext);
62 userName = ovsdbNorthboundV3.getUserName();
63 assertEquals(USER, userName);
67 public void testGetNode () {
68 PowerMockito.mockStatic(NorthboundUtils.class);
69 when(NorthboundUtils.isAuthorized(anyString(), eq("default"), eq(Privilege.WRITE), anyObject()))
73 OvsdbNorthboundV3 ovsdbNorthboundV3 = new OvsdbNorthboundV3();
75 // Check for unauthorized user
77 NodeResource nodeResource = ovsdbNorthboundV3.getNode();
78 fail("Expected an UnauthorizedException to be thrown");
79 } catch (UnauthorizedException e) {
80 assertSame(UnauthorizedException.class, e.getClass());
84 NodeResource nodeResource = ovsdbNorthboundV3.getNode();
85 assertNotNull(nodeResource);