From 6c02b0c04e4e983f91d95c48e3fbf955394da120 Mon Sep 17 00:00:00 2001 From: John Blum Date: Tue, 26 Jan 2021 21:58:36 -0800 Subject: [PATCH] Add new 'quick-start' module to the Spring Geode Samples. The new 'quick-start' Spring Geode Sample/module backs the 'Get Started' example Spring Boot, Apache Geode application referenced in the README. --- .../intro/quick-start/lombok.config | 2 + .../spring-geode-samples-quick-start.gradle | 22 ++++ .../example/app/user/UserApplication.java | 110 ++++++++++++++++++ .../user/UserApplicationIntegrationTests.java | 42 +++++++ 4 files changed, 176 insertions(+) create mode 100644 spring-geode-samples/intro/quick-start/lombok.config create mode 100644 spring-geode-samples/intro/quick-start/spring-geode-samples-quick-start.gradle create mode 100644 spring-geode-samples/intro/quick-start/src/main/java/example/app/user/UserApplication.java create mode 100644 spring-geode-samples/intro/quick-start/src/test/java/example/app/user/UserApplicationIntegrationTests.java diff --git a/spring-geode-samples/intro/quick-start/lombok.config b/spring-geode-samples/intro/quick-start/lombok.config new file mode 100644 index 00000000..6aa51d71 --- /dev/null +++ b/spring-geode-samples/intro/quick-start/lombok.config @@ -0,0 +1,2 @@ +# This file is generated by the 'io.freefair.lombok' Gradle plugin +config.stopBubbling = true diff --git a/spring-geode-samples/intro/quick-start/spring-geode-samples-quick-start.gradle b/spring-geode-samples/intro/quick-start/spring-geode-samples-quick-start.gradle new file mode 100644 index 00000000..1587d98c --- /dev/null +++ b/spring-geode-samples/intro/quick-start/spring-geode-samples-quick-start.gradle @@ -0,0 +1,22 @@ +plugins { + id "io.freefair.lombok" version "5.3.0" +} + +apply plugin: 'io.spring.convention.spring-sample-boot' + +description = "Quick Start for Spring Boot for Apache Geode" + +dependencies { + + compile project(":spring-geode-starter") + + compile "org.assertj:assertj-core" + compile "org.projectlombok:lombok" + + //runtime project(":spring-geode-starter-logging") + + testCompile project(":spring-geode-starter-test") + + testCompile "org.springframework.boot:spring-boot-starter-test" + +} diff --git a/spring-geode-samples/intro/quick-start/src/main/java/example/app/user/UserApplication.java b/spring-geode-samples/intro/quick-start/src/main/java/example/app/user/UserApplication.java new file mode 100644 index 00000000..46eb18fa --- /dev/null +++ b/spring-geode-samples/intro/quick-start/src/main/java/example/app/user/UserApplication.java @@ -0,0 +1,110 @@ +/* + * Copyright 2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ +package example.app.user; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.data.annotation.Id; +import org.springframework.data.gemfire.config.annotation.EnableEntityDefinedRegions; +import org.springframework.data.gemfire.mapping.annotation.Region; +import org.springframework.data.gemfire.repository.GemfireRepository; +import org.springframework.geode.config.annotation.EnableClusterAware; + +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import lombok.ToString; +import lombok.extern.slf4j.Slf4j; + +/** + * Example {@link SpringBootApplication} using Apache Geode to manage Users. + * + * @author John Blum + * @see org.springframework.boot.ApplicationRunner + * @see org.springframework.boot.SpringApplication + * @see org.springframework.boot.autoconfigure.SpringBootApplication + * @see org.springframework.context.annotation.Bean + * @see org.springframework.data.annotation.Id + * @see org.springframework.data.repository.CrudRepository + * @see org.springframework.data.gemfire.config.annotation.EnableEntityDefinedRegions + * @see org.springframework.data.gemfire.mapping.annotation.Region + * @see org.springframework.data.gemfire.repository.GemfireRepository + * @see org.springframework.geode.config.annotation.EnableClusterAware + * @since 1.0.0 + */ +@Slf4j +@SpringBootApplication +@EnableClusterAware +@EnableEntityDefinedRegions(basePackageClasses = User.class) +public class UserApplication { + + public static void main(String[] args) { + SpringApplication.run(UserApplication.class, args); + } + + @Bean + @SuppressWarnings("unused") + ApplicationRunner runner(UserRepository userRepository) { + + return args -> { + + long count = userRepository.count(); + + assertThat(count).isZero(); + + log.info("Number of Users [{}]", count); + + User jonDoe = new User("jonDoe"); + + log.info("Created User [{}]", jonDoe); + + userRepository.save(jonDoe); + + log.info("Saved User [{}]", jonDoe); + + count = userRepository.count(); + + assertThat(count).isOne(); + + log.info("Number of Users [{}]", count); + + User jonDoeFoundById = userRepository.findById(jonDoe.getName()).orElse(null); + + assertThat(jonDoeFoundById).isEqualTo(jonDoe); + + log.info("Found User by ID (name) [{}]", jonDoeFoundById); + }; + } +} + +@Getter +@ToString +@EqualsAndHashCode +@RequiredArgsConstructor +@Region("Users") +class User { + + @lombok.NonNull @Id + private final String name; + +} + +//interface UserRepository extends CrudRepository { } +interface UserRepository extends GemfireRepository { } diff --git a/spring-geode-samples/intro/quick-start/src/test/java/example/app/user/UserApplicationIntegrationTests.java b/spring-geode-samples/intro/quick-start/src/test/java/example/app/user/UserApplicationIntegrationTests.java new file mode 100644 index 00000000..5306df95 --- /dev/null +++ b/spring-geode-samples/intro/quick-start/src/test/java/example/app/user/UserApplicationIntegrationTests.java @@ -0,0 +1,42 @@ +/* + * Copyright 2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ +package example.app.user; + +import org.junit.jupiter.api.Test; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.gemfire.tests.integration.IntegrationTestsSupport; +import org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects; + +/** + * Integration Tests for {@link UserApplication}. + * + * @author John Blum + * @see org.junit.Test + * @see org.springframework.boot.test.context.SpringBootTest + * @see org.springframework.data.gemfire.tests.integration.IntegrationTestsSupport + * @see org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects + * @see example.app.user.UserApplication + * @since 1.5.0 + */ +@SpringBootTest +@EnableGemFireMockObjects +public class UserApplicationIntegrationTests extends IntegrationTestsSupport { + + @Test + public void contextLoads() { } + +}