<commit_before>/**
 * The contents of this file are subject to the OpenMRS Public License
 * Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */
package org.openmrs.module.webservices.rest.web.v1_0.controller.openmrs1_8;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.openmrs.User;
import org.openmrs.api.context.Context;
import org.openmrs.module.webservices.rest.SimpleObject;
import org.openmrs.module.webservices.rest.test.Util;
import org.openmrs.module.webservices.rest.web.RestConstants;
import org.openmrs.module.webservices.rest.web.UserAndPassword;
import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8.UserResource;
import org.openmrs.web.test.BaseModuleWebContextSensitiveTest;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.context.request.WebRequest;

public class UserControllerTest extends BaseModuleWebContextSensitiveTest {
	
	//	private MockHttpServletRequest emptyRequest() {
	//		return new MockHttpServletRequest();
	//	}
	//	
	//	/**
	//	 * @see UserController#createUser(SimpleObject,WebRequest)
	//	 * @verifies create a new user
	//	 */
	//	@Test
	//	public void createUser_shouldCreateANewUser() throws Exception {
	//		int before = Context.getUserService().getAllUsers().size();
	//		String json = "{\"username\":\"test\",\"password\":\"<PASSWORD>\",\"person\":\"da<PASSWORD>-27ce-4bb2-86d6-6d<PASSWORD>5312bd5\"}}";
	//		SimpleObject post = new ObjectMapper().readValue(json, SimpleObject.class);
	//		Object newUser = new UserController().create(post, emptyRequest(), new MockHttpServletResponse());
	//		Util.log("Created User", newUser);
	//		Assert.assertEquals(before + 1, Context.getUserService().getAllUsers().size());
	//	}
	//	
	//	/**
	//	 * @see UserController#createUser(SimpleObject,WebRequest)
	//	 * @verifies create a new user
	//	 */
	//	@Test
	//	public void createUser_shouldCreateANewUserWithRoles() throws Exception {
	//		int before = Context.getUserService().getAllUsers().size();
	//		String json = "{\"username\":\"test\",\"password\":\"<PASSWORD>\",\"person\":\"<PASSWORD>\",\"roles\":[\"3480cb6d-c291-46c8-8d3a-96dc33d199fb\"]}";
	//		SimpleObject post = new ObjectMapper().readValue(json, SimpleObject.class);
	//		Object newUser = new UserController().create(post, emptyRequest(), new MockHttpServletResponse());
	//		Util.log("Created another user with a role this time", newUser);
	//		Assert.assertEquals(before + 1, Context.getUserService().getAllUsers().size());
	//		User createdUser = Context.getUserService().getUserByUsername("test");
	//		Assert.assertTrue(createdUser.hasRole("Provider"));
	//	}
	//	
	//	/**
	//	 * @see UserController#getUser(UserAndPassword,WebRequest)
	//	 * @verifies get a default representation of a UserAndPassword
	//	 */
	//	@Test
	//	public void getUser_shouldGetADefaultRepresentationOfAUser() throws Exception {
	//		Object result = new UserController().retrieve("c98a1558-e131-11de-babe-001e378eb67e", emptyRequest());
	//		Assert.assertNotNull(result);
	//		Util.log("User retrieved (default)", result);
	//		Assert.assertEquals("c98a1558-e131-11de-babe-001e378eb67e", PropertyUtils.getProperty(result, "uuid"));
	//		Assert.assertNotNull(PropertyUtils.getProperty(result, "username"));
	//		Assert.assertEquals("butch", PropertyUtils.getProperty(result, "username"));
	//		Assert.assertNull(PropertyUtils.getProperty(result, "auditInfo"));
	//	}
	//	
	//	/**
	//	 * @see PatientController#getPatient(String,WebRequest)
	//	 * @verifies get a full representation of a patient
	//	 */
	//	@Test
	//	public void getUser_shouldGetAFullRepresentationOfAPatient() throws Exception {
	//		MockHttpServletRequest req = new MockHttpServletRequest();
	//		req.addParameter(RestConstants.REQUEST_PROPERTY_FOR_REPRESENTATION, RestConstants.REPRESENTATION_FULL);
	//		Object result = new UserController().retrieve("c1d8f5c2-e131-11de-babe-001e378eb67e", req);
	//		Util.log("User retrieved (full)", result);
	//		Assert.assertNotNull(result);
	//		Assert.assertEquals("c1d8f5c2-e131-11de-babe-001e378eb67e", PropertyUtils.getProperty(result, "uuid"));
	//		Assert.assertNotNull(PropertyUtils.getProperty(result, "secretQuestion"));
	//		Assert.assertEquals("", PropertyUtils.getProperty(result, "secretQuestion"));
	//	}
	//	
	//	/**
	//	 * @see UserController#updateUser(UserAndPassword,SimpleObject,WebRequest)
	//	 * @verifies change a property on a patient
	//	 */
	//	@Test
	//	@Ignore("RESTWS-242: Define creatable/updatable properties on UserResource resource")
	//	public void updateUser_shouldChangeAPropertyOnAUser() throws Exception {
	//		UserAndPassword user = new UserResource().getByUniqueId("c98a1558-e131-11de-babe-001e378eb67e");
	//		Assert.assertFalse("5-6".equals(user.getUser().getSystemId()));
	//		SimpleObject post = new ObjectMapper().readValue("{\"systemId\":\"5-6\",\"password\":\"<PASSWORD>\"}",
	//		    SimpleObject.class);
	//		Object editedUser = new UserController().update("c98a1558-e131-11de-babe-001e378eb67e", post, emptyRequest(),
	//		    new MockHttpServletResponse());
	//		Util.log("Edited SystemId", editedUser);
	//		user = new UserResource().getByUniqueId("c98a1558-e131-11de-babe-001e378eb67e");
	//		Assert.assertEquals("5-6", user.getUser().getSystemId());
	//	}
	//	
	//	/**
	//	 * @see UserController#retireUser(User,String,WebRequest)
	//	 * @verifies void a patient
	//	 */
	//	@Test
	//	public void retireUser_shouldRetireAUser() throws Exception {
	//		UserAndPassword user = new UserResource().getByUniqueId("c98a1558-e131-11de-babe-001e378eb67e");
	//		Assert.assertFalse(user.isRetired());
	//		new UserController().delete("c98a1558-e131-11de-babe-001e378eb67e", "unit test", emptyRequest(),
	//		    new MockHttpServletResponse());
	//		user = new UserResource().getByUniqueId("c98a1558-e131-11de-babe-001e378eb67e");
	//		Assert.assertTrue(user.getUser().isRetired());
	//		Assert.assertEquals("unit test", user.getUser().getRetireReason());
	//	}
	//	
	//	/**
	//	 * @see UserController#findUsers(String,WebRequest,HttpServletResponse)
	//	 * @verifies return no results if there are no matching users
	//	 */
	//	@Test
	//	public void findUsers_shouldReturnNoResultsIfThereAreNoMatchingUsers() throws Exception {
	//		List<Object> results = (List<Object>) new UserController().search("zzzznobody", emptyRequest(),
	//		    new MockHttpServletResponse()).get("results");
	//		Assert.assertEquals(0, results.size());
	//	}
	//	
	//	/**
	//	 * @see UserController#findUsers(String,WebRequest,HttpServletResponse)
	//	 * @verifies find matching users
	//	 */
	//	@Test
	//	public void findUsers_shouldFindMatchingUsers() throws Exception {
	//		List<Object> results = (List<Object>) new UserController().search("but", emptyRequest(),
	//		    new MockHttpServletResponse()).get("results");
	//		Assert.assertEquals(1, results.size());
	//		Util.log("Found " + results.size() + " user(s)", results);
	//		Object result = results.get(0);
	//		Assert.assertEquals("c98a1558-e131-11de-babe-001e378eb67e", PropertyUtils.getProperty(result, "uuid"));
	//	}
	//	
	//	@Test
	//	public void shouldListAllUsers() throws Exception {
	//		int totalCount = Context.getUserService().getAllUsers().size();
	//		
	//		SimpleObject result = new UserController().getAll(emptyRequest(), new MockHttpServletResponse());
	//		Assert.assertNotNull(result);
	//		Assert.assertEquals(totalCount, Util.getResultsSize(result));
	//	}
	
