diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 1f33738b3eaae1d1942e0d1c0703a297052faa64..cb282f6d42063de00c779dc53ba1f0ecb4d73424 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -14,14 +14,36 @@ default: stages: - build - - test + - tests build: stage: build script: + - echo "We are building your project" - mvn clean install $MAVEN_CLI_OPTS -DskipTests -test: - stage: test +unit_test: + stage: tests script: - - mvn test $MAVEN_CLI_OPTS \ No newline at end of file + - echo "We are testing your project build with unit tests" + - mvn test $MAVEN_CLI_OPTS + artifacts: + expire_in: 10 min + paths: + - "*/target/surefire-reports/*" + reports: + junit: + - "*/target/surefire-reports/*.xml" + +integration_test: + stage: tests + script: + - echo "We are testing your project build with integration tests" + - mvn verify $MAVEN_CLI_OPTS + artifacts: + expire_in: 10 min + paths: + - "*/target/failsafe-reports/*" + reports: + junit: + - "*/target/failsafe-reports/*.xml" diff --git a/README.md b/README.md index 42bc434a7713c6ddb31032480915d91e798bd618..28fdd3b788c7ce36daa084e9bbff23444ae24075 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,15 @@ Just note that each module runs on a different port by default, so care where yo These ports can be overridden either in the application.properties file of each module, or by specifying the port manually when running the "mvn" command to run the module. +## Seed and clear DB +DB is at the start of the app seeded with some Data, thus you don't have to do it manually. +For users are available 2 endpoints: + +- /seed - Seed DB with some Data +- /clear - Clear DB whenever during running + +*Note: /seed will always seed DB with the same Data. Keep in mind, If you seed DB after startup, DB will contain the same data twice.* + ## Build and run the app with Docker For purpose of this build and run the installation of Docker/Podman diff --git a/application/openapi.yaml b/application/openapi.yaml index ad0409608d74bee41087e980e18ff58fe5d8b776..b53ae168b2a36251d71568da139505eb53934226 100644 --- a/application/openapi.yaml +++ b/application/openapi.yaml @@ -39,10 +39,12 @@ components: type: string description: Name of the person example: Max + maxLength: 35 surname: type: string description: Surname of the person example: Verstappen + maxLength: 35 birthday: type: string description: Date of birth @@ -64,9 +66,11 @@ components: name: type: string description: Name of the person + maxLength: 35 surname: type: string description: Name of the person + maxLength: 35 birthday: type: string description: Date of birth diff --git a/application/src/main/java/cz/muni/pa165/data/model/Application.java b/application/src/main/java/cz/muni/pa165/data/model/Application.java index c597fbb4ad52e08dc26dc9f9e8b81d55b2533217..578ee814be2e0b1b3992ba684129a0c529561387 100644 --- a/application/src/main/java/cz/muni/pa165/data/model/Application.java +++ b/application/src/main/java/cz/muni/pa165/data/model/Application.java @@ -22,9 +22,11 @@ public class Application implements Serializable { private ApplicationStatusEnum status; @NotEmpty + @Size(max = 35) private String name; @NotEmpty + @Size(max = 35) private String surname; @Past diff --git a/application/src/test/java/cz/muni/pa165/rest/IntegrationTests.java b/application/src/test/java/cz/muni/pa165/ApplicationIT.java similarity index 93% rename from application/src/test/java/cz/muni/pa165/rest/IntegrationTests.java rename to application/src/test/java/cz/muni/pa165/ApplicationIT.java index 624ff7406426fc025bed692a05a9a828c298c91a..ea447845786e1d3b105a1b551683339e63a64526 100644 --- a/application/src/test/java/cz/muni/pa165/rest/IntegrationTests.java +++ b/application/src/test/java/cz/muni/pa165/ApplicationIT.java @@ -1,4 +1,4 @@ -package cz.muni.pa165.rest; +package cz.muni.pa165; import org.junit.jupiter.api.Test; import org.slf4j.Logger; @@ -23,9 +23,9 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. @SpringBootTest @AutoConfigureMockMvc @ActiveProfiles("test") -class IntegrationTests { +class ApplicationIT { - private static final Logger log = LoggerFactory.getLogger(IntegrationTests.class); + private static final Logger log = LoggerFactory.getLogger(ApplicationIT.class); @Autowired private MockMvc mockMvc; diff --git a/core/openapi.yaml b/core/openapi.yaml index 03bf9819526868392e5e44723e99504c801a1b14..0d2b2786ce14a421db580106320e362d256d488b 100644 --- a/core/openapi.yaml +++ b/core/openapi.yaml @@ -47,6 +47,7 @@ components: type: string description: Specific information about component example: lightweight front wing v2 (black) + maxLength: 100 CarComponentCreateDto: type: object properties: @@ -58,6 +59,7 @@ components: information: type: string description: Specific information about component + maxLength: 100 required: - componentType - weight @@ -73,6 +75,7 @@ components: information: type: string description: Specific information about component + maxLength: 100 CarDto: type: object @@ -130,10 +133,12 @@ components: type: string description: Name of the driver example: Max + maxLength: 35 surname: type: string description: Surname of the driver example: Verstappen + maxLength: 35 height: type: integer description: Height in cm @@ -147,6 +152,7 @@ components: type: string description: Nationality of the driver example: Dutch + maxLength: 20 characteristics: type: array description: Set of driver's characteristics @@ -158,12 +164,15 @@ components: name: type: string description: Name of the driver + maxLength: 35 surname: type: string description: Name of the driver + maxLength: 35 nationality: type: string description: nationality of the driver + maxLength: 20 height: type: integer description: Height in cm @@ -178,12 +187,15 @@ components: name: type: string description: Name of the driver + maxLength: 35 surname: type: string description: Name of the driver + maxLength: 35 nationality: type: string description: Nationality of the driver + maxLength: 20 height: type: integer description: Height in cm @@ -220,10 +232,12 @@ components: type: string description: Name of the engineer example: John + maxLength: 35 surname: type: string description: Surname of the engineer example: Doe + maxLength: 35 EngineerCreateDto: type: object title: Engineer @@ -236,10 +250,12 @@ components: type: string description: Name of the engineer example: John + maxLength: 35 surname: type: string description: Surname of the engineer example: Doe + maxLength: 35 DepartmentDto: type: object @@ -259,6 +275,7 @@ components: specialization: type: string example: "Aerodynamics" + maxLength: 100 DepartmentUpdateDto: type: object properties: @@ -266,6 +283,7 @@ components: type: string example: "Aerodynamics" description: New specialization + maxLength: 100 required: - specialization DepartmentCreateDto: @@ -274,6 +292,7 @@ components: specialization: type: string example: "Aerodynamics" + maxLength: 100 required: - specialization Pageable: diff --git a/core/src/main/java/cz/muni/pa165/data/model/Car.java b/core/src/main/java/cz/muni/pa165/data/model/Car.java index 883d1caf6a5ebe96aa6c8f78f3d2b4b1b29bef0f..7bc7ff16dfa0666c31be90f237e7bcfd82355009 100644 --- a/core/src/main/java/cz/muni/pa165/data/model/Car.java +++ b/core/src/main/java/cz/muni/pa165/data/model/Car.java @@ -12,7 +12,7 @@ import java.util.Set; @Entity @Table(name = "car") -public class Car extends DomainObject implements Serializable { +public class Car extends DomainObject<Long> implements Serializable { @OneToOne(fetch = FetchType.LAZY) @JoinColumn(name = "driver_id") @Nullable diff --git a/core/src/main/java/cz/muni/pa165/data/model/CarComponent.java b/core/src/main/java/cz/muni/pa165/data/model/CarComponent.java index 4dd89fd90717b45df9ae4f60f3006ba491cccf98..068b397de21927684429b764d7e5ab2ccd8134d6 100644 --- a/core/src/main/java/cz/muni/pa165/data/model/CarComponent.java +++ b/core/src/main/java/cz/muni/pa165/data/model/CarComponent.java @@ -7,13 +7,14 @@ import jakarta.annotation.Nullable; import jakarta.persistence.*; import jakarta.validation.constraints.NotEmpty; import jakarta.validation.constraints.Positive; +import jakarta.validation.constraints.Size; import java.io.Serializable; import java.util.Objects; @Entity @Table(name = "car_component") -public class CarComponent extends DomainObject implements Serializable { +public class CarComponent extends DomainObject<Long> implements Serializable { @Column(name = "component_type") @Enumerated(EnumType.STRING) @@ -25,6 +26,7 @@ public class CarComponent extends DomainObject implements Serializable { private Double weight; @NotEmpty + @Size(max = 100) private String information; @ManyToOne(fetch = FetchType.LAZY) diff --git a/core/src/main/java/cz/muni/pa165/data/model/Department.java b/core/src/main/java/cz/muni/pa165/data/model/Department.java index ca27780d3ef0eb0a4a98055a5e5b18a4a43db1c7..43a7be25ad8c4c6543667c61cb9dcca9c080c3ef 100644 --- a/core/src/main/java/cz/muni/pa165/data/model/Department.java +++ b/core/src/main/java/cz/muni/pa165/data/model/Department.java @@ -3,6 +3,7 @@ package cz.muni.pa165.data.model; import jakarta.annotation.Nullable; import jakarta.persistence.*; import jakarta.validation.constraints.NotEmpty; +import jakarta.validation.constraints.Size; import java.io.Serializable; import java.util.Objects; @@ -10,9 +11,10 @@ import java.util.Set; @Entity @Table(name = "department") -public class Department extends DomainObject implements Serializable { +public class Department extends DomainObject<Long> implements Serializable { @NotEmpty + @Size(max = 100) private String specialization; @OneToMany(fetch = FetchType.LAZY, diff --git a/core/src/main/java/cz/muni/pa165/data/model/DomainObject.java b/core/src/main/java/cz/muni/pa165/data/model/DomainObject.java index 1fa12590cf56406a08fb1a554ecef9e94b484014..7071f57d04baa00e3b7fda2e2f2af36deac209c6 100644 --- a/core/src/main/java/cz/muni/pa165/data/model/DomainObject.java +++ b/core/src/main/java/cz/muni/pa165/data/model/DomainObject.java @@ -6,20 +6,22 @@ import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.MappedSuperclass; +import java.io.Serializable; + /** * @author Michal Badin */ @MappedSuperclass -public abstract class DomainObject { +public abstract class DomainObject<PK extends Serializable> implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; + private PK id; - public Long getId() { + public PK getId() { return id; } - public void setId(Long id) { + public void setId(PK id) { this.id = id; } } diff --git a/core/src/main/java/cz/muni/pa165/data/model/Driver.java b/core/src/main/java/cz/muni/pa165/data/model/Driver.java index 8650b6fadf0153173d23a646641035c660ad5c9f..8bf59f42d0e06727262661bc2332759e68ebd639 100644 --- a/core/src/main/java/cz/muni/pa165/data/model/Driver.java +++ b/core/src/main/java/cz/muni/pa165/data/model/Driver.java @@ -5,10 +5,7 @@ import cz.muni.pa165.data.enums.CharacteristicsEnum; import jakarta.annotation.Nonnull; import jakarta.annotation.Nullable; import jakarta.persistence.*; -import jakarta.validation.constraints.Max; -import jakarta.validation.constraints.Min; -import jakarta.validation.constraints.NotEmpty; -import jakarta.validation.constraints.Past; +import jakarta.validation.constraints.*; import java.io.Serializable; import java.time.LocalDate; @@ -17,12 +14,14 @@ import java.util.Set; @Entity @Table(name = "driver") -public class Driver extends DomainObject implements Serializable { +public class Driver extends DomainObject<Long> implements Serializable { @NotEmpty + @Size(max = 35) private String name; @NotEmpty + @Size(max = 35) private String surname; @Nullable @@ -35,6 +34,7 @@ public class Driver extends DomainObject implements Serializable { private LocalDate birthday; @NotEmpty + @Size(max = 20) private String nationality; @ElementCollection(targetClass = CharacteristicsEnum.class) diff --git a/core/src/main/java/cz/muni/pa165/data/model/Engineer.java b/core/src/main/java/cz/muni/pa165/data/model/Engineer.java index 7fa3623617173ca276db256d65cde8716efa63c8..1e8e5dbad8c6b3f68ace657aaa60ce16d50dc3cc 100644 --- a/core/src/main/java/cz/muni/pa165/data/model/Engineer.java +++ b/core/src/main/java/cz/muni/pa165/data/model/Engineer.java @@ -3,18 +3,21 @@ package cz.muni.pa165.data.model; import jakarta.annotation.Nullable; import jakarta.persistence.*; import jakarta.validation.constraints.NotEmpty; +import jakarta.validation.constraints.Size; import java.io.Serializable; import java.util.Objects; @Entity @Table(name = "engineer") -public class Engineer extends DomainObject implements Serializable { +public class Engineer extends DomainObject<Long> implements Serializable { @NotEmpty + @Size(max = 35) private String name; @NotEmpty + @Size(max = 35) private String surname; @ManyToOne(fetch = FetchType.LAZY) diff --git a/core/src/main/java/cz/muni/pa165/data/repository/CarRepository.java b/core/src/main/java/cz/muni/pa165/data/repository/CarRepository.java index 9b0400b217d452c01911b581b5107403a12f733a..7d4091c81cab8aa9924b98d0b82242d17d216c63 100644 --- a/core/src/main/java/cz/muni/pa165/data/repository/CarRepository.java +++ b/core/src/main/java/cz/muni/pa165/data/repository/CarRepository.java @@ -10,6 +10,6 @@ import java.util.Optional; @Repository public interface CarRepository extends JpaRepository<Car, Long> { - @Query("SELECT c FROM Car c LEFT JOIN FETCH c.components LEFT JOIN FETCH c.driver WHERE c.id = :id") + @Query("SELECT c FROM Car c LEFT JOIN FETCH c.components cc LEFT JOIN FETCH c.driver d WHERE c.id = :id") Optional<Car> findById(@Param("id") Long id); } diff --git a/core/src/test/java/cz/muni/pa165/CoreIT.java b/core/src/test/java/cz/muni/pa165/CoreIT.java new file mode 100644 index 0000000000000000000000000000000000000000..aa9991cf066891580d1fa8b00f4ae490195594e4 --- /dev/null +++ b/core/src/test/java/cz/muni/pa165/CoreIT.java @@ -0,0 +1,344 @@ +package cz.muni.pa165; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import cz.muni.pa165.data.enums.ComponentTypeEnum; +import cz.muni.pa165.data.model.*; +import cz.muni.pa165.generated.core.model.*; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +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.context.ActiveProfiles; +import org.springframework.test.web.servlet.MockMvc; + +import java.math.BigDecimal; +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.stream.Collectors; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +/** + * Integration tests. Run by "mvn verify / mvn test / mvn clean install". + * Testing functionality of the module as a whole. + */ +@SpringBootTest +@AutoConfigureMockMvc +@ActiveProfiles("test") +class CoreIT { + + private static final Logger log = LoggerFactory.getLogger(CoreIT.class); + private final ObjectMapper mapper = new ObjectMapper().registerModule(new JavaTimeModule()); + + @Autowired + private MockMvc mockMvc; + + @Test + void testCreateCar() throws Exception { + log.info("testCreateCar() running"); + + CarDto carDto = new CarDto(); + String requestBody = mapper.writeValueAsString(carDto); + + String response = mockMvc.perform( + post("/car") + .accept(MediaType.APPLICATION_JSON) + .contentType(MediaType.APPLICATION_JSON) + .content(requestBody)) + .andExpect(status().isOk()) + .andReturn().getResponse().getContentAsString(); + log.info("response: {}", response); + + Car received = mapper.readValue(response, Car.class); + Assertions.assertNotNull(received); + } + + @Test + void testCreateComponent() throws Exception { + log.info("testCreateComponent() running"); + + CarComponentDto componentDto = new CarComponentDto() + .componentType(CarComponentType.CHASSIS) + .weight(BigDecimal.valueOf(50.5)) + .information("lightweight front wing v2 (black)"); + String requestBody = mapper.writeValueAsString(componentDto); + + String response = mockMvc.perform( + post("/carComponent") + .accept(MediaType.APPLICATION_JSON) + .contentType(MediaType.APPLICATION_JSON) + .content(requestBody)) + .andExpect(status().isOk()) + .andReturn().getResponse().getContentAsString(); + + CarComponent received = mapper.readValue(response, CarComponent.class); + Assertions.assertEquals(ComponentTypeEnum.CHASSIS, received.getComponentType()); + Assertions.assertEquals(50.5, received.getWeight()); + Assertions.assertEquals("lightweight front wing v2 (black)", received.getInformation()); + } + + @Test + void testCreateComponentWrongData() throws Exception { + log.info("testCreateComponentWrongData() running"); + + CarComponentDto componentDto = new CarComponentDto() + .componentType(CarComponentType.CHASSIS) + .weight(BigDecimal.valueOf(-5)) + .information("lightweight front wing v2 (black)"); + String requestBody = mapper.writeValueAsString(componentDto); + + mockMvc.perform( + post("/carComponent") + .accept(MediaType.APPLICATION_JSON) + .contentType(MediaType.APPLICATION_JSON) + .content(requestBody)) + .andExpect(status().is5xxServerError()); + } + + @Test + void testCreateDriver() throws Exception { + log.info("testCreateDriver running"); + + DriverDto driverDto = new DriverDto() + .name("John") + .surname("Doe") + .nationality("Italian") + .height(189) + .birthday(LocalDate.of(1990, 10, 1)) + .addCharacteristicsItem(Characteristic.AGGRESSIVENESS); + String requestBody = mapper.writeValueAsString(driverDto); + + String response = mockMvc.perform( + post("/driver") + .accept(MediaType.APPLICATION_JSON) + .contentType(MediaType.APPLICATION_JSON) + .content(requestBody)) + .andExpect(status().isOk()) + .andReturn().getResponse().getContentAsString(); + log.info("response: {}", response); + + Driver received = mapper.readValue(response, Driver.class); + Assertions.assertEquals("John", received.getName()); + Assertions.assertEquals("Doe", received.getSurname()); + Assertions.assertEquals("Italian", received.getNationality()); + Assertions.assertEquals(189, received.getHeight()); + Assertions.assertEquals(LocalDate.of(1990, 10, 1), received.getBirthday()); + } + + @Test + void testCreateDriverWrongData() throws Exception { + log.info("testCreateDriverWrongData running"); + + DriverDto driverDto = new DriverDto() + .name("John") + .surname("Doe") + .nationality("Italian") + .height(50) + .birthday(LocalDate.of(2999, 10, 1)) + .addCharacteristicsItem(Characteristic.AGGRESSIVENESS); + String requestBody = mapper.writeValueAsString(driverDto); + + mockMvc.perform( + post("/driver") + .accept(MediaType.APPLICATION_JSON) + .contentType(MediaType.APPLICATION_JSON) + .content(requestBody)) + .andExpect(status().is5xxServerError()); + } + + @Test + void testCreateDepartment() throws Exception { + log.info("testCreateDepartment() running"); + + DepartmentDto departmentDto = new DepartmentDto().specialization("aero"); + String requestBody = mapper.writeValueAsString(departmentDto); + + String response = mockMvc.perform( + post("/department") + .accept(MediaType.APPLICATION_JSON) + .contentType(MediaType.APPLICATION_JSON) + .content(requestBody)) + .andExpect(status().isOk()) + .andReturn().getResponse().getContentAsString(); + log.info("response: {}", response); + + Department received = mapper.readValue(response, Department.class); + Assertions.assertEquals("aero", received.getSpecialization()); + } + + @Test + void testCreateDepartmentWrongData() throws Exception { + log.info("testCreateDepartmentWrongData() running"); + + DepartmentDto departmentDto = new DepartmentDto().specialization(""); + String requestBody = mapper.writeValueAsString(departmentDto); + + mockMvc.perform( + post("/department") + .accept(MediaType.APPLICATION_JSON) + .contentType(MediaType.APPLICATION_JSON) + .content(requestBody)) + .andExpect(status().is5xxServerError()); + } + + @Test + void testComplexScenario1() throws Exception { + log.info("testComplexScenario1() running"); + + Car car = loadEmptyCar(); + Driver driver = loadDriver(); + List<CarComponent> components = loadComponents(); + + String carWithDriverResponse = mockMvc.perform( + put("/car/driver?carId=" + car.getId() + "&driverId=" + driver.getId()) + .accept(MediaType.APPLICATION_JSON)) + .andReturn().getResponse().getContentAsString(); + + Car carWithDriver = mapper.readValue(carWithDriverResponse, Car.class); + car.setDriver(driver); + Assertions.assertEquals(car, carWithDriver); + + car.setComponents(new HashSet<>(components)); + String carUpdateRequestQuery = components.stream().map(CarComponent::getId).map(String::valueOf).collect(Collectors.joining("&componentIds=")); + String carWithComponentsResponse = mockMvc.perform( + patch("/car/" + car.getId() + "?componentIds=" + carUpdateRequestQuery) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()).andReturn().getResponse().getContentAsString(); + + Car carWithComponents = mapper.readValue(carWithComponentsResponse, Car.class); + Assertions.assertEquals(car, carWithComponents); + + mockMvc.perform( + delete("/car/driver?carId=" + car.getId())) + .andExpect(status().isOk()); + + String findCarDeletedDriver = mockMvc.perform( + get("/car/" + car.getId())).andExpect(status().isOk()).andReturn().getResponse().getContentAsString(); + + Car carDeletedDriver = mapper.readValue(findCarDeletedDriver, Car.class); + Assertions.assertNull(carDeletedDriver.getDriver()); + + mockMvc.perform(delete("/car/" + car.getId())).andExpect(status().isOk()); + + mockMvc.perform(get("/car/" + car.getId())).andExpect(status().isNotFound()); + } + + @Test + void testComplexScenario2() throws Exception { + log.info("testComplexScenario1() running"); + + Department department = loadEmptyDepartment(); + Engineer engineer = loadEngineer(); + + String departmentWithEngineerResponse = mockMvc.perform( + put("/department/" + department.getId() + "?engineerId=" + engineer.getId()) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()).andReturn().getResponse().getContentAsString(); + + Department departmentWithEngineer = mapper.readValue(departmentWithEngineerResponse, Department.class); + + department.addEngineer(engineer); + Assertions.assertEquals(1, departmentWithEngineer.getEngineers().size()); + Assertions.assertEquals(department, departmentWithEngineer); + + mockMvc.perform(delete("/department/" + department.getId())).andExpect(status().isOk()); + + mockMvc.perform(get("/department/" + department.getId())).andExpect(status().isNotFound()); + } + + private Department loadEmptyDepartment() throws Exception { + DepartmentDto departmentRequest = new DepartmentDto().specialization("aero").engineers(new ArrayList<>()); + String departmentRequestBody = mapper.writeValueAsString(departmentRequest); + String departmentResponse = mockMvc.perform(post("/department") + .accept(MediaType.APPLICATION_JSON) + .contentType(MediaType.APPLICATION_JSON) + .content(departmentRequestBody)) + .andExpect(status().isOk()) + .andReturn().getResponse().getContentAsString(); + Department received = mapper.readValue(departmentResponse, Department.class); + received.setEngineers(new HashSet<>()); + return received; + } + + private Engineer loadEngineer() throws Exception { + EngineerDto engineerRequest = new EngineerDto().name("John").surname("Doe"); + String engineerRequestBody = mapper.writeValueAsString(engineerRequest); + String engineerResponse = mockMvc.perform(post("/engineer") + .accept(MediaType.APPLICATION_JSON) + .contentType(MediaType.APPLICATION_JSON) + .content(engineerRequestBody)) + .andExpect(status().isOk()) + .andReturn().getResponse().getContentAsString(); + return mapper.readValue(engineerResponse, Engineer.class); + } + + private Car loadEmptyCar() throws Exception { + CarDto carRequest = new CarDto(); + String carRequestBody = mapper.writeValueAsString(carRequest); + String carResponse = mockMvc.perform(post("/car") + .accept(MediaType.APPLICATION_JSON) + .contentType(MediaType.APPLICATION_JSON) + .content(carRequestBody)) + .andExpect(status().isOk()) + .andReturn().getResponse().getContentAsString(); + return mapper.readValue(carResponse, Car.class); + } + + private Driver loadDriver() throws Exception { + DriverDto driverRequest = new DriverDto() + .name("John") + .surname("Doe") + .nationality("Italian") + .height(189) + .birthday(LocalDate.of(1990, 10, 1)) + .addCharacteristicsItem(Characteristic.AGGRESSIVENESS); + String driverRequestBody = mapper.writeValueAsString(driverRequest); + + String driverResponse = mockMvc.perform(post("/driver") + .accept(MediaType.APPLICATION_JSON) + .contentType(MediaType.APPLICATION_JSON) + .content(driverRequestBody)) + .andExpect(status().isOk()) + .andReturn().getResponse().getContentAsString(); + + return mapper.readValue(driverResponse, Driver.class); + } + + private List<CarComponent> loadComponents() throws Exception { + CarComponentDto componentRequestChassis = new CarComponentDto() + .componentType(CarComponentType.CHASSIS) + .weight(BigDecimal.valueOf(50.5)) + .information("new aerodynamic chassis"); + CarComponentDto componentRequestEngine = new CarComponentDto() + .componentType(CarComponentType.ENGINE) + .weight(BigDecimal.valueOf(100)) + .information("honda engine v2 500HP"); + String componentRequestBodyChassis = mapper.writeValueAsString(componentRequestChassis); + String componentRequestBodyEngine = mapper.writeValueAsString(componentRequestEngine); + String componentResponseChassis = mockMvc.perform(post("/carComponent") + .accept(MediaType.APPLICATION_JSON) + .contentType(MediaType.APPLICATION_JSON) + .content(componentRequestBodyChassis)) + .andExpect(status().isOk()) + .andReturn().getResponse().getContentAsString(); + String componentResponseEngine = mockMvc.perform(post("/carComponent") + .accept(MediaType.APPLICATION_JSON) + .contentType(MediaType.APPLICATION_JSON) + .content(componentRequestBodyEngine)) + .andExpect(status().isOk()) + .andReturn().getResponse().getContentAsString(); + CarComponent componentChassis = mapper.readValue(componentResponseChassis, CarComponent.class); + CarComponent componentEngine = mapper.readValue(componentResponseEngine, CarComponent.class); + return List.of(componentChassis, componentEngine); + } + +} + diff --git a/core/src/test/java/cz/muni/pa165/IntegrationTests.java b/core/src/test/java/cz/muni/pa165/IntegrationTests.java deleted file mode 100644 index a89afcf72521f44f3f08696c383b35ed6e66973d..0000000000000000000000000000000000000000 --- a/core/src/test/java/cz/muni/pa165/IntegrationTests.java +++ /dev/null @@ -1,342 +0,0 @@ -//package cz.muni.pa165; -// -//import com.fasterxml.jackson.databind.ObjectMapper; -//import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; -//import cz.muni.pa165.data.enums.ComponentTypeEnum; -//import cz.muni.pa165.data.model.*; -//import cz.muni.pa165.generated.core.model.*; -//import org.junit.jupiter.api.Assertions; -//import org.junit.jupiter.api.Test; -//import org.slf4j.Logger; -//import org.slf4j.LoggerFactory; -//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.math.BigDecimal; -//import java.time.LocalDate; -//import java.util.ArrayList; -//import java.util.HashSet; -//import java.util.List; -//import java.util.stream.Collectors; -// -//import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; -//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -// -///** -// * Integration tests. Run by "mvn verify / mvn test / mvn clean install". -// * Testing functionality of the module as a whole. -// */ -//@SpringBootTest -//@AutoConfigureMockMvc -//class IntegrationTests { -// -// private static final Logger log = LoggerFactory.getLogger(IntegrationTests.class); -// private final ObjectMapper mapper = new ObjectMapper().registerModule(new JavaTimeModule()); -// -// @Autowired -// private MockMvc mockMvc; -// -// @Test -// void testCreateCar() throws Exception { -// log.info("testCreateCar() running"); -// -// CarDto carDto = new CarDto(); -// String requestBody = mapper.writeValueAsString(carDto); -// -// String response = mockMvc.perform( -// post("/car") -// .accept(MediaType.APPLICATION_JSON) -// .contentType(MediaType.APPLICATION_JSON) -// .content(requestBody)) -// .andExpect(status().isOk()) -// .andReturn().getResponse().getContentAsString(); -// log.info("response: {}", response); -// -// Car received = mapper.readValue(response, Car.class); -// Assertions.assertNotNull(received); -// } -// -// @Test -// void testCreateComponent() throws Exception { -// log.info("testCreateComponent() running"); -// -// CarComponentDto componentDto = new CarComponentDto() -// .componentType(CarComponentType.CHASSIS) -// .weight(BigDecimal.valueOf(50.5)) -// .information("lightweight front wing v2 (black)"); -// String requestBody = mapper.writeValueAsString(componentDto); -// -// String response = mockMvc.perform( -// post("/carComponent") -// .accept(MediaType.APPLICATION_JSON) -// .contentType(MediaType.APPLICATION_JSON) -// .content(requestBody)) -// .andExpect(status().isOk()) -// .andReturn().getResponse().getContentAsString(); -// -// CarComponent received = mapper.readValue(response, CarComponent.class); -// Assertions.assertEquals(ComponentTypeEnum.CHASSIS, received.getComponentType()); -// Assertions.assertEquals(50.5, received.getWeight()); -// Assertions.assertEquals("lightweight front wing v2 (black)", received.getInformation()); -// } -// -// @Test -// void testCreateComponentWrongData() throws Exception { -// log.info("testCreateComponentWrongData() running"); -// -// CarComponentDto componentDto = new CarComponentDto() -// .componentType(CarComponentType.CHASSIS) -// .weight(BigDecimal.valueOf(-5)) -// .information("lightweight front wing v2 (black)"); -// String requestBody = mapper.writeValueAsString(componentDto); -// -// mockMvc.perform( -// post("/carComponent") -// .accept(MediaType.APPLICATION_JSON) -// .contentType(MediaType.APPLICATION_JSON) -// .content(requestBody)) -// .andExpect(status().is5xxServerError()); -// } -// -// @Test -// void testCreateDriver() throws Exception { -// log.info("testCreateDriver running"); -// -// DriverDto driverDto = new DriverDto() -// .name("John") -// .surname("Doe") -// .nationality("Italian") -// .height(189) -// .birthday(LocalDate.of(1990, 10, 1)) -// .addCharacteristicsItem(Characteristic.AGGRESSIVENESS); -// String requestBody = mapper.writeValueAsString(driverDto); -// -// String response = mockMvc.perform( -// post("/driver") -// .accept(MediaType.APPLICATION_JSON) -// .contentType(MediaType.APPLICATION_JSON) -// .content(requestBody)) -// .andExpect(status().isOk()) -// .andReturn().getResponse().getContentAsString(); -// log.info("response: {}", response); -// -// Driver received = mapper.readValue(response, Driver.class); -// Assertions.assertEquals("John", received.getName()); -// Assertions.assertEquals("Doe", received.getSurname()); -// Assertions.assertEquals("Italian", received.getNationality()); -// Assertions.assertEquals(189, received.getHeight()); -// Assertions.assertEquals(LocalDate.of(1990, 10, 1), received.getBirthday()); -// } -// -// @Test -// void testCreateDriverWrongData() throws Exception { -// log.info("testCreateDriverWrongData running"); -// -// DriverDto driverDto = new DriverDto() -// .name("John") -// .surname("Doe") -// .nationality("Italian") -// .height(50) -// .birthday(LocalDate.of(2999, 10, 1)) -// .addCharacteristicsItem(Characteristic.AGGRESSIVENESS); -// String requestBody = mapper.writeValueAsString(driverDto); -// -// mockMvc.perform( -// post("/driver") -// .accept(MediaType.APPLICATION_JSON) -// .contentType(MediaType.APPLICATION_JSON) -// .content(requestBody)) -// .andExpect(status().is5xxServerError()); -// } -// -// @Test -// void testCreateDepartment() throws Exception { -// log.info("testCreateDepartment() running"); -// -// DepartmentDto departmentDto = new DepartmentDto().specialization("aero"); -// String requestBody = mapper.writeValueAsString(departmentDto); -// -// String response = mockMvc.perform( -// post("/department") -// .accept(MediaType.APPLICATION_JSON) -// .contentType(MediaType.APPLICATION_JSON) -// .content(requestBody)) -// .andExpect(status().isOk()) -// .andReturn().getResponse().getContentAsString(); -// log.info("response: {}", response); -// -// Department received = mapper.readValue(response, Department.class); -// Assertions.assertEquals("aero", received.getSpecialization()); -// } -// -// @Test -// void testCreateDepartmentWrongData() throws Exception { -// log.info("testCreateDepartmentWrongData() running"); -// -// DepartmentDto departmentDto = new DepartmentDto().specialization(""); -// String requestBody = mapper.writeValueAsString(departmentDto); -// -// mockMvc.perform( -// post("/department") -// .accept(MediaType.APPLICATION_JSON) -// .contentType(MediaType.APPLICATION_JSON) -// .content(requestBody)) -// .andExpect(status().is5xxServerError()); -// } -// -// @Test -// void testComplexScenario1() throws Exception { -// log.info("testComplexScenario1() running"); -// -// Car car = loadEmptyCar(); -// Driver driver = loadDriver(); -// List<CarComponent> components = loadComponents(); -// -// String carWithDriverResponse = mockMvc.perform( -// put("/car/driver?carId=" + car.getId() + "&driverId=" + driver.getId()) -// .accept(MediaType.APPLICATION_JSON)) -// .andReturn().getResponse().getContentAsString(); -// -// Car carWithDriver = mapper.readValue(carWithDriverResponse, Car.class); -// car.setDriver(driver); -// Assertions.assertEquals(car, carWithDriver); -// -// car.setComponents(new HashSet<>(components)); -// String carUpdateRequestQuery = components.stream().map(CarComponent::getId).map(String::valueOf).collect(Collectors.joining("&componentIds=")); -// String carWithComponentsResponse = mockMvc.perform( -// patch("/car/" + car.getId() + "?componentIds=" + carUpdateRequestQuery) -// .accept(MediaType.APPLICATION_JSON)) -// .andExpect(status().isOk()).andReturn().getResponse().getContentAsString(); -// -// Car carWithComponents = mapper.readValue(carWithComponentsResponse, Car.class); -// Assertions.assertEquals(car, carWithComponents); -// -// mockMvc.perform( -// delete("/car/driver?carId=" + car.getId())) -// .andExpect(status().isOk()); -// -// String findCarDeletedDriver = mockMvc.perform( -// get("/car/" + car.getId())).andExpect(status().isOk()).andReturn().getResponse().getContentAsString(); -// -// Car carDeletedDriver = mapper.readValue(findCarDeletedDriver, Car.class); -// Assertions.assertNull(carDeletedDriver.getDriver()); -// -// mockMvc.perform(delete("/car/" + car.getId())).andExpect(status().isOk()); -// -// mockMvc.perform(get("/car/" + car.getId())).andExpect(status().isNotFound()); -// } -// -// @Test -// void testComplexScenario2() throws Exception { -// log.info("testComplexScenario1() running"); -// -// Department department = loadEmptyDepartment(); -// Engineer engineer = loadEngineer(); -// -// String departmentWithEngineerResponse = mockMvc.perform( -// put("/department/" + department.getId() + "?engineerId=" + engineer.getId()) -// .accept(MediaType.APPLICATION_JSON)) -// .andExpect(status().isOk()).andReturn().getResponse().getContentAsString(); -// -// Department departmentWithEngineer = mapper.readValue(departmentWithEngineerResponse, Department.class); -// -// department.addEngineer(engineer); -// Assertions.assertEquals(1, departmentWithEngineer.getEngineers().size()); -// Assertions.assertEquals(department, departmentWithEngineer); -// -// mockMvc.perform(delete("/department/" + department.getId())).andExpect(status().isOk()); -// -// mockMvc.perform(get("/department/" + department.getId())).andExpect(status().isNotFound()); -// } -// -// private Department loadEmptyDepartment() throws Exception { -// DepartmentDto departmentRequest = new DepartmentDto().specialization("aero").engineers(new ArrayList<>()); -// String departmentRequestBody = mapper.writeValueAsString(departmentRequest); -// String departmentResponse = mockMvc.perform(post("/department") -// .accept(MediaType.APPLICATION_JSON) -// .contentType(MediaType.APPLICATION_JSON) -// .content(departmentRequestBody)) -// .andExpect(status().isOk()) -// .andReturn().getResponse().getContentAsString(); -// Department received = mapper.readValue(departmentResponse, Department.class); -// received.setEngineers(new HashSet<>()); -// return received; -// } -// -// private Engineer loadEngineer() throws Exception { -// EngineerDto engineerRequest = new EngineerDto().name("John").surname("Doe"); -// String engineerRequestBody = mapper.writeValueAsString(engineerRequest); -// String engineerResponse = mockMvc.perform(post("/engineer") -// .accept(MediaType.APPLICATION_JSON) -// .contentType(MediaType.APPLICATION_JSON) -// .content(engineerRequestBody)) -// .andExpect(status().isOk()) -// .andReturn().getResponse().getContentAsString(); -// return mapper.readValue(engineerResponse, Engineer.class); -// } -// -// private Car loadEmptyCar() throws Exception { -// CarDto carRequest = new CarDto(); -// String carRequestBody = mapper.writeValueAsString(carRequest); -// String carResponse = mockMvc.perform(post("/car") -// .accept(MediaType.APPLICATION_JSON) -// .contentType(MediaType.APPLICATION_JSON) -// .content(carRequestBody)) -// .andExpect(status().isOk()) -// .andReturn().getResponse().getContentAsString(); -// return mapper.readValue(carResponse, Car.class); -// } -// -// private Driver loadDriver() throws Exception { -// DriverDto driverRequest = new DriverDto() -// .name("John") -// .surname("Doe") -// .nationality("Italian") -// .height(189) -// .birthday(LocalDate.of(1990, 10, 1)) -// .addCharacteristicsItem(Characteristic.AGGRESSIVENESS); -// String driverRequestBody = mapper.writeValueAsString(driverRequest); -// -// String driverResponse = mockMvc.perform(post("/driver") -// .accept(MediaType.APPLICATION_JSON) -// .contentType(MediaType.APPLICATION_JSON) -// .content(driverRequestBody)) -// .andExpect(status().isOk()) -// .andReturn().getResponse().getContentAsString(); -// -// return mapper.readValue(driverResponse, Driver.class); -// } -// -// private List<CarComponent> loadComponents() throws Exception { -// CarComponentDto componentRequestChassis = new CarComponentDto() -// .componentType(CarComponentType.CHASSIS) -// .weight(BigDecimal.valueOf(50.5)) -// .information("new aerodynamic chassis"); -// CarComponentDto componentRequestEngine = new CarComponentDto() -// .componentType(CarComponentType.ENGINE) -// .weight(BigDecimal.valueOf(100)) -// .information("honda engine v2 500HP"); -// String componentRequestBodyChassis = mapper.writeValueAsString(componentRequestChassis); -// String componentRequestBodyEngine = mapper.writeValueAsString(componentRequestEngine); -// String componentResponseChassis = mockMvc.perform(post("/carComponent") -// .accept(MediaType.APPLICATION_JSON) -// .contentType(MediaType.APPLICATION_JSON) -// .content(componentRequestBodyChassis)) -// .andExpect(status().isOk()) -// .andReturn().getResponse().getContentAsString(); -// String componentResponseEngine = mockMvc.perform(post("/carComponent") -// .accept(MediaType.APPLICATION_JSON) -// .contentType(MediaType.APPLICATION_JSON) -// .content(componentRequestBodyEngine)) -// .andExpect(status().isOk()) -// .andReturn().getResponse().getContentAsString(); -// CarComponent componentChassis = mapper.readValue(componentResponseChassis, CarComponent.class); -// CarComponent componentEngine = mapper.readValue(componentResponseEngine, CarComponent.class); -// return List.of(componentChassis, componentEngine); -// } -// -//} -// diff --git a/notification/src/test/java/cz/muni/pa165/rest/IntegrationTests.java b/notification/src/test/java/cz/muni/pa165/rest/NotificationIT.java similarity index 95% rename from notification/src/test/java/cz/muni/pa165/rest/IntegrationTests.java rename to notification/src/test/java/cz/muni/pa165/rest/NotificationIT.java index f05e0787f0cb302dfa48f455fefe2cfdb97e0cb5..9bb60eb71a5561d8b62b37c747686d0dec765306 100644 --- a/notification/src/test/java/cz/muni/pa165/rest/IntegrationTests.java +++ b/notification/src/test/java/cz/muni/pa165/rest/NotificationIT.java @@ -19,9 +19,9 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. */ @SpringBootTest @AutoConfigureMockMvc -class IntegrationTests { +class NotificationIT { - private static final Logger log = LoggerFactory.getLogger(IntegrationTests.class); + private static final Logger log = LoggerFactory.getLogger(NotificationIT.class); @Autowired private MockMvc mockMvc; diff --git a/visualization/src/test/java/cz/muni/pa165/rest/IntegrationTests.java b/visualization/src/test/java/cz/muni/pa165/rest/VisualizationIT.java similarity index 100% rename from visualization/src/test/java/cz/muni/pa165/rest/IntegrationTests.java rename to visualization/src/test/java/cz/muni/pa165/rest/VisualizationIT.java