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-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..85f1e4110 --- /dev/null +++ b/data/data-samples/sample-data-redis-reactive-book/pom.xml @@ -0,0 +1,51 @@ + + + + 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 + + + + 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..a2aac4825 --- /dev/null +++ b/data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/config/BookDataConfig.java @@ -0,0 +1,78 @@ +/* + * 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); + } + + /** + * 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 + .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..adc3f5fba --- /dev/null +++ b/data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/entity/BookEntity.java @@ -0,0 +1,88 @@ +/* + * 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; + + /** + * Instantiates a new Book entity. + * + * @param title the title + * @param author the author + */ + 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; + } + +} + 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..2cd7aad98 --- /dev/null +++ b/data/data-samples/sample-data-redis-reactive-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java @@ -0,0 +1,49 @@ +/* + * 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 io.americanexpress.synapse.data.redis.repository.BaseRedisHashReactiveRepository; +import org.springframework.data.redis.core.ReactiveRedisOperations; +import org.springframework.stereotype.Repository; +import reactor.core.publisher.Mono; + +/** + * {@code BookRepository} is used to access redis store. + */ +@Repository +public class BookRepository extends BaseRedisHashReactiveRepository { + protected BookRepository(ReactiveRedisOperations reactiveRedisOperations) { + super(reactiveRedisOperations); + } + + @Override + public String getKey() { + return "books"; + } + + /** + * 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(getKey()) + .filter(book -> book.getTitle().equals(title)) + .singleOrEmpty(); + } +} + 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/synapse-data-redis/pom.xml b/data/synapse-data-redis/pom.xml index fb4f92c12..2b654583d 100644 --- a/data/synapse-data-redis/pom.xml +++ b/data/synapse-data-redis/pom.xml @@ -26,7 +26,7 @@ org.springframework.boot - spring-boot-starter-data-redis + spring-boot-starter-data-redis-reactive org.springframework.data @@ -36,8 +36,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..0f9a837fc --- /dev/null +++ b/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseReactiveRedisDataConfig.java @@ -0,0 +1,86 @@ +/* + * 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; +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; +import org.springframework.data.redis.core.ReactiveRedisTemplate; +import org.springframework.data.redis.repository.configuration.EnableRedisRepositories; +import org.springframework.data.redis.serializer.RedisSerializationContext; + +/** + * {@code BaseReactiveRedisDataConfig} class is used to hold the common configuration for all reactive data-redis modules. + */ +@Configuration +@EnableRedisRepositories +public class BaseReactiveRedisDataConfig extends BaseRedisConfig { + + /** + * Instantiates a new Base reactive redis data config. + * + * @param environment the environment + */ + public BaseReactiveRedisDataConfig(Environment environment) { + super(environment); + } + + /** + * Creates a LettuceConnectionFactory bean for reactive redis connection. + * + * @return the lettuce connection factory. + */ + @Bean + public LettuceConnectionFactory lettuceConnectionFactory() { + return new LettuceConnectionFactory(redisStandaloneConfiguration(), lettuceClientConfiguration()); + } + + /** + * Creates a LettuceClientConfiguration for configuring LettuceConnection. + * This method can be overridden by subclasses to fit redis connection configuration needs. + * + * @return the lettuce client configuration + */ + @Bean + public LettuceClientConfiguration lettuceClientConfiguration() { + return LettuceClientConfiguration.builder() + .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 new file mode 100644 index 000000000..b97926d80 --- /dev/null +++ b/data/synapse-data-redis/src/main/java/io/americanexpress/synapse/data/redis/config/BaseRedisConfig.java @@ -0,0 +1,98 @@ +/* + * 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; +import org.springframework.data.redis.connection.RedisPassword; +import org.springframework.data.redis.connection.RedisStandaloneConfiguration; + +/** + * {@code BaseRedisConfig} contains common configuration for redis configuration such as methods to retrieve port, host, etc. + */ +public abstract class BaseRedisConfig { + + /** + * Used to retrieve properties from property files. + */ + private final Environment environment; + + /** + * Instantiates a new Base redis config. + * + * @param environment the environment + */ + protected BaseRedisConfig(Environment environment) { + this.environment = environment; + } + + /** + * Creates a RedisStandaloneConfiguration, this method can be overridden for further redis configurations. + * + * @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..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 @@ -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,16 @@ 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)); + + JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisStandaloneConfiguration(), + jedisClientConfiguration.build()); + jedisConnectionFactory.afterPropertiesSet(); + return jedisConnectionFactory; } + /** * 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 @@ -83,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/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/pom.xml b/pom.xml index 76ddfde93..24b575abd 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} + @@ -1010,14 +1014,19 @@ org.springframework.boot - spring-boot-starter-data-redis - 2.7.3 + spring-boot-starter-data-redis-reactive + 2.7.5 org.springframework.data spring-data-redis 2.7.3 + + redis.clients + jedis + 3.8.0 + diff --git a/service/service-samples/pom.xml b/service/service-samples/pom.xml index d41c7dca5..9cbb7bf20 100644 --- a/service/service-samples/pom.xml +++ b/service/service-samples/pom.xml @@ -1,4 +1,17 @@ + @@ -26,6 +39,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..619ac0749 --- /dev/null +++ b/service/service-samples/sample-service-reactive-redis-book/pom.xml @@ -0,0 +1,41 @@ + + + + 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 + + + + 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..dc52e0636 --- /dev/null +++ b/service/service-samples/sample-service-reactive-redis-book/src/main/java/io/americanexpress/service/book/rest/BookApplication.java @@ -0,0 +1,48 @@ +/* + * 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 { + + /** + * Used for logging. + */ + 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/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..9170572f1 --- /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,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.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} is the controller for creating book in redis store. + */ +@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/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/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..073b659f5 --- /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} 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/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/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..f2f3a1831 --- /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,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.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; + +import java.util.UUID; + +/** + * {@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()); + book.setIdentifier(UUID.randomUUID().toString()); + 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 new file mode 100644 index 000000000..1e80d56ae --- /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,62 @@ +/* + * 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 { + + /** + * 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; + } + + /** + * 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) { + 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..a7b915a46 --- /dev/null +++ b/service/service-samples/sample-service-reactive-redis-book/src/main/resources/service-book-application.properties @@ -0,0 +1,17 @@ +# +# 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..1a5ba3552 --- /dev/null +++ b/service/service-samples/sample-service-rest-redis-book/pom.xml @@ -0,0 +1,39 @@ + + + + 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 + + + + 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..95a30f2df --- /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,72 @@ +/* + * 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 { + + /** + * The book title. + */ + @NotBlank + private String title; + + /** + * The book author. + */ + @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..180569816 --- /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,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.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 { + + /** + * The book title. + */ + private String title; + + /** + * The book author. + */ + 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/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..4f954e3f5 --- /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,57 @@ +/* + * 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 { + + /** + * Used to save book to redis store. + */ + private final BookRepository bookRepository; + + /** + * Instantiates a new CreateBookService. + * + * @param bookRepository the book repository + */ + 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()); + 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