	@Test
	public void fakeTest() {
		
	}
}<commit_msg>RESTWS-347: Fix UserControllerTest<commit_after>/**
 * The contents of this file are subject to the OpenMRS Public License
 * Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */
package org.openmrs.module.webservices.rest.web.v1_0.controller.openmrs1_8;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openmrs.User;
import org.openmrs.api.UserService;
import org.openmrs.api.context.Context;
import org.openmrs.module.webservices.rest.SimpleObject;
import org.openmrs.module.webservices.rest.test.Util;
import org.openmrs.module.webservices.rest.web.RestConstants;
import org.openmrs.module.webservices.rest.web.UserAndPassword;
import org.openmrs.module.webservices.rest.web.v1_0.controller.BaseCrudControllerTest;
import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8.ResourceTestConstants;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.WebRequest;

public class UserControllerTest extends BaseCrudControllerTest {

    private UserService service;

    @Before
    public void init() {
        service = Context.getUserService();
    }

    /**
     * @see UserController#createUser(SimpleObject,WebRequest)
     * @throws Exception 
     * @verifies create a new user
     */
    @Test
    public void createUser_shouldCreateANewUser() throws Exception {

        long originalCount = getAllCount();

        SimpleObject user = new SimpleObject();
        user.add("username", "test user");
        user.add("password", "<PASSWORD>");
        user.add("person", "da7f524f-27ce-4bb2-86d6-6d1d05312bd5");

        String json = new ObjectMapper().writeValueAsString(user);

        MockHttpServletRequest req = request(RequestMethod.POST, getURI());
        req.setContent(json.getBytes());

        SimpleObject newUser = deserialize(handle(req));

        Util.log("Created User", newUser);
        Assert.assertNotNull(PropertyUtils.getProperty(newUser, "uuid"));
        Assert.assertEquals(originalCount + 1, getAllCount());
    }

