package fr.ekito.example.web.rest;
import fr.ekito.example.Application;
import fr.ekito.example.config.MongoConfiguration;
import fr.ekito.example.repository.UserRepository;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import javax.inject.Inject;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
/**
* Test class for the UserResource REST controller.
*
* @see UserResource
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@Import(MongoConfiguration.class)
public class UserResourceTest {
@Inject
private UserRepository userRepository;
private MockMvc restUserMockMvc;
@Before
public void setup() {
UserResource userResource = new UserResource();
ReflectionTestUtils.setField(userResource, "userRepository", userRepository);
this.restUserMockMvc = MockMvcBuilders.standaloneSetup(userResource).build();
}
@Test
public void testGetExistingUser() throws Exception {
restUserMockMvc.perform(get("/app/rest/users/admin")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json"))
.andExpect(jsonPath("$.lastName").value("Administrator"));
}
@Test
public void testGetUnknownUser() throws Exception {
restUserMockMvc.perform(get("/app/rest/users/unknown")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound());
}
}