<commit_before>package sg.ncl.service.user.controllers;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.apache.commons.lang3.RandomStringUtils;
import org.hamcrest.collection.IsIterableContainingInAnyOrder;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;
import sg.ncl.service.user.AbstractTest;
import sg.ncl.service.user.data.jpa.entities.AddressEntity;
import sg.ncl.service.user.data.jpa.entities.UserDetailsEntity;
import sg.ncl.service.user.data.jpa.entities.UserEntity;
import sg.ncl.service.user.data.jpa.repositories.UserRepository;
import sg.ncl.service.user.domain.User;

import javax.inject.Inject;
import java.time.ZonedDateTime;
import java.util.List;

import static org.hamcrest.core.Is.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

/**
 * Created by Desmond
 */
public class UsersControllerTest extends AbstractTest {
    @Inject
    private UserRepository userRepository;

    private MockMvc mockMvc;
    @Inject
    private WebApplicationContext webApplicationContext;

    @Before
    public void setup() {
        mockMvc = webAppContextSetup(webApplicationContext).build();
    }

    @Test
    public void getAllUserWithNoUserInDbTest() throws Exception {
        MvcResult result = mockMvc.perform(get("/users")).andReturn();
        Assert.assertTrue(result.getResponse().getContentLength() == 0);
    }

    @Test
    public void getAllUserTest() throws Exception {
        MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(),
                MediaType.APPLICATION_JSON.getSubtype());

        final UserEntity[] userEntityList = new UserEntity[3];

        for (int i = 0; i < 3; i++) {
            Object[] objArray = addUser();
            userEntityList[i] = (UserEntity) objArray[0];
        }

        MvcResult result = mockMvc.perform(get("/users"))
                .andExpect(status().isOk())
                .andExpect(content().contentType(contentType))
                .andReturn();

