Merge branch 'Add_tests' into 'master'
Merge Add_tests branch into master See merge request cse1105/2018-2019/oopp-group-43/template!28
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.SpringBootConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootConfiguration
|
||||
public class ApplicationTest {
|
||||
|
||||
@Test
|
||||
public void contextLoads() throws Exception{ }
|
||||
}
|
||||
|
||||
@@ -131,4 +131,4 @@ public class Activity {
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, name, description, score);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,4 +91,21 @@ public class User {
|
||||
public void setVeganMeal(int veganMeal) {
|
||||
this.veganMeal = veganMeal;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if(other instanceof User){
|
||||
User that = (User)other;
|
||||
if(that.id != this.id)
|
||||
return false;
|
||||
if(!that.name.equals(this.name))
|
||||
return false;
|
||||
if(!that.password.equals(this.password))
|
||||
return false;
|
||||
if(that.veganMeal != this.veganMeal)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
public class RestExceptionHandler {
|
||||
@ExceptionHandler(ApplicationException.class)
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
public ErrorResponse applicationException(ApplicationException ex) {
|
||||
public static ErrorResponse applicationException(ApplicationException ex) {
|
||||
return new ErrorResponse(ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,10 +16,6 @@ public class UserController {
|
||||
@Autowired
|
||||
UserService userService;
|
||||
|
||||
@Autowired // This means to get the bean called userRepository
|
||||
// Which is auto-generated by Spring, we will use it to handle the data
|
||||
UserRepository userRepository;
|
||||
|
||||
@RequestMapping("/registerUser")
|
||||
public UserDto registerUser(@RequestParam(value = "name") String name,
|
||||
@RequestParam(value = "password") String password) {
|
||||
@@ -37,11 +33,4 @@ public class UserController {
|
||||
@RequestParam(value = "name") String name) {
|
||||
userService.addVeganMeal(id, name);
|
||||
}
|
||||
|
||||
@GetMapping(path = "/all")
|
||||
@ResponseBody
|
||||
public Iterable<User> getAllUsers() {
|
||||
// This returns a JSON or XML with the users
|
||||
return userRepository.findAll();
|
||||
}
|
||||
}
|
||||
@@ -8,10 +8,13 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
@Service
|
||||
public class UserService {
|
||||
Logger logger = LoggerFactory.getLogger(UserService.class);
|
||||
|
||||
@Autowired
|
||||
UserRepository userRepository;
|
||||
|
||||
@@ -64,5 +67,12 @@ public class UserService {
|
||||
logger.info("Added vegan meal to user(id=" + user.getId()
|
||||
+ ", name=" + user.getName() + ")");
|
||||
}
|
||||
|
||||
@GetMapping(path = "/all")
|
||||
@ResponseBody
|
||||
public Iterable<User> getAllUsers() {
|
||||
// This returns a JSON or XML with the users
|
||||
return userRepository.findAll();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,18 +1,19 @@
|
||||
import greenify.server.data.model.Activity;
|
||||
import org.junit.Test;
|
||||
package greenify.server.data.model;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class ActivityTest {
|
||||
@Test
|
||||
public void setAndGetTest() {
|
||||
Activity activity = new Activity(1L, "Vegetarian", "Eating", 100);
|
||||
Activity testActivity = new Activity(0, null, null, 0);
|
||||
testActivity.setId(1L);
|
||||
testActivity.setName("Vegetarian");
|
||||
testActivity.setDescription("Eating");
|
||||
testActivity.setScore(100);
|
||||
Activity activity = new Activity(1L, "Vegetarian", "Eating", 100);
|
||||
assertTrue(activity.getId().equals(1L));
|
||||
assertEquals(activity.getName(), "Vegetarian");
|
||||
assertEquals(activity.getDescription(), "Eating");
|
||||
@@ -23,7 +24,8 @@ public class ActivityTest {
|
||||
@Test
|
||||
public void toStringTest() {
|
||||
Activity activity = new Activity(1, "Solar panels", "Installed", 10000);
|
||||
assertEquals("Activity(id=1, name=Solar panels, description=Installed, score=10000)", activity.toString());
|
||||
assertEquals("Activity(id=1, name=Solar panels, "
|
||||
+ "description=Installed, score=10000)", activity.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -43,6 +45,5 @@ public class ActivityTest {
|
||||
Activity second = new Activity(1, "Solar panels", "Installed", 10000);
|
||||
assertEquals(first, second);
|
||||
assertEquals(first.hashCode(), second.hashCode());
|
||||
// assertTrue(first.hashCode() == second.hashCode());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package greenify.server.data.model;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
@@ -35,6 +36,14 @@ public class UserTest {
|
||||
assertEquals(first.getName(), second.getName());
|
||||
assertEquals(first.getPassword(), second.getPassword());
|
||||
assertEquals(first.getVeganMeal(), second.getVeganMeal());
|
||||
assertTrue(first.equals(second));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void instanceOfTest() {
|
||||
User first = new User();
|
||||
Object second = new Object();
|
||||
assertFalse(first.equals(second));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -42,7 +51,7 @@ public class UserTest {
|
||||
User first = new User(1L, "greenify", "password", 3);
|
||||
User second = new User(1L, "greenify", "password", 3);
|
||||
assertTrue(first.equals(second) && second.equals(first));
|
||||
assertTrue(first.hashCode() == second.hashCode());
|
||||
assertEquals(first, second);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,37 +1,24 @@
|
||||
//package greenify.server.data.repository;
|
||||
//
|
||||
//import greenify.server.data.model.User;
|
||||
//import org.junit.Test;
|
||||
//import org.junit.runner.RunWith;
|
||||
//import org.springframework.beans.factory.annotation.Autowired;
|
||||
//import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||
//import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
|
||||
//import org.springframework.test.context.junit4.SpringRunner;
|
||||
//
|
||||
//import static junit.framework.TestCase.assertTrue;
|
||||
//import static org.junit.Assert.assertEquals;
|
||||
//
|
||||
//@RunWith(SpringRunner.class)
|
||||
//@DataJpaTest
|
||||
//public class UserRepositoryTest {
|
||||
//
|
||||
// @Autowired
|
||||
// private TestEntityManager entityManager;
|
||||
//
|
||||
// @Autowired
|
||||
// private UserRepository repository;
|
||||
//
|
||||
// @Test
|
||||
// public void findByUsernameShouldReturnUser() throws Exception {
|
||||
// this.entityManager.persist(new User(296L, "cugurlu", "password", 6));
|
||||
// User user = this.repository.findByName("cugurlu");
|
||||
// assertEquals(user.getName(), "cugurlu");
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void findByUsernameWhenNoUserShouldReturnNull() throws Exception {
|
||||
// this.entityManager.persist(new User(296L, "cugurlu", "password", 6));
|
||||
// User user = this.repository.findByName("mouse");
|
||||
// assertTrue(user == null);
|
||||
// }
|
||||
//}
|
||||
package greenify.server.data.repository;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import greenify.server.data.model.User;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@DataJpaTest
|
||||
public class UserRepositoryTest {
|
||||
@Autowired
|
||||
private UserRepository repository;
|
||||
|
||||
@Test
|
||||
public void findByUsernameTest() throws Exception {
|
||||
repository.save(new User(296L, "cugurlu", "password", 6));
|
||||
User user = this.repository.findByName("cugurlu");
|
||||
assertEquals(user.getName(), "cugurlu");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package greenify.server.rest;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import greenify.common.ApplicationException;
|
||||
import greenify.common.ErrorResponse;
|
||||
import org.junit.Test;
|
||||
|
||||
public class RestExceptionHandlerTest {
|
||||
@Test
|
||||
public void test() {
|
||||
ApplicationException ex = new ApplicationException("testing");
|
||||
ErrorResponse response = new ErrorResponse("testing");
|
||||
assertEquals(RestExceptionHandler.applicationException(ex)
|
||||
.getMessage(), response.getMessage());
|
||||
}
|
||||
}
|
||||
@@ -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())));
|
||||
// }
|
||||
//}
|
||||
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'}"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,15 +51,30 @@ public class UserServiceTest {
|
||||
assertEquals(found.getName(), name);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAllUserTest() {
|
||||
assertEquals(userRepository.findAll(), userService.getAllUsers());
|
||||
}
|
||||
|
||||
// @Test
|
||||
// public void addVeganMealTest() {
|
||||
// User user = new User(1L, "x", "y", 3);
|
||||
// userRepository.save(user);
|
||||
// System.out.println(userRepository);
|
||||
// userService.addVeganMeal(1L, "x");
|
||||
// assertEquals(user.getVeganMeal(), 7);
|
||||
// public void validRegisterTest() {
|
||||
// String name = "new";
|
||||
// String password = "user";
|
||||
// if(userRepository.findByName(name) == null) {
|
||||
// UserDto found = userService.registerUser(name, password);
|
||||
// assertEquals(found.getName(), name);
|
||||
// }
|
||||
// }
|
||||
|
||||
// @Test
|
||||
// public void addVeganMealTest() {
|
||||
// User user = new User(1L, "name", "password", 0);
|
||||
// User user = userRepository.findByName("x");
|
||||
// int veganMeal = user.getVeganMeal();
|
||||
// userService.addVeganMeal(user.getId(), user.getName());
|
||||
// assertEquals(user.getVeganMeal(), veganMeal++);
|
||||
// }
|
||||
|
||||
@Test
|
||||
public void invalidLoginTest() {
|
||||
User user = null;
|
||||
|
||||
Reference in New Issue
Block a user