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
96
|
/*
* 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.*;
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.User;
import org.opendaylight.aaa.api.model.Users;
public class UserHandlerTest extends HandlerTest {
@Test
public void testUserHandler() {
//check default users
Users users = resource().path("/v1/users").get(Users.class);
assertNotNull(users);
List<User> usrList = users.getUsers();
assertEquals(2, usrList.size());
for (User usr : usrList) {
assertTrue(usr.getName().equals("admin") || usr.getName().equals("user"));
}
//check existing user
User usr = resource().path("/v1/users/0").get(User.class);
assertNotNull(usr);
assertTrue(usr.getName().equals("admin"));
//check not exist user
try {
resource().path("/v1/users/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("user not found"));
}
// check create user
Map<String, String> usrData = new HashMap<String, String>();
usrData.put("name","usr1");
usrData.put("description","test user");
usrData.put("enabled","true");
usrData.put("email","user1@usr.org");
usrData.put("password","ChangeZbadPa$$w0rd");
usrData.put("domainid","0");
ClientResponse clientResponse = resource().path("/v1/users").type(MediaType.APPLICATION_JSON).post(ClientResponse.class, usrData);
assertEquals(201, clientResponse.getStatus());
// check create user missing name data
usrData.remove("name");
try {
clientResponse = resource().path("/v1/users").type(MediaType.APPLICATION_JSON).post(ClientResponse.class, usrData);
assertEquals(400, clientResponse.getStatus());
} catch (UniformInterfaceException e) {
ClientResponse resp = e.getResponse();
assertEquals(500, resp.getStatus());
}
// check update user data
usrData.put("name","usr1Update");
clientResponse = resource().path("/v1/users/2").type(MediaType.APPLICATION_JSON).put(ClientResponse.class, usrData);
assertEquals(200, clientResponse.getStatus());
usr = resource().path("/v1/users/2").get(User.class);
assertNotNull(usr);
assertTrue(usr.getName().equals("usr1Update"));
// check delete user
clientResponse = resource().path("/v1/users/2").delete(ClientResponse.class);
assertEquals(204, clientResponse.getStatus());
// check delete not existing user
try {
resource().path("/v1/users/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("Couldn't find user"));
}
}
}
|