This commit is contained in:
Phan Huy Tran 2024-10-02 10:14:47 +02:00 committed by Phan Huy Tran
parent dcaeef600d
commit 41b64e0094
9 changed files with 267 additions and 169 deletions

@ -1,33 +1,33 @@
package de.szut.lf8_starter.config;
import de.szut.lf8_starter.hello.HelloEntity;
import de.szut.lf8_starter.hello.HelloRepository;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
@Component
public class SampleDataCreator implements ApplicationRunner {
private HelloRepository repository;
public SampleDataCreator(HelloRepository repository) {
this.repository = repository;
}
public void run(ApplicationArguments args) {
repository.save(new HelloEntity("Hallo Welt!"));
repository.save(new HelloEntity("Schöner Tag heute"));
repository.save(new HelloEntity("FooBar"));
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
package de.szut.lf8_starter.config;
import de.szut.lf8_starter.hello.HelloEntity;
import de.szut.lf8_starter.hello.HelloRepository;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
@Component
public class SampleDataCreator implements ApplicationRunner {
private HelloRepository repository;
public SampleDataCreator(HelloRepository repository) {
this.repository = repository;
}
public void run(ApplicationArguments args) {
repository.save(new HelloEntity("Hallo Welt!"));
repository.save(new HelloEntity("Schöner Tag heute"));
repository.save(new HelloEntity("FooBar"));
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}

@ -1,27 +1,27 @@
package de.szut.lf8_starter.hello;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Entity
@Table(name = "hello")
public class HelloEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String message;
public HelloEntity(String message) {
this.message = message;
}
}
package de.szut.lf8_starter.hello;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Entity
@Table(name = "hello")
public class HelloEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String message;
public HelloEntity(String message) {
this.message = message;
}
}

@ -1,20 +1,20 @@
package de.szut.lf8_starter.hello;
import de.szut.lf8_starter.hello.dto.HelloCreateDto;
import de.szut.lf8_starter.hello.dto.HelloGetDto;
import org.springframework.stereotype.Service;
@Service
public class HelloMapper {
public HelloGetDto mapToGetDto(HelloEntity entity) {
return new HelloGetDto(entity.getId(), entity.getMessage());
}
public HelloEntity mapCreateDtoToEntity(HelloCreateDto dto) {
var entity = new HelloEntity();
entity.setMessage(dto.getMessage());
return entity;
}
}
package de.szut.lf8_starter.hello;
import de.szut.lf8_starter.hello.dto.HelloCreateDto;
import de.szut.lf8_starter.hello.dto.HelloGetDto;
import org.springframework.stereotype.Service;
@Service
public class HelloMapper {
public HelloGetDto mapToGetDto(HelloEntity entity) {
return new HelloGetDto(entity.getId(), entity.getMessage());
}
public HelloEntity mapCreateDtoToEntity(HelloCreateDto dto) {
var entity = new HelloEntity();
entity.setMessage(dto.getMessage());
return entity;
}
}

@ -1,12 +1,12 @@
package de.szut.lf8_starter.hello;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface HelloRepository extends JpaRepository<HelloEntity, Long> {
List<HelloEntity> findByMessage(String message);
}
package de.szut.lf8_starter.hello;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface HelloRepository extends JpaRepository<HelloEntity, Long> {
List<HelloEntity> findByMessage(String message);
}

@ -1,40 +1,40 @@
package de.szut.lf8_starter.hello;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class HelloService {
private final HelloRepository repository;
public HelloService(HelloRepository repository) {
this.repository = repository;
}
public HelloEntity create(HelloEntity entity) {
return this.repository.save(entity);
}
public List<HelloEntity> readAll() {
return this.repository.findAll();
}
public HelloEntity readById(long id) {
Optional<HelloEntity> optionalQualification = this.repository.findById(id);
if (optionalQualification.isEmpty()) {
return null;
}
return optionalQualification.get();
}
public void delete(HelloEntity entity) {
this.repository.delete(entity);
}
public List<HelloEntity> findByMessage(String message) {
return this.repository.findByMessage(message);
}
}
package de.szut.lf8_starter.hello;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class HelloService {
private final HelloRepository repository;
public HelloService(HelloRepository repository) {
this.repository = repository;
}
public HelloEntity create(HelloEntity entity) {
return this.repository.save(entity);
}
public List<HelloEntity> readAll() {
return this.repository.findAll();
}
public HelloEntity readById(long id) {
Optional<HelloEntity> optionalQualification = this.repository.findById(id);
if (optionalQualification.isEmpty()) {
return null;
}
return optionalQualification.get();
}
public void delete(HelloEntity entity) {
this.repository.delete(entity);
}
public List<HelloEntity> findByMessage(String message) {
return this.repository.findByMessage(message);
}
}

@ -1,20 +1,20 @@
package de.szut.lf8_starter.hello.dto;
import com.fasterxml.jackson.annotation.JsonCreator;
import jakarta.validation.constraints.Size;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class HelloCreateDto {
@Size(min = 3, message = "at least length of 3")
private String message;
@JsonCreator
public HelloCreateDto(String message) {
this.message = message;
}
}
package de.szut.lf8_starter.hello.dto;
import com.fasterxml.jackson.annotation.JsonCreator;
import jakarta.validation.constraints.Size;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class HelloCreateDto {
@Size(min = 3, message = "at least length of 3")
private String message;
@JsonCreator
public HelloCreateDto(String message) {
this.message = message;
}
}

@ -1,17 +1,17 @@
package de.szut.lf8_starter.hello.dto;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@AllArgsConstructor
@Getter
@Setter
public class HelloGetDto {
private long id;
private String message;
}
package de.szut.lf8_starter.hello.dto;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@AllArgsConstructor
@Getter
@Setter
public class HelloGetDto {
private long id;
private String message;
}

@ -5,6 +5,11 @@ import de.szut.lf8_starter.project.ProjectMapper;
import de.szut.lf8_starter.project.ProjectService;
import de.szut.lf8_starter.project.dto.GetProjectDto;
import de.szut.lf8_starter.project.dto.UpdateProjectDto;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import jakarta.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@ -23,6 +28,12 @@ public class UpdateProjectAction {
this.projectMapper = mappingService;
}
@Operation(summary = "Update a project by ID")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Project updated successfully",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = GetProjectDto.class))),
@ApiResponse(responseCode = "404", description = "Project not found", content = @Content)
})
@PutMapping("/{id}")
public ResponseEntity<GetProjectDto> updateSupplier(@PathVariable Long id, @Valid @RequestBody UpdateProjectDto updateProjectDto) {
Optional<ProjectEntity> project = this.projectService.findById(id);

@ -0,0 +1,87 @@
package de.szut.lf8_starter.integration.project;
import de.szut.lf8_starter.project.ProjectEntity;
import de.szut.lf8_starter.project.ProjectRepository;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.is;
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.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@AutoConfigureMockMvc(addFilters = false)
class UpdateProjectActionTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private ProjectRepository projectRepository;
@Test
void updateProjectTest() throws Exception {
ProjectEntity project = new ProjectEntity();
project.setId(1);
project.setComment("comment");
project.setContractor(1);
project.setContractorName("contractorName");
project.setEndDate(LocalDate.of(2024, 1, 1));
project.setLeadingEmployee(1);
project.setName("name");
project.setStartDate(LocalDate.of(2021, 1, 1));
project.setEmployees(List.of(1L, 2L, 3L));
this.projectRepository.save(project);
String content = """
{
"name": "updatedName",
"leading_employee": 2,
"employees": [3, 4, 5],
"contractor": 6,
"contractor_name": "Updated Contractor name",
"comment": "new goal of project",
"start_date": "01.01.2021",
"planned_end_date": "01.01.2022"
}
""";
final var contentAsString = this.mockMvc.perform(
put("/projects/1").content(content).contentType(MediaType.APPLICATION_JSON)
)
.andExpect(status().isOk())
.andExpect(jsonPath("name", is("updatedName")))
.andExpect(jsonPath("leading_employee", is(2)))
.andExpect(jsonPath("employees", is(Arrays.asList(3, 4, 5))))
.andExpect(jsonPath("contractor", is(6)))
.andExpect(jsonPath("contractor_name", is("Updated Contractor name")))
.andExpect(jsonPath("comment", is("new goal of project")))
.andExpect(jsonPath("start_date", is("01.01.2021")))
.andExpect(jsonPath("planned_end_date", is("01.01.2022")))
.andReturn()
.getResponse()
.getContentAsString();
final var id = Long.parseLong(new JSONObject(contentAsString).get("id").toString());
final var existingProject = this.projectRepository.findById(id);
assertThat(existingProject.get().getName()).isEqualTo("updatedName");
assertThat(existingProject.get().getLeadingEmployee()).isEqualTo(2);
assertThat(existingProject.get().getContractor()).isEqualTo(6);
assertThat(existingProject.get().getContractorName()).isEqualTo("Updated Contractor name");
assertThat(existingProject.get().getComment()).isEqualTo("new goal of project");
assertThat(existingProject.get().getStartDate()).isEqualTo(LocalDate.of(2021, 1, 1));
assertThat(existingProject.get().getPlannedEndDate()).isEqualTo(LocalDate.of(2022, 1, 1));
}
}