use of lunigorn.task_book_service.persistence.domain.Task in project task-book by Lunigorn.
the class TaskControllerTests method taskInsertSucces.
@Test
@SneakyThrows({ Exception.class })
public void taskInsertSucces() {
// arrange
String randomName = RandomStringUtils.randomAlphabetic(10);
String randomDescription = RandomStringUtils.randomAlphabetic(1000);
// act
MvcResult result = mockMvc.perform(post("/tasks/").content("{\"name\":\"" + randomName + "\",\"description\":\"" + randomDescription + "\"}").contentType(contentType)).andDo(print()).andExpect(status().is2xxSuccessful()).andExpect(jsonPath("$.id", anything())).andExpect(jsonPath("$.name", is(randomName))).andExpect(jsonPath("$.description", is(randomDescription))).andReturn();
String responseText = result.getResponse().getContentAsString();
// assert returned task has id
Integer savedTaskId = JsonPath.read(responseText, "$.id");
assertNotNull(savedTaskId);
// assert returned task is saved in database
Task savedTask = tasks.findOne(savedTaskId.longValue());
assertEquals(savedTask.getName(), randomName);
assertEquals(savedTask.getDescription(), randomDescription);
}
use of lunigorn.task_book_service.persistence.domain.Task in project task-book by Lunigorn.
the class TaskControllerTests method taskGetByIdSucces.
@Test
@SneakyThrows({ Exception.class })
public void taskGetByIdSucces() {
// arrange
Task task = new Task();
String randomName = RandomStringUtils.randomAlphabetic(10);
String randomDescription = RandomStringUtils.randomAlphabetic(1000);
task.setName(randomName);
task.setDescription(randomDescription);
tasks.save(task);
// act
mockMvc.perform(get("/tasks/" + task.getId()).contentType(contentType)).andDo(print()).andExpect(status().is2xxSuccessful()).andExpect(jsonPath("$.id", is(task.getId().intValue()))).andExpect(jsonPath("$.name", is(task.getName()))).andExpect(jsonPath("$.description", is(task.getDescription())));
}
use of lunigorn.task_book_service.persistence.domain.Task in project task-book by Lunigorn.
the class TaskControllerTests method taskDeleteByIdSucces.
@Test
@SneakyThrows({ Exception.class })
public void taskDeleteByIdSucces() {
// arrange
Task task = new Task();
String randomName = RandomStringUtils.randomAlphabetic(10);
String randomDescription = RandomStringUtils.randomAlphabetic(1000);
task.setName(randomName);
task.setDescription(randomDescription);
tasks.save(task);
Task foundBeforeDelete = tasks.findOne(task.getId());
assertNotNull(foundBeforeDelete);
// act
mockMvc.perform(delete("/tasks/" + task.getId()).contentType(contentType)).andDo(print()).andExpect(status().is2xxSuccessful()).andExpect(jsonPath("$.id", is(task.getId().intValue()))).andExpect(jsonPath("$.name", is(task.getName()))).andExpect(jsonPath("$.description", is(task.getDescription())));
Task foundAfterDelete = tasks.findOne(task.getId());
assertNull(foundAfterDelete);
}
use of lunigorn.task_book_service.persistence.domain.Task in project task-book by Lunigorn.
the class TaskControllerTests method taskInsertWithIdFail.
@Test
@SneakyThrows({ Exception.class })
public void taskInsertWithIdFail() {
// arrange
Integer randomId = new Random().nextInt();
String randomName = RandomStringUtils.randomAlphabetic(10);
String randomDescription = RandomStringUtils.randomAlphabetic(1000);
Task foundByRandomId = tasks.findOne(randomId.longValue());
if (foundByRandomId != null) {
assertNotEquals(randomName, foundByRandomId.getName());
assertNotEquals(randomDescription, foundByRandomId.getDescription());
}
// act
MvcResult result = mockMvc.perform(post("/tasks/").content("{\"id\":\"" + randomId + "\",\"name\":\"" + randomName + "\",\"description\":\"" + randomDescription + "\"}").contentType(contentType)).andDo(print()).andExpect(status().isBadRequest()).andReturn();
Task foundByRandomIdAfterError = tasks.findOne(randomId.longValue());
assertEquals(foundByRandomId, foundByRandomIdAfterError);
}
use of lunigorn.task_book_service.persistence.domain.Task in project task-book by Lunigorn.
the class TaskControllerTests method taskGetAlldSucces.
@Test
@SneakyThrows({ Exception.class })
public void taskGetAlldSucces() {
// arrange
// результат прямого запроса в БД
Iterable<Task> allRowsDB = tasks.findAll();
ObjectMapper objectMapper = new ObjectMapper();
String allRowsDbAll;
try {
allRowsDbAll = objectMapper.writeValueAsString(allRowsDB);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
// act
MvcResult result = mockMvc.perform(get("/tasks").contentType(contentType)).andDo(print()).andReturn();
// результат GET запроса
String responseText = result.getResponse().getContentAsString();
// assert
System.out.println("responseText = " + responseText);
System.out.println("allRowsDbAll = " + allRowsDbAll);
assertEquals(responseText, allRowsDbAll);
}
Aggregations