        String allUserJsonString = result.getResponse().getContentAsString();

        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new JavaTimeModule());
        List<UserEntity> userEntityList2 = mapper.readValue(allUserJsonString, new TypeReference<List<UserEntity>>(){});

        Assert.assertThat(userEntityList2, IsIterableContainingInAnyOrder.containsInAnyOrder(userEntityList));
    }

    @Test
    public void getUserWithNoUserInDbTest() throws Exception {
        mockMvc.perform(get("/users/" + RandomStringUtils.randomAlphabetic(20)))
                .andExpect(status().isNotFound())
                .andExpect(status().reason("User not found"));
    }

    @Test
    public void getUserTest() throws Exception {
        MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(),
                MediaType.APPLICATION_JSON.getSubtype());

        final UserEntity[] userArray = addUser();
        final String idString = userArray[0].getId();
        final UserEntity originalEntity = userArray[1];

        mockMvc.perform(get("/users/" + idString))
                .andExpect(status().isOk())
                .andExpect(content().contentType(contentType))
                .andExpect(jsonPath("$.userDetails.firstName", is(originalEntity.getUserDetails().getFirstName())))
                .andExpect(jsonPath("$.userDetails.lastName", is(originalEntity.getUserDetails().getLastName())))
                .andExpect(jsonPath("$.userDetails.email", is(originalEntity.getUserDetails().getEmail())))
                .andExpect(jsonPath("$.userDetails.phone", is(originalEntity.getUserDetails().getPhone())))
                .andExpect(jsonPath("$.userDetails.address.address1", is(originalEntity.getUserDetails().getAddress().getAddress1())))
                .andExpect(jsonPath("$.userDetails.address.address2", is(originalEntity.getUserDetails().getAddress().getAddress2())))
                .andExpect(jsonPath("$.userDetails.address.country", is(originalEntity.getUserDetails().getAddress().getCountry())))
                .andExpect(jsonPath("$.userDetails.address.region", is(originalEntity.getUserDetails().getAddress().getRegion())))
                .andExpect(jsonPath("$.userDetails.address.zipCode", is(originalEntity.getUserDetails().getAddress().getZipCode())));
    }

    @Test
    public void putUserTest() throws Exception {
        MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(),
                MediaType.APPLICATION_JSON.getSubtype());

        final UserEntity[] userEntityArray = addUser();
        final String idString = userEntityArray[0].getId();

        // get user
        MvcResult result = mockMvc.perform(get("/users/" + idString))
                .andExpect(status().isOk())
                .andExpect(content().contentType(contentType))
                .andReturn();

        String jsonString = result.getResponse().getContentAsString();
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new JavaTimeModule());

        UserEntity userEntity = mapper.readValue(jsonString, UserEntity.class);
        String originalLastName = userEntity.getUserDetails().getLastName();

        // change first name
        String newFirstName = RandomStringUtils.randomAlphabetic(20);
        userEntity.getUserDetails().setFirstName(newFirstName);

        String jsonInString = mapper.writeValueAsString(userEntity);

        // put
        mockMvc.perform(put("/users/" + idString).contentType(contentType).content(jsonInString))
                .andExpect(status().isAccepted());

        // check if first name is new first name and last name is the same
        mockMvc.perform(get("/users/" + idString))
                .andExpect(status().isOk())
                .andExpect(content().contentType(contentType))
                .andExpect(jsonPath("$.userDetails.firstName", is(newFirstName)))
                .andExpect(jsonPath("$.userDetails.lastName", is(originalLastName)));
    }

    private UserEntity[] addUser() throws Exception {
        final UserEntity userEntity = new UserEntity();
        userEntity.setApplicationDate(ZonedDateTime.now());

        final UserDetailsEntity userDetailsEntity = new UserDetailsEntity();
        userDetailsEntity.setFirstName(RandomStringUtils.randomAlphabetic(20));<commit_msg>Add test for put null field and put wrong user id.<commit_after>package sg.ncl.service.user.controllers;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.apache.commons.lang3.RandomStringUtils;
import org.hamcrest.collection.IsIterableContainingInAnyOrder;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;
import sg.ncl.service.user.AbstractTest;
import sg.ncl.service.user.data.jpa.entities.AddressEntity;
import sg.ncl.service.user.data.jpa.entities.UserDetailsEntity;
import sg.ncl.service.user.data.jpa.entities.UserEntity;
import sg.ncl.service.user.data.jpa.repositories.UserRepository;
import sg.ncl.service.user.domain.User;

import javax.inject.Inject;
import java.time.ZonedDateTime;
import java.util.List;

import static org.hamcrest.core.Is.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

/**
 * Created by Desmond
 */
public class UsersControllerTest extends AbstractTest {
    @Inject
    private UserRepository userRepository;

    private MockMvc mockMvc;
    @Inject
    private WebApplicationContext webApplicationContext;

    @Before
    public void setup() {
        mockMvc = webAppContextSetup(webApplicationContext).build();
    }

    @Test
    public void getAllUserWithNoUserInDbTest() throws Exception {
        MvcResult result = mockMvc.perform(get("/users")).andReturn();
        Assert.assertTrue(result.getResponse().getContentLength() == 0);
    }

    @Test
    public void getAllUserTest() throws Exception {
        MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(),
                MediaType.APPLICATION_JSON.getSubtype());

        final UserEntity[] userEntityList = new UserEntity[3];

        for (int i = 0; i < 3; i++) {
            Object[] objArray = addUser();
            userEntityList[i] = (UserEntity) objArray[0];
        }

        MvcResult result = mockMvc.perform(get("/users"))
                .andExpect(status().isOk())
                .andExpect(content().contentType(contentType))
                .andReturn();

        String allUserJsonString = result.getResponse().getContentAsString();

        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new JavaTimeModule());
        List<UserEntity> userEntityList2 = mapper.readValue(allUserJsonString, new TypeReference<List<UserEntity>>(){});

        Assert.assertThat(userEntityList2, IsIterableContainingInAnyOrder.containsInAnyOrder(userEntityList));
    }

    @Test
    public void getUserWithNoUserInDbTest() throws Exception {
        mockMvc.perform(get("/users/" + RandomStringUtils.randomAlphabetic(20)))
                .andExpect(status().isNotFound())
                .andExpect(status().reason("User not found"));
    }

    @Test
    public void getUserTest() throws Exception {
        MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(),
                MediaType.APPLICATION_JSON.getSubtype());

        final UserEntity[] userArray = addUser();
        final String idString = userArray[0].getId();
        final UserEntity originalEntity = userArray[1];

        mockMvc.perform(get("/users/" + idString))
                .andExpect(status().isOk())
                .andExpect(content().contentType(contentType))
                .andExpect(jsonPath("$.userDetails.firstName", is(originalEntity.getUserDetails().getFirstName())))
                .andExpect(jsonPath("$.userDetails.lastName", is(originalEntity.getUserDetails().getLastName())))
                .andExpect(jsonPath("$.userDetails.email", is(originalEntity.getUserDetails().getEmail())))
                .andExpect(jsonPath("$.userDetails.phone", is(originalEntity.getUserDetails().getPhone())))
                .andExpect(jsonPath("$.userDetails.address.address1", is(originalEntity.getUserDetails().getAddress().getAddress1())))
                .andExpect(jsonPath("$.userDetails.address.address2", is(originalEntity.getUserDetails().getAddress().getAddress2())))
                .andExpect(jsonPath("$.userDetails.address.country", is(originalEntity.getUserDetails().getAddress().getCountry())))
                .andExpect(jsonPath("$.userDetails.address.region", is(originalEntity.getUserDetails().getAddress().getRegion())))
                .andExpect(jsonPath("$.userDetails.address.zipCode", is(originalEntity.getUserDetails().getAddress().getZipCode())));
    }

    @Test
    public void putUserTest() throws Exception {
        MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(),
                MediaType.APPLICATION_JSON.getSubtype());

        final UserEntity[] userEntityArray = addUser();
        final String idString = userEntityArray[0].getId();

        // get user
        MvcResult result = mockMvc.perform(get("/users/" + idString))
                .andExpect(status().isOk())
                .andExpect(content().contentType(contentType))
                .andReturn();

        String jsonString = result.getResponse().getContentAsString();
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new JavaTimeModule());

        UserEntity userEntity = mapper.readValue(jsonString, UserEntity.class);
        String originalLastName = userEntity.getUserDetails().getLastName();

        // change first name
        String newFirstName = RandomStringUtils.randomAlphabetic(20);
        userEntity.getUserDetails().setFirstName(newFirstName);

        String jsonInString = mapper.writeValueAsString(userEntity);

        // put
        mockMvc.perform(put("/users/" + idString).contentType(contentType).content(jsonInString))
                .andExpect(status().isAccepted());

        // check if first name is new first name and last name is the same
        mockMvc.perform(get("/users/" + idString))
                .andExpect(status().isOk())
                .andExpect(content().contentType(contentType))
                .andExpect(jsonPath("$.userDetails.firstName", is(newFirstName)))
                .andExpect(jsonPath("$.userDetails.lastName", is(originalLastName)));
    }

    @Test
    public void putUserNullFieldTest() throws Exception {
        MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(),
                MediaType.APPLICATION_JSON.getSubtype());

        final UserEntity[] userEntityArray = addUser();
        final String idString = userEntityArray[0].getId();

        // get user
        MvcResult result = mockMvc.perform(get("/users/" + idString))
                .andExpect(status().isOk())
                .andExpect(content().contentType(contentType))
                .andReturn();

        String jsonString = result.getResponse().getContentAsString();
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new JavaTimeModule());

        UserEntity userEntity = mapper.readValue(jsonString, UserEntity.class);
        userEntity.getUserDetails().setFirstName(null);

        String jsonInString = mapper.writeValueAsString(userEntity);

        // put
        mockMvc.perform(put("/users/" + idString).contentType(contentType).content(jsonInString))
                .andExpect(status().isAccepted());
    }

    @Test
    public void putUserWithWrongIdTest() throws Exception {
        MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(),
                MediaType.APPLICATION_JSON.getSubtype());

        final String idString = "123456";

        // put
        mockMvc.perform(put("/users/" + idString).contentType(contentType).content("{}"))
                .andExpect(status().isNotFound())
                .andExpect(status().reason("User not found"));
    }

    private UserEntity[] addUser() throws Exception {
        final UserEntity userEntity = new UserEntity();
        userEntity.setApplicationDate(ZonedDateTime.now());

        final UserDetailsEntity userDetailsEntity = new UserDetailsEntity();
        userDetailsEntity.setFirstName(RandomStringUtils.randomAlphabetic(20));
{'fraction_non_alphanumeric': 0.11989066175068336, 'fraction_numerical': 0.0034962812281482424, 'mean_word_length': 3.42656162070906, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '3528539', 'n_tokens_mistral': 4305, 'n_tokens_neox': 4163, 'n_words': 684}