From 3d6d0765a3c8be0fe8e18323fca7c35f4d6dd487 Mon Sep 17 00:00:00 2001 From: Philippe Date: Sun, 19 Jan 2020 13:26:41 -0300 Subject: [PATCH 1/5] [BAEL-3311] Spring Cloud Gateway Routing Predicate Factories --- spring-cloud/spring-cloud-gateway/pom.xml | 4 + .../CustomRoutersGatewayApplication.java | 15 +++ .../config/CustomPredicatesConfig.java | 38 +++++++ .../GoldenCustomerRoutePredicateFactory.java | 104 ++++++++++++++++++ .../service/GoldenCustomerService.java | 26 +++++ .../resources/application-customroutes.yml | 26 +++++ 6 files changed, 213 insertions(+) create mode 100644 spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/custompredicates/CustomRoutersGatewayApplication.java create mode 100644 spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/custompredicates/config/CustomPredicatesConfig.java create mode 100644 spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/custompredicates/factories/GoldenCustomerRoutePredicateFactory.java create mode 100644 spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/custompredicates/service/GoldenCustomerService.java create mode 100644 spring-cloud/spring-cloud-gateway/src/main/resources/application-customroutes.yml diff --git a/spring-cloud/spring-cloud-gateway/pom.xml b/spring-cloud/spring-cloud-gateway/pom.xml index 10cd49cc04..0f62c031cf 100644 --- a/spring-cloud/spring-cloud-gateway/pom.xml +++ b/spring-cloud/spring-cloud-gateway/pom.xml @@ -68,6 +68,10 @@ spring-boot-starter-test test + + org.springframework.boot + spring-boot-devtools + diff --git a/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/custompredicates/CustomRoutersGatewayApplication.java b/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/custompredicates/CustomRoutersGatewayApplication.java new file mode 100644 index 0000000000..b7897edfa3 --- /dev/null +++ b/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/custompredicates/CustomRoutersGatewayApplication.java @@ -0,0 +1,15 @@ +package com.baeldung.springcloudgateway.custompredicates; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; + +@SpringBootApplication +public class CustomRoutersGatewayApplication { + + public static void main(String[] args) { + new SpringApplicationBuilder(CustomRoutersGatewayApplication.class) + .profiles("customroutes") + .run(args); + } + +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/custompredicates/config/CustomPredicatesConfig.java b/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/custompredicates/config/CustomPredicatesConfig.java new file mode 100644 index 0000000000..ec75f2152c --- /dev/null +++ b/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/custompredicates/config/CustomPredicatesConfig.java @@ -0,0 +1,38 @@ +package com.baeldung.springcloudgateway.custompredicates.config; + +import org.springframework.cloud.gateway.filter.GatewayFilter; +import org.springframework.cloud.gateway.route.RouteLocator; +import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.baeldung.springcloudgateway.custompredicates.factories.GoldenCustomerRoutePredicateFactory; +import com.baeldung.springcloudgateway.custompredicates.factories.GoldenCustomerRoutePredicateFactory.Config; +import com.baeldung.springcloudgateway.custompredicates.service.GoldenCustomerService; + +@Configuration +public class CustomPredicatesConfig { + + + @Bean + public GoldenCustomerRoutePredicateFactory goldenCustomer(GoldenCustomerService goldenCustomerService) { + return new GoldenCustomerRoutePredicateFactory(goldenCustomerService); + } + + + //@Bean + public RouteLocator routes(RouteLocatorBuilder builder, GoldenCustomerRoutePredicateFactory gf ) { + + return builder.routes() + .route("dsl_golden_route", r -> r.path("/dsl_api/**") + .filters(f -> f.stripPrefix(1)) + .uri("https://httpbin.org") + .predicate(gf.apply(new Config(true, "customerId")))) + .route("dsl_common_route", r -> r.path("/dsl_api/**") + .filters(f -> f.stripPrefix(1)) + .uri("https://httpbin.org") + .predicate(gf.apply(new Config(false, "customerId")))) + .build(); + } + +} diff --git a/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/custompredicates/factories/GoldenCustomerRoutePredicateFactory.java b/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/custompredicates/factories/GoldenCustomerRoutePredicateFactory.java new file mode 100644 index 0000000000..a60932bd91 --- /dev/null +++ b/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/custompredicates/factories/GoldenCustomerRoutePredicateFactory.java @@ -0,0 +1,104 @@ +/** + * + */ +package com.baeldung.springcloudgateway.custompredicates.factories; + +import java.util.Arrays; +import java.util.List; +import java.util.function.Predicate; + +import javax.validation.constraints.NotEmpty; + +import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory; +import org.springframework.http.HttpCookie; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.server.ServerWebExchange; + +import com.baeldung.springcloudgateway.custompredicates.service.GoldenCustomerService; + +/** + * @author Philippe + * + */ +public class GoldenCustomerRoutePredicateFactory extends AbstractRoutePredicateFactory { + + private final GoldenCustomerService goldenCustomerService; + + public GoldenCustomerRoutePredicateFactory(GoldenCustomerService goldenCustomerService ) { + super(Config.class); + this.goldenCustomerService = goldenCustomerService; + } + + + @Override + public List shortcutFieldOrder() { + return Arrays.asList("isGolden","customerIdCookie"); + } + + + @Override + public Predicate apply(Config config) { + + return (ServerWebExchange t) -> { + List cookies = t.getRequest() + .getCookies() + .get(config.getCustomerIdCookie()); + + boolean isGolden; + if ( cookies == null || cookies.isEmpty()) { + isGolden = false; + } + else { + String customerId = cookies.get(0).getValue(); + isGolden = goldenCustomerService.isGoldenCustomer(customerId); + } + + return config.isGolden()?isGolden:!isGolden; + }; + + } + + + @Validated + public static class Config { + + boolean isGolden = true; + + @NotEmpty + String customerIdCookie = "customerId"; + + + public Config() {} + + public Config( boolean isGolden, String customerIdCookie) { + this.isGolden = isGolden; + this.customerIdCookie = customerIdCookie; + } + + public boolean isGolden() { + return isGolden; + } + + public void setGolden(boolean value) { + this.isGolden = value; + } + + /** + * @return the customerIdCookie + */ + public String getCustomerIdCookie() { + return customerIdCookie; + } + + /** + * @param customerIdCookie the customerIdCookie to set + */ + public void setCustomerIdCookie(String customerIdCookie) { + this.customerIdCookie = customerIdCookie; + } + + + + } + +} diff --git a/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/custompredicates/service/GoldenCustomerService.java b/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/custompredicates/service/GoldenCustomerService.java new file mode 100644 index 0000000000..82bf2e6ae9 --- /dev/null +++ b/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/custompredicates/service/GoldenCustomerService.java @@ -0,0 +1,26 @@ +/** + * + */ +package com.baeldung.springcloudgateway.custompredicates.service; + +import org.springframework.stereotype.Component; + +/** + * @author Philippe + * + */ +@Component +public class GoldenCustomerService { + + public boolean isGoldenCustomer(String customerId) { + + // TODO: Add some AI logic to check is this customer deserves a "golden" status ;^) + if ( "baeldung".equalsIgnoreCase(customerId)) { + return true; + } + else { + return false; + } + } + +} diff --git a/spring-cloud/spring-cloud-gateway/src/main/resources/application-customroutes.yml b/spring-cloud/spring-cloud-gateway/src/main/resources/application-customroutes.yml new file mode 100644 index 0000000000..859aa60bda --- /dev/null +++ b/spring-cloud/spring-cloud-gateway/src/main/resources/application-customroutes.yml @@ -0,0 +1,26 @@ +spring: + cloud: + gateway: + routes: + - id: golden_route + uri: https://httpbin.org + predicates: + - Path=/api/** + - GoldenCustomer=true + filters: + - StripPrefix=1 + - AddRequestHeader=GoldenCustomer,true + - id: common_route + uri: https://httpbin.org + predicates: + - Path=/api/** + - name: GoldenCustomer + args: + golden: false + customerIdCookie: customerId + filters: + - StripPrefix=1 + - AddRequestHeader=GoldenCustomer,false + + + \ No newline at end of file From 68dee742007e87c5a10e67205072eee209750a91 Mon Sep 17 00:00:00 2001 From: Philippe Date: Sun, 19 Jan 2020 13:46:57 -0300 Subject: [PATCH 2/5] [BAEL-3311] Code formatting --- .../CustomRoutersGatewayApplication.java | 4 +-- .../config/CustomPredicatesConfig.java | 18 ++++++------- .../GoldenCustomerRoutePredicateFactory.java | 26 +++++++++---------- 3 files changed, 23 insertions(+), 25 deletions(-) diff --git a/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/custompredicates/CustomRoutersGatewayApplication.java b/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/custompredicates/CustomRoutersGatewayApplication.java index b7897edfa3..18a36952fd 100644 --- a/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/custompredicates/CustomRoutersGatewayApplication.java +++ b/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/custompredicates/CustomRoutersGatewayApplication.java @@ -8,8 +8,8 @@ public class CustomRoutersGatewayApplication { public static void main(String[] args) { new SpringApplicationBuilder(CustomRoutersGatewayApplication.class) - .profiles("customroutes") - .run(args); + .profiles("customroutes") + .run(args); } } \ No newline at end of file diff --git a/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/custompredicates/config/CustomPredicatesConfig.java b/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/custompredicates/config/CustomPredicatesConfig.java index ec75f2152c..0e88b29bcf 100644 --- a/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/custompredicates/config/CustomPredicatesConfig.java +++ b/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/custompredicates/config/CustomPredicatesConfig.java @@ -24,15 +24,15 @@ public class CustomPredicatesConfig { public RouteLocator routes(RouteLocatorBuilder builder, GoldenCustomerRoutePredicateFactory gf ) { return builder.routes() - .route("dsl_golden_route", r -> r.path("/dsl_api/**") - .filters(f -> f.stripPrefix(1)) - .uri("https://httpbin.org") - .predicate(gf.apply(new Config(true, "customerId")))) - .route("dsl_common_route", r -> r.path("/dsl_api/**") - .filters(f -> f.stripPrefix(1)) - .uri("https://httpbin.org") - .predicate(gf.apply(new Config(false, "customerId")))) - .build(); + .route("dsl_golden_route", r -> r.path("/dsl_api/**") + .filters(f -> f.stripPrefix(1)) + .uri("https://httpbin.org") + .predicate(gf.apply(new Config(true, "customerId")))) + .route("dsl_common_route", r -> r.path("/dsl_api/**") + .filters(f -> f.stripPrefix(1)) + .uri("https://httpbin.org") + .predicate(gf.apply(new Config(false, "customerId")))) + .build(); } } diff --git a/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/custompredicates/factories/GoldenCustomerRoutePredicateFactory.java b/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/custompredicates/factories/GoldenCustomerRoutePredicateFactory.java index a60932bd91..cb5c3a0b50 100644 --- a/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/custompredicates/factories/GoldenCustomerRoutePredicateFactory.java +++ b/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/custompredicates/factories/GoldenCustomerRoutePredicateFactory.java @@ -41,27 +41,25 @@ public class GoldenCustomerRoutePredicateFactory extends AbstractRoutePredicateF return (ServerWebExchange t) -> { List cookies = t.getRequest() - .getCookies() - .get(config.getCustomerIdCookie()); + .getCookies() + .get(config.getCustomerIdCookie()); - boolean isGolden; - if ( cookies == null || cookies.isEmpty()) { - isGolden = false; - } - else { - String customerId = cookies.get(0).getValue(); - isGolden = goldenCustomerService.isGoldenCustomer(customerId); - } + boolean isGolden; + if ( cookies == null || cookies.isEmpty()) { + isGolden = false; + } + else { + String customerId = cookies.get(0).getValue(); + isGolden = goldenCustomerService.isGoldenCustomer(customerId); + } - return config.isGolden()?isGolden:!isGolden; + return config.isGolden()?isGolden:!isGolden; }; - } @Validated - public static class Config { - + public static class Config { boolean isGolden = true; @NotEmpty From 69005500f165b76892ad0770d8a114657ccb2a53 Mon Sep 17 00:00:00 2001 From: Philippe Date: Tue, 21 Jan 2020 00:09:15 -0300 Subject: [PATCH 3/5] [BAEL-3311] LiveTests --- ....java => CustomPredicatesApplication.java} | 4 +- .../gatewayapp/CustomFiltersLiveTest.java | 2 + .../CustomPredicatesApplicationLiveTest.java | 67 +++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) rename spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/custompredicates/{CustomRoutersGatewayApplication.java => CustomPredicatesApplication.java} (72%) create mode 100644 spring-cloud/spring-cloud-gateway/src/test/java/com/baeldung/springcloudgateway/custompredicates/CustomPredicatesApplicationLiveTest.java diff --git a/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/custompredicates/CustomRoutersGatewayApplication.java b/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/custompredicates/CustomPredicatesApplication.java similarity index 72% rename from spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/custompredicates/CustomRoutersGatewayApplication.java rename to spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/custompredicates/CustomPredicatesApplication.java index 18a36952fd..e209b6cdf0 100644 --- a/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/custompredicates/CustomRoutersGatewayApplication.java +++ b/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/custompredicates/CustomPredicatesApplication.java @@ -4,10 +4,10 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; @SpringBootApplication -public class CustomRoutersGatewayApplication { +public class CustomPredicatesApplication { public static void main(String[] args) { - new SpringApplicationBuilder(CustomRoutersGatewayApplication.class) + new SpringApplicationBuilder(CustomPredicatesApplication.class) .profiles("customroutes") .run(args); } diff --git a/spring-cloud/spring-cloud-gateway/src/test/java/com/baeldung/springcloudgateway/customfilters/gatewayapp/CustomFiltersLiveTest.java b/spring-cloud/spring-cloud-gateway/src/test/java/com/baeldung/springcloudgateway/customfilters/gatewayapp/CustomFiltersLiveTest.java index a4bb3f8068..f49f8c68b6 100644 --- a/spring-cloud/spring-cloud-gateway/src/test/java/com/baeldung/springcloudgateway/customfilters/gatewayapp/CustomFiltersLiveTest.java +++ b/spring-cloud/spring-cloud-gateway/src/test/java/com/baeldung/springcloudgateway/customfilters/gatewayapp/CustomFiltersLiveTest.java @@ -5,6 +5,7 @@ import static org.assertj.core.api.Assertions.assertThat; import org.assertj.core.api.Condition; import org.junit.jupiter.api.BeforeEach; 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.boot.web.server.LocalServerPort; @@ -27,6 +28,7 @@ public class CustomFiltersLiveTest { @LocalServerPort String port; + @Autowired private WebTestClient client; @BeforeEach diff --git a/spring-cloud/spring-cloud-gateway/src/test/java/com/baeldung/springcloudgateway/custompredicates/CustomPredicatesApplicationLiveTest.java b/spring-cloud/spring-cloud-gateway/src/test/java/com/baeldung/springcloudgateway/custompredicates/CustomPredicatesApplicationLiveTest.java new file mode 100644 index 0000000000..d9988ceb5e --- /dev/null +++ b/spring-cloud/spring-cloud-gateway/src/test/java/com/baeldung/springcloudgateway/custompredicates/CustomPredicatesApplicationLiveTest.java @@ -0,0 +1,67 @@ +package com.baeldung.springcloudgateway.custompredicates; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; + +import java.net.URI; + +import org.json.JSONException; +import org.json.JSONObject; +import org.json.JSONTokener; +import org.junit.Before; +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.boot.test.web.client.TestRestTemplate; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.http.HttpStatus; +import org.springframework.http.RequestEntity; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.ActiveProfiles; + +/** + * This test requires + */ +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +@ActiveProfiles("customroutes") +public class CustomPredicatesApplicationLiveTest { + + @LocalServerPort + String serverPort; + + @Autowired + private TestRestTemplate restTemplate; + + @Test + void givenNormalCustomer_whenCallHeadersApi_thenResponseForNormalCustomer() throws JSONException { + + String url = "http://localhost:" + serverPort + "/api/headers"; + ResponseEntity response = restTemplate.getForEntity(url, String.class); + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); + + JSONObject json = new JSONObject(response.getBody()); + JSONObject headers = json.getJSONObject("headers"); + assertThat(headers.getString("Goldencustomer")).isEqualTo("false"); + + } + + @Test + void givenGoldenCustomer_whenCallHeadersApi_thenResponseForGoldenCustomer() throws JSONException { + + String url = "http://localhost:" + serverPort + "/api/headers"; + RequestEntity request = RequestEntity + .get(URI.create(url)) + .header("Cookie", "customerId=baeldung") + .build(); + + ResponseEntity response = restTemplate.exchange(request, String.class); + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); + + JSONObject json = new JSONObject(response.getBody()); + JSONObject headers = json.getJSONObject("headers"); + assertThat(headers.getString("Goldencustomer")).isEqualTo("true"); + + } + +} From 45de2abf96d1dadcdfea988e2074d6354fc5181c Mon Sep 17 00:00:00 2001 From: Philippe Date: Sun, 26 Jan 2020 18:04:24 -0300 Subject: [PATCH 4/5] [BAEL-3164] Rename module to spring-boot-persistence-2 --- .../spring-boot-persistence-2/HELP.md | 9 ++ .../spring-boot-persistence-2/README.md | 3 + .../spring-boot-persistence-2/pom.xml | 102 +++++++++++++++ .../baeldung/boot/jdbi/JdbiConfiguration.java | 57 +++++++++ .../boot/jdbi/SpringBootJdbiApplication.java | 15 +++ .../baeldung/boot/jdbi/dao/CarMakerDao.java | 35 +++++ .../baeldung/boot/jdbi/dao/CarModelDao.java | 28 ++++ .../baeldung/boot/jdbi/domain/CarMaker.java | 14 ++ .../baeldung/boot/jdbi/domain/CarModel.java | 14 ++ .../boot/jdbi/mapper/CarMakerMapper.java | 27 ++++ .../boot/jdbi/mapper/CarModelMapper.java | 25 ++++ .../boot/jdbi/service/CarMakerService.java | 48 +++++++ .../src/main/resources/application.yml | 1 + .../boot/jdbi/dao/CarMakerDao/findById.sql | 11 ++ .../boot/jdbi/dao/CarMakerDao/insert.sql | 4 + .../dao/CarModelDao/findByMakerIdAndSku.sql | 10 ++ .../boot/jdbi/dao/CarModelDao/insert.sql | 8 ++ .../SpringBootJdbiApplicationUnitTest.java | 121 ++++++++++++++++++ .../src/test/resources/data.sql | 12 ++ .../src/test/resources/schema.sql | 24 ++++ 20 files changed, 568 insertions(+) create mode 100644 persistence-modules/spring-boot-persistence-2/HELP.md create mode 100644 persistence-modules/spring-boot-persistence-2/README.md create mode 100644 persistence-modules/spring-boot-persistence-2/pom.xml create mode 100644 persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/JdbiConfiguration.java create mode 100644 persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/SpringBootJdbiApplication.java create mode 100644 persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/dao/CarMakerDao.java create mode 100644 persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/dao/CarModelDao.java create mode 100644 persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/domain/CarMaker.java create mode 100644 persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/domain/CarModel.java create mode 100644 persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/mapper/CarMakerMapper.java create mode 100644 persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/mapper/CarModelMapper.java create mode 100644 persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/service/CarMakerService.java create mode 100644 persistence-modules/spring-boot-persistence-2/src/main/resources/application.yml create mode 100644 persistence-modules/spring-boot-persistence-2/src/main/resources/com/baeldung/boot/jdbi/dao/CarMakerDao/findById.sql create mode 100644 persistence-modules/spring-boot-persistence-2/src/main/resources/com/baeldung/boot/jdbi/dao/CarMakerDao/insert.sql create mode 100644 persistence-modules/spring-boot-persistence-2/src/main/resources/com/baeldung/boot/jdbi/dao/CarModelDao/findByMakerIdAndSku.sql create mode 100644 persistence-modules/spring-boot-persistence-2/src/main/resources/com/baeldung/boot/jdbi/dao/CarModelDao/insert.sql create mode 100644 persistence-modules/spring-boot-persistence-2/src/test/java/com/baeldung/boot/jdbi/SpringBootJdbiApplicationUnitTest.java create mode 100644 persistence-modules/spring-boot-persistence-2/src/test/resources/data.sql create mode 100644 persistence-modules/spring-boot-persistence-2/src/test/resources/schema.sql diff --git a/persistence-modules/spring-boot-persistence-2/HELP.md b/persistence-modules/spring-boot-persistence-2/HELP.md new file mode 100644 index 0000000000..d5a5463718 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/HELP.md @@ -0,0 +1,9 @@ +# Getting Started + +### Reference Documentation +For further reference, please consider the following sections: + +* [Official Apache Maven documentation](https://maven.apache.org/guides/index.html) +* [Spring Boot DevTools](https://docs.spring.io/spring-boot/docs/{bootVersion}/reference/htmlsingle/#using-boot-devtools) +* [Spring Configuration Processor](https://docs.spring.io/spring-boot/docs/{bootVersion}/reference/htmlsingle/#configuration-metadata-annotation-processor) + diff --git a/persistence-modules/spring-boot-persistence-2/README.md b/persistence-modules/spring-boot-persistence-2/README.md new file mode 100644 index 0000000000..5d171fb2ca --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/README.md @@ -0,0 +1,3 @@ +### Relevant Articles: + +- [Using JDBI with Spring Boot](https://www.baeldung.com/spring-boot-jdbi) diff --git a/persistence-modules/spring-boot-persistence-2/pom.xml b/persistence-modules/spring-boot-persistence-2/pom.xml new file mode 100644 index 0000000000..51fa56bf17 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/pom.xml @@ -0,0 +1,102 @@ + + + 4.0.0 + com.baeldung.boot.persistence + spring-boot-persistence-2 + 0.0.1-SNAPSHOT + spring-boot-jdbi + Sample SpringBoot JDBI Project + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + ../../pom.xml + + + + + + org.springframework.boot + spring-boot-dependencies + 2.1.8.RELEASE + pom + import + + + + org.jdbi + jdbi3-spring4 + ${jdbi.version} + + + + org.jdbi + jdbi3-sqlobject + ${jdbi.version} + + + + + + + + org.springframework.boot + spring-boot-starter-jdbc + + + + org.jdbi + jdbi3-spring4 + + + + org.jdbi + jdbi3-sqlobject + + + + org.springframework.boot + spring-boot-devtools + runtime + true + + + com.h2database + h2 + runtime + + + org.springframework.boot + spring-boot-configuration-processor + true + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + 1.8 + 3.9.1 + + + diff --git a/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/JdbiConfiguration.java b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/JdbiConfiguration.java new file mode 100644 index 0000000000..ddbe6cc118 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/JdbiConfiguration.java @@ -0,0 +1,57 @@ +package com.baeldung.boot.jdbi; + +import java.util.List; + +import javax.sql.DataSource; + +import org.jdbi.v3.core.Jdbi; +import org.jdbi.v3.core.mapper.RowMapper; +import org.jdbi.v3.core.spi.JdbiPlugin; +import org.jdbi.v3.sqlobject.SqlObjectPlugin; +import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Lazy; +import org.springframework.jdbc.datasource.DataSourceTransactionManager; +import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy; +import org.springframework.transaction.PlatformTransactionManager; + +import com.baeldung.boot.jdbi.dao.CarMakerDao; +import com.baeldung.boot.jdbi.dao.CarModelDao; + +import lombok.extern.slf4j.Slf4j; + +@Configuration +@Slf4j +public class JdbiConfiguration { + @Bean + public Jdbi jdbi(DataSource ds,List jdbiPlugins, List> rowMappers) { + TransactionAwareDataSourceProxy proxy = new TransactionAwareDataSourceProxy(ds); + Jdbi jdbi = Jdbi.create(proxy); + + // Register all available plugins + log.info("[I27] Installing plugins... ({} found)", jdbiPlugins.size()); + jdbiPlugins.forEach(plugin -> jdbi.installPlugin(plugin)); + + // Register all available rowMappers + log.info("[I31] Installing rowMappers... ({} found)", rowMappers.size()); + rowMappers.forEach(mapper -> jdbi.registerRowMapper(mapper)); + + return jdbi; + } + + @Bean + public JdbiPlugin sqlObjectPlugin() { + return new SqlObjectPlugin(); + } + + @Bean + public CarMakerDao carMakerDao(Jdbi jdbi) { + return jdbi.onDemand(CarMakerDao.class); + } + + @Bean + public CarModelDao carModelDao(Jdbi jdbi) { + return jdbi.onDemand(CarModelDao.class); + } +} diff --git a/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/SpringBootJdbiApplication.java b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/SpringBootJdbiApplication.java new file mode 100644 index 0000000000..63afe3a3bf --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/SpringBootJdbiApplication.java @@ -0,0 +1,15 @@ +package com.baeldung.boot.jdbi; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +@SpringBootApplication +@EnableTransactionManagement +public class SpringBootJdbiApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootJdbiApplication.class, args); + } + +} diff --git a/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/dao/CarMakerDao.java b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/dao/CarMakerDao.java new file mode 100644 index 0000000000..6cc7268144 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/dao/CarMakerDao.java @@ -0,0 +1,35 @@ +/** + * + */ +package com.baeldung.boot.jdbi.dao; + +import java.util.List; + +import org.jdbi.v3.sqlobject.customizer.Bind; +import org.jdbi.v3.sqlobject.customizer.BindBean; +import org.jdbi.v3.sqlobject.locator.UseClasspathSqlLocator; +import org.jdbi.v3.sqlobject.statement.GetGeneratedKeys; +import org.jdbi.v3.sqlobject.statement.SqlBatch; +import org.jdbi.v3.sqlobject.statement.SqlQuery; +import org.jdbi.v3.sqlobject.statement.SqlUpdate; + +import com.baeldung.boot.jdbi.domain.CarMaker; + +/** + * @author Philippe + * + */ +@UseClasspathSqlLocator +public interface CarMakerDao { + + @SqlUpdate + @GetGeneratedKeys + Long insert(@BindBean CarMaker carMaker); + + @SqlBatch("insert") + @GetGeneratedKeys + List bulkInsert(@BindBean List carMakers); + + @SqlQuery + CarMaker findById(@Bind("id") Long id); +} diff --git a/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/dao/CarModelDao.java b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/dao/CarModelDao.java new file mode 100644 index 0000000000..18a05c6108 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/dao/CarModelDao.java @@ -0,0 +1,28 @@ +package com.baeldung.boot.jdbi.dao; + +import java.util.List; + +import org.jdbi.v3.sqlobject.customizer.Bind; +import org.jdbi.v3.sqlobject.customizer.BindBean; +import org.jdbi.v3.sqlobject.locator.UseClasspathSqlLocator; +import org.jdbi.v3.sqlobject.statement.GetGeneratedKeys; +import org.jdbi.v3.sqlobject.statement.SqlBatch; +import org.jdbi.v3.sqlobject.statement.SqlQuery; +import org.jdbi.v3.sqlobject.statement.SqlUpdate; + +import com.baeldung.boot.jdbi.domain.CarModel; + +@UseClasspathSqlLocator +public interface CarModelDao { + + @SqlUpdate("insert") + @GetGeneratedKeys + Long insert(@BindBean CarModel carModel); + + @SqlBatch("insert") + @GetGeneratedKeys + List bulkInsert(@BindBean List models); + + @SqlQuery + CarModel findByMakerIdAndSku(@Bind("makerId") Long makerId, @Bind("sku") String sku ); +} diff --git a/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/domain/CarMaker.java b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/domain/CarMaker.java new file mode 100644 index 0000000000..c32b0c30db --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/domain/CarMaker.java @@ -0,0 +1,14 @@ +package com.baeldung.boot.jdbi.domain; + +import java.util.List; + +import lombok.Builder; +import lombok.Data; + +@Data +@Builder +public class CarMaker { + private Long id; + private String name; + private List models; +} diff --git a/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/domain/CarModel.java b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/domain/CarModel.java new file mode 100644 index 0000000000..80b615801b --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/domain/CarModel.java @@ -0,0 +1,14 @@ +package com.baeldung.boot.jdbi.domain; + +import lombok.Builder; +import lombok.Data; + +@Builder +@Data +public class CarModel { + private Long id; + private String name; + private Integer year; + private String sku; + private Long makerId; +} diff --git a/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/mapper/CarMakerMapper.java b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/mapper/CarMakerMapper.java new file mode 100644 index 0000000000..54fc80d4ab --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/mapper/CarMakerMapper.java @@ -0,0 +1,27 @@ +package com.baeldung.boot.jdbi.mapper; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +import org.jdbi.v3.core.mapper.RowMapper; +import org.jdbi.v3.core.statement.StatementContext; +import org.springframework.stereotype.Component; + +import com.baeldung.boot.jdbi.domain.CarMaker; +import com.baeldung.boot.jdbi.domain.CarModel; + +@Component +public class CarMakerMapper implements RowMapper { + + @Override + public CarMaker map(ResultSet rs, StatementContext ctx) throws SQLException { + CarMaker maker = CarMaker.builder() + .id(rs.getLong("id")) + .name(rs.getString("name")) + .models(new ArrayList()) + .build(); + + return maker; + } +} diff --git a/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/mapper/CarModelMapper.java b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/mapper/CarModelMapper.java new file mode 100644 index 0000000000..eeceafd649 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/mapper/CarModelMapper.java @@ -0,0 +1,25 @@ +package com.baeldung.boot.jdbi.mapper; + +import java.sql.ResultSet; +import java.sql.SQLException; + +import org.jdbi.v3.core.mapper.RowMapper; +import org.jdbi.v3.core.statement.StatementContext; +import org.springframework.stereotype.Component; + +import com.baeldung.boot.jdbi.domain.CarModel; + +@Component +public class CarModelMapper implements RowMapper{ + + @Override + public CarModel map(ResultSet rs, StatementContext ctx) throws SQLException { + return CarModel.builder() + .id(rs.getLong("id")) + .name(rs.getString("name")) + .sku(rs.getString("sku")) + .year(rs.getInt("year")) + .build(); + } + +} diff --git a/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/service/CarMakerService.java b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/service/CarMakerService.java new file mode 100644 index 0000000000..a058130563 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/boot/jdbi/service/CarMakerService.java @@ -0,0 +1,48 @@ +/** + * + */ +package com.baeldung.boot.jdbi.service; + +import java.util.List; + +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import com.baeldung.boot.jdbi.dao.CarMakerDao; +import com.baeldung.boot.jdbi.dao.CarModelDao; +import com.baeldung.boot.jdbi.domain.CarMaker; +import com.baeldung.boot.jdbi.domain.CarModel; + +/** + * @author Philippe + * + */ +@Service +public class CarMakerService { + + private CarMakerDao carMakerDao; + private CarModelDao carModelDao; + + public CarMakerService(CarMakerDao carMakerDao,CarModelDao carModelDao) { + + this.carMakerDao = carMakerDao; + this.carModelDao = carModelDao; + } + + @Transactional + public int bulkInsert(CarMaker carMaker) { + Long carMakerId; + if (carMaker.getId() == null ) { + carMakerId = carMakerDao.insert(carMaker); + carMaker.setId(carMakerId); + } + + // Make sure all models belong to the same maker + carMaker.getModels().forEach(m -> { + m.setMakerId(carMaker.getId()); + carModelDao.insert(m); + }); + + return carMaker.getModels().size(); + } +} diff --git a/persistence-modules/spring-boot-persistence-2/src/main/resources/application.yml b/persistence-modules/spring-boot-persistence-2/src/main/resources/application.yml new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/main/resources/application.yml @@ -0,0 +1 @@ + diff --git a/persistence-modules/spring-boot-persistence-2/src/main/resources/com/baeldung/boot/jdbi/dao/CarMakerDao/findById.sql b/persistence-modules/spring-boot-persistence-2/src/main/resources/com/baeldung/boot/jdbi/dao/CarMakerDao/findById.sql new file mode 100644 index 0000000000..b36659110a --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/main/resources/com/baeldung/boot/jdbi/dao/CarMakerDao/findById.sql @@ -0,0 +1,11 @@ +-- +-- findById +-- +select + id, + name +from + car_maker +where + id = :id +; diff --git a/persistence-modules/spring-boot-persistence-2/src/main/resources/com/baeldung/boot/jdbi/dao/CarMakerDao/insert.sql b/persistence-modules/spring-boot-persistence-2/src/main/resources/com/baeldung/boot/jdbi/dao/CarMakerDao/insert.sql new file mode 100644 index 0000000000..0e045d7274 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/main/resources/com/baeldung/boot/jdbi/dao/CarMakerDao/insert.sql @@ -0,0 +1,4 @@ +-- +-- Insert +-- +insert into car_maker(id,name) values (:id,:name); diff --git a/persistence-modules/spring-boot-persistence-2/src/main/resources/com/baeldung/boot/jdbi/dao/CarModelDao/findByMakerIdAndSku.sql b/persistence-modules/spring-boot-persistence-2/src/main/resources/com/baeldung/boot/jdbi/dao/CarModelDao/findByMakerIdAndSku.sql new file mode 100644 index 0000000000..270d9baaa8 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/main/resources/com/baeldung/boot/jdbi/dao/CarModelDao/findByMakerIdAndSku.sql @@ -0,0 +1,10 @@ +-- +-- Insert +-- +select * +from + car_model +where + maker_fk = :makerId and + sku = :sku +; \ No newline at end of file diff --git a/persistence-modules/spring-boot-persistence-2/src/main/resources/com/baeldung/boot/jdbi/dao/CarModelDao/insert.sql b/persistence-modules/spring-boot-persistence-2/src/main/resources/com/baeldung/boot/jdbi/dao/CarModelDao/insert.sql new file mode 100644 index 0000000000..b277213584 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/main/resources/com/baeldung/boot/jdbi/dao/CarModelDao/insert.sql @@ -0,0 +1,8 @@ +-- +-- Insert +-- +insert into car_model(maker_fk,name,sku,year) values ( + :makerId, + :name, + :sku, + :year ); diff --git a/persistence-modules/spring-boot-persistence-2/src/test/java/com/baeldung/boot/jdbi/SpringBootJdbiApplicationUnitTest.java b/persistence-modules/spring-boot-persistence-2/src/test/java/com/baeldung/boot/jdbi/SpringBootJdbiApplicationUnitTest.java new file mode 100644 index 0000000000..e4b623ee2b --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/test/java/com/baeldung/boot/jdbi/SpringBootJdbiApplicationUnitTest.java @@ -0,0 +1,121 @@ +package com.baeldung.boot.jdbi; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.jdbi.v3.core.Jdbi; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import com.baeldung.boot.jdbi.dao.CarMakerDao; +import com.baeldung.boot.jdbi.dao.CarModelDao; +import com.baeldung.boot.jdbi.domain.CarMaker; +import com.baeldung.boot.jdbi.domain.CarModel; +import com.baeldung.boot.jdbi.service.CarMakerService; + +import lombok.extern.slf4j.Slf4j; + +@RunWith(SpringRunner.class) +@SpringBootTest +@Slf4j +public class SpringBootJdbiApplicationUnitTest { + + + @Autowired + private CarMakerDao carMakerDao; + + @Autowired + private CarModelDao carModelDao; + + @Autowired + private CarMakerService carMakerService; + + @Test + public void givenNewCarMaker_whenInsertNewCarMaker_thenSuccess() { + + assertNotNull(carMakerDao); + + CarMaker carMaker = CarMaker.builder() + .name("Diamond Motors") + .build(); + + Long generatedId = carMakerDao.insert(carMaker); + log.info("[I37] generatedId = {}", generatedId); + assertThat(generatedId).isGreaterThan(0); + } + + @Test + public void givenNewCarMakers_whenInsertNewCarMakers_thenSuccess() { + + assertNotNull(carMakerDao); + + CarMaker carMaker1 = CarMaker.builder() + .name("maker1") + .build(); + + CarMaker carMaker2 = CarMaker.builder() + .name("maker2") + .build(); + + List makers = new ArrayList<>(); + makers.add(carMaker1); + makers.add(carMaker2); + + List generatedIds = carMakerDao.bulkInsert(makers); + log.info("[I37] generatedIds = {}", generatedIds); + assertThat(generatedIds).size().isEqualTo(makers.size()); + } + + + @Test + public void givenExistingCarMaker_whenFindById_thenReturnExistingCarMaker() { + + CarMaker maker = carMakerDao.findById(1l); + assertThat(maker).isNotNull(); + assertThat(maker.getId()).isEqualTo(1); + + } + + @Test + public void givenExistingCarMaker_whenBulkInsertFails_thenRollback() { + + CarMaker maker = carMakerDao.findById(1l); + CarModel m1 = CarModel.builder() + .makerId(maker.getId()) + .name("Model X1") + .sku("1-M1") + .year(2019) + .build(); + maker.getModels().add(m1); + + CarModel m2 = CarModel.builder() + .makerId(maker.getId()) + .name("Model X1") + .sku("1-M1") + .year(2019) + .build(); + maker.getModels().add(m2); + + // This insert fails because we have the same SKU + try { + carMakerService.bulkInsert(maker); + assertTrue("Insert must fail", true); + } + catch(Exception ex) { + log.info("[I113] Exception: {}", ex.getMessage()); + } + + CarModel m = carModelDao.findByMakerIdAndSku(maker.getId(), "1-M1"); + assertThat(m).isNull(); + + } + +} diff --git a/persistence-modules/spring-boot-persistence-2/src/test/resources/data.sql b/persistence-modules/spring-boot-persistence-2/src/test/resources/data.sql new file mode 100644 index 0000000000..e3e1f4ae32 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/test/resources/data.sql @@ -0,0 +1,12 @@ + +insert into car_maker(id,name) values (1,'Special Motors'); +insert into car_maker(id,name) values (2,'BWM'); +insert into car_maker(id,name) values (3,'Dolores'); + +insert into car_model(id,maker_fk,name,sku,year) values(1,1,'Muze','SM001',2018); +insert into car_model(id,maker_fk,name,sku,year) values(2,1,'Empada','SM002',2008); + +insert into car_model(id,maker_fk,name,sku,year) values(4,2,'BWM-100','BWM100',2008); +insert into car_model(id,maker_fk,name,sku,year) values(5,2,'BWM-200','BWM200',2009); +insert into car_model(id,maker_fk,name,sku,year) values(6,2,'BWM-300','BWM300',2008); + diff --git a/persistence-modules/spring-boot-persistence-2/src/test/resources/schema.sql b/persistence-modules/spring-boot-persistence-2/src/test/resources/schema.sql new file mode 100644 index 0000000000..a0d0eaf62e --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/test/resources/schema.sql @@ -0,0 +1,24 @@ +-- +-- Car makers table +-- +create table car_maker( + id identity, + name varchar(128) not null +); + +create unique index ui_car_maker_01 on car_maker(name); + +-- +-- Car models table +-- +create table car_model( + id identity, + maker_fk int not null, + name varchar(128) not null, + sku varchar(128) not null, + year int not null +); + +create unique index ui_car_model_01 on car_model(maker_fk,sku); +create unique index ui_car_model_02 on car_model(maker_fk,name,year); + From a9eb0eaf8fc0c4c0fad1d1028c7fd03e02aa6d57 Mon Sep 17 00:00:00 2001 From: Philippe Date: Sun, 26 Jan 2020 22:14:04 -0300 Subject: [PATCH 5/5] [BAEL-3164] Rename module to spring-boot-persistence-2 --- persistence-modules/pom.xml | 2 +- persistence-modules/spring-boot-jdbi/HELP.md | 9 -- .../spring-boot-jdbi/README.md | 3 - persistence-modules/spring-boot-jdbi/pom.xml | 102 --------------- .../baeldung/boot/jdbi/JdbiConfiguration.java | 57 --------- .../boot/jdbi/SpringBootJdbiApplication.java | 15 --- .../baeldung/boot/jdbi/dao/CarMakerDao.java | 35 ----- .../baeldung/boot/jdbi/dao/CarModelDao.java | 28 ---- .../baeldung/boot/jdbi/domain/CarMaker.java | 14 -- .../baeldung/boot/jdbi/domain/CarModel.java | 14 -- .../boot/jdbi/mapper/CarMakerMapper.java | 27 ---- .../boot/jdbi/mapper/CarModelMapper.java | 25 ---- .../boot/jdbi/service/CarMakerService.java | 48 ------- .../src/main/resources/application.yml | 1 - .../boot/jdbi/dao/CarMakerDao/findById.sql | 11 -- .../boot/jdbi/dao/CarMakerDao/insert.sql | 4 - .../dao/CarModelDao/findByMakerIdAndSku.sql | 10 -- .../boot/jdbi/dao/CarModelDao/insert.sql | 8 -- .../SpringBootJdbiApplicationUnitTest.java | 121 ------------------ .../src/test/resources/data.sql | 12 -- .../src/test/resources/schema.sql | 24 ---- 21 files changed, 1 insertion(+), 569 deletions(-) delete mode 100644 persistence-modules/spring-boot-jdbi/HELP.md delete mode 100644 persistence-modules/spring-boot-jdbi/README.md delete mode 100644 persistence-modules/spring-boot-jdbi/pom.xml delete mode 100644 persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/JdbiConfiguration.java delete mode 100644 persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/SpringBootJdbiApplication.java delete mode 100644 persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/dao/CarMakerDao.java delete mode 100644 persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/dao/CarModelDao.java delete mode 100644 persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/domain/CarMaker.java delete mode 100644 persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/domain/CarModel.java delete mode 100644 persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/mapper/CarMakerMapper.java delete mode 100644 persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/mapper/CarModelMapper.java delete mode 100644 persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/service/CarMakerService.java delete mode 100644 persistence-modules/spring-boot-jdbi/src/main/resources/application.yml delete mode 100644 persistence-modules/spring-boot-jdbi/src/main/resources/com/baeldung/boot/jdbi/dao/CarMakerDao/findById.sql delete mode 100644 persistence-modules/spring-boot-jdbi/src/main/resources/com/baeldung/boot/jdbi/dao/CarMakerDao/insert.sql delete mode 100644 persistence-modules/spring-boot-jdbi/src/main/resources/com/baeldung/boot/jdbi/dao/CarModelDao/findByMakerIdAndSku.sql delete mode 100644 persistence-modules/spring-boot-jdbi/src/main/resources/com/baeldung/boot/jdbi/dao/CarModelDao/insert.sql delete mode 100644 persistence-modules/spring-boot-jdbi/src/test/java/com/baeldung/boot/jdbi/SpringBootJdbiApplicationUnitTest.java delete mode 100644 persistence-modules/spring-boot-jdbi/src/test/resources/data.sql delete mode 100644 persistence-modules/spring-boot-jdbi/src/test/resources/schema.sql diff --git a/persistence-modules/pom.xml b/persistence-modules/pom.xml index c0f7c07b70..7ab9f33085 100644 --- a/persistence-modules/pom.xml +++ b/persistence-modules/pom.xml @@ -42,7 +42,7 @@ redis solr - spring-boot-jdbi + spring-boot-persistence-2 spring-boot-mysql spring-boot-persistence spring-boot-persistence-h2 diff --git a/persistence-modules/spring-boot-jdbi/HELP.md b/persistence-modules/spring-boot-jdbi/HELP.md deleted file mode 100644 index d5a5463718..0000000000 --- a/persistence-modules/spring-boot-jdbi/HELP.md +++ /dev/null @@ -1,9 +0,0 @@ -# Getting Started - -### Reference Documentation -For further reference, please consider the following sections: - -* [Official Apache Maven documentation](https://maven.apache.org/guides/index.html) -* [Spring Boot DevTools](https://docs.spring.io/spring-boot/docs/{bootVersion}/reference/htmlsingle/#using-boot-devtools) -* [Spring Configuration Processor](https://docs.spring.io/spring-boot/docs/{bootVersion}/reference/htmlsingle/#configuration-metadata-annotation-processor) - diff --git a/persistence-modules/spring-boot-jdbi/README.md b/persistence-modules/spring-boot-jdbi/README.md deleted file mode 100644 index 5d171fb2ca..0000000000 --- a/persistence-modules/spring-boot-jdbi/README.md +++ /dev/null @@ -1,3 +0,0 @@ -### Relevant Articles: - -- [Using JDBI with Spring Boot](https://www.baeldung.com/spring-boot-jdbi) diff --git a/persistence-modules/spring-boot-jdbi/pom.xml b/persistence-modules/spring-boot-jdbi/pom.xml deleted file mode 100644 index db36185acd..0000000000 --- a/persistence-modules/spring-boot-jdbi/pom.xml +++ /dev/null @@ -1,102 +0,0 @@ - - - 4.0.0 - com.baeldung.boot.jdbi - spring-boot-jdbi - 0.0.1-SNAPSHOT - spring-boot-jdbi - Sample SpringBoot JDBI Project - - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - ../../pom.xml - - - - - - org.springframework.boot - spring-boot-dependencies - 2.1.8.RELEASE - pom - import - - - - org.jdbi - jdbi3-spring4 - ${jdbi.version} - - - - org.jdbi - jdbi3-sqlobject - ${jdbi.version} - - - - - - - - org.springframework.boot - spring-boot-starter-jdbc - - - - org.jdbi - jdbi3-spring4 - - - - org.jdbi - jdbi3-sqlobject - - - - org.springframework.boot - spring-boot-devtools - runtime - true - - - com.h2database - h2 - runtime - - - org.springframework.boot - spring-boot-configuration-processor - true - - - org.projectlombok - lombok - true - - - org.springframework.boot - spring-boot-starter-test - test - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - - - - 1.8 - 3.9.1 - - - diff --git a/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/JdbiConfiguration.java b/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/JdbiConfiguration.java deleted file mode 100644 index ddbe6cc118..0000000000 --- a/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/JdbiConfiguration.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.baeldung.boot.jdbi; - -import java.util.List; - -import javax.sql.DataSource; - -import org.jdbi.v3.core.Jdbi; -import org.jdbi.v3.core.mapper.RowMapper; -import org.jdbi.v3.core.spi.JdbiPlugin; -import org.jdbi.v3.sqlobject.SqlObjectPlugin; -import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Lazy; -import org.springframework.jdbc.datasource.DataSourceTransactionManager; -import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy; -import org.springframework.transaction.PlatformTransactionManager; - -import com.baeldung.boot.jdbi.dao.CarMakerDao; -import com.baeldung.boot.jdbi.dao.CarModelDao; - -import lombok.extern.slf4j.Slf4j; - -@Configuration -@Slf4j -public class JdbiConfiguration { - @Bean - public Jdbi jdbi(DataSource ds,List jdbiPlugins, List> rowMappers) { - TransactionAwareDataSourceProxy proxy = new TransactionAwareDataSourceProxy(ds); - Jdbi jdbi = Jdbi.create(proxy); - - // Register all available plugins - log.info("[I27] Installing plugins... ({} found)", jdbiPlugins.size()); - jdbiPlugins.forEach(plugin -> jdbi.installPlugin(plugin)); - - // Register all available rowMappers - log.info("[I31] Installing rowMappers... ({} found)", rowMappers.size()); - rowMappers.forEach(mapper -> jdbi.registerRowMapper(mapper)); - - return jdbi; - } - - @Bean - public JdbiPlugin sqlObjectPlugin() { - return new SqlObjectPlugin(); - } - - @Bean - public CarMakerDao carMakerDao(Jdbi jdbi) { - return jdbi.onDemand(CarMakerDao.class); - } - - @Bean - public CarModelDao carModelDao(Jdbi jdbi) { - return jdbi.onDemand(CarModelDao.class); - } -} diff --git a/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/SpringBootJdbiApplication.java b/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/SpringBootJdbiApplication.java deleted file mode 100644 index 63afe3a3bf..0000000000 --- a/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/SpringBootJdbiApplication.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.baeldung.boot.jdbi; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.transaction.annotation.EnableTransactionManagement; - -@SpringBootApplication -@EnableTransactionManagement -public class SpringBootJdbiApplication { - - public static void main(String[] args) { - SpringApplication.run(SpringBootJdbiApplication.class, args); - } - -} diff --git a/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/dao/CarMakerDao.java b/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/dao/CarMakerDao.java deleted file mode 100644 index 6cc7268144..0000000000 --- a/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/dao/CarMakerDao.java +++ /dev/null @@ -1,35 +0,0 @@ -/** - * - */ -package com.baeldung.boot.jdbi.dao; - -import java.util.List; - -import org.jdbi.v3.sqlobject.customizer.Bind; -import org.jdbi.v3.sqlobject.customizer.BindBean; -import org.jdbi.v3.sqlobject.locator.UseClasspathSqlLocator; -import org.jdbi.v3.sqlobject.statement.GetGeneratedKeys; -import org.jdbi.v3.sqlobject.statement.SqlBatch; -import org.jdbi.v3.sqlobject.statement.SqlQuery; -import org.jdbi.v3.sqlobject.statement.SqlUpdate; - -import com.baeldung.boot.jdbi.domain.CarMaker; - -/** - * @author Philippe - * - */ -@UseClasspathSqlLocator -public interface CarMakerDao { - - @SqlUpdate - @GetGeneratedKeys - Long insert(@BindBean CarMaker carMaker); - - @SqlBatch("insert") - @GetGeneratedKeys - List bulkInsert(@BindBean List carMakers); - - @SqlQuery - CarMaker findById(@Bind("id") Long id); -} diff --git a/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/dao/CarModelDao.java b/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/dao/CarModelDao.java deleted file mode 100644 index 18a05c6108..0000000000 --- a/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/dao/CarModelDao.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.baeldung.boot.jdbi.dao; - -import java.util.List; - -import org.jdbi.v3.sqlobject.customizer.Bind; -import org.jdbi.v3.sqlobject.customizer.BindBean; -import org.jdbi.v3.sqlobject.locator.UseClasspathSqlLocator; -import org.jdbi.v3.sqlobject.statement.GetGeneratedKeys; -import org.jdbi.v3.sqlobject.statement.SqlBatch; -import org.jdbi.v3.sqlobject.statement.SqlQuery; -import org.jdbi.v3.sqlobject.statement.SqlUpdate; - -import com.baeldung.boot.jdbi.domain.CarModel; - -@UseClasspathSqlLocator -public interface CarModelDao { - - @SqlUpdate("insert") - @GetGeneratedKeys - Long insert(@BindBean CarModel carModel); - - @SqlBatch("insert") - @GetGeneratedKeys - List bulkInsert(@BindBean List models); - - @SqlQuery - CarModel findByMakerIdAndSku(@Bind("makerId") Long makerId, @Bind("sku") String sku ); -} diff --git a/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/domain/CarMaker.java b/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/domain/CarMaker.java deleted file mode 100644 index c32b0c30db..0000000000 --- a/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/domain/CarMaker.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.baeldung.boot.jdbi.domain; - -import java.util.List; - -import lombok.Builder; -import lombok.Data; - -@Data -@Builder -public class CarMaker { - private Long id; - private String name; - private List models; -} diff --git a/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/domain/CarModel.java b/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/domain/CarModel.java deleted file mode 100644 index 80b615801b..0000000000 --- a/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/domain/CarModel.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.baeldung.boot.jdbi.domain; - -import lombok.Builder; -import lombok.Data; - -@Builder -@Data -public class CarModel { - private Long id; - private String name; - private Integer year; - private String sku; - private Long makerId; -} diff --git a/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/mapper/CarMakerMapper.java b/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/mapper/CarMakerMapper.java deleted file mode 100644 index 54fc80d4ab..0000000000 --- a/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/mapper/CarMakerMapper.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.baeldung.boot.jdbi.mapper; - -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; - -import org.jdbi.v3.core.mapper.RowMapper; -import org.jdbi.v3.core.statement.StatementContext; -import org.springframework.stereotype.Component; - -import com.baeldung.boot.jdbi.domain.CarMaker; -import com.baeldung.boot.jdbi.domain.CarModel; - -@Component -public class CarMakerMapper implements RowMapper { - - @Override - public CarMaker map(ResultSet rs, StatementContext ctx) throws SQLException { - CarMaker maker = CarMaker.builder() - .id(rs.getLong("id")) - .name(rs.getString("name")) - .models(new ArrayList()) - .build(); - - return maker; - } -} diff --git a/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/mapper/CarModelMapper.java b/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/mapper/CarModelMapper.java deleted file mode 100644 index eeceafd649..0000000000 --- a/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/mapper/CarModelMapper.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.baeldung.boot.jdbi.mapper; - -import java.sql.ResultSet; -import java.sql.SQLException; - -import org.jdbi.v3.core.mapper.RowMapper; -import org.jdbi.v3.core.statement.StatementContext; -import org.springframework.stereotype.Component; - -import com.baeldung.boot.jdbi.domain.CarModel; - -@Component -public class CarModelMapper implements RowMapper{ - - @Override - public CarModel map(ResultSet rs, StatementContext ctx) throws SQLException { - return CarModel.builder() - .id(rs.getLong("id")) - .name(rs.getString("name")) - .sku(rs.getString("sku")) - .year(rs.getInt("year")) - .build(); - } - -} diff --git a/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/service/CarMakerService.java b/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/service/CarMakerService.java deleted file mode 100644 index a058130563..0000000000 --- a/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/service/CarMakerService.java +++ /dev/null @@ -1,48 +0,0 @@ -/** - * - */ -package com.baeldung.boot.jdbi.service; - -import java.util.List; - -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -import com.baeldung.boot.jdbi.dao.CarMakerDao; -import com.baeldung.boot.jdbi.dao.CarModelDao; -import com.baeldung.boot.jdbi.domain.CarMaker; -import com.baeldung.boot.jdbi.domain.CarModel; - -/** - * @author Philippe - * - */ -@Service -public class CarMakerService { - - private CarMakerDao carMakerDao; - private CarModelDao carModelDao; - - public CarMakerService(CarMakerDao carMakerDao,CarModelDao carModelDao) { - - this.carMakerDao = carMakerDao; - this.carModelDao = carModelDao; - } - - @Transactional - public int bulkInsert(CarMaker carMaker) { - Long carMakerId; - if (carMaker.getId() == null ) { - carMakerId = carMakerDao.insert(carMaker); - carMaker.setId(carMakerId); - } - - // Make sure all models belong to the same maker - carMaker.getModels().forEach(m -> { - m.setMakerId(carMaker.getId()); - carModelDao.insert(m); - }); - - return carMaker.getModels().size(); - } -} diff --git a/persistence-modules/spring-boot-jdbi/src/main/resources/application.yml b/persistence-modules/spring-boot-jdbi/src/main/resources/application.yml deleted file mode 100644 index 8b13789179..0000000000 --- a/persistence-modules/spring-boot-jdbi/src/main/resources/application.yml +++ /dev/null @@ -1 +0,0 @@ - diff --git a/persistence-modules/spring-boot-jdbi/src/main/resources/com/baeldung/boot/jdbi/dao/CarMakerDao/findById.sql b/persistence-modules/spring-boot-jdbi/src/main/resources/com/baeldung/boot/jdbi/dao/CarMakerDao/findById.sql deleted file mode 100644 index b36659110a..0000000000 --- a/persistence-modules/spring-boot-jdbi/src/main/resources/com/baeldung/boot/jdbi/dao/CarMakerDao/findById.sql +++ /dev/null @@ -1,11 +0,0 @@ --- --- findById --- -select - id, - name -from - car_maker -where - id = :id -; diff --git a/persistence-modules/spring-boot-jdbi/src/main/resources/com/baeldung/boot/jdbi/dao/CarMakerDao/insert.sql b/persistence-modules/spring-boot-jdbi/src/main/resources/com/baeldung/boot/jdbi/dao/CarMakerDao/insert.sql deleted file mode 100644 index 0e045d7274..0000000000 --- a/persistence-modules/spring-boot-jdbi/src/main/resources/com/baeldung/boot/jdbi/dao/CarMakerDao/insert.sql +++ /dev/null @@ -1,4 +0,0 @@ --- --- Insert --- -insert into car_maker(id,name) values (:id,:name); diff --git a/persistence-modules/spring-boot-jdbi/src/main/resources/com/baeldung/boot/jdbi/dao/CarModelDao/findByMakerIdAndSku.sql b/persistence-modules/spring-boot-jdbi/src/main/resources/com/baeldung/boot/jdbi/dao/CarModelDao/findByMakerIdAndSku.sql deleted file mode 100644 index 270d9baaa8..0000000000 --- a/persistence-modules/spring-boot-jdbi/src/main/resources/com/baeldung/boot/jdbi/dao/CarModelDao/findByMakerIdAndSku.sql +++ /dev/null @@ -1,10 +0,0 @@ --- --- Insert --- -select * -from - car_model -where - maker_fk = :makerId and - sku = :sku -; \ No newline at end of file diff --git a/persistence-modules/spring-boot-jdbi/src/main/resources/com/baeldung/boot/jdbi/dao/CarModelDao/insert.sql b/persistence-modules/spring-boot-jdbi/src/main/resources/com/baeldung/boot/jdbi/dao/CarModelDao/insert.sql deleted file mode 100644 index b277213584..0000000000 --- a/persistence-modules/spring-boot-jdbi/src/main/resources/com/baeldung/boot/jdbi/dao/CarModelDao/insert.sql +++ /dev/null @@ -1,8 +0,0 @@ --- --- Insert --- -insert into car_model(maker_fk,name,sku,year) values ( - :makerId, - :name, - :sku, - :year ); diff --git a/persistence-modules/spring-boot-jdbi/src/test/java/com/baeldung/boot/jdbi/SpringBootJdbiApplicationUnitTest.java b/persistence-modules/spring-boot-jdbi/src/test/java/com/baeldung/boot/jdbi/SpringBootJdbiApplicationUnitTest.java deleted file mode 100644 index e4b623ee2b..0000000000 --- a/persistence-modules/spring-boot-jdbi/src/test/java/com/baeldung/boot/jdbi/SpringBootJdbiApplicationUnitTest.java +++ /dev/null @@ -1,121 +0,0 @@ -package com.baeldung.boot.jdbi; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -import org.jdbi.v3.core.Jdbi; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; - -import com.baeldung.boot.jdbi.dao.CarMakerDao; -import com.baeldung.boot.jdbi.dao.CarModelDao; -import com.baeldung.boot.jdbi.domain.CarMaker; -import com.baeldung.boot.jdbi.domain.CarModel; -import com.baeldung.boot.jdbi.service.CarMakerService; - -import lombok.extern.slf4j.Slf4j; - -@RunWith(SpringRunner.class) -@SpringBootTest -@Slf4j -public class SpringBootJdbiApplicationUnitTest { - - - @Autowired - private CarMakerDao carMakerDao; - - @Autowired - private CarModelDao carModelDao; - - @Autowired - private CarMakerService carMakerService; - - @Test - public void givenNewCarMaker_whenInsertNewCarMaker_thenSuccess() { - - assertNotNull(carMakerDao); - - CarMaker carMaker = CarMaker.builder() - .name("Diamond Motors") - .build(); - - Long generatedId = carMakerDao.insert(carMaker); - log.info("[I37] generatedId = {}", generatedId); - assertThat(generatedId).isGreaterThan(0); - } - - @Test - public void givenNewCarMakers_whenInsertNewCarMakers_thenSuccess() { - - assertNotNull(carMakerDao); - - CarMaker carMaker1 = CarMaker.builder() - .name("maker1") - .build(); - - CarMaker carMaker2 = CarMaker.builder() - .name("maker2") - .build(); - - List makers = new ArrayList<>(); - makers.add(carMaker1); - makers.add(carMaker2); - - List generatedIds = carMakerDao.bulkInsert(makers); - log.info("[I37] generatedIds = {}", generatedIds); - assertThat(generatedIds).size().isEqualTo(makers.size()); - } - - - @Test - public void givenExistingCarMaker_whenFindById_thenReturnExistingCarMaker() { - - CarMaker maker = carMakerDao.findById(1l); - assertThat(maker).isNotNull(); - assertThat(maker.getId()).isEqualTo(1); - - } - - @Test - public void givenExistingCarMaker_whenBulkInsertFails_thenRollback() { - - CarMaker maker = carMakerDao.findById(1l); - CarModel m1 = CarModel.builder() - .makerId(maker.getId()) - .name("Model X1") - .sku("1-M1") - .year(2019) - .build(); - maker.getModels().add(m1); - - CarModel m2 = CarModel.builder() - .makerId(maker.getId()) - .name("Model X1") - .sku("1-M1") - .year(2019) - .build(); - maker.getModels().add(m2); - - // This insert fails because we have the same SKU - try { - carMakerService.bulkInsert(maker); - assertTrue("Insert must fail", true); - } - catch(Exception ex) { - log.info("[I113] Exception: {}", ex.getMessage()); - } - - CarModel m = carModelDao.findByMakerIdAndSku(maker.getId(), "1-M1"); - assertThat(m).isNull(); - - } - -} diff --git a/persistence-modules/spring-boot-jdbi/src/test/resources/data.sql b/persistence-modules/spring-boot-jdbi/src/test/resources/data.sql deleted file mode 100644 index e3e1f4ae32..0000000000 --- a/persistence-modules/spring-boot-jdbi/src/test/resources/data.sql +++ /dev/null @@ -1,12 +0,0 @@ - -insert into car_maker(id,name) values (1,'Special Motors'); -insert into car_maker(id,name) values (2,'BWM'); -insert into car_maker(id,name) values (3,'Dolores'); - -insert into car_model(id,maker_fk,name,sku,year) values(1,1,'Muze','SM001',2018); -insert into car_model(id,maker_fk,name,sku,year) values(2,1,'Empada','SM002',2008); - -insert into car_model(id,maker_fk,name,sku,year) values(4,2,'BWM-100','BWM100',2008); -insert into car_model(id,maker_fk,name,sku,year) values(5,2,'BWM-200','BWM200',2009); -insert into car_model(id,maker_fk,name,sku,year) values(6,2,'BWM-300','BWM300',2008); - diff --git a/persistence-modules/spring-boot-jdbi/src/test/resources/schema.sql b/persistence-modules/spring-boot-jdbi/src/test/resources/schema.sql deleted file mode 100644 index a0d0eaf62e..0000000000 --- a/persistence-modules/spring-boot-jdbi/src/test/resources/schema.sql +++ /dev/null @@ -1,24 +0,0 @@ --- --- Car makers table --- -create table car_maker( - id identity, - name varchar(128) not null -); - -create unique index ui_car_maker_01 on car_maker(name); - --- --- Car models table --- -create table car_model( - id identity, - maker_fk int not null, - name varchar(128) not null, - sku varchar(128) not null, - year int not null -); - -create unique index ui_car_model_01 on car_model(maker_fk,sku); -create unique index ui_car_model_02 on car_model(maker_fk,name,year); -