baf59558be445375a5e77660765fc3963905557f
[moon.git] /
1 /*
2  * Copyright (c) 2016 Inocybe Technologies 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
9 package org.opendaylight.aaa.idm.rest.test;
10
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;
19 import java.util.Map;
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;
25
26
27 public class RoleHandlerTest extends HandlerTest{
28
29     @Test
30     public void testRoleHandler() {
31         //check default roles
32         Roles roles = resource().path("/v1/roles").get(Roles.class);
33         assertNotNull(roles);
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"));
38         }
39
40         //check existing role
41         Role role = resource().path("/v1/roles/0").get(Role.class);
42         assertNotNull(role);
43         assertTrue(role.getName().equals("admin"));
44
45         //check not exist Role
46         try {
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"));
53         }
54
55         // check create Role
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());
62
63         // check create Role missing name data
64         roleData.remove("name");
65         try {
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());
71         }
72
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);
78         assertNotNull(role);
79         assertTrue(role.getName().equals("role1Update"));
80
81         // check delete Role
82         clientResponse = resource().path("/v1/roles/2").delete(ClientResponse.class);
83         assertEquals(204, clientResponse.getStatus());
84
85         // check delete not existing Role
86         try {
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"));
93         }
94     }
95 }