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:
Sem van der Hoeven
2019-03-17 23:39:20 +00:00
12 changed files with 176 additions and 125 deletions

View File

@@ -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{ }
}

View File

@@ -91,4 +91,21 @@ public class User {
public void setVeganMeal(int veganMeal) { public void setVeganMeal(int veganMeal) {
this.veganMeal = 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;
}
} }

View File

@@ -11,7 +11,7 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
public class RestExceptionHandler { public class RestExceptionHandler {
@ExceptionHandler(ApplicationException.class) @ExceptionHandler(ApplicationException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST) @ResponseStatus(HttpStatus.BAD_REQUEST)
public ErrorResponse applicationException(ApplicationException ex) { public static ErrorResponse applicationException(ApplicationException ex) {
return new ErrorResponse(ex.getMessage()); return new ErrorResponse(ex.getMessage());
} }
} }

View File

@@ -16,10 +16,6 @@ public class UserController {
@Autowired @Autowired
UserService userService; 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") @RequestMapping("/registerUser")
public UserDto registerUser(@RequestParam(value = "name") String name, public UserDto registerUser(@RequestParam(value = "name") String name,
@RequestParam(value = "password") String password) { @RequestParam(value = "password") String password) {
@@ -37,11 +33,4 @@ public class UserController {
@RequestParam(value = "name") String name) { @RequestParam(value = "name") String name) {
userService.addVeganMeal(id, 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();
}
} }

View File

@@ -8,10 +8,13 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Service @Service
public class UserService { public class UserService {
Logger logger = LoggerFactory.getLogger(UserService.class); Logger logger = LoggerFactory.getLogger(UserService.class);
@Autowired @Autowired
UserRepository userRepository; UserRepository userRepository;
@@ -64,5 +67,12 @@ public class UserService {
logger.info("Added vegan meal to user(id=" + user.getId() logger.info("Added vegan meal to user(id=" + user.getId()
+ ", name=" + user.getName() + ")"); + ", name=" + user.getName() + ")");
} }
@GetMapping(path = "/all")
@ResponseBody
public Iterable<User> getAllUsers() {
// This returns a JSON or XML with the users
return userRepository.findAll();
}
} }

View File

@@ -1,18 +1,19 @@
import greenify.server.data.model.Activity; package greenify.server.data.model;
import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.Test;
public class ActivityTest { public class ActivityTest {
@Test @Test
public void setAndGetTest() { public void setAndGetTest() {
Activity activity = new Activity(1L, "Vegetarian", "Eating", 100);
Activity testActivity = new Activity(0, null, null, 0); Activity testActivity = new Activity(0, null, null, 0);
testActivity.setId(1L); testActivity.setId(1L);
testActivity.setName("Vegetarian"); testActivity.setName("Vegetarian");
testActivity.setDescription("Eating"); testActivity.setDescription("Eating");
testActivity.setScore(100); testActivity.setScore(100);
Activity activity = new Activity(1L, "Vegetarian", "Eating", 100);
assertTrue(activity.getId().equals(1L)); assertTrue(activity.getId().equals(1L));
assertEquals(activity.getName(), "Vegetarian"); assertEquals(activity.getName(), "Vegetarian");
assertEquals(activity.getDescription(), "Eating"); assertEquals(activity.getDescription(), "Eating");
@@ -23,7 +24,8 @@ public class ActivityTest {
@Test @Test
public void toStringTest() { public void toStringTest() {
Activity activity = new Activity(1, "Solar panels", "Installed", 10000); 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 @Test
@@ -43,6 +45,5 @@ public class ActivityTest {
Activity second = new Activity(1, "Solar panels", "Installed", 10000); Activity second = new Activity(1, "Solar panels", "Installed", 10000);
assertEquals(first, second); assertEquals(first, second);
assertEquals(first.hashCode(), second.hashCode()); assertEquals(first.hashCode(), second.hashCode());
// assertTrue(first.hashCode() == second.hashCode());
} }
} }

View File

@@ -1,6 +1,7 @@
package greenify.server.data.model; package greenify.server.data.model;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.Test; import org.junit.Test;
@@ -35,6 +36,14 @@ public class UserTest {
assertEquals(first.getName(), second.getName()); assertEquals(first.getName(), second.getName());
assertEquals(first.getPassword(), second.getPassword()); assertEquals(first.getPassword(), second.getPassword());
assertEquals(first.getVeganMeal(), second.getVeganMeal()); 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 @Test
@@ -42,7 +51,7 @@ public class UserTest {
User first = new User(1L, "greenify", "password", 3); User first = new User(1L, "greenify", "password", 3);
User second = new User(1L, "greenify", "password", 3); User second = new User(1L, "greenify", "password", 3);
assertTrue(first.equals(second) && second.equals(first)); assertTrue(first.equals(second) && second.equals(first));
assertTrue(first.hashCode() == second.hashCode()); assertEquals(first, second);
} }
} }

View File

@@ -1,37 +1,24 @@
//package greenify.server.data.repository; package greenify.server.data.repository;
//
//import greenify.server.data.model.User; import static org.junit.Assert.assertEquals;
//import org.junit.Test;
//import org.junit.runner.RunWith; import greenify.server.data.model.User;
//import org.springframework.beans.factory.annotation.Autowired; import org.junit.Test;
//import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.junit.runner.RunWith;
//import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.test.context.junit4.SpringRunner; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
// import org.springframework.test.context.junit4.SpringRunner;
//import static junit.framework.TestCase.assertTrue;
//import static org.junit.Assert.assertEquals; @RunWith(SpringRunner.class)
// @DataJpaTest
//@RunWith(SpringRunner.class) public class UserRepositoryTest {
//@DataJpaTest @Autowired
//public class UserRepositoryTest { private UserRepository repository;
//
// @Autowired @Test
// private TestEntityManager entityManager; public void findByUsernameTest() throws Exception {
// repository.save(new User(296L, "cugurlu", "password", 6));
// @Autowired User user = this.repository.findByName("cugurlu");
// private UserRepository repository; assertEquals(user.getName(), "cugurlu");
// }
// @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);
// }
//}

View File

@@ -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());
}
}

View File

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

View File

@@ -51,15 +51,30 @@ public class UserServiceTest {
assertEquals(found.getName(), name); assertEquals(found.getName(), name);
} }
@Test
public void getAllUserTest() {
assertEquals(userRepository.findAll(), userService.getAllUsers());
}
// @Test // @Test
// public void addVeganMealTest() { // public void validRegisterTest() {
// User user = new User(1L, "x", "y", 3); // String name = "new";
// userRepository.save(user); // String password = "user";
// System.out.println(userRepository); // if(userRepository.findByName(name) == null) {
// userService.addVeganMeal(1L, "x"); // UserDto found = userService.registerUser(name, password);
// assertEquals(user.getVeganMeal(), 7); // 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 @Test
public void invalidLoginTest() { public void invalidLoginTest() {
User user = null; User user = null;