From a92756154decc21f89f850bf51066084a67438b1 Mon Sep 17 00:00:00 2001 From: Harry9656 Date: Mon, 4 Mar 2024 21:59:42 +0100 Subject: [PATCH] [JAVA-32077] Fix broken spring-boot-graphql module integration tests. (#16026) --- spring-boot-modules/pom.xml | 2 +- .../spring-boot-graphql/pom.xml | 1 + .../controller/VehicleController.java | 6 ++--- .../error/handling/domain/Location.java | 9 ++++---- .../error/handling/domain/Vehicle.java | 6 ++++- .../VehicleAlreadyPresentException.java | 4 ---- .../handling/service/InventoryService.java | 16 +++++++++---- .../graphql/intro/GraphqlApplication.java | 6 ++--- .../resources/application-error-handling.yml | 7 ++++-- .../src/main/resources/import.sql | 6 ++--- ...BooksControllerGraphQLIntegrationTest.java | 20 ++++++++-------- .../GraphQLErrorHandlerIntegrationTest.java | 23 ++++++++++--------- .../graphql/intro/SpringContextTest.java | 4 ++-- 13 files changed, 60 insertions(+), 50 deletions(-) diff --git a/spring-boot-modules/pom.xml b/spring-boot-modules/pom.xml index 4f70896803..221c3876d4 100644 --- a/spring-boot-modules/pom.xml +++ b/spring-boot-modules/pom.xml @@ -41,7 +41,7 @@ spring-boot-environment spring-boot-exceptions spring-boot-flowable - + spring-boot-graphql spring-boot-jasypt diff --git a/spring-boot-modules/spring-boot-graphql/pom.xml b/spring-boot-modules/spring-boot-graphql/pom.xml index b3f27f1b84..cff039283d 100644 --- a/spring-boot-modules/spring-boot-graphql/pom.xml +++ b/spring-boot-modules/spring-boot-graphql/pom.xml @@ -119,6 +119,7 @@ 1.6.2 3.3.2 1.7.0 + com.baeldung.chooseapi.ChooseApiApp \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/controller/VehicleController.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/controller/VehicleController.java index 21aa1c7d47..b34bef0578 100644 --- a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/controller/VehicleController.java +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/controller/VehicleController.java @@ -35,9 +35,9 @@ public class VehicleController { } @MutationMapping - public Vehicle addVehicle(@Argument String vin, @Argument Integer year, - @Argument String make, @Argument String model, @Argument String trim, - @Argument Location location) { + public Vehicle addVehicle(@Argument("vin") String vin, @Argument("year") Integer year, + @Argument("make") String make, @Argument("model") String model, @Argument("trim") String trim, + @Argument("location") Location location) { return this.inventoryService.addVehicle(vin, year, make, model, trim, location); } } diff --git a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/domain/Location.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/domain/Location.java index 815bf3a26a..411ae5f382 100644 --- a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/domain/Location.java +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/domain/Location.java @@ -5,10 +5,10 @@ import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; -import javax.persistence.Entity; -import javax.persistence.FetchType; -import javax.persistence.Id; -import javax.persistence.OneToMany; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.Id; +import jakarta.persistence.OneToMany; import java.util.ArrayList; import java.util.List; @@ -20,7 +20,6 @@ import java.util.List; public class Location { @Id private String zipcode; - private String city; private String state; diff --git a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/domain/Vehicle.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/domain/Vehicle.java index e206bdb009..36104347dd 100644 --- a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/domain/Vehicle.java +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/domain/Vehicle.java @@ -1,11 +1,15 @@ package com.baeldung.graphql.error.handling.domain; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; -import javax.persistence.*; @Data @Entity diff --git a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/exception/VehicleAlreadyPresentException.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/exception/VehicleAlreadyPresentException.java index 8f6f0ce615..c22604dd8f 100644 --- a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/exception/VehicleAlreadyPresentException.java +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/exception/VehicleAlreadyPresentException.java @@ -4,10 +4,6 @@ import java.util.Map; public class VehicleAlreadyPresentException extends AbstractGraphQLException { - public VehicleAlreadyPresentException(String message) { - super(message); - } - public VehicleAlreadyPresentException(String message, Map additionParams) { super(message, additionParams); } diff --git a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/service/InventoryService.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/service/InventoryService.java index 9b8d3716d6..45441d45b7 100644 --- a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/service/InventoryService.java +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/error/handling/service/InventoryService.java @@ -1,5 +1,14 @@ package com.baeldung.graphql.error.handling.service; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; + import com.baeldung.graphql.error.handling.domain.Location; import com.baeldung.graphql.error.handling.domain.Vehicle; import com.baeldung.graphql.error.handling.exception.InvalidInputException; @@ -7,11 +16,8 @@ import com.baeldung.graphql.error.handling.exception.VehicleAlreadyPresentExcept import com.baeldung.graphql.error.handling.exception.VehicleNotFoundException; import com.baeldung.graphql.error.handling.repository.InventoryRepository; import com.baeldung.graphql.error.handling.repository.LocationRepository; -import org.apache.commons.lang3.StringUtils; -import org.springframework.stereotype.Service; -import javax.transaction.Transactional; -import java.util.*; +import jakarta.transaction.Transactional; @Service public class InventoryService { @@ -49,7 +55,7 @@ public class InventoryService { } public List searchByLocation(String zipcode) { - if (StringUtils.isEmpty(zipcode) || zipcode.length() != 5) { + if (StringUtils.hasText(zipcode) || zipcode.length() != 5) { throw new InvalidInputException("Invalid zipcode " + zipcode + " provided."); } return this.locationRepository.findById(zipcode) diff --git a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/intro/GraphqlApplication.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/intro/GraphqlApplication.java index 3910b4331b..bf603da84c 100644 --- a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/intro/GraphqlApplication.java +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/intro/GraphqlApplication.java @@ -6,10 +6,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; -@SpringBootApplication -@EnableAutoConfiguration(exclude = { - SecurityAutoConfiguration.class, - HibernateJpaAutoConfiguration.class +@SpringBootApplication(exclude = { + SecurityAutoConfiguration.class }) public class GraphqlApplication { diff --git a/spring-boot-modules/spring-boot-graphql/src/main/resources/application-error-handling.yml b/spring-boot-modules/spring-boot-graphql/src/main/resources/application-error-handling.yml index 298eeb16c1..ee0dd19d7c 100644 --- a/spring-boot-modules/spring-boot-graphql/src/main/resources/application-error-handling.yml +++ b/spring-boot-modules/spring-boot-graphql/src/main/resources/application-error-handling.yml @@ -10,8 +10,6 @@ spring: driverClassName: "org.h2.Driver" username: sa password: - initialization-mode: always - platform: h2 jpa: show-sql: true properties: @@ -19,6 +17,11 @@ spring: dialect: org.hibernate.dialect.H2Dialect ddl-auto: none globally_quoted_identifiers: true + defer-datasource-initialization: true h2: console.enabled: true + sql: + init: + platform: h2 + mode: always \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-graphql/src/main/resources/import.sql b/spring-boot-modules/spring-boot-graphql/src/main/resources/import.sql index 647d17a76d..f925f325fe 100644 --- a/spring-boot-modules/spring-boot-graphql/src/main/resources/import.sql +++ b/spring-boot-modules/spring-boot-graphql/src/main/resources/import.sql @@ -1,6 +1,6 @@ -insert into "location" values('07092', 'Mountainside', 'NJ'); -insert into "location" values ('94118', 'San Francisco', 'CA'); -insert into "location" values ('10002', 'New York', 'NY'); +insert into "location" ("zipcode", "city", "state") values ('07092', 'Mountainside', 'NJ'); +insert into "location" ("zipcode", "city", "state") values ('94118', 'San Francisco', 'CA'); +insert into "location" ("zipcode", "city", "state") values ('10002', 'New York', 'NY'); insert into "vehicle" ("vin", "year", "make", "model", "trim", "fk_location") values('KM8JN72DX7U587496', 2007, 'Hyundai', 'Tucson', null, '07092'); insert into "vehicle" ("vin", "year", "make", "model", "trim", "fk_location") values('JTKKU4B41C1023346', 2012, 'Toyota', 'Scion', 'Xd', '94118'); diff --git a/spring-boot-modules/spring-boot-graphql/src/test/java/com/baeldung/chooseapi/controllers/BooksControllerGraphQLIntegrationTest.java b/spring-boot-modules/spring-boot-graphql/src/test/java/com/baeldung/chooseapi/controllers/BooksControllerGraphQLIntegrationTest.java index 48ed73fbde..8e79e573d5 100644 --- a/spring-boot-modules/spring-boot-graphql/src/test/java/com/baeldung/chooseapi/controllers/BooksControllerGraphQLIntegrationTest.java +++ b/spring-boot-modules/spring-boot-graphql/src/test/java/com/baeldung/chooseapi/controllers/BooksControllerGraphQLIntegrationTest.java @@ -1,20 +1,22 @@ package com.baeldung.chooseapi.controllers; -import com.baeldung.chooseapi.ChooseApiApp; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.graphql.test.tester.HttpGraphQlTester; -import org.springframework.test.context.ActiveProfiles; - import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, - properties = { "grpc.server.port=-1" }, // Disable gRPC external server +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.graphql.tester.AutoConfigureHttpGraphQlTester; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.graphql.test.tester.HttpGraphQlTester; +import org.springframework.test.context.ActiveProfiles; + +import com.baeldung.chooseapi.ChooseApiApp; + +@SpringBootTest(properties = { "grpc.server.port=-1" }, // Disable gRPC external server classes = ChooseApiApp.class) @ActiveProfiles("chooseapi") +@AutoConfigureHttpGraphQlTester class BooksControllerGraphQLIntegrationTest { @Autowired diff --git a/spring-boot-modules/spring-boot-graphql/src/test/java/com/baeldung/graphql/error/handling/GraphQLErrorHandlerIntegrationTest.java b/spring-boot-modules/spring-boot-graphql/src/test/java/com/baeldung/graphql/error/handling/GraphQLErrorHandlerIntegrationTest.java index b9b88e921b..f09bd16d3c 100644 --- a/spring-boot-modules/spring-boot-graphql/src/test/java/com/baeldung/graphql/error/handling/GraphQLErrorHandlerIntegrationTest.java +++ b/spring-boot-modules/spring-boot-graphql/src/test/java/com/baeldung/graphql/error/handling/GraphQLErrorHandlerIntegrationTest.java @@ -1,24 +1,25 @@ package com.baeldung.graphql.error.handling; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; -import org.springframework.graphql.test.tester.HttpGraphQlTester; -import org.springframework.test.context.ActiveProfiles; +import static graphql.ErrorType.NullValueInNonNullableField; +import static org.springframework.graphql.execution.ErrorType.INTERNAL_ERROR; +import static org.springframework.graphql.execution.ErrorType.NOT_FOUND; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import static graphql.ErrorType.NullValueInNonNullableField; -import static org.springframework.graphql.execution.ErrorType.INTERNAL_ERROR; -import static org.springframework.graphql.execution.ErrorType.NOT_FOUND; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.graphql.tester.AutoConfigureHttpGraphQlTester; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.graphql.test.tester.HttpGraphQlTester; +import org.springframework.test.context.ActiveProfiles; -@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = GraphQLErrorHandlerApplication.class) +@SpringBootTest(classes = GraphQLErrorHandlerApplication.class) @ActiveProfiles("error-handling") -public class GraphQLErrorHandlerIntegrationTest { +@AutoConfigureHttpGraphQlTester +class GraphQLErrorHandlerIntegrationTest { private static final String GRAPHQL_TEST_REQUEST_PATH = "src/test/resources/graphql-files/request/%s_request.graphql"; private static final String GRAPHQL_TEST_RESPONSE_PATH = "src/test/resources/graphql-files/response/%s_response.json"; diff --git a/spring-boot-modules/spring-boot-graphql/src/test/java/com/baeldung/graphql/intro/SpringContextTest.java b/spring-boot-modules/spring-boot-graphql/src/test/java/com/baeldung/graphql/intro/SpringContextTest.java index a2e63a96f8..6e1307c8b7 100644 --- a/spring-boot-modules/spring-boot-graphql/src/test/java/com/baeldung/graphql/intro/SpringContextTest.java +++ b/spring-boot-modules/spring-boot-graphql/src/test/java/com/baeldung/graphql/intro/SpringContextTest.java @@ -4,9 +4,9 @@ import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest(classes = GraphqlApplication.class) -public class SpringContextTest { +class SpringContextTest { @Test - public void whenSpringContextIsBootstrapped_thenNoExceptions() { + void whenSpringContextIsBootstrapped_thenNoExceptions() { } }