Search in sources :

Example 1 with Task

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);
}
Also used : Task(lunigorn.task_book_service.persistence.domain.Task) MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) SneakyThrows(lombok.SneakyThrows)

Example 2 with Task

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())));
}
Also used : Task(lunigorn.task_book_service.persistence.domain.Task) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) SneakyThrows(lombok.SneakyThrows)

Example 3 with Task

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);
}
Also used : Task(lunigorn.task_book_service.persistence.domain.Task) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) SneakyThrows(lombok.SneakyThrows)

Example 4 with Task

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);
}
Also used : Task(lunigorn.task_book_service.persistence.domain.Task) Random(java.util.Random) MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) SneakyThrows(lombok.SneakyThrows)

Example 5 with Task

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);
}
Also used : Task(lunigorn.task_book_service.persistence.domain.Task) MvcResult(org.springframework.test.web.servlet.MvcResult) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) SneakyThrows(lombok.SneakyThrows)

Aggregations

Task (lunigorn.task_book_service.persistence.domain.Task)9 Test (org.junit.Test)8 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)8 SneakyThrows (lombok.SneakyThrows)6 MvcResult (org.springframework.test.web.servlet.MvcResult)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Random (java.util.Random)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1