Add smoke tests to test Spring Data's multi-store support using SD JPA and an HSQLDB database as the application System of Record (SOR) and Apache Geode as the caching provider in Spring's Cache Abstraction.
Resolves gh-51.
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright 2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
||||
* or implied. See the License for the specific language governing
|
||||
* permissions and limitations under the License.
|
||||
*/
|
||||
package example.app;
|
||||
|
||||
import org.apache.geode.cache.client.ClientRegionShortcut;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.gemfire.config.annotation.EnableCachingDefinedRegions;
|
||||
|
||||
import example.app.model.Contact;
|
||||
|
||||
/**
|
||||
* The {@link DatabaseWithLookAsideCachingApplication} class is a Spring Boot application testing Spring Data's
|
||||
* multi-store support along with using Apache Geode as a caching provider in Spring's Cache Abstraction.
|
||||
*
|
||||
* @author John Blum
|
||||
* @see org.springframework.boot.SpringApplication
|
||||
* @see org.springframework.boot.autoconfigure.SpringBootApplication
|
||||
* @see org.springframework.boot.autoconfigure.domain.EntityScan
|
||||
* @see org.springframework.context.annotation.Configuration
|
||||
* @see org.springframework.data.gemfire.config.annotation.EnableEntityDefinedRegions
|
||||
* @see example.app.model.Contact
|
||||
* @since 1.2.0
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@SuppressWarnings("unused")
|
||||
public class DatabaseWithLookAsideCachingApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(DatabaseWithLookAsideCachingApplication.class, args);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EntityScan(basePackageClasses = Contact.class)
|
||||
@EnableCachingDefinedRegions(clientRegionShortcut = ClientRegionShortcut.LOCAL)
|
||||
static class ApplicationConfiguration { }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright 2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
||||
* or implied. See the License for the specific language governing
|
||||
* permissions and limitations under the License.
|
||||
*/
|
||||
package example.app.model;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.lang.NonNull;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
/**
|
||||
* Abstract Data Type (ADT) modeling contact information for a person.
|
||||
*
|
||||
* @author John Blum
|
||||
* @see javax.persistence.Entity
|
||||
* @see org.springframework.data.annotation.Id
|
||||
* @since 1.2.0
|
||||
*/
|
||||
@Data
|
||||
@Entity
|
||||
@Table(name = "Contacts")
|
||||
@ToString(of = "name")
|
||||
@EqualsAndHashCode(of = "name")
|
||||
@RequiredArgsConstructor(staticName = "newContact")
|
||||
@SuppressWarnings("unused")
|
||||
public class Contact {
|
||||
|
||||
@javax.persistence.Id @Id @NonNull
|
||||
private String name;
|
||||
|
||||
@Column(name = "email_address")
|
||||
private String emailAddress;
|
||||
|
||||
@Column(name = "phone_number")
|
||||
private String phoneNumber;
|
||||
|
||||
protected Contact() { }
|
||||
|
||||
public Contact withEmailAddress(String emailAddress) {
|
||||
setEmailAddress(emailAddress);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Contact withPhoneNumber(String phoneNumber) {
|
||||
setPhoneNumber(phoneNumber);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright 2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
||||
* or implied. See the License for the specific language governing
|
||||
* permissions and limitations under the License.
|
||||
*/
|
||||
package example.app.repo;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import example.app.model.Contact;
|
||||
|
||||
/**
|
||||
* Spring Data {@link CrudRepository} and Data Access Object (DAO) for performing basic CRUD and simple Query
|
||||
* data access operations on a {@link Contact}.
|
||||
*
|
||||
* @author John Blum
|
||||
* @see org.springframework.data.repository.CrudRepository
|
||||
* @see example.app.model.Contact
|
||||
* @since 1.2.0
|
||||
*/
|
||||
public interface ContactRepository extends CrudRepository<Contact, String> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright 2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
||||
* or implied. See the License for the specific language governing
|
||||
* permissions and limitations under the License.
|
||||
*/
|
||||
package example.app.service;
|
||||
|
||||
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalStateException;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import org.springframework.cache.annotation.CachePut;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import example.app.model.Contact;
|
||||
import example.app.repo.ContactRepository;
|
||||
|
||||
/**
|
||||
* Spring {@link Service} class for servicing {@link Contact Contacts}.
|
||||
*
|
||||
* @author John Blum
|
||||
* @see org.springframework.cache.annotation.Cacheable
|
||||
* @see org.springframework.cache.annotation.CachePut
|
||||
* @see org.springframework.stereotype.Service
|
||||
* @see example.app.model.Contact
|
||||
* @see example.app.repo.ContactRepository
|
||||
* @since 1.2.0
|
||||
*/
|
||||
@Service
|
||||
public class ContactsService {
|
||||
|
||||
private final AtomicBoolean cacheMiss = new AtomicBoolean(false);
|
||||
|
||||
private final ContactRepository contactRepository;
|
||||
|
||||
public ContactsService(ContactRepository contactRepository) {
|
||||
|
||||
Assert.notNull(contactRepository, "ContactRepository is required");
|
||||
|
||||
this.contactRepository = contactRepository;
|
||||
}
|
||||
|
||||
public boolean isCacheMiss() {
|
||||
return this.cacheMiss.getAndSet(false);
|
||||
}
|
||||
|
||||
@Cacheable(cacheNames = "ContactsByName")
|
||||
public Contact findByName(String name) {
|
||||
|
||||
this.cacheMiss.set(true);
|
||||
|
||||
return this.contactRepository.findById(name).orElseThrow(() ->
|
||||
newIllegalStateException("No Contact with name [%s] was found", name));
|
||||
}
|
||||
|
||||
@CachePut(cacheNames = "ContactsByName", key="#contact.name")
|
||||
public Contact save(Contact contact) {
|
||||
return this.contactRepository.save(contact);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user