a8b964ae8253f661096722d39cdd91861567fc32
[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.Map;
19 import javax.ws.rs.core.MediaType;
20 import org.junit.Test;
21 import org.opendaylight.aaa.api.model.Domain;
22 import org.opendaylight.aaa.api.model.Domains;
23 import org.opendaylight.aaa.api.model.IDMError;
24 import org.opendaylight.aaa.api.model.Roles;
25
26 public class DomainHandlerTest extends HandlerTest{
27
28     @Test
29     public void testDomainHandler() {
30         //check default domains
31         Domains domains = resource().path("/v1/domains").get(Domains.class);
32         assertNotNull(domains);
33         assertEquals(1, domains.getDomains().size());
34         assertTrue(domains.getDomains().get(0).getName().equals("sdn"));
35
36         //check existing domain
37         Domain domain = resource().path("/v1/domains/0").get(Domain.class);
38         assertNotNull(domain);
39         assertTrue(domain.getName().equals("sdn"));
40
41         //check not exist domain
42         try {
43             resource().path("/v1/domains/5").get(IDMError.class);
44             fail("Should failed with 404!");
45         } catch (UniformInterfaceException e) {
46             ClientResponse resp = e.getResponse();
47             assertEquals(404, resp.getStatus());
48             assertTrue(resp.getEntity(IDMError.class).getMessage().contains("Not found! domain id"));
49         }
50
51         // check create domain
52         Map<String, String> domainData = new HashMap<String, String>();
53         domainData.put("name","dom1");
54         domainData.put("description","test dom");
55         domainData.put("domainid","1");
56         domainData.put("enabled","true");
57         ClientResponse clientResponse = resource().path("/v1/domains").type(MediaType.APPLICATION_JSON).post(ClientResponse.class, domainData);
58         assertEquals(201, clientResponse.getStatus());
59
60         // check update domain data
61         domainData.put("name","dom1Update");
62         clientResponse = resource().path("/v1/domains/1").type(MediaType.APPLICATION_JSON).put(ClientResponse.class, domainData);
63         assertEquals(200, clientResponse.getStatus());
64         domain = resource().path("/v1/domains/1").get(Domain.class);
65         assertNotNull(domain);
66         assertTrue(domain.getName().equals("dom1Update"));
67
68         // check create grant
69         Map<String, String> grantData = new HashMap<String, String>();
70         grantData.put("roleid","1");
71         clientResponse = resource().path("/v1/domains/1/users/0/roles").type(MediaType.APPLICATION_JSON).post(ClientResponse.class, grantData);
72         assertEquals(201, clientResponse.getStatus());
73
74         // check create existing grant
75         clientResponse = resource().path("/v1/domains/1/users/0/roles").type(MediaType.APPLICATION_JSON).post(ClientResponse.class, grantData);
76         assertEquals(403, clientResponse.getStatus());
77
78         // check create grant with invalid domain id
79         clientResponse = resource().path("/v1/domains/5/users/0/roles").type(MediaType.APPLICATION_JSON).post(ClientResponse.class, grantData);
80         assertEquals(404, clientResponse.getStatus());
81
82         // check validate user (admin)
83         Map<String, String> usrPwdData = new HashMap<String, String>();
84         usrPwdData.put("username","admin");
85         usrPwdData.put("userpwd","admin");
86         clientResponse = resource().path("/v1/domains/0/users/roles").type(MediaType.APPLICATION_JSON).post(ClientResponse.class, usrPwdData);
87         assertEquals(200, clientResponse.getStatus());
88
89         // check validate user (admin) with wrong password
90         usrPwdData.put("userpwd","1234");
91         clientResponse = resource().path("/v1/domains/0/users/roles").type(MediaType.APPLICATION_JSON).post(ClientResponse.class, usrPwdData);
92         assertEquals(401, clientResponse.getStatus());
93
94         // check get user (admin) roles
95         Roles usrRoles = resource().path("/v1/domains/0/users/0/roles").get(Roles.class);
96         assertNotNull(usrRoles);
97         assertTrue(usrRoles.getRoles().size() > 1);
98
99         // check get invalid user roles
100         try {
101             resource().path("/v1/domains/0/users/5/roles").get(IDMError.class);
102             fail("Should failed with 404!");
103         } catch (UniformInterfaceException e) {
104             ClientResponse resp = e.getResponse();
105             assertEquals(404, resp.getStatus());
106         }
107
108         // check delete grant
109         clientResponse = resource().path("/v1/domains/0/users/0/roles/0").delete(ClientResponse.class);
110         assertEquals(204, clientResponse.getStatus());
111
112         // check delete grant for invalid domain
113         clientResponse = resource().path("/v1/domains/3/users/0/roles/0").delete(ClientResponse.class);
114         assertEquals(404, clientResponse.getStatus());
115
116         // check delete domain
117         clientResponse = resource().path("/v1/domains/1").delete(ClientResponse.class);
118         assertEquals(204, clientResponse.getStatus());
119
120         // check delete not existing domain
121         try {
122             resource().path("/v1/domains/1").delete(IDMError.class);
123             fail("Shoulda failed with 404!");
124         } catch (UniformInterfaceException e) {
125             ClientResponse resp = e.getResponse();
126             assertEquals(404, resp.getStatus());
127             assertTrue(resp.getEntity(IDMError.class).getMessage().contains("Not found! Domain id"));
128         }
129     }
130 }