Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package io.americanexpress.data.book.config;

import io.americanexpress.synapse.data.cassandra.config.BaseCassandraDataConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
Expand All @@ -22,17 +24,45 @@
/**
* {@code BookDataConfig} is the configuration class to load all the properties for the book data module.
*/
@ComponentScan(BookDataConfig.PACKAGE_NAME)
@Configuration
@EnableCassandraRepositories(BookDataConfig.PACKAGE_NAME)
@PropertySource("classpath:/data-book-application.properties")
@EnableCassandraRepositories("io.americanexpress.data.book")
public class BookDataConfig extends BaseCassandraDataConfig {

public static final String PACKAGE_NAME = "io.americanexpress.data.book";
/**
* The environment.
*/
private final Environment environment;

/**
* Constructor that initializes the base Cassandra data configuration with the environment and keyspace.
*
* @param environment the Spring environment containing properties.
*/
public BookDataConfig(Environment environment) {
super(environment, "book");
this.environment = environment;
}

/**
* Returns the base packages for entity scanning.
*
* @return an array of base package names.
*/
@Override
public String[] getEntityBasePackages() {
return new String[]{"io.americanexpress.data.book.entity"};
}

/**
* The time to live for the book.
*
* @return the time to live in seconds.
*/
@Bean("bookTimeToLive")
public int bookTimeToLive() {
return environment.getProperty("book.time-to-live", Integer.class, 600);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.cassandra.repository.CustomCassandraRepository;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.cassandra.core.CassandraTemplate;
import org.springframework.stereotype.Repository;

/**
* {@code CustomBookRepository} is the custom repository to handle the queries for the books table.
*
* @author brenoreis
*/
@Repository
public class CustomBookRepository extends CustomCassandraRepository<BookEntity> {

/**
* Creates a new instance of {@code CustomCassandraRepository} given a Cassandra template and time to live.
*
* @param cassandraTemplate the Cassandra template.
* @param timeToLive the time to live.
*/
protected CustomBookRepository(CassandraTemplate cassandraTemplate, @Qualifier("bookTimeToLive") int timeToLive) {
super(cassandraTemplate, timeToLive);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ spring.data.cassandra.book.contact-points=127.0.0.1
spring.data.cassandra.book.schema-action=CREATE_IF_NOT_EXISTS
#spring.data.cassandra.book.username = cassandra
#spring.data.cassandra.book.password = cassandra
book.time-to-live=3
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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.config.BookDataTestConfig;
import io.americanexpress.data.book.entity.BookEntity;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.boot.test.autoconfigure.data.cassandra.DataCassandraTest;
import org.springframework.test.context.ContextConfiguration;
import java.util.Optional;

/**
* {@code CustomBookRepositoryIT} class runs integration test on local Cassandra instance test database.
*/
@DataCassandraTest
@ContextConfiguration(classes = BookDataTestConfig.class)
@AutoConfigurationPackage
class CustomBookRepositoryIT {

private final CustomBookRepository customBookRepository;

private final BookRepository bookRepository;

@Autowired
public CustomBookRepositoryIT(CustomBookRepository customBookRepository, BookRepository bookRepository) {
this.customBookRepository = customBookRepository;
this.bookRepository = bookRepository;
}

@BeforeEach
void clean() {
bookRepository.deleteAll();
}

@Test
void findAll_givenEmptyBookCollection_expectedNoBooksFound() {
Assertions.assertEquals(0, bookRepository.findAll().size());
}

@Test
void findByTitleAndAuthor_givenBook_expectedBookFound() throws InterruptedException {
BookEntity bookEntity = new BookEntity("Alice In Wonderland", "Lewis Carroll");
customBookRepository.save(bookEntity);

Optional<BookEntity> book = bookRepository.findByTitleAndAuthor("Alice In Wonderland", "Lewis Carroll");
Assertions.assertEquals(bookEntity.getTitle(), book.get().getTitle());

Thread.sleep(5000);

book = bookRepository.findByTitleAndAuthor("Alice In Wonderland", "Lewis Carroll");
Assertions.assertFalse(book.isPresent());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.americanexpress.synapse.data.cassandra.repository;

import org.springframework.data.cassandra.core.CassandraTemplate;
import org.springframework.data.cassandra.core.InsertOptions;
import java.time.Duration;

/**
* {@code CustomCassandraRepository} is the custom repository for Cassandra.
* This Repository is used to add ttl to your entities records.
* The ttl is taken from the bean created in the config which get the ttl value from the property file.
*
* @author breisalm
*/
public abstract class CustomCassandraRepository<T> {

/**
* The Cassandra template.
*/
private final CassandraTemplate cassandraTemplate;

/**
* The time to live.
*/
private final int timeToLive;

/**
* Creates a new instance of {@code CustomCassandraRepository} given a Cassandra template and time to live.
*
* @param cassandraTemplate the Cassandra template.
* @param timeToLive the time to live.
*/
protected CustomCassandraRepository(CassandraTemplate cassandraTemplate,
int timeToLive) {
this.cassandraTemplate = cassandraTemplate;
this.timeToLive = timeToLive;
}

/**
* Saves the entity.
*
* @param entity the entity.
* @return the saved entity.
*/
public T save(T entity) {
return cassandraTemplate.insert(entity,
InsertOptions.builder()
.ttl(Duration.ofSeconds(timeToLive))
.build())
.getEntity();
}
}