From d3725310e1da80da1855bf9c96d8747cfb85d3aa Mon Sep 17 00:00:00 2001 From: Wendy Hu Date: Mon, 1 May 2023 16:50:29 -0400 Subject: [PATCH 01/11] feat(data-redis): init reactive redis data config, refactoring to baseRedisConfig --- data/synapse-data-redis/pom.xml | 6 -- .../config/BaseReactiveRedisDataConfig.java | 46 +++++++++++ .../data/redis/config/BaseRedisConfig.java | 82 +++++++++++++++++++ .../redis/config/BaseRedisDataConfig.java | 29 +++---- pom.xml | 10 +-- 5 files changed, 145 insertions(+), 28 deletions(-) create mode 100644 data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseReactiveRedisDataConfig.java create mode 100644 data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseRedisConfig.java diff --git a/data/synapse-data-redis/pom.xml b/data/synapse-data-redis/pom.xml index fb4f92c12..3fc88edbf 100644 --- a/data/synapse-data-redis/pom.xml +++ b/data/synapse-data-redis/pom.xml @@ -24,10 +24,6 @@ - - org.springframework.boot - spring-boot-starter-data-redis - org.springframework.data spring-data-redis @@ -36,8 +32,6 @@ redis.clients jedis - 3.3.0 - jar diff --git a/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseReactiveRedisDataConfig.java b/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseReactiveRedisDataConfig.java new file mode 100644 index 000000000..e5bede280 --- /dev/null +++ b/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseReactiveRedisDataConfig.java @@ -0,0 +1,46 @@ +package io.americanexpress.synapse.data.redis.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; +import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory; +import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration; +import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; + +/** + * {@code BaseReactiveRedisDataConfig} + */ +@Configuration +public class BaseReactiveRedisDataConfig extends BaseRedisConfig { + + /** + * Instantiates a new Base reactive redis data config. + * + * @param environment the environment + */ + public BaseReactiveRedisDataConfig(Environment environment) { + super(environment); + } + + /** + * Lettuce connection factory reactive redis connection factory. + * + * @return the reactive redis connection factory + */ + @Bean + public ReactiveRedisConnectionFactory lettuceConnectionFactory() { + return new LettuceConnectionFactory(redisStandaloneConfiguration(), lettuceClientConfiguration()); + } + + /** + * Lettuce client configuration lettuce client configuration. + * + * @return the lettuce client configuration + */ + @Bean + public LettuceClientConfiguration lettuceClientConfiguration() { + return LettuceClientConfiguration.builder() + .build(); + } + +} diff --git a/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseRedisConfig.java b/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseRedisConfig.java new file mode 100644 index 000000000..75b4b46dc --- /dev/null +++ b/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseRedisConfig.java @@ -0,0 +1,82 @@ +package io.americanexpress.synapse.data.redis.config; + +import org.springframework.core.env.Environment; +import org.springframework.data.redis.connection.RedisPassword; +import org.springframework.data.redis.connection.RedisStandaloneConfiguration; + +/** + * The type Base redis config. + */ +public abstract class BaseRedisConfig { + + private final Environment environment; + + /** + * Instantiates a new Base redis config. + * + * @param environment the environment + */ + protected BaseRedisConfig(Environment environment) { + this.environment = environment; + } + + /** + * Redis standalone configuration redis standalone configuration. + * + * @return the redis standalone configuration + */ + protected RedisStandaloneConfiguration redisStandaloneConfiguration() { + RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(); + redisStandaloneConfiguration.setHostName(getRedisHost()); + redisStandaloneConfiguration.setPort(getRedisPort()); + redisStandaloneConfiguration.setDatabase(getRedisDatabase()); + redisStandaloneConfiguration.setUsername(getRedisUsername()); + redisStandaloneConfiguration.setPassword(RedisPassword.of(getRedisPassword())); + return redisStandaloneConfiguration; + } + + /** + * Gets redis host. + * + * @return the redis host + */ + protected String getRedisHost() { + return environment.getRequiredProperty("spring.data.redis.host"); + } + + /** + * Gets redis port. + * + * @return the redis port + */ + protected Integer getRedisPort() { + return Integer.valueOf(environment.getRequiredProperty("spring.data.redis.port")); + } + + /** + * Gets redis database. + * + * @return the redis database + */ + protected Integer getRedisDatabase() { + return Integer.valueOf(environment.getRequiredProperty("spring.data.redis.database")); + } + + /** + * Gets redis username. + * + * @return the redis username + */ + protected String getRedisUsername() { + return environment.getProperty("spring.data.redis.username"); + } + + /** + * Gets redis password. + * + * @return the redis password + */ + protected String getRedisPassword() { + return environment.getProperty("spring.data.redis.password"); + } +} diff --git a/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseRedisDataConfig.java b/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseRedisDataConfig.java index deecb9f5a..59d5afb6a 100644 --- a/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseRedisDataConfig.java +++ b/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseRedisDataConfig.java @@ -19,12 +19,15 @@ import org.springframework.data.redis.connection.RedisClusterConfiguration; import org.springframework.data.redis.connection.RedisSentinelConfiguration; import org.springframework.data.redis.connection.RedisStandaloneConfiguration; +import org.springframework.data.redis.connection.jedis.JedisClientConfiguration; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.repository.configuration.EnableRedisRepositories; import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; +import java.time.Duration; + /** * {@code BaseRedisDataConfig} class is used to hold the common configuration for all data-redis modules. * It provides easy configuration and access to Redis from Spring applications. @@ -33,24 +36,15 @@ */ @Configuration @EnableRedisRepositories -public class BaseRedisDataConfig { - - /** - * {@link Environment} interface representing the environment in which the current application is running. - * Models two key aspects of the application environment (profiles and properties). - * A profile is a named, logical group of bean definitions to be registered with the container - * only if the given profile is active. Beans may be assigned to a profile whether defined in XML or via - * annotations; The role of the {@link Environment} object with relation to profiles is in determining which - * profiles (if any) are currently active, and which profiles (if any) should be active by default. - */ - private final Environment environment; +public class BaseRedisDataConfig extends BaseRedisConfig { /** * The overloaded constructor for the base redis data config that initialized the {@link Environment}. + * * @param environment the environment */ public BaseRedisDataConfig(Environment environment) { - this.environment = environment; + super(environment); } /** @@ -64,13 +58,14 @@ public BaseRedisDataConfig(Environment environment) { */ @Bean JedisConnectionFactory jedisConnectionFactory() { - JedisConnectionFactory jedisConFactory = new JedisConnectionFactory(); - jedisConFactory.setHostName("localhost"); - jedisConFactory.setPort(6379); - jedisConFactory.afterPropertiesSet(); - return jedisConFactory; + JedisClientConfiguration.JedisClientConfigurationBuilder jedisClientConfiguration = JedisClientConfiguration.builder(); + jedisClientConfiguration.connectTimeout(Duration.ofSeconds(60)); + + return new JedisConnectionFactory(redisStandaloneConfiguration(), + jedisClientConfiguration.build()); } + /** * Performs automatic serialization/deserialization between the given objects and the underlying binary data in * the Redis store. By default, it uses Java serialization for its objects diff --git a/pom.xml b/pom.xml index 76ddfde93..a9fe88180 100644 --- a/pom.xml +++ b/pom.xml @@ -1008,16 +1008,16 @@ - - org.springframework.boot - spring-boot-starter-data-redis - 2.7.3 - org.springframework.data spring-data-redis 2.7.3 + + redis.clients + jedis + 3.3.0 + From 4d9059ca67de21d1538d1bf35a072ff4e40a3e60 Mon Sep 17 00:00:00 2001 From: Wendy Hu Date: Tue, 2 May 2023 12:00:51 -0400 Subject: [PATCH 02/11] feat(data-redis): adding sample modules --- .../data/book/config/BookDataConfig.java | 1 + .../data-book-application.properties | 4 +- .../data/book/config/BookDataConfigTest.java | 2 +- .../data/book/config/BookDataTestConfig.java | 2 +- .../book/repository/BookRepositoryIT.java | 3 +- .../sample-data-redis-reactive-book/pom.xml | 38 +++++ .../data/book/config/BookDataConfig.java | 67 ++++++++ .../data/book/entity/BookEntity.java | 69 ++++++++ .../data/book/repository/BookRepository.java | 60 +++++++ .../data-book-application.properties} | 7 +- data/pom.xml | 1 + data/synapse-data-redis/pom.xml | 5 + .../config/BaseReactiveRedisDataConfig.java | 42 ++++- .../data/redis/config/BaseRedisConfig.java | 13 ++ .../redis/config/BaseRedisDataConfig.java | 5 +- pom.xml | 2 +- service/pom.xml | 1 + service/service-samples/pom.xml | 1 + .../pom.xml | 28 ++++ .../service/book/rest/BookApplication.java | 45 +++++ .../service/book/rest/config/BookConfig.java | 41 +++++ .../book/rest/config/BookEndpoints.java | 25 +++ .../ReadMonoBookReactiveController.java | 30 ++++ .../book/rest/model/ReadBookRequest.java | 158 ++++++++++++++++++ .../book/rest/model/ReadBookResponse.java | 123 ++++++++++++++ .../rest/service/ReadBookReactiveService.java | 52 ++++++ .../service-book-application.properties | 18 ++ .../sample-service-rest-redis-book/pom.xml | 26 +++ .../service/book/rest/BookApplication.java | 45 +++++ .../service/book/rest/config/BookConfig.java | 32 ++++ .../book/rest/config/BookEndpoints.java | 27 +++ .../rest/controller/CreateBookController.java | 31 ++++ .../service/book/rest/model/BookRequest.java | 66 ++++++++ .../book/rest/model/CreateBookRequest.java | 21 +++ .../book/rest/model/CreateBookResponse.java | 22 +++ .../book/rest/model/ReadBookRequest.java | 20 +++ .../book/rest/model/ReadBookResponse.java | 82 +++++++++ .../book/rest/service/CreateBookService.java | 47 ++++++ .../service-book-application.properties | 18 ++ 39 files changed, 1260 insertions(+), 20 deletions(-) create mode 100644 data/data-samples/sample-data-redis-reactive-book/pom.xml create mode 100644 data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/config/BookDataConfig.java create mode 100644 data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/entity/BookEntity.java create mode 100644 data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java rename data/data-samples/{sample-data-redis-book/src/test/resources/application.properties => sample-data-redis-reactive-book/src/main/resources/data-book-application.properties} (80%) create mode 100644 service/service-samples/sample-service-reactive-redis-book/pom.xml create mode 100644 service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/BookApplication.java create mode 100644 service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/config/BookConfig.java create mode 100644 service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/config/BookEndpoints.java create mode 100644 service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/controller/ReadMonoBookReactiveController.java create mode 100644 service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/model/ReadBookRequest.java create mode 100644 service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/model/ReadBookResponse.java create mode 100644 service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/service/ReadBookReactiveService.java create mode 100644 service/service-samples/sample-service-reactive-redis-book/src/main/resources/service-book-application.properties create mode 100644 service/service-samples/sample-service-rest-redis-book/pom.xml create mode 100644 service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/BookApplication.java create mode 100644 service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/config/BookConfig.java create mode 100644 service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/config/BookEndpoints.java create mode 100644 service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/controller/CreateBookController.java create mode 100644 service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/model/BookRequest.java create mode 100644 service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/model/CreateBookRequest.java create mode 100644 service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/model/CreateBookResponse.java create mode 100644 service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/model/ReadBookRequest.java create mode 100644 service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/model/ReadBookResponse.java create mode 100644 service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookService.java create mode 100644 service/service-samples/sample-service-rest-redis-book/src/main/resources/service-book-application.properties diff --git a/data/data-samples/sample-data-redis-book/src/main/java/io/americanexpress/data/book/config/BookDataConfig.java b/data/data-samples/sample-data-redis-book/src/main/java/io/americanexpress/data/book/config/BookDataConfig.java index 5a215b7d6..f708ce15e 100644 --- a/data/data-samples/sample-data-redis-book/src/main/java/io/americanexpress/data/book/config/BookDataConfig.java +++ b/data/data-samples/sample-data-redis-book/src/main/java/io/americanexpress/data/book/config/BookDataConfig.java @@ -14,6 +14,7 @@ package io.americanexpress.data.book.config; import io.americanexpress.synapse.data.redis.config.BaseRedisDataConfig; +import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.core.env.Environment; diff --git a/data/data-samples/sample-data-redis-book/src/main/resources/data-book-application.properties b/data/data-samples/sample-data-redis-book/src/main/resources/data-book-application.properties index 17bb89c41..63f0bb279 100644 --- a/data/data-samples/sample-data-redis-book/src/main/resources/data-book-application.properties +++ b/data/data-samples/sample-data-redis-book/src/main/resources/data-book-application.properties @@ -13,6 +13,6 @@ # spring.data.redis.database=0 spring.data.redis.host=localhost -spring.data.redis.port=16379 -spring.data.redis.password=password +spring.data.redis.port=6379 +spring.data.redis.password= spring.data.redis.timeout=60000 diff --git a/data/data-samples/sample-data-redis-book/src/test/java/io/americanexpress/data/book/config/BookDataConfigTest.java b/data/data-samples/sample-data-redis-book/src/test/java/io/americanexpress/data/book/config/BookDataConfigTest.java index 4917a1c66..af80b67b3 100644 --- a/data/data-samples/sample-data-redis-book/src/test/java/io/americanexpress/data/book/config/BookDataConfigTest.java +++ b/data/data-samples/sample-data-redis-book/src/test/java/io/americanexpress/data/book/config/BookDataConfigTest.java @@ -27,7 +27,7 @@ public class BookDataConfigTest { private RedisServer redisServer; public BookDataConfigTest(Environment environment) { - this.redisServer = new RedisServer(Integer.parseInt(environment.getRequiredProperty("spring.redis.port"))); + this.redisServer = new RedisServer(Integer.parseInt(environment.getRequiredProperty("spring.data.redis.port"))); } @PostConstruct diff --git a/data/data-samples/sample-data-redis-book/src/test/java/io/americanexpress/data/book/config/BookDataTestConfig.java b/data/data-samples/sample-data-redis-book/src/test/java/io/americanexpress/data/book/config/BookDataTestConfig.java index 5d09ded8a..d8da1a83b 100644 --- a/data/data-samples/sample-data-redis-book/src/test/java/io/americanexpress/data/book/config/BookDataTestConfig.java +++ b/data/data-samples/sample-data-redis-book/src/test/java/io/americanexpress/data/book/config/BookDataTestConfig.java @@ -20,6 +20,6 @@ * {@code BookDataTestConfig} class contains configurations for tests. */ @Configuration -@Import({BookDataConfig.class}) +@Import({BookDataConfigTest.class}) public class BookDataTestConfig { } diff --git a/data/data-samples/sample-data-redis-book/src/test/java/io/americanexpress/data/book/repository/BookRepositoryIT.java b/data/data-samples/sample-data-redis-book/src/test/java/io/americanexpress/data/book/repository/BookRepositoryIT.java index 28e15ddce..0d52dbe42 100644 --- a/data/data-samples/sample-data-redis-book/src/test/java/io/americanexpress/data/book/repository/BookRepositoryIT.java +++ b/data/data-samples/sample-data-redis-book/src/test/java/io/americanexpress/data/book/repository/BookRepositoryIT.java @@ -38,8 +38,7 @@ class BookRepositoryIT { private BookRepository bookRepository; @Test - public void save_givenValidBook_expectedSavedBookSuccess() { - UUID id = UUID.randomUUID(); + void save_givenValidBook_expectedSavedBookSuccess() { BookEntity bookEntity = new BookEntity("Alice Wonderland", "Lewis Carroll"); BookEntity saved = bookRepository.save(bookEntity); assertNotNull(saved); diff --git a/data/data-samples/sample-data-redis-reactive-book/pom.xml b/data/data-samples/sample-data-redis-reactive-book/pom.xml new file mode 100644 index 000000000..7496ee0df --- /dev/null +++ b/data/data-samples/sample-data-redis-reactive-book/pom.xml @@ -0,0 +1,38 @@ + + + 4.0.0 + + data-samples + io.americanexpress.synapse + 0.3.14-SNAPSHOT + + + sample-data-redis-reactive-book + + + + + io.americanexpress.synapse + synapse-data-redis + + + + org.springframework.boot + spring-boot-starter-test + + + org.springframework.boot + spring-boot-test-autoconfigure + + + + it.ozimov + embedded-redis + 0.7.2 + test + + + + \ No newline at end of file diff --git a/data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/config/BookDataConfig.java b/data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/config/BookDataConfig.java new file mode 100644 index 000000000..2aeafff6e --- /dev/null +++ b/data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/config/BookDataConfig.java @@ -0,0 +1,67 @@ +/* + * Copyright 2020 American Express Travel Related Services Company, Inc. + * + * 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 + * + * http://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 io.americanexpress.data.book.config; + +import io.americanexpress.data.book.entity.BookEntity; +import io.americanexpress.synapse.data.redis.config.BaseReactiveRedisDataConfig; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.core.env.Environment; +import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory; +import org.springframework.data.redis.core.ReactiveRedisTemplate; +import org.springframework.data.redis.repository.configuration.EnableRedisRepositories; +import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.GenericToStringSerializer; +import org.springframework.data.redis.serializer.RedisSerializationContext; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +/** + * {@code BookDataConfig} is the configuration class to load all the properties for the book data module. + */ +@Configuration +@PropertySource("classpath:data-book-application.properties") +@ComponentScan(basePackages = BookDataConfig.PACKAGE_NAME) +@EnableRedisRepositories(basePackages = BookDataConfig.PACKAGE_NAME) +public class BookDataConfig extends BaseReactiveRedisDataConfig { + + /** + * The Package name. + */ + static final String PACKAGE_NAME = "io.americanexpress.data.book"; + + /** + * The {@link BookDataConfig} overloaded constructor. + * @param environment the environment + */ + public BookDataConfig(Environment environment) { + super(environment); + } + + @Override + public ReactiveRedisTemplate reactiveRedisTemplate(ReactiveRedisConnectionFactory factory) { + return new ReactiveRedisTemplate<>(factory, getSerializationContext()); + } + + @Override + protected RedisSerializationContext getSerializationContext() { + return RedisSerializationContext + .newSerializationContext(new StringRedisSerializer()) + .key(new StringRedisSerializer()) + .value(new GenericToStringSerializer<>(BookEntity.class)) + .hashKey(new StringRedisSerializer()) + .hashValue(new GenericJackson2JsonRedisSerializer()) + .build(); + } +} diff --git a/data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/entity/BookEntity.java b/data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/entity/BookEntity.java new file mode 100644 index 000000000..09c48e95d --- /dev/null +++ b/data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/entity/BookEntity.java @@ -0,0 +1,69 @@ +/* + * Copyright 2020 American Express Travel Related Services Company, Inc. + * + * 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 + * + * http://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 io.americanexpress.data.book.entity; + +import io.americanexpress.synapse.data.redis.entity.BaseEntity; +import org.springframework.data.redis.core.RedisHash; + +/** + * {@code BookEntity} class represents the domain of the books table. + */ +@RedisHash("books") +public class BookEntity extends BaseEntity { + + /** + * The title. + */ + private String title; + + /** + * The author. + */ + private String author; + + /** + * The number of copies. + */ + private int numberOfCopies; + + public BookEntity(String title, String author) { + this.title = title; + this.author = author; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + public int getNumberOfCopies() { + return numberOfCopies; + } + + public void setNumberOfCopies(int numberOfCopies) { + this.numberOfCopies = numberOfCopies; + } +} + diff --git a/data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java b/data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java new file mode 100644 index 000000000..888e6a5b4 --- /dev/null +++ b/data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java @@ -0,0 +1,60 @@ +/* + * Copyright 2020 American Express Travel Related Services Company, Inc. + * + * 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 + * + * http://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 io.americanexpress.data.book.repository; + +import io.americanexpress.data.book.entity.BookEntity; +import org.springframework.data.redis.core.ReactiveRedisOperations; +import org.springframework.stereotype.Repository; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +import java.util.UUID; + +@Repository +public class BookRepository { + + private final ReactiveRedisOperations reactiveRedisOperations; + + private final String BOOKS = "books"; + + public BookRepository(ReactiveRedisOperations reactiveRedisOperations) { + this.reactiveRedisOperations = reactiveRedisOperations; + } + + public Mono findByTitle(String title) { + return reactiveRedisOperations.opsForHash() + .values(BOOKS) + .filter(book -> book.getTitle().equals(title)) + .singleOrEmpty(); + } + public Mono save(BookEntity book) { + if (book.getIdentifier() != null) { + String id = UUID.randomUUID().toString(); + book.setIdentifier(id); + } + return reactiveRedisOperations.opsForHash().put(BOOKS, book.getIdentifier(), book) + .log() + .map(p -> book); + + } + + public Mono findById(String id) { + return reactiveRedisOperations.opsForHash().get(BOOKS, id); + } + + public Flux findAll() { + return this.reactiveRedisOperations.opsForHash().values(BOOKS); + } +} + diff --git a/data/data-samples/sample-data-redis-book/src/test/resources/application.properties b/data/data-samples/sample-data-redis-reactive-book/src/main/resources/data-book-application.properties similarity index 80% rename from data/data-samples/sample-data-redis-book/src/test/resources/application.properties rename to data/data-samples/sample-data-redis-reactive-book/src/main/resources/data-book-application.properties index 4f6411d05..63f0bb279 100644 --- a/data/data-samples/sample-data-redis-book/src/test/resources/application.properties +++ b/data/data-samples/sample-data-redis-reactive-book/src/main/resources/data-book-application.properties @@ -11,5 +11,8 @@ # or implied. See the License for the specific language governing permissions and limitations under # the License. # -#spring.redis.host=localhost -#spring.redis.port=6370 +spring.data.redis.database=0 +spring.data.redis.host=localhost +spring.data.redis.port=6379 +spring.data.redis.password= +spring.data.redis.timeout=60000 diff --git a/data/pom.xml b/data/pom.xml index 0c7958ef6..b3627ef38 100644 --- a/data/pom.xml +++ b/data/pom.xml @@ -35,6 +35,7 @@ synapse-data-oracle synapse-data-postgres synapse-data-redis + data-samples/sample-data-redis-reactive-book diff --git a/data/synapse-data-redis/pom.xml b/data/synapse-data-redis/pom.xml index 3fc88edbf..abe3ff2b0 100644 --- a/data/synapse-data-redis/pom.xml +++ b/data/synapse-data-redis/pom.xml @@ -28,6 +28,11 @@ org.springframework.data spring-data-redis + + org.springframework.boot + spring-boot-starter-data-redis-reactive + 2.7.5 + redis.clients diff --git a/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseReactiveRedisDataConfig.java b/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseReactiveRedisDataConfig.java index e5bede280..1e904b5d1 100644 --- a/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseReactiveRedisDataConfig.java +++ b/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseReactiveRedisDataConfig.java @@ -1,3 +1,16 @@ +/* + * Copyright 2020 American Express Travel Related Services Company, Inc. + * + * 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 + * + * http://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 io.americanexpress.synapse.data.redis.config; import org.springframework.context.annotation.Bean; @@ -6,6 +19,8 @@ import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory; import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; +import org.springframework.data.redis.core.ReactiveRedisTemplate; +import org.springframework.data.redis.serializer.RedisSerializationContext; /** * {@code BaseReactiveRedisDataConfig} @@ -22,15 +37,15 @@ public BaseReactiveRedisDataConfig(Environment environment) { super(environment); } - /** - * Lettuce connection factory reactive redis connection factory. - * - * @return the reactive redis connection factory - */ - @Bean - public ReactiveRedisConnectionFactory lettuceConnectionFactory() { - return new LettuceConnectionFactory(redisStandaloneConfiguration(), lettuceClientConfiguration()); - } +// /** +// * Lettuce connection factory reactive redis connection factory. +// * +// * @return the reactive redis connection factory +// */ +// @Bean +// public ReactiveRedisConnectionFactory lettuceConnectionFactory() { +// return new LettuceConnectionFactory(redisStandaloneConfiguration(), lettuceClientConfiguration()); +// } /** * Lettuce client configuration lettuce client configuration. @@ -43,4 +58,13 @@ public LettuceClientConfiguration lettuceClientConfiguration() { .build(); } + @Bean + public ReactiveRedisTemplate reactiveRedisTemplate(ReactiveRedisConnectionFactory factory) { + return new ReactiveRedisTemplate<>(factory, getSerializationContext()); + } + + protected RedisSerializationContext getSerializationContext() { + return RedisSerializationContext.string(); + } + } diff --git a/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseRedisConfig.java b/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseRedisConfig.java index 75b4b46dc..cc3e85c74 100644 --- a/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseRedisConfig.java +++ b/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseRedisConfig.java @@ -1,3 +1,16 @@ +/* + * Copyright 2020 American Express Travel Related Services Company, Inc. + * + * 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 + * + * http://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 io.americanexpress.synapse.data.redis.config; import org.springframework.core.env.Environment; diff --git a/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseRedisDataConfig.java b/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseRedisDataConfig.java index 59d5afb6a..7d00eeb53 100644 --- a/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseRedisDataConfig.java +++ b/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseRedisDataConfig.java @@ -61,8 +61,10 @@ JedisConnectionFactory jedisConnectionFactory() { JedisClientConfiguration.JedisClientConfigurationBuilder jedisClientConfiguration = JedisClientConfiguration.builder(); jedisClientConfiguration.connectTimeout(Duration.ofSeconds(60)); - return new JedisConnectionFactory(redisStandaloneConfiguration(), + JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisStandaloneConfiguration(), jedisClientConfiguration.build()); + jedisConnectionFactory.afterPropertiesSet(); + return jedisConnectionFactory; } @@ -78,7 +80,6 @@ JedisConnectionFactory jedisConnectionFactory() { public RedisTemplate redisTemplate() { RedisTemplate template = new RedisTemplate<>(); template.setConnectionFactory(jedisConnectionFactory()); - template.setEnableTransactionSupport(true); template.setHashKeySerializer(new StringRedisSerializer()); template.setHashKeySerializer(new JdkSerializationRedisSerializer()); diff --git a/pom.xml b/pom.xml index a9fe88180..1cbe59896 100644 --- a/pom.xml +++ b/pom.xml @@ -1016,7 +1016,7 @@ redis.clients jedis - 3.3.0 + 3.8.0 diff --git a/service/pom.xml b/service/pom.xml index 911bf5fc8..a67a09c37 100644 --- a/service/pom.xml +++ b/service/pom.xml @@ -30,6 +30,7 @@ synapse-service-reactive-rest synapse-service-rest synapse-service-test + service-samples/sample-service-rest-redis-book diff --git a/service/service-samples/pom.xml b/service/service-samples/pom.xml index d41c7dca5..57ee3f832 100644 --- a/service/service-samples/pom.xml +++ b/service/service-samples/pom.xml @@ -26,6 +26,7 @@ sample-service-rest-mysql-book sample-service-rest-oracle-book sample-service-rest-postgres-book + sample-service-reactive-redis-book diff --git a/service/service-samples/sample-service-reactive-redis-book/pom.xml b/service/service-samples/sample-service-reactive-redis-book/pom.xml new file mode 100644 index 000000000..5e96823f3 --- /dev/null +++ b/service/service-samples/sample-service-reactive-redis-book/pom.xml @@ -0,0 +1,28 @@ + + + 4.0.0 + + io.americanexpress.synapse + service-samples + 0.3.14-SNAPSHOT + + + sample-service-reactive-redis-book + + + + + + io.americanexpress.synapse + synapse-service-reactive-rest + + + io.americanexpress.synapse + sample-data-redis-reactive-book + 0.3.14-SNAPSHOT + + + + \ No newline at end of file diff --git a/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/BookApplication.java b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/BookApplication.java new file mode 100644 index 000000000..91c6630b5 --- /dev/null +++ b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/BookApplication.java @@ -0,0 +1,45 @@ +/* + * Copyright 2020 American Express Travel Related Services Company, Inc. + * + * 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 + * + * http://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 io.americanexpress.service.book.rest; + +import io.swagger.v3.oas.annotations.OpenAPIDefinition; +import io.swagger.v3.oas.annotations.info.Info; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * BookApplication starts the Spring Boot Application for the book rest sample. + */ +@OpenAPIDefinition(info = @Info( + title = "Book API", + version = "v1.0.0", + description = "Rest API that provides book related information.")) +@SpringBootApplication +public class BookApplication { + + private static final XLogger LOGGER = XLoggerFactory.getXLogger(BookApplication.class); + + /** + * Main method to run the Spring Boot Book Application. + * + * @param args the args + */ + public static void main(String[] args) { + SpringApplication.run(BookApplication.class, args); + LOGGER.info("Rest Book Application is up and running ..."); + } + +} diff --git a/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/config/BookConfig.java b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/config/BookConfig.java new file mode 100644 index 000000000..248537a78 --- /dev/null +++ b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/config/BookConfig.java @@ -0,0 +1,41 @@ +/* + * Copyright 2020 American Express Travel Related Services Company, Inc. + * + * 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 + * + * http://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 io.americanexpress.service.book.rest.config; + +import com.fasterxml.jackson.databind.ObjectMapper; +import io.americanexpress.data.book.config.BookDataConfig; +import io.americanexpress.synapse.service.reactive.rest.config.BaseServiceReactiveRestConfig; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.context.annotation.PropertySource; + +/** + * {@code BookConfig} is the configuration class for the Book Application. + */ +@Configuration +@PropertySource("classpath:/service-book-application.properties") +@ComponentScan(basePackages = "io.americanexpress.service.book.rest") +@Import({BookDataConfig.class}) +public class BookConfig extends BaseServiceReactiveRestConfig { + + /** + * Constructor taking in objectMapper. + * + * @param defaultObjectMapper the default object mapper. + */ + public BookConfig(ObjectMapper defaultObjectMapper) { + super(defaultObjectMapper); + } +} diff --git a/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/config/BookEndpoints.java b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/config/BookEndpoints.java new file mode 100644 index 000000000..cc45d78e5 --- /dev/null +++ b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/config/BookEndpoints.java @@ -0,0 +1,25 @@ +/* + * Copyright 2020 American Express Travel Related Services Company, Inc. + * + * 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 + * + * http://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 io.americanexpress.service.book.rest.config; + +/** + * {@code BookEndpoints} contains the endpoint for the Book API. + */ +public class BookEndpoints { + + /** + * The constant BOOK_ENDPOINT. + */ + public static final String BOOK_ENDPOINT = "/v1/books"; +} diff --git a/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/controller/ReadMonoBookReactiveController.java b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/controller/ReadMonoBookReactiveController.java new file mode 100644 index 000000000..ae755bc4d --- /dev/null +++ b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/controller/ReadMonoBookReactiveController.java @@ -0,0 +1,30 @@ +/* + * Copyright 2020 American Express Travel Related Services Company, Inc. + * + * 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 + * + * http://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 io.americanexpress.service.book.rest.controller; + +import io.americanexpress.service.book.rest.config.BookEndpoints; +import io.americanexpress.service.book.rest.model.ReadBookRequest; +import io.americanexpress.service.book.rest.model.ReadBookResponse; +import io.americanexpress.service.book.rest.service.ReadBookReactiveService; +import io.americanexpress.synapse.service.reactive.rest.controller.BaseReadMonoReactiveController; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * {@code ReadMonoBookReactiveController} retrieves ReadBookResponses by ReadPolyBookRequest + */ +@RestController +@RequestMapping(BookEndpoints.BOOK_ENDPOINT) +public class ReadMonoBookReactiveController extends BaseReadMonoReactiveController { +} diff --git a/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/model/ReadBookRequest.java b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/model/ReadBookRequest.java new file mode 100644 index 000000000..770348263 --- /dev/null +++ b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/model/ReadBookRequest.java @@ -0,0 +1,158 @@ +/* + * Copyright 2020 American Express Travel Related Services Company, Inc. + * + * 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 + * + * http://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 io.americanexpress.service.book.rest.model; + +import io.americanexpress.synapse.service.reactive.rest.model.BaseServiceRequest; + +import java.util.Objects; + +/** + * {@code ReadBookRequest} is the model used on the request of the Read Book Controller. + */ +public class ReadBookRequest implements BaseServiceRequest { + + /** + * The book identifier. + */ + private String identifier; + + /** + * The book title. + */ + private String title; + + /** + * The author of the book. + */ + private String author; + + /** + * Instantiates a new Read book request. + */ + public ReadBookRequest() { + } + + /** + * Instantiates a new ReadBookRequest. + * + * @param title the title + * @param author the author + */ + public ReadBookRequest(String title, String author) { + this.title = title; + this.author = author; + } + + /** + * Instantiates a new ReadBookRequest. + * @param identifier the identifier + * @param title the title + * @param author the author + */ + public ReadBookRequest(String identifier, String title, String author) { + this.identifier = identifier; + this.title = title; + this.author = author; + } + + /** + * Gets identifier. + * + * @return the identifier + */ + public String getIdentifier() { + return identifier; + } + + /** + * Gets identifier. + * + * @param identifier the identifier + */ + public void setIdentifier(String identifier) { + this.identifier = identifier; + } + + /** + * Gets title. + * + * @return the title + */ + public String getTitle() { + return title; + } + + /** + * Sets title. + * + * @param title the title + */ + public void setTitle(String title) { + this.title = title; + } + + /** + * Gets author. + * + * @return the author + */ + public String getAuthor() { + return author; + } + + /** + * Sets author. + * + * @param author the author + */ + public void setAuthor(String author) { + this.author = author; + } + + /** + * Determines if one object is equal to another. + * + * @param o the object + * @return true if equal, else false + */ + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ReadBookRequest that = (ReadBookRequest) o; + return identifier == that.identifier && Objects.equals(title, that.title) && Objects.equals(author, that.author); + } + + /** + * Returns a hash code value for the object. + * @return the object's hashcode + */ + @Override + public int hashCode() { + return Objects.hash(identifier, title, author); + } + + /** + * Returns a string representation of the object. + * @return the string representation of the object + */ + @Override + public String toString() { + return "ReadPolyBookRequest{" + + "identifier=" + identifier + + ", title='" + title + '\'' + + ", author='" + author + '\'' + + '}'; + } +} diff --git a/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/model/ReadBookResponse.java b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/model/ReadBookResponse.java new file mode 100644 index 000000000..20fe791c1 --- /dev/null +++ b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/model/ReadBookResponse.java @@ -0,0 +1,123 @@ +/* + * Copyright 2020 American Express Travel Related Services Company, Inc. + * + * 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 + * + * http://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 io.americanexpress.service.book.rest.model; + +import io.americanexpress.synapse.service.reactive.rest.model.BaseServiceResponse; + +import java.util.Objects; + +/** + * {@code ReadBookResponse} is the response used on the mono and poly read controllers. + */ +public class ReadBookResponse extends BaseServiceResponse { + + /** + * The book title. + */ + private String title; + + /** + * The author of the book. + */ + private String author; + + /** + * Instantiates a new ReadBookResponse. + */ + public ReadBookResponse() { + } + + /** + * Instantiates a new ReadBookResponse. + * + * @param title the title + * @param author the author + */ + public ReadBookResponse(String title, String author) { + this.title = title; + this.author = author; + } + + /** + * Instantiates a new ReadBookResponse. + * + * @param id the identifier + * @param title the title + * @param author the author + */ + public ReadBookResponse(String id, String title, String author) { + this.title = title; + this.author = author; + super.setId(id); + } + + /** + * Gets title. + * + * @return the title + */ + public String getTitle() { + return title; + } + + /** + * Sets title. + * + * @param title the title + */ + public void setTitle(String title) { + this.title = title; + } + + /** + * Gets author. + * + * @return the author + */ + public String getAuthor() { + return author; + } + + /** + * Sets author. + * + * @param author the author + */ + public void setAuthor(String author) { + this.author = author; + } + + /** + * Determines if one object is equal to another. + * + * @param o the object + * @return true if equal, else false + */ + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ReadBookResponse that = (ReadBookResponse) o; + return Objects.equals(title, that.title) && Objects.equals(author, that.author); + } + + /** + * Returns a hash code value for the object. + * @return the object's hashcode + */ + @Override + public int hashCode() { + return Objects.hash(title, author); + } +} diff --git a/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/service/ReadBookReactiveService.java b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/service/ReadBookReactiveService.java new file mode 100644 index 000000000..57acc3937 --- /dev/null +++ b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/service/ReadBookReactiveService.java @@ -0,0 +1,52 @@ +/* + * Copyright 2020 American Express Travel Related Services Company, Inc. + * + * 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 + * + * http://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 io.americanexpress.service.book.rest.service; + +import io.americanexpress.data.book.repository.BookRepository; +import io.americanexpress.service.book.rest.model.ReadBookRequest; +import io.americanexpress.service.book.rest.model.ReadBookResponse; +import io.americanexpress.synapse.service.reactive.rest.service.BaseReadMonoReactiveService; +import org.springframework.http.HttpHeaders; +import org.springframework.stereotype.Service; +import reactor.core.publisher.Mono; + +/** + * {@code ReadMonoBookReactiveService} service layer for retrieving a book resources + */ +@Service +public class ReadBookReactiveService extends BaseReadMonoReactiveService { + + private final BookRepository bookRepository; + + public ReadBookReactiveService(BookRepository bookRepository) { + this.bookRepository = bookRepository; + } + + /** + * Overriding executeRead. + * @param request a read mono book service request + * @return a mono read book response + */ + @Override + protected Mono executeRead(HttpHeaders headers, ReadBookRequest request) { + return bookRepository.findByTitle(request.getTitle()) + .map(book -> { + ReadBookResponse readBookResponse = new ReadBookResponse(); + readBookResponse.setTitle(book.getTitle()); + readBookResponse.setAuthor(book.getAuthor()); + return readBookResponse; + }); + } + +} diff --git a/service/service-samples/sample-service-reactive-redis-book/src/main/resources/service-book-application.properties b/service/service-samples/sample-service-reactive-redis-book/src/main/resources/service-book-application.properties new file mode 100644 index 000000000..ed8abeb0c --- /dev/null +++ b/service/service-samples/sample-service-reactive-redis-book/src/main/resources/service-book-application.properties @@ -0,0 +1,18 @@ +#/* +#* Copyright 2020 American Express Travel Related Services Company, Inc. +#* +#* 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 +#* +#* http://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. +#*/ +spring.application.name=Book Rest Service +spring.main.allow-bean-definition-overriding=true +server.port=8080 +wavefront.application.name=bookstore +wavefront.application.service=example-service-rest-book diff --git a/service/service-samples/sample-service-rest-redis-book/pom.xml b/service/service-samples/sample-service-rest-redis-book/pom.xml new file mode 100644 index 000000000..3bd240d98 --- /dev/null +++ b/service/service-samples/sample-service-rest-redis-book/pom.xml @@ -0,0 +1,26 @@ + + + 4.0.0 + + io.americanexpress.synapse + service + 0.3.14-SNAPSHOT + ../../pom.xml + + + sample-service-rest-redis-book + + + + io.americanexpress.synapse + sample-data-redis-book + + + io.americanexpress.synapse + synapse-service-rest + + + + \ No newline at end of file diff --git a/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/BookApplication.java b/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/BookApplication.java new file mode 100644 index 000000000..d6d65306e --- /dev/null +++ b/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/BookApplication.java @@ -0,0 +1,45 @@ +/* + * Copyright 2020 American Express Travel Related Services Company, Inc. + * + * 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 + * + * http://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 io.americanexpress.service.book.rest; + +import io.swagger.v3.oas.annotations.OpenAPIDefinition; +import io.swagger.v3.oas.annotations.info.Info; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * {@code BookApplication} starts the Spring Boot Application for the book rest sample. + */ +@OpenAPIDefinition(info = @Info( + title = "Book API", + version = "v1.0.0", + description = "Rest API that provides book related information.")) +@SpringBootApplication +public class BookApplication { + + private static final XLogger LOGGER = XLoggerFactory.getXLogger(BookApplication.class); + + /** + * Main method to run the Spring Boot Book Application. + * + * @param args the args + */ + public static void main(String[] args) { + SpringApplication.run(BookApplication.class, args); + LOGGER.info("Redis Rest Book Application is up and running ..."); + } + +} diff --git a/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/config/BookConfig.java b/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/config/BookConfig.java new file mode 100644 index 000000000..e5f323220 --- /dev/null +++ b/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/config/BookConfig.java @@ -0,0 +1,32 @@ +/* + * Copyright 2020 American Express Travel Related Services Company, Inc. + * + * 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 + * + * http://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 io.americanexpress.service.book.rest.config; + +import io.americanexpress.data.book.config.BookDataConfig; +import io.americanexpress.synapse.service.rest.config.ServiceRestConfig; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.context.annotation.PropertySource; + +/** + * {@code BookConfig} is the configuration class for the Book Application. + */ +@Configuration +@PropertySource("classpath:/service-book-application.properties") +@ComponentScan(basePackages = "io.americanexpress.service.book.rest") +@Import({ServiceRestConfig.class, BookDataConfig.class}) +public class BookConfig { + +} diff --git a/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/config/BookEndpoints.java b/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/config/BookEndpoints.java new file mode 100644 index 000000000..cb22a2329 --- /dev/null +++ b/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/config/BookEndpoints.java @@ -0,0 +1,27 @@ +/* + * Copyright 2020 American Express Travel Related Services Company, Inc. + * + * 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 + * + * http://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 io.americanexpress.service.book.rest.config; + +/** + * {@code BookEndpoints} contains the endpoints for the Book Application. + */ +public class BookEndpoints { + + private BookEndpoints() {} + + /** + * The constant BOOK_ENDPOINT. + */ + public static final String BOOK_ENDPOINT = "/v1/books"; +} diff --git a/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/controller/CreateBookController.java b/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/controller/CreateBookController.java new file mode 100644 index 000000000..4fd499b51 --- /dev/null +++ b/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/controller/CreateBookController.java @@ -0,0 +1,31 @@ +/* + * Copyright 2020 American Express Travel Related Services Company, Inc. + * + * 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 + * + * http://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 io.americanexpress.service.book.rest.controller; + +import io.americanexpress.service.book.rest.config.BookEndpoints; +import io.americanexpress.service.book.rest.model.CreateBookRequest; +import io.americanexpress.service.book.rest.model.CreateBookResponse; +import io.americanexpress.service.book.rest.service.CreateBookService; +import io.americanexpress.synapse.service.rest.controller.BaseCreateController; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * {@code CreateBookController} is the controller class for creating a book in the Cassandra Book database. + */ +@RestController +@RequestMapping(BookEndpoints.BOOK_ENDPOINT) +public class CreateBookController extends BaseCreateController { + +} diff --git a/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/model/BookRequest.java b/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/model/BookRequest.java new file mode 100644 index 000000000..843c7776b --- /dev/null +++ b/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/model/BookRequest.java @@ -0,0 +1,66 @@ +/* + * Copyright 2020 American Express Travel Related Services Company, Inc. + * + * 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 + * + * http://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 io.americanexpress.service.book.rest.model; + +import io.americanexpress.synapse.service.rest.model.BaseServiceRequest; + +import javax.validation.constraints.NotBlank; + +/** + * {@code BookRequest} is the base book request object. + */ +public class BookRequest implements BaseServiceRequest { + + @NotBlank + private String title; + + @NotBlank + private String author; + + /** + * Gets title. + * + * @return the title + */ + public String getTitle() { + return title; + } + + /** + * Sets title. + * + * @param title the title + */ + public void setTitle(String title) { + this.title = title; + } + + /** + * Gets author. + * + * @return the author + */ + public String getAuthor() { + return author; + } + + /** + * Sets author. + * + * @param author the author + */ + public void setAuthor(String author) { + this.author = author; + } +} diff --git a/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/model/CreateBookRequest.java b/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/model/CreateBookRequest.java new file mode 100644 index 000000000..5379e9000 --- /dev/null +++ b/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/model/CreateBookRequest.java @@ -0,0 +1,21 @@ +/* + * Copyright 2020 American Express Travel Related Services Company, Inc. + * + * 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 + * + * http://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 io.americanexpress.service.book.rest.model; + +/** + * {@code CreateBookRequest} is the request object for creating book. + */ +public class CreateBookRequest extends BookRequest { + +} diff --git a/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/model/CreateBookResponse.java b/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/model/CreateBookResponse.java new file mode 100644 index 000000000..94114ee0f --- /dev/null +++ b/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/model/CreateBookResponse.java @@ -0,0 +1,22 @@ +/* + * Copyright 2020 American Express Travel Related Services Company, Inc. + * + * 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 + * + * http://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 io.americanexpress.service.book.rest.model; + +import io.americanexpress.synapse.service.rest.model.BaseServiceResponse; + +/** + * {@code CreateBookResponse} is the response object for creating book. + */ +public class CreateBookResponse extends BaseServiceResponse { +} diff --git a/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/model/ReadBookRequest.java b/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/model/ReadBookRequest.java new file mode 100644 index 000000000..93d177ec3 --- /dev/null +++ b/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/model/ReadBookRequest.java @@ -0,0 +1,20 @@ +/* + * Copyright 2020 American Express Travel Related Services Company, Inc. + * + * 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 + * + * http://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 io.americanexpress.service.book.rest.model; + +/** + * {@code ReadBookRequest} is the request object for retrieving a book. + */ +public class ReadBookRequest extends BookRequest { +} diff --git a/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/model/ReadBookResponse.java b/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/model/ReadBookResponse.java new file mode 100644 index 000000000..082ea258d --- /dev/null +++ b/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/model/ReadBookResponse.java @@ -0,0 +1,82 @@ +/* + * Copyright 2020 American Express Travel Related Services Company, Inc. + * + * 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 + * + * http://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 io.americanexpress.service.book.rest.model; + +import io.americanexpress.synapse.service.rest.model.BaseServiceResponse; + +/** + * {@code ReadBookResponse} is the response object for retrieving a book. + */ +public class ReadBookResponse extends BaseServiceResponse { + + private String title; + + private String author; + + private int numberOfCopies; + + /** + * Gets title. + * + * @return the title + */ + public String getTitle() { + return title; + } + + /** + * Sets title. + * + * @param title the title + */ + public void setTitle(String title) { + this.title = title; + } + + /** + * Gets author. + * + * @return the author + */ + public String getAuthor() { + return author; + } + + /** + * Sets author. + * + * @param author the author + */ + public void setAuthor(String author) { + this.author = author; + } + + /** + * Gets number of copies. + * + * @return the number of copies + */ + public int getNumberOfCopies() { + return numberOfCopies; + } + + /** + * Sets number of copies. + * + * @param numberOfCopies the number of copies + */ + public void setNumberOfCopies(int numberOfCopies) { + this.numberOfCopies = numberOfCopies; + } +} diff --git a/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookService.java b/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookService.java new file mode 100644 index 000000000..482a3f57b --- /dev/null +++ b/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookService.java @@ -0,0 +1,47 @@ +/* + * Copyright 2020 American Express Travel Related Services Company, Inc. + * + * 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 + * + * http://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 io.americanexpress.service.book.rest.service; + +import io.americanexpress.data.book.entity.BookEntity; +import io.americanexpress.data.book.repository.BookRepository; +import io.americanexpress.service.book.rest.model.CreateBookRequest; +import io.americanexpress.service.book.rest.model.CreateBookResponse; +import io.americanexpress.synapse.service.rest.service.BaseCreateService; +import org.springframework.http.HttpHeaders; +import org.springframework.stereotype.Service; + +/** + * {@code CreateBookService} is the service class for creating a book in the Cassandra Book database. + */ +@Service +public class CreateBookService extends BaseCreateService { + + private final BookRepository bookRepository; + + /** + * Instantiates a new CreateBookService. + * + * @param bookRepository the book repository + */ + public CreateBookService(BookRepository bookRepository) { + this.bookRepository = bookRepository; + } + + @Override + protected CreateBookResponse executeCreate(HttpHeaders headers, CreateBookRequest request) { + BookEntity book = new BookEntity(request.getTitle(), request.getAuthor()); + bookRepository.save(book); + return new CreateBookResponse(); + } +} diff --git a/service/service-samples/sample-service-rest-redis-book/src/main/resources/service-book-application.properties b/service/service-samples/sample-service-rest-redis-book/src/main/resources/service-book-application.properties new file mode 100644 index 000000000..ed8abeb0c --- /dev/null +++ b/service/service-samples/sample-service-rest-redis-book/src/main/resources/service-book-application.properties @@ -0,0 +1,18 @@ +#/* +#* Copyright 2020 American Express Travel Related Services Company, Inc. +#* +#* 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 +#* +#* http://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. +#*/ +spring.application.name=Book Rest Service +spring.main.allow-bean-definition-overriding=true +server.port=8080 +wavefront.application.name=bookstore +wavefront.application.service=example-service-rest-book From c2c32bc113dd70483fc3051aabe529165792a4b9 Mon Sep 17 00:00:00 2001 From: Wendy Hu Date: Tue, 2 May 2023 12:54:09 -0400 Subject: [PATCH 03/11] feat(data-redis): adding sample reactive redis service --- .../data/book/entity/BookEntity.java | 2 + .../data/book/repository/BookRepository.java | 2 +- .../config/BaseReactiveRedisDataConfig.java | 18 +- .../CreateBookReactiveController.java | 17 ++ .../book/rest/model/CreateBookRequest.java | 158 ++++++++++++++++++ .../book/rest/model/CreateBookResponse.java | 24 +++ .../service/CreateBookReactiveService.java | 29 ++++ 7 files changed, 240 insertions(+), 10 deletions(-) create mode 100644 service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/controller/CreateBookReactiveController.java create mode 100644 service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/model/CreateBookRequest.java create mode 100644 service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/model/CreateBookResponse.java create mode 100644 service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookReactiveService.java diff --git a/data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/entity/BookEntity.java b/data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/entity/BookEntity.java index 09c48e95d..3ec286d7f 100644 --- a/data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/entity/BookEntity.java +++ b/data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/entity/BookEntity.java @@ -42,6 +42,8 @@ public BookEntity(String title, String author) { this.author = author; } + public BookEntity() {} + public String getTitle() { return title; } diff --git a/data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java b/data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java index 888e6a5b4..2578a5f77 100644 --- a/data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java +++ b/data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java @@ -39,7 +39,7 @@ public Mono findByTitle(String title) { .singleOrEmpty(); } public Mono save(BookEntity book) { - if (book.getIdentifier() != null) { + if (book.getIdentifier() == null) { String id = UUID.randomUUID().toString(); book.setIdentifier(id); } diff --git a/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseReactiveRedisDataConfig.java b/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseReactiveRedisDataConfig.java index 1e904b5d1..6ccabf651 100644 --- a/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseReactiveRedisDataConfig.java +++ b/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseReactiveRedisDataConfig.java @@ -37,15 +37,15 @@ public BaseReactiveRedisDataConfig(Environment environment) { super(environment); } -// /** -// * Lettuce connection factory reactive redis connection factory. -// * -// * @return the reactive redis connection factory -// */ -// @Bean -// public ReactiveRedisConnectionFactory lettuceConnectionFactory() { -// return new LettuceConnectionFactory(redisStandaloneConfiguration(), lettuceClientConfiguration()); -// } + /** + * Lettuce connection factory reactive redis connection factory. + * + * @return the reactive redis connection factory + */ + @Bean + public LettuceConnectionFactory lettuceConnectionFactory() { + return new LettuceConnectionFactory(redisStandaloneConfiguration(), lettuceClientConfiguration()); + } /** * Lettuce client configuration lettuce client configuration. diff --git a/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/controller/CreateBookReactiveController.java b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/controller/CreateBookReactiveController.java new file mode 100644 index 000000000..9a4b3e0d5 --- /dev/null +++ b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/controller/CreateBookReactiveController.java @@ -0,0 +1,17 @@ +package io.americanexpress.service.book.rest.controller; + +import io.americanexpress.service.book.rest.config.BookEndpoints; +import io.americanexpress.service.book.rest.model.CreateBookRequest; +import io.americanexpress.service.book.rest.model.CreateBookResponse; +import io.americanexpress.service.book.rest.service.CreateBookReactiveService; +import io.americanexpress.synapse.service.reactive.rest.controller.BaseCreateReactiveController; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * {@code CreateBookReactiveController} + */ +@RestController +@RequestMapping(BookEndpoints.BOOK_ENDPOINT) +public class CreateBookReactiveController extends BaseCreateReactiveController { +} diff --git a/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/model/CreateBookRequest.java b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/model/CreateBookRequest.java new file mode 100644 index 000000000..987691026 --- /dev/null +++ b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/model/CreateBookRequest.java @@ -0,0 +1,158 @@ +/* + * Copyright 2020 American Express Travel Related Services Company, Inc. + * + * 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 + * + * http://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 io.americanexpress.service.book.rest.model; + +import io.americanexpress.synapse.service.reactive.rest.model.BaseServiceRequest; + +import java.util.Objects; + +/** + * {@code CreateBookRequest} is the model used on the request of the Create Book Controller. + */ +public class CreateBookRequest implements BaseServiceRequest { + + /** + * The book identifier. + */ + private String identifier; + + /** + * The book title. + */ + private String title; + + /** + * The author of the book. + */ + private String author; + + /** + * Instantiates a new Read book request. + */ + public CreateBookRequest() { + } + + /** + * Instantiates a new ReadBookRequest. + * + * @param title the title + * @param author the author + */ + public CreateBookRequest(String title, String author) { + this.title = title; + this.author = author; + } + + /** + * Instantiates a new ReadBookRequest. + * @param identifier the identifier + * @param title the title + * @param author the author + */ + public CreateBookRequest(String identifier, String title, String author) { + this.identifier = identifier; + this.title = title; + this.author = author; + } + + /** + * Gets identifier. + * + * @return the identifier + */ + public String getIdentifier() { + return identifier; + } + + /** + * Gets identifier. + * + * @param identifier the identifier + */ + public void setIdentifier(String identifier) { + this.identifier = identifier; + } + + /** + * Gets title. + * + * @return the title + */ + public String getTitle() { + return title; + } + + /** + * Sets title. + * + * @param title the title + */ + public void setTitle(String title) { + this.title = title; + } + + /** + * Gets author. + * + * @return the author + */ + public String getAuthor() { + return author; + } + + /** + * Sets author. + * + * @param author the author + */ + public void setAuthor(String author) { + this.author = author; + } + + /** + * Determines if one object is equal to another. + * + * @param o the object + * @return true if equal, else false + */ + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + CreateBookRequest that = (CreateBookRequest) o; + return identifier == that.identifier && Objects.equals(title, that.title) && Objects.equals(author, that.author); + } + + /** + * Returns a hash code value for the object. + * @return the object's hashcode + */ + @Override + public int hashCode() { + return Objects.hash(identifier, title, author); + } + + /** + * Returns a string representation of the object. + * @return the string representation of the object + */ + @Override + public String toString() { + return "ReadPolyBookRequest{" + + "identifier=" + identifier + + ", title='" + title + '\'' + + ", author='" + author + '\'' + + '}'; + } +} diff --git a/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/model/CreateBookResponse.java b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/model/CreateBookResponse.java new file mode 100644 index 000000000..317efa21d --- /dev/null +++ b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/model/CreateBookResponse.java @@ -0,0 +1,24 @@ +/* + * Copyright 2020 American Express Travel Related Services Company, Inc. + * + * 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 + * + * http://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 io.americanexpress.service.book.rest.model; + +import io.americanexpress.synapse.service.reactive.rest.model.BaseServiceResponse; + + +/** + * {@code CreateBookResponse} + */ +public class CreateBookResponse extends BaseServiceResponse { + +} diff --git a/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookReactiveService.java b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookReactiveService.java new file mode 100644 index 000000000..e8fe95c47 --- /dev/null +++ b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookReactiveService.java @@ -0,0 +1,29 @@ +package io.americanexpress.service.book.rest.service; + +import io.americanexpress.data.book.entity.BookEntity; +import io.americanexpress.data.book.repository.BookRepository; +import io.americanexpress.service.book.rest.model.CreateBookRequest; +import io.americanexpress.service.book.rest.model.CreateBookResponse; +import io.americanexpress.synapse.service.reactive.rest.service.BaseCreateReactiveService; +import org.springframework.http.HttpHeaders; +import org.springframework.stereotype.Service; +import reactor.core.publisher.Mono; + +@Service +public class CreateBookReactiveService extends BaseCreateReactiveService { + + private final BookRepository bookRepository; + + public CreateBookReactiveService(BookRepository bookRepository) { + this.bookRepository = bookRepository; + } + + @Override + protected Mono executeCreate(HttpHeaders headers, CreateBookRequest request) { + BookEntity book = new BookEntity(request.getTitle(), request.getAuthor()); + return bookRepository.save(book) + .map(bookEntity -> { + return new CreateBookResponse(); + }); + } +} From c8a1578469b78f6d1fc3ca30ce69512000fb02eb Mon Sep 17 00:00:00 2001 From: Wendy Hu Date: Tue, 2 May 2023 15:36:31 -0400 Subject: [PATCH 04/11] fix(): adding javadocs, fixing deps --- data/data-samples/pom.xml | 1 + .../sample-data-redis-reactive-book/pom.xml | 2 +- data/pom.xml | 1 - .../config/BaseReactiveRedisDataConfig.java | 24 +++++++++++++++---- .../data/redis/config/BaseRedisConfig.java | 4 ++-- pom.xml | 6 ++++- .../pom.xml | 2 +- .../sample-service-rest-redis-book/pom.xml | 2 +- 8 files changed, 31 insertions(+), 11 deletions(-) diff --git a/data/data-samples/pom.xml b/data/data-samples/pom.xml index a2642e504..23b40157e 100644 --- a/data/data-samples/pom.xml +++ b/data/data-samples/pom.xml @@ -35,6 +35,7 @@ sample-data-mysql-book sample-data-mysql-reactive-book sample-data-redis-book + sample-data-redis-reactive-book diff --git a/data/data-samples/sample-data-redis-reactive-book/pom.xml b/data/data-samples/sample-data-redis-reactive-book/pom.xml index 7496ee0df..22be9b785 100644 --- a/data/data-samples/sample-data-redis-reactive-book/pom.xml +++ b/data/data-samples/sample-data-redis-reactive-book/pom.xml @@ -35,4 +35,4 @@ - \ No newline at end of file + diff --git a/data/pom.xml b/data/pom.xml index b3627ef38..0c7958ef6 100644 --- a/data/pom.xml +++ b/data/pom.xml @@ -35,7 +35,6 @@ synapse-data-oracle synapse-data-postgres synapse-data-redis - data-samples/sample-data-redis-reactive-book diff --git a/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseReactiveRedisDataConfig.java b/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseReactiveRedisDataConfig.java index 6ccabf651..0f9a837fc 100644 --- a/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseReactiveRedisDataConfig.java +++ b/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseReactiveRedisDataConfig.java @@ -20,12 +20,14 @@ import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.ReactiveRedisTemplate; +import org.springframework.data.redis.repository.configuration.EnableRedisRepositories; import org.springframework.data.redis.serializer.RedisSerializationContext; /** - * {@code BaseReactiveRedisDataConfig} + * {@code BaseReactiveRedisDataConfig} class is used to hold the common configuration for all reactive data-redis modules. */ @Configuration +@EnableRedisRepositories public class BaseReactiveRedisDataConfig extends BaseRedisConfig { /** @@ -38,9 +40,9 @@ public BaseReactiveRedisDataConfig(Environment environment) { } /** - * Lettuce connection factory reactive redis connection factory. + * Creates a LettuceConnectionFactory bean for reactive redis connection. * - * @return the reactive redis connection factory + * @return the lettuce connection factory. */ @Bean public LettuceConnectionFactory lettuceConnectionFactory() { @@ -48,7 +50,8 @@ public LettuceConnectionFactory lettuceConnectionFactory() { } /** - * Lettuce client configuration lettuce client configuration. + * Creates a LettuceClientConfiguration for configuring LettuceConnection. + * This method can be overridden by subclasses to fit redis connection configuration needs. * * @return the lettuce client configuration */ @@ -58,11 +61,24 @@ public LettuceClientConfiguration lettuceClientConfiguration() { .build(); } + /** + * Performs automatic serialization/deserialization between the given objects and the underlying binary data in the Redis store + * This method can be overridden by subclasses to fit redis access needs. + * + * @return the ReactiveRedisTemplate + */ @Bean public ReactiveRedisTemplate reactiveRedisTemplate(ReactiveRedisConnectionFactory factory) { return new ReactiveRedisTemplate<>(factory, getSerializationContext()); } + /** + * This method returns the RedisSerializationContext which is used to tell ReactiveRedisTemplate + * what the retrieved object from Redis should be deserialized/serialized to. + * This method can be overridden by subclasses to fit their redis serialization needs. + * + * @return RedisSerializationContext + */ protected RedisSerializationContext getSerializationContext() { return RedisSerializationContext.string(); } diff --git a/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseRedisConfig.java b/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseRedisConfig.java index cc3e85c74..97c589753 100644 --- a/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseRedisConfig.java +++ b/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseRedisConfig.java @@ -18,7 +18,7 @@ import org.springframework.data.redis.connection.RedisStandaloneConfiguration; /** - * The type Base redis config. + * {@code BaseRedisConfig} contains common configuration for redis configuration such as methods to retrieve port, host, etc. */ public abstract class BaseRedisConfig { @@ -34,7 +34,7 @@ protected BaseRedisConfig(Environment environment) { } /** - * Redis standalone configuration redis standalone configuration. + * Creates a RedisStandaloneConfiguration, this method can be overridden for further redis configurations. * * @return the redis standalone configuration */ diff --git a/pom.xml b/pom.xml index 1cbe59896..13133f431 100644 --- a/pom.xml +++ b/pom.xml @@ -424,7 +424,11 @@ sample-data-redis-book ${project.version} - + + io.americanexpress.synapse + sample-data-redis-reactive-book + ${project.version} + diff --git a/service/service-samples/sample-service-reactive-redis-book/pom.xml b/service/service-samples/sample-service-reactive-redis-book/pom.xml index 5e96823f3..d820cc772 100644 --- a/service/service-samples/sample-service-reactive-redis-book/pom.xml +++ b/service/service-samples/sample-service-reactive-redis-book/pom.xml @@ -25,4 +25,4 @@ - \ No newline at end of file + diff --git a/service/service-samples/sample-service-rest-redis-book/pom.xml b/service/service-samples/sample-service-rest-redis-book/pom.xml index 3bd240d98..623e6fdf7 100644 --- a/service/service-samples/sample-service-rest-redis-book/pom.xml +++ b/service/service-samples/sample-service-rest-redis-book/pom.xml @@ -23,4 +23,4 @@ - \ No newline at end of file + From 7ef99a483ab0b3d73f0086e2f6764d275afe24f9 Mon Sep 17 00:00:00 2001 From: Wendy Hu Date: Tue, 2 May 2023 15:37:36 -0400 Subject: [PATCH 05/11] fix(): adding copyright --- .../sample-service-rest-redis-book/pom.xml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/service/service-samples/sample-service-rest-redis-book/pom.xml b/service/service-samples/sample-service-rest-redis-book/pom.xml index 623e6fdf7..1a5ba3552 100644 --- a/service/service-samples/sample-service-rest-redis-book/pom.xml +++ b/service/service-samples/sample-service-rest-redis-book/pom.xml @@ -1,4 +1,17 @@ + From 0771cf42b395341e9bbd980bdfa25b3bd880169b Mon Sep 17 00:00:00 2001 From: Wendy Hu Date: Wed, 3 May 2023 10:21:04 -0400 Subject: [PATCH 06/11] fix(): adding javadocs --- .../data/book/config/BookDataConfig.java | 1 - .../data/book/config/BookDataConfig.java | 11 +++++ .../data/book/entity/BookEntity.java | 37 ++++++++++++----- .../data/book/repository/BookRepository.java | 41 ++++++++++++++++++- .../data/redis/config/BaseRedisConfig.java | 3 ++ service/pom.xml | 1 - .../service/book/rest/BookApplication.java | 3 ++ .../CreateBookReactiveController.java | 2 +- .../book/rest/model/CreateBookResponse.java | 2 +- .../service/CreateBookReactiveService.java | 23 +++++++++-- .../rest/service/ReadBookReactiveService.java | 16 ++++++-- .../book/rest/service/CreateBookService.java | 10 +++++ 12 files changed, 128 insertions(+), 22 deletions(-) diff --git a/data/data-samples/sample-data-redis-book/src/main/java/io/americanexpress/data/book/config/BookDataConfig.java b/data/data-samples/sample-data-redis-book/src/main/java/io/americanexpress/data/book/config/BookDataConfig.java index f708ce15e..5a215b7d6 100644 --- a/data/data-samples/sample-data-redis-book/src/main/java/io/americanexpress/data/book/config/BookDataConfig.java +++ b/data/data-samples/sample-data-redis-book/src/main/java/io/americanexpress/data/book/config/BookDataConfig.java @@ -14,7 +14,6 @@ package io.americanexpress.data.book.config; import io.americanexpress.synapse.data.redis.config.BaseRedisDataConfig; -import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.core.env.Environment; diff --git a/data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/config/BookDataConfig.java b/data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/config/BookDataConfig.java index 2aeafff6e..a2aac4825 100644 --- a/data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/config/BookDataConfig.java +++ b/data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/config/BookDataConfig.java @@ -49,11 +49,22 @@ public BookDataConfig(Environment environment) { super(environment); } + /** + * Overriding method to configure redis template for serialization/deserialization for key, value mapping of String and {@link BookEntity} type. + * + * @param factory the redis connection factory + * @return the reactive redis template specifically for + */ @Override public ReactiveRedisTemplate reactiveRedisTemplate(ReactiveRedisConnectionFactory factory) { return new ReactiveRedisTemplate<>(factory, getSerializationContext()); } + /** + * Overriding method to provide configuration for serialization/deserialization of {@link BookEntity}. + * + * @return the redis serialization context + */ @Override protected RedisSerializationContext getSerializationContext() { return RedisSerializationContext diff --git a/data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/entity/BookEntity.java b/data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/entity/BookEntity.java index 3ec286d7f..adc3f5fba 100644 --- a/data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/entity/BookEntity.java +++ b/data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/entity/BookEntity.java @@ -33,39 +33,56 @@ public class BookEntity extends BaseEntity { private String author; /** - * The number of copies. + * Instantiates a new Book entity. + * + * @param title the title + * @param author the author */ - private int numberOfCopies; - public BookEntity(String title, String author) { this.title = title; this.author = author; } + /** + * Instantiates a new Book entity. + */ public BookEntity() {} + /** + * Gets title. + * + * @return the title + */ public String getTitle() { return title; } + /** + * Sets title. + * + * @param title the title + */ public void setTitle(String title) { this.title = title; } + /** + * Gets author. + * + * @return the author + */ public String getAuthor() { return author; } + /** + * Sets author. + * + * @param author the author + */ public void setAuthor(String author) { this.author = author; } - public int getNumberOfCopies() { - return numberOfCopies; - } - - public void setNumberOfCopies(int numberOfCopies) { - this.numberOfCopies = numberOfCopies; - } } diff --git a/data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java b/data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java index 2578a5f77..13f957869 100644 --- a/data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java +++ b/data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java @@ -21,23 +21,51 @@ import java.util.UUID; +/** + * {@code BookRepository} is used to access redis store by using {@link ReactiveRedisOperations}. + * Spring-data-redis does not provide built in reactive repository so need to create access methods using ReactiveRedisOperations. + */ @Repository public class BookRepository { + /** + * Used to access data in redis store. + */ private final ReactiveRedisOperations reactiveRedisOperations; - private final String BOOKS = "books"; + /** + * The constant BOOKS. + */ + private static final String BOOKS = "books"; + /** + * Instantiates a new Book repository. + * + * @param reactiveRedisOperations the reactive redis operations + */ public BookRepository(ReactiveRedisOperations reactiveRedisOperations) { this.reactiveRedisOperations = reactiveRedisOperations; } + /** + * Find by title. + * + * @param title the title + * @return the mono BookEntity if book exists with title + */ public Mono findByTitle(String title) { return reactiveRedisOperations.opsForHash() .values(BOOKS) .filter(book -> book.getTitle().equals(title)) .singleOrEmpty(); } + + /** + * Save bookEntity to redis store. + * + * @param book the bookEntity + * @return the mono BookEntity if saved successfully + */ public Mono save(BookEntity book) { if (book.getIdentifier() == null) { String id = UUID.randomUUID().toString(); @@ -49,10 +77,21 @@ public Mono save(BookEntity book) { } + /** + * Find by id. + * + * @param id the id of the bookEntity in redis store. + * @return the mono BookEntity if id found + */ public Mono findById(String id) { return reactiveRedisOperations.opsForHash().get(BOOKS, id); } + /** + * Find all + * + * @return the flux of BookEntity + */ public Flux findAll() { return this.reactiveRedisOperations.opsForHash().values(BOOKS); } diff --git a/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseRedisConfig.java b/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseRedisConfig.java index 97c589753..b97926d80 100644 --- a/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseRedisConfig.java +++ b/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseRedisConfig.java @@ -22,6 +22,9 @@ */ public abstract class BaseRedisConfig { + /** + * Used to retrieve properties from property files. + */ private final Environment environment; /** diff --git a/service/pom.xml b/service/pom.xml index a67a09c37..911bf5fc8 100644 --- a/service/pom.xml +++ b/service/pom.xml @@ -30,7 +30,6 @@ synapse-service-reactive-rest synapse-service-rest synapse-service-test - service-samples/sample-service-rest-redis-book diff --git a/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/BookApplication.java b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/BookApplication.java index 91c6630b5..dc52e0636 100644 --- a/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/BookApplication.java +++ b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/BookApplication.java @@ -30,6 +30,9 @@ @SpringBootApplication public class BookApplication { + /** + * Used for logging. + */ private static final XLogger LOGGER = XLoggerFactory.getXLogger(BookApplication.class); /** diff --git a/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/controller/CreateBookReactiveController.java b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/controller/CreateBookReactiveController.java index 9a4b3e0d5..5168591c1 100644 --- a/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/controller/CreateBookReactiveController.java +++ b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/controller/CreateBookReactiveController.java @@ -9,7 +9,7 @@ import org.springframework.web.bind.annotation.RestController; /** - * {@code CreateBookReactiveController} + * {@code CreateBookReactiveController} is the controller for creating book in redis store. */ @RestController @RequestMapping(BookEndpoints.BOOK_ENDPOINT) diff --git a/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/model/CreateBookResponse.java b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/model/CreateBookResponse.java index 317efa21d..073b659f5 100644 --- a/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/model/CreateBookResponse.java +++ b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/model/CreateBookResponse.java @@ -17,7 +17,7 @@ /** - * {@code CreateBookResponse} + * {@code CreateBookResponse} is the response for create book. */ public class CreateBookResponse extends BaseServiceResponse { diff --git a/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookReactiveService.java b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookReactiveService.java index e8fe95c47..125950d65 100644 --- a/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookReactiveService.java +++ b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookReactiveService.java @@ -9,21 +9,36 @@ import org.springframework.stereotype.Service; import reactor.core.publisher.Mono; +/** + * {@code CreateBookReactiveService} service layer to create and save book to redis store. + */ @Service public class CreateBookReactiveService extends BaseCreateReactiveService { + /** + * Used to save book to redis store. + */ private final BookRepository bookRepository; + /** + * Instantiates a new Create book reactive service. + * + * @param bookRepository the book repository + */ public CreateBookReactiveService(BookRepository bookRepository) { this.bookRepository = bookRepository; } + /** + * Creates book from request and saves to redis store. + * + * @param headers the httpHeaders + * @param request the request + * @return mono CreateBookResponse if save is successful + */ @Override protected Mono executeCreate(HttpHeaders headers, CreateBookRequest request) { BookEntity book = new BookEntity(request.getTitle(), request.getAuthor()); - return bookRepository.save(book) - .map(bookEntity -> { - return new CreateBookResponse(); - }); + return bookRepository.save(book).map(bookEntity -> new CreateBookResponse()); } } diff --git a/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/service/ReadBookReactiveService.java b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/service/ReadBookReactiveService.java index 57acc3937..1e80d56ae 100644 --- a/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/service/ReadBookReactiveService.java +++ b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/service/ReadBookReactiveService.java @@ -27,16 +27,26 @@ @Service public class ReadBookReactiveService extends BaseReadMonoReactiveService { + /** + * Used to retrieve book from redis store. + */ private final BookRepository bookRepository; + /** + * Instantiates a new Read book reactive service. + * + * @param bookRepository the book repository + */ public ReadBookReactiveService(BookRepository bookRepository) { this.bookRepository = bookRepository; } /** - * Overriding executeRead. - * @param request a read mono book service request - * @return a mono read book response + * Retrieves book from redis store using book title provided in request. + * + * @param headers the httpHeaders + * @param request the request + * @return a mono read book response if book is found in redis store */ @Override protected Mono executeRead(HttpHeaders headers, ReadBookRequest request) { diff --git a/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookService.java b/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookService.java index 482a3f57b..4f954e3f5 100644 --- a/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookService.java +++ b/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookService.java @@ -27,6 +27,9 @@ @Service public class CreateBookService extends BaseCreateService { + /** + * Used to save book to redis store. + */ private final BookRepository bookRepository; /** @@ -38,6 +41,13 @@ public CreateBookService(BookRepository bookRepository) { this.bookRepository = bookRepository; } + /** + * Creates book entity and saves to redis store. + * + * @param headers the httpHeaders + * @param request the request + * @return createBookResponse + */ @Override protected CreateBookResponse executeCreate(HttpHeaders headers, CreateBookRequest request) { BookEntity book = new BookEntity(request.getTitle(), request.getAuthor()); From 873be4f9c1f81d004ebd8005712b644c4eaf4414 Mon Sep 17 00:00:00 2001 From: Wendy Hu Date: Thu, 4 May 2023 10:52:02 -0400 Subject: [PATCH 07/11] fix(): adding javadocs --- .../service/book/rest/model/BookRequest.java | 6 +++++ .../book/rest/model/ReadBookResponse.java | 25 +++++-------------- 2 files changed, 12 insertions(+), 19 deletions(-) diff --git a/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/model/BookRequest.java b/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/model/BookRequest.java index 843c7776b..95a30f2df 100644 --- a/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/model/BookRequest.java +++ b/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/model/BookRequest.java @@ -22,9 +22,15 @@ */ public class BookRequest implements BaseServiceRequest { + /** + * The book title. + */ @NotBlank private String title; + /** + * The book author. + */ @NotBlank private String author; diff --git a/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/model/ReadBookResponse.java b/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/model/ReadBookResponse.java index 082ea258d..180569816 100644 --- a/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/model/ReadBookResponse.java +++ b/service/service-samples/sample-service-rest-redis-book/src/main/java/io/americanexpress/service/book/rest/model/ReadBookResponse.java @@ -20,12 +20,16 @@ */ public class ReadBookResponse extends BaseServiceResponse { + /** + * The book title. + */ private String title; + /** + * The book author. + */ private String author; - private int numberOfCopies; - /** * Gets title. * @@ -62,21 +66,4 @@ public void setAuthor(String author) { this.author = author; } - /** - * Gets number of copies. - * - * @return the number of copies - */ - public int getNumberOfCopies() { - return numberOfCopies; - } - - /** - * Sets number of copies. - * - * @param numberOfCopies the number of copies - */ - public void setNumberOfCopies(int numberOfCopies) { - this.numberOfCopies = numberOfCopies; - } } From e270d386b3e7b780dc577a5272ecca4cd67dadf8 Mon Sep 17 00:00:00 2001 From: Wendy Hu Date: Wed, 24 May 2023 09:48:02 -0400 Subject: [PATCH 08/11] fix(): adding copyright --- .../sample-data-redis-reactive-book/pom.xml | 13 ++++++++++ data/synapse-data-redis/pom.xml | 1 - pom.xml | 5 ++++ .../pom.xml | 13 ++++++++++ .../service/CreateBookReactiveService.java | 13 ++++++++++ .../service-book-application.properties | 25 +++++++++---------- .../book/reactive/rest/config/BookConfig.java | 2 +- .../rest/service/ReadBookReactiveService.java | 4 +-- 8 files changed, 59 insertions(+), 17 deletions(-) diff --git a/data/data-samples/sample-data-redis-reactive-book/pom.xml b/data/data-samples/sample-data-redis-reactive-book/pom.xml index 22be9b785..85f1e4110 100644 --- a/data/data-samples/sample-data-redis-reactive-book/pom.xml +++ b/data/data-samples/sample-data-redis-reactive-book/pom.xml @@ -1,4 +1,17 @@ + diff --git a/data/synapse-data-redis/pom.xml b/data/synapse-data-redis/pom.xml index abe3ff2b0..77e299b0e 100644 --- a/data/synapse-data-redis/pom.xml +++ b/data/synapse-data-redis/pom.xml @@ -31,7 +31,6 @@ org.springframework.boot spring-boot-starter-data-redis-reactive - 2.7.5 diff --git a/pom.xml b/pom.xml index 13133f431..24b575abd 100644 --- a/pom.xml +++ b/pom.xml @@ -1012,6 +1012,11 @@ + + org.springframework.boot + spring-boot-starter-data-redis-reactive + 2.7.5 + org.springframework.data spring-data-redis diff --git a/service/service-samples/sample-service-reactive-redis-book/pom.xml b/service/service-samples/sample-service-reactive-redis-book/pom.xml index d820cc772..619ac0749 100644 --- a/service/service-samples/sample-service-reactive-redis-book/pom.xml +++ b/service/service-samples/sample-service-reactive-redis-book/pom.xml @@ -1,4 +1,17 @@ + diff --git a/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookReactiveService.java b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookReactiveService.java index 125950d65..ef5e62958 100644 --- a/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookReactiveService.java +++ b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookReactiveService.java @@ -1,3 +1,16 @@ +/* + * Copyright 2020 American Express Travel Related Services Company, Inc. + * + * 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 + * + * http://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 io.americanexpress.service.book.rest.service; import io.americanexpress.data.book.entity.BookEntity; diff --git a/service/service-samples/sample-service-reactive-redis-book/src/main/resources/service-book-application.properties b/service/service-samples/sample-service-reactive-redis-book/src/main/resources/service-book-application.properties index ed8abeb0c..a7b915a46 100644 --- a/service/service-samples/sample-service-reactive-redis-book/src/main/resources/service-book-application.properties +++ b/service/service-samples/sample-service-reactive-redis-book/src/main/resources/service-book-application.properties @@ -1,16 +1,15 @@ -#/* -#* Copyright 2020 American Express Travel Related Services Company, Inc. -#* -#* 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 -#* -#* http://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. -#*/ +# +# Copyright 2020 American Express Travel Related Services Company, Inc. +# +# 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 +# +# http://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. spring.application.name=Book Rest Service spring.main.allow-bean-definition-overriding=true server.port=8080 diff --git a/service/service-samples/sample-service-reactive-rest-book/src/main/java/io/americanexpress/service/book/reactive/rest/config/BookConfig.java b/service/service-samples/sample-service-reactive-rest-book/src/main/java/io/americanexpress/service/book/reactive/rest/config/BookConfig.java index cfa0cc6a4..46a7aa707 100644 --- a/service/service-samples/sample-service-reactive-rest-book/src/main/java/io/americanexpress/service/book/reactive/rest/config/BookConfig.java +++ b/service/service-samples/sample-service-reactive-rest-book/src/main/java/io/americanexpress/service/book/reactive/rest/config/BookConfig.java @@ -30,7 +30,7 @@ public class BookConfig extends BaseServiceReactiveRestConfig { /** * The constant BOOK_ENDPOINT. */ - public static final String BOOK_ENDPOINT = "/v1/books"; + public static final String BOOK_ENDPOINT = ""; /** * Constructor taking in objectMapper & metricInterceptor diff --git a/service/service-samples/sample-service-reactive-rest-book/src/main/java/io/americanexpress/service/book/reactive/rest/service/ReadBookReactiveService.java b/service/service-samples/sample-service-reactive-rest-book/src/main/java/io/americanexpress/service/book/reactive/rest/service/ReadBookReactiveService.java index 3ee9b2cf6..2b9d09d6d 100644 --- a/service/service-samples/sample-service-reactive-rest-book/src/main/java/io/americanexpress/service/book/reactive/rest/service/ReadBookReactiveService.java +++ b/service/service-samples/sample-service-reactive-rest-book/src/main/java/io/americanexpress/service/book/reactive/rest/service/ReadBookReactiveService.java @@ -33,8 +33,8 @@ public class ReadBookReactiveService extends BaseReadMonoReactiveService executeRead(HttpHeaders headers, ReadBookRequest request) { - logger.entry(request); - logger.debug("emulating post read..."); +// logger.entry(request); +// logger.debug("emulating post read..."); return Mono.just(new ReadBookResponse("1", "title", "title")); } } From 9ab8de631c1bcde6bdd10aed4ca2cb3b54524722 Mon Sep 17 00:00:00 2001 From: Wendy Hu Date: Wed, 24 May 2023 09:51:40 -0400 Subject: [PATCH 09/11] fix(): alphabetizing deps and adding copyright --- data/synapse-data-redis/pom.xml | 8 ++++---- service/service-samples/pom.xml | 13 +++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/data/synapse-data-redis/pom.xml b/data/synapse-data-redis/pom.xml index 77e299b0e..2b654583d 100644 --- a/data/synapse-data-redis/pom.xml +++ b/data/synapse-data-redis/pom.xml @@ -24,14 +24,14 @@ - - org.springframework.data - spring-data-redis - org.springframework.boot spring-boot-starter-data-redis-reactive + + org.springframework.data + spring-data-redis + redis.clients diff --git a/service/service-samples/pom.xml b/service/service-samples/pom.xml index 57ee3f832..9cbb7bf20 100644 --- a/service/service-samples/pom.xml +++ b/service/service-samples/pom.xml @@ -1,4 +1,17 @@ + From 197b2bb78e95eec0ebc2de346adc4a944841ef75 Mon Sep 17 00:00:00 2001 From: Wendy Hu Date: Wed, 24 May 2023 09:54:59 -0400 Subject: [PATCH 10/11] fix(): undoing unnecessary changes --- .../controller/CreateBookReactiveController.java | 13 +++++++++++++ .../book/reactive/rest/config/BookConfig.java | 2 +- .../rest/service/ReadBookReactiveService.java | 4 ++-- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/controller/CreateBookReactiveController.java b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/controller/CreateBookReactiveController.java index 5168591c1..9170572f1 100644 --- a/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/controller/CreateBookReactiveController.java +++ b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/controller/CreateBookReactiveController.java @@ -1,3 +1,16 @@ +/* + * Copyright 2020 American Express Travel Related Services Company, Inc. + * + * 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 + * + * http://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 io.americanexpress.service.book.rest.controller; import io.americanexpress.service.book.rest.config.BookEndpoints; diff --git a/service/service-samples/sample-service-reactive-rest-book/src/main/java/io/americanexpress/service/book/reactive/rest/config/BookConfig.java b/service/service-samples/sample-service-reactive-rest-book/src/main/java/io/americanexpress/service/book/reactive/rest/config/BookConfig.java index 46a7aa707..cfa0cc6a4 100644 --- a/service/service-samples/sample-service-reactive-rest-book/src/main/java/io/americanexpress/service/book/reactive/rest/config/BookConfig.java +++ b/service/service-samples/sample-service-reactive-rest-book/src/main/java/io/americanexpress/service/book/reactive/rest/config/BookConfig.java @@ -30,7 +30,7 @@ public class BookConfig extends BaseServiceReactiveRestConfig { /** * The constant BOOK_ENDPOINT. */ - public static final String BOOK_ENDPOINT = ""; + public static final String BOOK_ENDPOINT = "/v1/books"; /** * Constructor taking in objectMapper & metricInterceptor diff --git a/service/service-samples/sample-service-reactive-rest-book/src/main/java/io/americanexpress/service/book/reactive/rest/service/ReadBookReactiveService.java b/service/service-samples/sample-service-reactive-rest-book/src/main/java/io/americanexpress/service/book/reactive/rest/service/ReadBookReactiveService.java index 2b9d09d6d..3ee9b2cf6 100644 --- a/service/service-samples/sample-service-reactive-rest-book/src/main/java/io/americanexpress/service/book/reactive/rest/service/ReadBookReactiveService.java +++ b/service/service-samples/sample-service-reactive-rest-book/src/main/java/io/americanexpress/service/book/reactive/rest/service/ReadBookReactiveService.java @@ -33,8 +33,8 @@ public class ReadBookReactiveService extends BaseReadMonoReactiveService executeRead(HttpHeaders headers, ReadBookRequest request) { -// logger.entry(request); -// logger.debug("emulating post read..."); + logger.entry(request); + logger.debug("emulating post read..."); return Mono.just(new ReadBookResponse("1", "title", "title")); } } From 9c488dd886e5659b51bea5c6f3a9b4ef6ab7a278 Mon Sep 17 00:00:00 2001 From: Wendy Hu Date: Wed, 31 May 2023 10:15:04 -0400 Subject: [PATCH 11/11] feat(data-redis): adding base repository impl --- .../data/book/repository/BookRepository.java | 70 ++------- .../BaseRedisHashReactiveRepository.java | 136 ++++++++++++++++++ .../service/CreateBookReactiveService.java | 3 + 3 files changed, 149 insertions(+), 60 deletions(-) create mode 100644 data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/repository/BaseRedisHashReactiveRepository.java diff --git a/data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java b/data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java index 13f957869..2cd7aad98 100644 --- a/data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java +++ b/data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java @@ -14,37 +14,23 @@ package io.americanexpress.data.book.repository; import io.americanexpress.data.book.entity.BookEntity; +import io.americanexpress.synapse.data.redis.repository.BaseRedisHashReactiveRepository; import org.springframework.data.redis.core.ReactiveRedisOperations; import org.springframework.stereotype.Repository; -import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; -import java.util.UUID; - /** - * {@code BookRepository} is used to access redis store by using {@link ReactiveRedisOperations}. - * Spring-data-redis does not provide built in reactive repository so need to create access methods using ReactiveRedisOperations. + * {@code BookRepository} is used to access redis store. */ @Repository -public class BookRepository { - - /** - * Used to access data in redis store. - */ - private final ReactiveRedisOperations reactiveRedisOperations; - - /** - * The constant BOOKS. - */ - private static final String BOOKS = "books"; +public class BookRepository extends BaseRedisHashReactiveRepository { + protected BookRepository(ReactiveRedisOperations reactiveRedisOperations) { + super(reactiveRedisOperations); + } - /** - * Instantiates a new Book repository. - * - * @param reactiveRedisOperations the reactive redis operations - */ - public BookRepository(ReactiveRedisOperations reactiveRedisOperations) { - this.reactiveRedisOperations = reactiveRedisOperations; + @Override + public String getKey() { + return "books"; } /** @@ -55,45 +41,9 @@ public BookRepository(ReactiveRedisOperations reactiveRedisO */ public Mono findByTitle(String title) { return reactiveRedisOperations.opsForHash() - .values(BOOKS) + .values(getKey()) .filter(book -> book.getTitle().equals(title)) .singleOrEmpty(); } - - /** - * Save bookEntity to redis store. - * - * @param book the bookEntity - * @return the mono BookEntity if saved successfully - */ - public Mono save(BookEntity book) { - if (book.getIdentifier() == null) { - String id = UUID.randomUUID().toString(); - book.setIdentifier(id); - } - return reactiveRedisOperations.opsForHash().put(BOOKS, book.getIdentifier(), book) - .log() - .map(p -> book); - - } - - /** - * Find by id. - * - * @param id the id of the bookEntity in redis store. - * @return the mono BookEntity if id found - */ - public Mono findById(String id) { - return reactiveRedisOperations.opsForHash().get(BOOKS, id); - } - - /** - * Find all - * - * @return the flux of BookEntity - */ - public Flux findAll() { - return this.reactiveRedisOperations.opsForHash().values(BOOKS); - } } diff --git a/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/repository/BaseRedisHashReactiveRepository.java b/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/repository/BaseRedisHashReactiveRepository.java new file mode 100644 index 000000000..92519be35 --- /dev/null +++ b/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/repository/BaseRedisHashReactiveRepository.java @@ -0,0 +1,136 @@ +/* + * Copyright 2020 American Express Travel Related Services Company, Inc. + * + * 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 + * + * http://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 io.americanexpress.synapse.data.redis.repository; + +import io.americanexpress.synapse.data.redis.entity.BaseEntity; +import org.springframework.data.redis.core.ReactiveHashOperations; +import org.springframework.data.redis.core.ReactiveRedisOperations; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +/** + * {@code BaseRedisHashReactiveRepository} contains custom base crud operations for operating on Redis hashes. + * + * @param the key type + * @param the value type + */ +public abstract class BaseRedisHashReactiveRepository { + + /** + * The Reactive redis operations. + */ + protected final ReactiveRedisOperations reactiveRedisOperations; + + /** + * The Reactive hash operations. + */ + protected final ReactiveHashOperations reactiveHashOperations; + + /** + * Instantiates a new Base redis hash reactive repository. + * + * @param reactiveRedisOperations the reactive redis operations + */ + protected BaseRedisHashReactiveRepository(ReactiveRedisOperations reactiveRedisOperations) { + this.reactiveRedisOperations = reactiveRedisOperations; + this.reactiveHashOperations = reactiveRedisOperations.opsForHash(); + } + + /** + * Abstract method for implementing repositories to override, to provide the key value for the redis hash. + * + * @return the key + */ + public abstract String getKey(); + + /** + * Exists by id mono. + * + * @param id the id + * @return the mono + */ + public Mono existsById(K id) { + return reactiveHashOperations.hasKey(getKey(), id); + } + + /** + * Find by id. + * + * @param id the id + * @return the mono + */ + public Mono findById(K id) { + return reactiveHashOperations.get(getKey(), id); + } + + /** + * Find by id. + * + * @return the flux + */ + public Flux findAll() { + return reactiveHashOperations.values(getKey()); + } + + + /** + * Save mono. + * + * @param value the value + * @return the mono + */ + public Mono save(V value) { + return reactiveHashOperations.put(getKey(), (K) value.getIdentifier(), value) + .log() + .map(p -> value); + } + + /** + * Delete all mono. + * + * @return the mono + */ + public Mono deleteAll() { + return reactiveHashOperations.delete(getKey()).then(); + } + + /** + * Delete mono. + * + * @param value the value + * @return the mono + */ + public Mono delete(V value) { + return reactiveHashOperations.remove(getKey(), value.getIdentifier()).then(); + } + + /** + * Delete by id mono. + * + * @param id the id + * @return the mono + */ + public Mono deleteById(String id) { + return reactiveHashOperations.remove(getKey(), id).then(); + } + + /** + * Count mono. + * + * @return the mono + */ + public Mono count() { + return reactiveHashOperations.values(getKey()).count(); + } +} diff --git a/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookReactiveService.java b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookReactiveService.java index ef5e62958..f2f3a1831 100644 --- a/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookReactiveService.java +++ b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookReactiveService.java @@ -22,6 +22,8 @@ import org.springframework.stereotype.Service; import reactor.core.publisher.Mono; +import java.util.UUID; + /** * {@code CreateBookReactiveService} service layer to create and save book to redis store. */ @@ -52,6 +54,7 @@ public CreateBookReactiveService(BookRepository bookRepository) { @Override protected Mono executeCreate(HttpHeaders headers, CreateBookRequest request) { BookEntity book = new BookEntity(request.getTitle(), request.getAuthor()); + book.setIdentifier(UUID.randomUUID().toString()); return bookRepository.save(book).map(bookEntity -> new CreateBookResponse()); } }