115546b604ec295c77b76041dd9f42be4731d468
[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.*;
12
13 import com.sun.jersey.api.client.ClientResponse;
14 import com.sun.jersey.api.client.UniformInterfaceException;
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Map;
18 import javax.ws.rs.core.MediaType;
19 import org.junit.Test;
20 import org.opendaylight.aaa.api.model.IDMError;
21 import org.opendaylight.aaa.api.model.User;
22 import org.opendaylight.aaa.api.model.Users;
23
24 public class UserHandlerTest extends HandlerTest {
25
26     @Test
27     public void testUserHandler() {
28         //check default users
29         Users users = resource().path("/v1/users").get(Users.class);
30         assertNotNull(users);
31         List<User> usrList = users.getUsers();
32         assertEquals(2, usrList.size());
33         for (User usr : usrList) {
34             assertTrue(usr.getName().equals("admin") || usr.getName().equals("user"));
35         }
36
37         //check existing user
38         User usr = resource().path("/v1/users/0").get(User.class);
39         assertNotNull(usr);
40         assertTrue(usr.getName().equals("admin"));
41
42         //check not exist user
43         try {
44             resource().path("/v1/users/5").get(IDMError.class);
45             fail("Should failed with 404!");
46         } catch (UniformInterfaceException e) {
47             ClientResponse resp = e.getResponse();
48             assertEquals(404, resp.getStatus());
49             assertTrue(resp.getEntity(IDMError.class).getMessage().contains("user not found"));
50         }
51
52         // check create user
53         Map<String, String> usrData = new HashMap<String, String>();
54         usrData.put("name","usr1");
55         usrData.put("description","test user");
56         usrData.put("enabled","true");
57         usrData.put("email","user1@usr.org");
58         usrData.put("password","ChangeZbadPa$$w0rd");
59         usrData.put("domainid","0");
60         ClientResponse clientResponse = resource().path("/v1/users").type(MediaType.APPLICATION_JSON).post(ClientResponse.class, usrData);
61         assertEquals(201, clientResponse.getStatus());
62
63         // check create user missing name data
64         usrData.remove("name");
65         try {
66             clientResponse = resource().path("/v1/users").type(MediaType.APPLICATION_JSON).post(ClientResponse.class, usrData);
67             assertEquals(400, clientResponse.getStatus());
68         } catch (UniformInterfaceException e) {
69             ClientResponse resp = e.getResponse();
70             assertEquals(500, resp.getStatus());
71         }
72
73         // check update user data
74         usrData.put("name","usr1Update");
75         clientResponse = resource().path("/v1/users/2").type(MediaType.APPLICATION_JSON).put(ClientResponse.class, usrData);
76         assertEquals(200, clientResponse.getStatus());
77         usr = resource().path("/v1/users/2").get(User.class);
78         assertNotNull(usr);
79         assertTrue(usr.getName().equals("usr1Update"));
80
81         // check delete user
82         clientResponse = resource().path("/v1/users/2").delete(ClientResponse.class);
83         assertEquals(204, clientResponse.getStatus());
84
85         // check delete not existing user
86         try {
87             resource().path("/v1/users/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("Couldn't find user"));
93         }
94     }
95
96 }