2 * Copyright (c) 2016 Inocybe Technologies 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.aaa.idm.rest.test;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15 import com.sun.jersey.api.client.ClientResponse;
16 import com.sun.jersey.api.client.UniformInterfaceException;
17 import java.util.HashMap;
18 import java.util.List;
20 import javax.ws.rs.core.MediaType;
21 import org.junit.Test;
22 import org.opendaylight.aaa.api.model.IDMError;
23 import org.opendaylight.aaa.api.model.Role;
24 import org.opendaylight.aaa.api.model.Roles;
27 public class RoleHandlerTest extends HandlerTest{
30 public void testRoleHandler() {
32 Roles roles = resource().path("/v1/roles").get(Roles.class);
34 List<Role> roleList = roles.getRoles();
35 assertEquals(2, roleList.size());
36 for (Role role : roleList) {
37 assertTrue(role.getName().equals("admin") || role.getName().equals("user"));
41 Role role = resource().path("/v1/roles/0").get(Role.class);
43 assertTrue(role.getName().equals("admin"));
45 //check not exist Role
47 resource().path("/v1/roles/5").get(IDMError.class);
48 fail("Should failed with 404!");
49 } catch (UniformInterfaceException e) {
50 ClientResponse resp = e.getResponse();
51 assertEquals(404, resp.getStatus());
52 assertTrue(resp.getEntity(IDMError.class).getMessage().contains("role not found"));
56 Map<String, String> roleData = new HashMap<String, String>();
57 roleData.put("name","role1");
58 roleData.put("description","test Role");
59 roleData.put("domainid","0");
60 ClientResponse clientResponse = resource().path("/v1/roles").type(MediaType.APPLICATION_JSON).post(ClientResponse.class, roleData);
61 assertEquals(201, clientResponse.getStatus());
63 // check create Role missing name data
64 roleData.remove("name");
66 clientResponse = resource().path("/v1/roles").type(MediaType.APPLICATION_JSON).post(ClientResponse.class, roleData);
67 assertEquals(404, clientResponse.getStatus());
68 } catch (UniformInterfaceException e) {
69 ClientResponse resp = e.getResponse();
70 assertEquals(500, resp.getStatus());
73 // check update Role data
74 roleData.put("name","role1Update");
75 clientResponse = resource().path("/v1/roles/2").type(MediaType.APPLICATION_JSON).put(ClientResponse.class, roleData);
76 assertEquals(200, clientResponse.getStatus());
77 role = resource().path("/v1/roles/2").get(Role.class);
79 assertTrue(role.getName().equals("role1Update"));
82 clientResponse = resource().path("/v1/roles/2").delete(ClientResponse.class);
83 assertEquals(204, clientResponse.getStatus());
85 // check delete not existing Role
87 resource().path("/v1/roles/2").delete(IDMError.class);
88 fail("Should failed with 404!");
89 } catch (UniformInterfaceException e) {
90 ClientResponse resp = e.getResponse();
91 assertEquals(404, resp.getStatus());
92 assertTrue(resp.getEntity(IDMError.class).getMessage().contains("role id not found"));