You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2021/01/13 17:09:44 UTC
[isis] branch master updated: ISIS-2033: Spring Data JPA: adds
repository bootstrapping tests
This is an automated email from the ASF dual-hosted git repository.
ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git
The following commit(s) were added to refs/heads/master by this push:
new 9434bf9 ISIS-2033: Spring Data JPA: adds repository bootstrapping tests
9434bf9 is described below
commit 9434bf92d0f75f71ba3a5860e13e1116aafaab69
Author: Andi Huber <ah...@apache.org>
AuthorDate: Wed Jan 13 18:06:17 2021 +0100
ISIS-2033: Spring Data JPA: adds repository bootstrapping tests
first prototypical tests to use Spring Data JPA
---
.../conf/Configuration_usingSpringDataJpa.java | 64 ++++++++++
.../isis/testdomain/jpa/springdata/Employee.java | 55 ++++++++
.../EmployeeManager.java} | 28 ++++-
.../springdata/EmployeeManager_deleteEmployee.java | 24 ++++
.../EmployeeManager_newEmployee.java} | 24 +++-
.../EmployeeRepository.java} | 12 +-
.../jpa/springdata/SpringDataJpaTestModule.java | 46 +++++++
.../springdata/SpringDataJpaBootstrappingTest.java | 139 +++++++++++++++++++++
8 files changed, 376 insertions(+), 16 deletions(-)
diff --git a/regressiontests/stable/src/main/java/org/apache/isis/testdomain/conf/Configuration_usingSpringDataJpa.java b/regressiontests/stable/src/main/java/org/apache/isis/testdomain/conf/Configuration_usingSpringDataJpa.java
new file mode 100644
index 0000000..8253a19
--- /dev/null
+++ b/regressiontests/stable/src/main/java/org/apache/isis/testdomain/conf/Configuration_usingSpringDataJpa.java
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.isis.testdomain.conf;
+
+import org.springframework.boot.SpringBootConfiguration;
+import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
+import org.springframework.boot.autoconfigure.domain.EntityScan;
+import org.springframework.context.annotation.Import;
+import org.springframework.context.annotation.PropertySource;
+import org.springframework.context.annotation.PropertySources;
+import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
+
+import org.apache.isis.core.config.presets.IsisPresets;
+import org.apache.isis.core.runtimeservices.IsisModuleCoreRuntimeServices;
+import org.apache.isis.persistence.jpa.eclipselink.IsisModuleJpaEclipseLink;
+import org.apache.isis.security.bypass.IsisModuleSecurityBypass;
+import org.apache.isis.testdomain.jpa.springdata.Employee;
+import org.apache.isis.testdomain.jpa.springdata.EmployeeRepository;
+import org.apache.isis.testdomain.jpa.springdata.SpringDataJpaTestModule;
+import org.apache.isis.testdomain.model.stereotypes.MyService;
+import org.apache.isis.testdomain.util.kv.KVStoreForTesting;
+import org.apache.isis.testing.fixtures.applib.IsisModuleTestingFixturesApplib;
+
+@SpringBootConfiguration
+@EnableAutoConfiguration
+@Import({
+
+ SpringDataJpaTestModule.class,
+
+ MyService.class, // testing injection into entities
+
+ IsisModuleCoreRuntimeServices.class
+ ,IsisModuleSecurityBypass.class
+ ,IsisModuleJpaEclipseLink.class
+ ,IsisModuleTestingFixturesApplib.class
+ ,KVStoreForTesting.class, // Helper for JUnit Tests
+})
+@EnableJpaRepositories(basePackageClasses = EmployeeRepository.class)
+@EntityScan(basePackageClasses = Employee.class)
+
+@PropertySources({
+ @PropertySource(IsisPresets.H2InMemory_withUniqueSchema),
+ @PropertySource(IsisPresets.NoTranslations),
+})
+public class Configuration_usingSpringDataJpa {
+
+
+}
\ No newline at end of file
diff --git a/regressiontests/stable/src/main/java/org/apache/isis/testdomain/jpa/springdata/Employee.java b/regressiontests/stable/src/main/java/org/apache/isis/testdomain/jpa/springdata/Employee.java
new file mode 100644
index 0000000..a637c68
--- /dev/null
+++ b/regressiontests/stable/src/main/java/org/apache/isis/testdomain/jpa/springdata/Employee.java
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.isis.testdomain.jpa.springdata;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+
+import org.apache.isis.applib.annotation.DomainObject;
+import org.apache.isis.applib.annotation.Nature;
+
+import lombok.AccessLevel;
+import lombok.EqualsAndHashCode;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.Setter;
+import lombok.ToString;
+
+@Entity
+@DomainObject(nature=Nature.JPA_ENTITY, objectType = "isisLab.Employee")
+@Getter @Setter @ToString @EqualsAndHashCode
+@NoArgsConstructor(access = AccessLevel.PROTECTED)
+public class Employee {
+
+ @Id
+ @GeneratedValue
+ private Long id;
+
+ private String firstName;
+
+ private String lastName;
+
+ public Employee(String firstName, String lastName) {
+ this.firstName = firstName;
+ this.lastName = lastName;
+ }
+
+
+}
diff --git a/regressiontests/stable/src/main/java/org/apache/isis/testdomain/jpa/JpaTestApplication.java b/regressiontests/stable/src/main/java/org/apache/isis/testdomain/jpa/springdata/EmployeeManager.java
similarity index 54%
copy from regressiontests/stable/src/main/java/org/apache/isis/testdomain/jpa/JpaTestApplication.java
copy to regressiontests/stable/src/main/java/org/apache/isis/testdomain/jpa/springdata/EmployeeManager.java
index eb17739..0dec371 100644
--- a/regressiontests/stable/src/main/java/org/apache/isis/testdomain/jpa/JpaTestApplication.java
+++ b/regressiontests/stable/src/main/java/org/apache/isis/testdomain/jpa/springdata/EmployeeManager.java
@@ -16,13 +16,29 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.isis.testdomain.jpa;
+package org.apache.isis.testdomain.jpa.springdata;
-import org.springframework.boot.autoconfigure.SpringBootApplication;
+import java.util.List;
-@SpringBootApplication(scanBasePackageClasses= {JpaTestDomainModule.class})
-@Deprecated
-public class JpaTestApplication {
+import javax.inject.Inject;
+import org.apache.isis.applib.annotation.Collection;
+import org.apache.isis.applib.annotation.DomainObject;
+import org.apache.isis.applib.annotation.Nature;
-}
+@DomainObject(nature=Nature.VIEW_MODEL, objectType = "isisLab.EmployeeManager")
+//@RequiredArgsConstructor(onConstructor_ = {@Inject}) //XXX not supported for view models yet
+public class EmployeeManager {
+
+ @Inject private EmployeeRepository employeeRepo;
+
+ public String title() {
+ return "Employee Manager";
+ }
+
+ @Collection
+ public List<Employee> getAllEmployees(){
+ return employeeRepo.findAll();
+ }
+
+}
\ No newline at end of file
diff --git a/regressiontests/stable/src/main/java/org/apache/isis/testdomain/jpa/springdata/EmployeeManager_deleteEmployee.java b/regressiontests/stable/src/main/java/org/apache/isis/testdomain/jpa/springdata/EmployeeManager_deleteEmployee.java
new file mode 100644
index 0000000..c851c2e
--- /dev/null
+++ b/regressiontests/stable/src/main/java/org/apache/isis/testdomain/jpa/springdata/EmployeeManager_deleteEmployee.java
@@ -0,0 +1,24 @@
+package org.apache.isis.testdomain.jpa.springdata;
+
+import java.util.List;
+
+import javax.inject.Inject;
+
+import org.apache.isis.applib.annotation.Action;
+
+import lombok.RequiredArgsConstructor;
+
+@Action(associateWith = "allEmployees")
+@RequiredArgsConstructor
+public class EmployeeManager_deleteEmployee {
+
+ @Inject private EmployeeRepository employeeRepo;
+
+ private final EmployeeManager holder;
+
+ public EmployeeManager act(List<Employee> employeesToRemove) {
+ employeesToRemove.forEach(employeeRepo::delete);
+ return holder;
+ }
+
+}
diff --git a/regressiontests/stable/src/main/java/org/apache/isis/testdomain/jpa/JpaTestApplication.java b/regressiontests/stable/src/main/java/org/apache/isis/testdomain/jpa/springdata/EmployeeManager_newEmployee.java
similarity index 57%
copy from regressiontests/stable/src/main/java/org/apache/isis/testdomain/jpa/JpaTestApplication.java
copy to regressiontests/stable/src/main/java/org/apache/isis/testdomain/jpa/springdata/EmployeeManager_newEmployee.java
index eb17739..d2b07da 100644
--- a/regressiontests/stable/src/main/java/org/apache/isis/testdomain/jpa/JpaTestApplication.java
+++ b/regressiontests/stable/src/main/java/org/apache/isis/testdomain/jpa/springdata/EmployeeManager_newEmployee.java
@@ -16,13 +16,27 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.isis.testdomain.jpa;
+package org.apache.isis.testdomain.jpa.springdata;
-import org.springframework.boot.autoconfigure.SpringBootApplication;
+import javax.inject.Inject;
-@SpringBootApplication(scanBasePackageClasses= {JpaTestDomainModule.class})
-@Deprecated
-public class JpaTestApplication {
+import org.apache.isis.applib.annotation.Action;
+import lombok.RequiredArgsConstructor;
+import lombok.val;
+@Action(associateWith = "allEmployees")
+@RequiredArgsConstructor
+public class EmployeeManager_newEmployee {
+
+ @Inject private EmployeeRepository employeeRepo;
+
+ private final EmployeeManager holder;
+
+ public EmployeeManager act(String firstName, String lastName) {
+ val newEmployee = new Employee(firstName, lastName);
+ employeeRepo.save(newEmployee);
+ return holder;
+ }
+
}
diff --git a/regressiontests/stable/src/main/java/org/apache/isis/testdomain/jpa/JpaTestApplication.java b/regressiontests/stable/src/main/java/org/apache/isis/testdomain/jpa/springdata/EmployeeRepository.java
similarity index 70%
rename from regressiontests/stable/src/main/java/org/apache/isis/testdomain/jpa/JpaTestApplication.java
rename to regressiontests/stable/src/main/java/org/apache/isis/testdomain/jpa/springdata/EmployeeRepository.java
index eb17739..3b9c83f 100644
--- a/regressiontests/stable/src/main/java/org/apache/isis/testdomain/jpa/JpaTestApplication.java
+++ b/regressiontests/stable/src/main/java/org/apache/isis/testdomain/jpa/springdata/EmployeeRepository.java
@@ -16,13 +16,15 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.isis.testdomain.jpa;
+package org.apache.isis.testdomain.jpa.springdata;
-import org.springframework.boot.autoconfigure.SpringBootApplication;
+import java.util.List;
-@SpringBootApplication(scanBasePackageClasses= {JpaTestDomainModule.class})
-@Deprecated
-public class JpaTestApplication {
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.stereotype.Repository;
+@Repository
+public interface EmployeeRepository extends JpaRepository<Employee, Long> {
+ List<Employee> findByLastNameStartsWithIgnoreCase(String lastName);
}
diff --git a/regressiontests/stable/src/main/java/org/apache/isis/testdomain/jpa/springdata/SpringDataJpaTestModule.java b/regressiontests/stable/src/main/java/org/apache/isis/testdomain/jpa/springdata/SpringDataJpaTestModule.java
new file mode 100644
index 0000000..5d39718
--- /dev/null
+++ b/regressiontests/stable/src/main/java/org/apache/isis/testdomain/jpa/springdata/SpringDataJpaTestModule.java
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.isis.testdomain.jpa.springdata;
+
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Import;
+
+import org.apache.isis.applib.services.factory.FactoryService;
+
+@Configuration
+@Import({
+ Employee.class,
+ EmployeeManager.class,
+ //EmployeeRepository.class,
+})
+public class SpringDataJpaTestModule {
+
+ public static void setupEmployeeFixture(final EmployeeRepository repository) {
+ repository.save(new Employee("Bill", "Gates"));
+ repository.save(new Employee("Mark", "Zuckerberg"));
+ repository.save(new Employee("Sundar", "Pichai"));
+ repository.save(new Employee("Jeff", "Bezos"));
+ }
+
+ public static EmployeeManager createEmployeeManager(final FactoryService factoryService){
+ return factoryService.viewModel(EmployeeManager.class);
+ }
+
+}
+
diff --git a/regressiontests/stable/src/test/java/org/apache/isis/testdomain/persistence/jpa/springdata/SpringDataJpaBootstrappingTest.java b/regressiontests/stable/src/test/java/org/apache/isis/testdomain/persistence/jpa/springdata/SpringDataJpaBootstrappingTest.java
new file mode 100644
index 0000000..cc798a2
--- /dev/null
+++ b/regressiontests/stable/src/test/java/org/apache/isis/testdomain/persistence/jpa/springdata/SpringDataJpaBootstrappingTest.java
@@ -0,0 +1,139 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.isis.testdomain.persistence.jpa.springdata;
+
+import java.util.Optional;
+
+import javax.inject.Inject;
+
+import org.junit.jupiter.api.MethodOrderer;
+import org.junit.jupiter.api.Order;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestMethodOrder;
+import org.springframework.boot.autoconfigure.domain.EntityScan;
+import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
+import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
+import org.springframework.test.annotation.Rollback;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.TestPropertySource;
+import org.springframework.transaction.PlatformTransactionManager;
+import org.springframework.transaction.support.DefaultTransactionDefinition;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.apache.isis.applib.services.repository.RepositoryService;
+import org.apache.isis.core.config.presets.IsisPresets;
+import org.apache.isis.core.metamodel.facets.object.entity.EntityFacet;
+import org.apache.isis.core.metamodel.specloader.SpecificationLoader;
+import org.apache.isis.testdomain.conf.Configuration_usingSpringDataJpa;
+import org.apache.isis.testdomain.jpa.springdata.Employee;
+import org.apache.isis.testdomain.jpa.springdata.EmployeeRepository;
+import org.apache.isis.testdomain.jpa.springdata.SpringDataJpaTestModule;
+import org.apache.isis.testing.integtestsupport.applib.IsisIntegrationTestAbstract;
+
+import lombok.val;
+
+@DataJpaTest
+@ContextConfiguration(classes = {
+ Configuration_usingSpringDataJpa.class,
+})
+@TestPropertySource(IsisPresets.UseLog4j2Test)
+@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
+class SpringDataJpaBootstrappingTest extends IsisIntegrationTestAbstract {
+
+ @Inject private Optional<PlatformTransactionManager> platformTransactionManager;
+ @Inject private RepositoryService repository;
+ @Inject private SpecificationLoader specLoader;
+
+ @Inject private EmployeeRepository employeeRepository;
+ //@Inject private FactoryService factoryService;
+ //@Inject private TransactionService transactionService;
+
+ void cleanUp() {
+ employeeRepository.deleteAllInBatch();
+ }
+
+ void setUp() {
+ SpringDataJpaTestModule.setupEmployeeFixture(employeeRepository);
+ }
+
+ @Test @Order(0)
+ void platformTransactionManager_shouldBeAvailable() {
+ assertTrue(platformTransactionManager.isPresent());
+ platformTransactionManager.ifPresent(ptm->{
+ assertEquals("JpaTransactionManager", ptm.getClass().getSimpleName());
+ });
+ }
+
+ @Test @Order(0)
+ void transactionalAnnotation_shouldBeSupported() {
+ assertTrue(platformTransactionManager.isPresent());
+ platformTransactionManager.ifPresent(ptm->{
+
+ val txDef = new DefaultTransactionDefinition();
+ txDef.setPropagationBehavior(DefaultTransactionDefinition.PROPAGATION_MANDATORY);
+
+ val txStatus = ptm.getTransaction(txDef);
+
+ assertNotNull(txStatus);
+ assertFalse(txStatus.isCompleted());
+
+ });
+ }
+
+ @Test @Order(0)
+ void jpaEntities_shouldBeRecognisedAsSuch() {
+ val productSpec = specLoader.loadSpecification(Employee.class);
+ assertTrue(productSpec.isEntity());
+ assertNotNull(productSpec.getFacet(EntityFacet.class));
+ }
+
+ @Test @Order(1) @Rollback(false)
+ void sampleEmployeesShouldBeSetUp() {
+
+ // given - expected pre condition: no inventories
+
+ cleanUp();
+ assertEquals(0, repository.allInstances(Employee.class).size());
+
+ // when
+
+ setUp();
+
+ // then - expected post condition: 4 employees
+
+ val employees = repository.allInstances(Employee.class);
+ assertEquals(4, employees.size());
+
+ val employee = employees.get(0);
+ assertNotNull(employee);
+ assertNotNull(employee.getLastName());
+
+ }
+
+ @Test @Order(2) @Rollback(false)
+ void aSecondRunShouldWorkAsWell() {
+ sampleEmployeesShouldBeSetUp();
+ }
+
+
+}