    /**
     * @see UserController#createUser(SimpleObject,WebRequest)
     * @throws Exception 
     * @verifies create a new user
     */
    @Test
    public void createUser_shouldCreateANewUserWithRoles() throws Exception {

        long originalCount = getAllCount();

        SimpleObject user = new SimpleObject();
        user.add("username", "test user");
        user.add("password", "<PASSWORD>");
        user.add("person", "da7f524f-27ce-4bb2-86d6-6d1d05312bd5");
        user.add("roles", new String[]{"3480cb6d-c291-46c8-8d3a-96dc33d199fb"});

        String json = new ObjectMapper().writeValueAsString(user);

        MockHttpServletRequest req = request(RequestMethod.POST, getURI());
        req.setContent(json.getBytes());

        SimpleObject newUser = deserialize(handle(req));

        Util.log("Created another user with a role this time.", newUser);
        Assert.assertNotNull(PropertyUtils.getProperty(newUser, "uuid"));
        Assert.assertEquals(originalCount + 1, getAllCount());

        User createdUser = service.getUserByUuid(getUuid());
        Assert.assertNotNull(createdUser);
        Assert.assertTrue(createdUser.hasRole("Provider"));
    }

    /**
     * @see UserController#getUser(UserAndPassword,WebRequest)
     * @throws Exception 
     * @verifies get a default representation of a UserAndPassword
     */
    @Test
    public void getUser_shouldGetADefaultRepresentationOfAUser() throws Exception {

        final String userName = "butch";

        MockHttpServletRequest req = request(RequestMethod.GET, getURI() + "/" + getUuid());
        SimpleObject result = deserialize(handle(req));

        Assert.assertNotNull(result);
        Util.log("User retrieved (default)", result);

        Assert.assertEquals(getUuid(), PropertyUtils.getProperty(result, "uuid"));
        Assert.assertNotNull(PropertyUtils.getProperty(result, "username"));

        Assert.assertEquals(userName, PropertyUtils.getProperty(result, "username"));
        Assert.assertNull(PropertyUtils.getProperty(result, "auditInfo"));
    }

    /**
     * @see PatientController#getPatient(String,WebRequest)
     * @throws Exception 
     * @verifies get a full representation of a patient
     */
    @Test
    public void getUser_shouldGetAFullRepresentationOfAPatient() throws Exception {

        MockHttpServletRequest req = request(RequestMethod.GET, getURI() + "/" + getUuid());
        req.addParameter(RestConstants.REQUEST_PROPERTY_FOR_REPRESENTATION, RestConstants.REPRESENTATION_FULL);

        SimpleObject result = deserialize(handle(req));
        Util.log("User retrieved (full)", result);

        Assert.assertNotNull(result);
        Assert.assertEquals(getUuid(), PropertyUtils.getProperty(result, "uuid"));

        Assert.assertNotNull(PropertyUtils.getProperty(result, "secretQuestion"));
        Assert.assertEquals("", PropertyUtils.getProperty(result, "secretQuestion"));
    }

