aboutsummaryrefslogtreecommitdiffstats
path: root/odl-aaa-moon/aaa/aaa-idmlight/src/test/java/org/opendaylight/aaa/idm/rest/test/RoleHandlerTest.java
blob: baf59558be445375a5e77660765fc3963905557f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * Copyright (c) 2016 Inocybe Technologies and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

package org.opendaylight.aaa.idm.rest.test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.UniformInterfaceException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ws.rs.core.MediaType;
import org.junit.Test;
import org.opendaylight.aaa.api.model.IDMError;
import org.opendaylight.aaa.api.model.Role;
import org.opendaylight.aaa.api.model.Roles;


public class RoleHandlerTest extends HandlerTest{

    @Test
    public void testRoleHandler() {
        //check default roles
        Roles roles = resource().path("/v1/roles").get(Roles.class);
        assertNotNull(roles);
        List<Role> roleList = roles.getRoles();
        assertEquals(2, roleList.size());
        for (Role role : roleList) {
            assertTrue(role.getName().equals("admin") || role.getName().equals("user"));
        }

        //check existing role
        Role role = resource().path("/v1/roles/0").get(Role.class);
        assertNotNull(role);
        assertTrue(role.getName().equals("admin"));

        //check not exist Role
        try {
            resource().path("/v1/roles/5").get(IDMError.class);
            fail("Should failed with 404!");
        } catch (UniformInterfaceException e) {
            ClientResponse resp = e.getResponse();
            assertEquals(404, resp.getStatus());
            assertTrue(resp.getEntity(IDMError.class).getMessage().contains("role not found"));
        }

        // check create Role
        Map<String, String> roleData = new HashMap<String, String>();
        roleData.put("name","role1");
        roleData.put("description","test Role");
        roleData.put("domainid","0");
        ClientResponse clientResponse = resource().path("/v1/roles").type(MediaType.APPLICATION_JSON).post(ClientResponse.class, roleData);
        assertEquals(201, clientResponse.getStatus());

        // check create Role missing name data
        roleData.remove("name");
        try {
            clientResponse = resource().path("/v1/roles").type(MediaType.APPLICATION_JSON).post(ClientResponse.class, roleData);
            assertEquals(404, clientResponse.getStatus());
        } catch (UniformInterfaceException e) {
            ClientResponse resp = e.getResponse();
            assertEquals(500, resp.getStatus());
        }

        // check update Role data
        roleData.put("name","role1Update");
        clientResponse = resource().path("/v1/roles/2").type(MediaType.APPLICATION_JSON).put(ClientResponse.class, roleData);
        assertEquals(200, clientResponse.getStatus());
        role = resource().path("/v1/roles/2").get(Role.class);
        assertNotNull(role);
        assertTrue(role.getName().equals("role1Update"));

        // check delete Role
        clientResponse = resource().path("/v1/roles/2").delete(ClientResponse.class);
        assertEquals(204, clientResponse.getStatus());

        // check delete not existing Role
        try {
            resource().path("/v1/roles/2").delete(IDMError.class);
            fail("Should failed with 404!");
        } catch (UniformInterfaceException e) {
            ClientResponse resp = e.getResponse();
            assertEquals(404, resp.getStatus());
            assertTrue(resp.getEntity(IDMError.class).getMessage().contains("role id not found"));
        }
    }
}