Skip to content

Commit

Permalink
add task stats test case
Browse files Browse the repository at this point in the history
  • Loading branch information
sergi-Jr committed Aug 21, 2024
1 parent d21d607 commit 39273a0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[![Actions Status](https://github.com/sergi-Jr/time-tracker/actions/workflows/main.yml/badge.svg)](https://github.com/sergi-Jr/time-tracker/actions)
[![Maintainability](https://api.codeclimate.com/v1/badges/12b45cc1b263ef3160bf/maintainability)](https://codeclimate.com/github/sergi-Jr/time-tracker/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/12b45cc1b263ef3160bf/test_coverage)](https://codeclimate.com/github/sergi-Jr/time-tracker/test_coverage)
36 changes: 34 additions & 2 deletions src/test/java/sergi/example/api/UserControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;
import sergi.example.task.Task;
import sergi.example.task.dal.TaskRepository;
import sergi.example.user.User;
import sergi.example.user.dal.UserRepository;
import sergi.example.user.dto.UserCreateDTO;
import utils.InitData;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.Optional;

import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Expand All @@ -38,6 +44,9 @@ class UserControllerTest {
@Autowired
private ObjectMapper mapper;

@Autowired
private TaskRepository taskRepository;

@Test
@Order(1)
void testCreate() throws Exception {
Expand All @@ -46,7 +55,7 @@ void testCreate() throws Exception {
dto.setEmail(user.getEmail());
dto.setName(JsonNullable.of(user.getName()));

var request = post("/users")
var request = post("/v1/users")
.contentType(MediaType.APPLICATION_JSON)
.content(mapper.writeValueAsString(dto));
mockMvc.perform(request)
Expand All @@ -68,7 +77,7 @@ void testUpdate() throws Exception {

Map<String, String> data = Map.of("email", "[email protected]", "name", "John Doe");

var request = put("/users/" + user.getId())
var request = put("/v1/users/" + user.getId())
.contentType(MediaType.APPLICATION_JSON)
.content(mapper.writeValueAsString(data));
mockMvc.perform(request)
Expand All @@ -82,4 +91,27 @@ void testUpdate() throws Exception {
.containsEntry("email", actual.getEmail())
.containsEntry("name", actual.getName());
}

@Test
@Order(3)
@Transactional
void testShowUserTrack() throws Exception {
User user = init.user();
Task task = init.task();
task.setStartedAt(LocalDateTime.parse("2024-10-10T11:53:32"));
task.setFinishedAt(LocalDateTime.parse("2024-10-10T16:53:32"));
taskRepository.save(task);
user.addTask(task);
userRepository.save(user);

var request = get("/v1/users/" + user.getEmail())
.param("start-period", "2024-10-10")
.param("end-period", "2024-10-10");
var result = mockMvc.perform(request)
.andExpect(status().isOk())
.andReturn();

String body = result.getResponse().getContentAsString();
assertThatJson(body).isArray();
}
}

0 comments on commit 39273a0

Please sign in to comment.