diff --git a/src/Server/src/test/java/greenify/server/rest/UserControllerTest.java b/src/Server/src/test/java/greenify/server/rest/UserControllerTest.java index a723114..3309583 100644 --- a/src/Server/src/test/java/greenify/server/rest/UserControllerTest.java +++ b/src/Server/src/test/java/greenify/server/rest/UserControllerTest.java @@ -1,63 +1,57 @@ -//package greenify.server.rest; -// -//import greenify.common.UserDto; -//import greenify.server.data.model.User; -//import greenify.server.service.UserService; -//import org.junit.Test; -//import org.junit.runner.RunWith; -//import org.springframework.beans.factory.annotation.Autowired; -//import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; -//import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; -//import org.springframework.boot.test.context.SpringBootTest; -//import org.springframework.boot.test.mock.mockito.MockBean; -//import org.springframework.context.ApplicationContext; -//import org.springframework.http.MediaType; -//import org.springframework.test.context.junit4.SpringRunner; -//import org.springframework.test.web.servlet.MockMvc; -//import org.springframework.test.web.servlet.ResultMatcher; -//import static org.assertj.core.internal.bytebuddy.matcher.ElementMatchers.is; -//import static org.hamcrest.Matchers.hasSize; -//import static org.mockito.BDDMockito.given; -//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.*; -// -//@RunWith(SpringRunner.class) -//@SpringBootTest -//@AutoConfigureMockMvc -//@AutoConfigureTestDatabase -//public class UserControllerTest { -// -// @Autowired -// private MockMvc mvc; -// -// @Autowired -// private ApplicationContext applicationContext; -// -// @MockBean -// private UserService userService; -// -// @Test -// public void getVehicleWhenRequestingTextShouldReturnMakeAndModel() throws Exception { -// given(this.userService.loginUser("name", "password")) -// .willReturn(new UserDto(1L, "name")); -// this.mvc.perform(get("/loginUser").accept(MediaType.APPLICATION_JSON)) -// .andExpect(status().isOk()) -// .andExpect(content() -// .json("name=name, password=password")); -// } -// -// -// @Test -// public void givenEmployees_whenGetEmployees_thenReturnJsonArray() throws Exception { -// User alex = new User(1L, "alex", "password", 0); -// UserDto user = userService.loginUser("alex", "password"); -// given(userService.loginUser("alex", "password")).willReturn(user); -// mvc.perform(get("/loginUser") -// .contentType(MediaType.ALL)) -// .andExpect(status().isOk()) -// .andExpect(jsonPath("$", hasSize(1))) -// .andExpect((ResultMatcher) jsonPath("$[0].name", is(alex.getName()))) -// .andExpect((ResultMatcher) jsonPath("$[0].password", is(alex.getPassword()))); -// } -//} \ No newline at end of file +package greenify.server.rest; + +import static org.mockito.BDDMockito.given; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import greenify.common.UserDto; +import greenify.server.service.UserService; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.ApplicationContext; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; + +@RunWith(SpringRunner.class) +@WebMvcTest(UserController.class) +public class UserControllerTest { + + @Autowired + private MockMvc mvc; + + @Autowired + private ApplicationContext applicationContext; + + @MockBean + private UserService userService; + + @Test + public void registerUserTest() throws Exception { + given(this.userService.registerUser("name", "password")) + .willReturn(new UserDto(1L, "name")); + mvc.perform(get("/registerUser") + .param("name", "name") + .param("password", "password") + .accept(MediaType.APPLICATION_JSON)) + .andDo(print()) + .andExpect(status().isOk()).andExpect(content().json("{'id':1,'name':'name'}")); + } + + @Test + public void loginUserTest() throws Exception { + given(this.userService.loginUser("ceren", "password")) + .willReturn(new UserDto(1L, "ceren")); + mvc.perform(get("/loginUser") + .param("name", "ceren") + .param("password", "password") + .accept(MediaType.APPLICATION_JSON)) + .andDo(print()) + .andExpect(status().isOk()).andExpect(content().json("{'id':1,'name':'ceren'}")); + } +}