    /**
     * @see UserController#updateUser(UserAndPassword,SimpleObject,WebRequest)
     * @throws Exception 
     * @verifies change a property on a patient
     */
    @Test
    public void updateUser_shouldChangeAPropertyOnAUser() throws Exception {

        User user = service.getUserByUuid(getUuid());
        Assert.assertNotNull(user);
        Assert.assertFalse("5-6".equals(user.getSystemId()));
        Util.log("Old User SystemId: ", user.getSystemId());

        String json = "{\"systemId\":\"5-6\",\"password\":\"<PASSWORD>\"}";
        MockHttpServletRequest req = request(RequestMethod.POST, getURI() + "/" + getUuid());
        req.setContent(json.getBytes());
        handle(req);

        User editedUser = service.getUserByUuid(getUuid());
        Assert.assertNotNull(editedUser);
        Assert.assertEquals("5-6", editedUser.getSystemId());
        Util.log("Edited User SystemId: ", editedUser.getSystemId());
    }

    /**
     * @see UserController#retireUser(User,String,WebRequest)
     * @throws Exception 
     * @verifies void a patient
     */
    @Test
    public void retireUser_shouldRetireAUser() throws Exception {

        User user = service.getUserByUuid(getUuid());
        Assert.assertFalse(user.isRetired());

        MockHttpServletRequest req = request(RequestMethod.DELETE, getURI() + "/" + user.getUuid());
        req.addParameter("!purge", "");
        req.addParameter("reason", "unit test");
        handle(req);

        User retiredUser = service.getUserByUuid(getUuid());
        Assert.assertTrue(retiredUser.isRetired());
        Assert.assertEquals("unit test", retiredUser.getRetireReason());
    }

    /**
     * @see UserController#findUsers(String,WebRequest,HttpServletResponse)
     * @throws Exception 
     * @verifies return no results if there are no matching users
     */
    @Test
    public void findUsers_shouldReturnNoResultsIfThereAreNoMatchingUsers() throws Exception {

        MockHttpServletRequest req = request(RequestMethod.GET, getURI());
        req.addParameter("q", "foo-bar-baz");

        SimpleObject result = deserialize(handle(req));
        Assert.assertNotNull(result);

        List<User> hits = (List<User>) result.get("results");
        Assert.assertEquals(0, hits.size());
    }

    /**
     * @see UserController#findUsers(String,WebRequest,HttpServletResponse)
     * @throws Exception 
     * @verifies find matching users
     */
    @Test
    public void findUsers_shouldFindMatchingUsers() throws Exception {

        MockHttpServletRequest req = request(RequestMethod.GET, getURI());
        req.addParameter("q", "but");

        SimpleObject result = deserialize(handle(req));
        Assert.assertNotNull(result);

        List<Object> hits = (List<Object>) result.get("results");
        Assert.assertEquals(1, hits.size());

        Util.log("Found " + hits.size() + " user(s)", result);
        Assert.assertEquals(service.getUserByUuid(getUuid()).getUuid(), PropertyUtils.getProperty(hits.get(0), "uuid"));
    }

    @Test
    public void getUser_shouldListAllUsers() throws Exception {

        MockHttpServletRequest req = request(RequestMethod.GET, getURI());
        SimpleObject result = deserialize(handle(req));

        Util.log("Users fetched: ", result);
        Assert.assertNotNull(result);

        Util.log("Total users fetched: ", getAllCount());
        Assert.assertEquals(getAllCount(), Util.getResultsSize(result));
    }

    /**
     * @see org.openmrs.module.webservices.rest.web.v1_0.controller.BaseCrudControllerTest#getURI()
     */
    @Override
    public String getURI() {
        return "user";
    }

    /**
     * @see org.openmrs.module.webservices.rest.web.v1_0.controller.BaseCrudControllerTest#getUuid()
     */
    @Override
    public String getUuid() {
        return ResourceTestConstants.USER_UUID;
    }

    /**
     * @see org.openmrs.module.webservices.rest.web.v1_0.controller.BaseCrudControllerTest#getAllCount()
     */
    @Override
    public long getAllCount() {
        return service.getAllUsers().size();
    }
}
{'fraction_non_alphanumeric': 0.13896503496503496, 'fraction_numerical': 0.019692307692307693, 'mean_word_length': 3.8774897680763982, 'pattern_counts': {'":': 11, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1095576', 'n_tokens_mistral': 5605, 'n_tokens_neox': 5404, 'n_words': 987}