2 * Copyright (c) 2016, 2017 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.shiro.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;
16 import com.sun.jersey.api.client.ClientResponse;
17 import com.sun.jersey.api.client.UniformInterfaceException;
18 import java.util.HashMap;
19 import java.util.List;
21 import javax.ws.rs.core.MediaType;
22 import org.junit.Ignore;
23 import org.junit.Test;
24 import org.opendaylight.aaa.api.model.IDMError;
25 import org.opendaylight.aaa.api.model.User;
26 import org.opendaylight.aaa.api.model.Users;
29 public class UserHandlerTest extends HandlerTest {
32 public void testUserHandler() {
33 // check default users
34 Users users = resource().path("/v1/users").get(Users.class);
36 List<User> usrList = users.getUsers();
37 assertEquals(1, usrList.size());
38 for (User usr : usrList) {
39 assertTrue(usr.getName().equals("admin"));
42 // check existing user
43 User usr = resource().path("/v1/users/0").get(User.class);
45 assertTrue(usr.getName().equals("admin"));
47 // check not exist user
49 resource().path("/v1/users/5").get(IDMError.class);
50 fail("Should failed with 404!");
51 } catch (UniformInterfaceException e) {
52 ClientResponse resp = e.getResponse();
53 assertEquals(404, resp.getStatus());
54 assertTrue(resp.getEntity(IDMError.class).getMessage().contains("user not found"));
58 Map<String, String> usrData = new HashMap<String, String>();
59 usrData.put("name", "usr1");
60 usrData.put("description", "test user");
61 usrData.put("enabled", "true");
62 usrData.put("email", "user1@usr.org");
63 usrData.put("password", "ChangeZbadPa$$w0rd");
64 usrData.put("domainid", "0");
65 ClientResponse clientResponse = resource().path("/v1/users").type(MediaType.APPLICATION_JSON)
66 .post(ClientResponse.class, usrData);
67 assertEquals(201, clientResponse.getStatus());
69 // check create user missing name data
70 usrData.remove("name");
72 clientResponse = resource().path("/v1/users").type(MediaType.APPLICATION_JSON).post(ClientResponse.class,
74 assertEquals(400, clientResponse.getStatus());
75 } catch (UniformInterfaceException e) {
76 ClientResponse resp = e.getResponse();
77 assertEquals(500, resp.getStatus());
80 // check update user data
81 usrData.put("name", "usr1Update");
82 clientResponse = resource().path("/v1/users/1").type(MediaType.APPLICATION_JSON).put(ClientResponse.class,
84 assertEquals(200, clientResponse.getStatus());
85 usr = resource().path("/v1/users/1").get(User.class);
87 assertTrue(usr.getName().equals("usr1Update"));
90 clientResponse = resource().path("/v1/users/1").delete(ClientResponse.class);
91 assertEquals(204, clientResponse.getStatus());
93 // check delete not existing user
95 resource().path("/v1/users/1").delete(IDMError.class);
96 fail("Should failed with 404!");
97 } catch (UniformInterfaceException e) {
98 ClientResponse resp = e.getResponse();
99 assertEquals(404, resp.getStatus());
100 assertTrue(resp.getEntity(IDMError.class).getMessage().contains("Couldn't find user"));
103 // Bug 8382: if a user id is specified, 400 is returned
104 usrData = new HashMap<String, String>();
105 usrData.put("name", "usr1");
106 usrData.put("description", "test user");
107 usrData.put("enabled", "true");
108 usrData.put("email", "user1@usr.org");
109 usrData.put("password", "ChangeZbadPa$$w0rd");
110 usrData.put("userid", "userid");
111 usrData.put("domainid", "0");
112 clientResponse = resource().path("/v1/users").type(MediaType.APPLICATION_JSON).post(ClientResponse.class,
114 assertEquals(400, clientResponse.getStatus());