diff --git a/pom.xml b/pom.xml index b4cad49..f6dade9 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.boot spring-boot-starter-parent - 1.5.6.RELEASE + 1.5.9.RELEASE @@ -40,7 +40,7 @@ com.google.cloud google-cloud-datastore - 1.0.0 + 1.27.0 org.hamcrest diff --git a/src/main/java/org/springframework/data/gclouddatastore/repository/GcloudDatastoreEntityInformation.java b/src/main/java/org/springframework/data/gclouddatastore/repository/GcloudDatastoreEntityInformation.java index 8e80504..82edb04 100644 --- a/src/main/java/org/springframework/data/gclouddatastore/repository/GcloudDatastoreEntityInformation.java +++ b/src/main/java/org/springframework/data/gclouddatastore/repository/GcloudDatastoreEntityInformation.java @@ -1,17 +1,14 @@ /* * Copyright 2017 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 + * 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 + * 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. + * 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.springframework.data.gclouddatastore.repository; @@ -34,6 +31,7 @@ public GcloudDatastoreEntityInformation(Class domainClass) { @SuppressWarnings("unchecked") @Override public ID getId(T entity) { + Class domainClass = getJavaType(); while (domainClass != Object.class) { for (Field field : domainClass.getDeclaredFields()) { @@ -43,7 +41,7 @@ public ID getId(T entity) { } catch (IllegalArgumentException | IllegalAccessException e) { BeanWrapper beanWrapper = PropertyAccessorFactory - .forBeanPropertyAccess(entity); + .forBeanPropertyAccess(entity); return (ID) beanWrapper.getPropertyValue(field.getName()); } } @@ -56,6 +54,7 @@ public ID getId(T entity) { @SuppressWarnings("unchecked") @Override public Class getIdType() { + Class domainClass = getJavaType(); while (domainClass != Object.class) { for (Field field : domainClass.getDeclaredFields()) { diff --git a/src/main/java/org/springframework/data/gclouddatastore/repository/GcloudDatastoreRepositoryConfigurationExtension.java b/src/main/java/org/springframework/data/gclouddatastore/repository/GcloudDatastoreRepositoryConfigurationExtension.java index a4807f4..32d85e7 100644 --- a/src/main/java/org/springframework/data/gclouddatastore/repository/GcloudDatastoreRepositoryConfigurationExtension.java +++ b/src/main/java/org/springframework/data/gclouddatastore/repository/GcloudDatastoreRepositoryConfigurationExtension.java @@ -1,33 +1,43 @@ /* * Copyright 2017 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 + * 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 + * 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. + * 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.springframework.data.gclouddatastore.repository; +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource; import org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport; -public class GcloudDatastoreRepositoryConfigurationExtension - extends RepositoryConfigurationExtensionSupport { +public class GcloudDatastoreRepositoryConfigurationExtension extends RepositoryConfigurationExtensionSupport { + + private static final String DATASTORE_OPTIONS = "datastoreOptions"; @Override public String getRepositoryFactoryClassName() { + return GcloudDatastoreRepositoryFactory.class.getName(); } @Override protected String getModulePrefix() { + return "gcloudds"; } + + @Override + public void postProcess(final BeanDefinitionBuilder builder, final AnnotationRepositoryConfigurationSource config) { + + builder.addDependsOn(DATASTORE_OPTIONS); + builder.addPropertyReference(DATASTORE_OPTIONS, DATASTORE_OPTIONS); + } + } diff --git a/src/main/java/org/springframework/data/gclouddatastore/repository/GcloudDatastoreRepositoryFactory.java b/src/main/java/org/springframework/data/gclouddatastore/repository/GcloudDatastoreRepositoryFactory.java index 21dcbac..e3e72c3 100644 --- a/src/main/java/org/springframework/data/gclouddatastore/repository/GcloudDatastoreRepositoryFactory.java +++ b/src/main/java/org/springframework/data/gclouddatastore/repository/GcloudDatastoreRepositoryFactory.java @@ -135,7 +135,7 @@ public Object next() { .newInstance(); unmarshaller.unmarshalToObject( results.next(), entity); - return entity; + return entity; } catch (InstantiationException | IllegalAccessException e) { diff --git a/src/main/java/org/springframework/data/gclouddatastore/repository/GcloudDatastoreRepositoryFactoryBean.java b/src/main/java/org/springframework/data/gclouddatastore/repository/GcloudDatastoreRepositoryFactoryBean.java index e629a1e..9befc45 100644 --- a/src/main/java/org/springframework/data/gclouddatastore/repository/GcloudDatastoreRepositoryFactoryBean.java +++ b/src/main/java/org/springframework/data/gclouddatastore/repository/GcloudDatastoreRepositoryFactoryBean.java @@ -1,17 +1,14 @@ /* * Copyright 2017 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 + * 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 + * 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. + * 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.springframework.data.gclouddatastore.repository; @@ -19,30 +16,35 @@ import java.io.Serializable; import com.google.cloud.datastore.DatastoreOptions; - import org.springframework.data.repository.Repository; import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport; import org.springframework.data.repository.core.support.RepositoryFactorySupport; +import org.springframework.util.Assert; public class GcloudDatastoreRepositoryFactoryBean, S, ID extends Serializable> extends RepositoryFactoryBeanSupport { - DatastoreOptions datastoreOptions; + private DatastoreOptions datastoreOptions; public GcloudDatastoreRepositoryFactoryBean(Class repositoryInterface) { super(repositoryInterface); - this.datastoreOptions = DatastoreOptions.getDefaultInstance(); } - public GcloudDatastoreRepositoryFactoryBean( - Class repositoryInterface, - DatastoreOptions datastoreOptions) { - super(repositoryInterface); + @Override + protected RepositoryFactorySupport createRepositoryFactory() { + + return new GcloudDatastoreRepositoryFactory(datastoreOptions); + } + + public void setDatastoreOptions(DatastoreOptions datastoreOptions) { + this.datastoreOptions = datastoreOptions; } @Override - protected RepositoryFactorySupport createRepositoryFactory() { - return new GcloudDatastoreRepositoryFactory(this.datastoreOptions); + public void afterPropertiesSet() { + + Assert.notNull(datastoreOptions, "you must provide DatastoreOptions it cannot be null!"); + super.afterPropertiesSet(); } } diff --git a/src/test/java/org/springframework/data/gclouddatastore/repository/SimpleGcloudDatastoreRepositoryTests.java b/src/test/java/org/springframework/data/gclouddatastore/repository/SimpleGcloudDatastoreRepositoryTests.java index 0bd335d..d2b95cf 100644 --- a/src/test/java/org/springframework/data/gclouddatastore/repository/SimpleGcloudDatastoreRepositoryTests.java +++ b/src/test/java/org/springframework/data/gclouddatastore/repository/SimpleGcloudDatastoreRepositoryTests.java @@ -1,40 +1,39 @@ /* * Copyright 2017 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 + * 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 + * 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. + * 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.springframework.data.gclouddatastore.repository; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.emptyIterable; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; + import java.util.Arrays; import java.util.stream.Collectors; +import com.google.cloud.datastore.DatastoreOptions; import com.google.cloud.datastore.PathElement; import org.junit.Test; import org.junit.runner.RunWith; - import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.data.gclouddatastore.repository.SimpleGcloudDatastoreRepositoryTests.TestConfig; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import static org.hamcrest.Matchers.contains; -import static org.hamcrest.Matchers.emptyIterable; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThat; - @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = SimpleGcloudDatastoreRepositoryTests.class) +@ContextConfiguration(classes = { SimpleGcloudDatastoreRepositoryTests.class, TestConfig.class }) @Configuration @EnableGcloudDatastoreRepositories public class SimpleGcloudDatastoreRepositoryTests { @@ -44,7 +43,8 @@ public class SimpleGcloudDatastoreRepositoryTests { @Test public void testCount1() throws Exception { - try (Context ctx = Context.with(PathElement.of("Kind", 1))) { + + try(Context ctx = Context.with(PathElement.of("Kind", 1))) { // Setup this.repo.deleteAll(); @@ -55,7 +55,8 @@ public void testCount1() throws Exception { @Test public void testCount2() throws Exception { - try (Context ctx = Context.with(PathElement.of("Kind", 1))) { + + try(Context ctx = Context.with(PathElement.of("Kind", 1))) { // Setup this.repo.deleteAll(); this.repo.save(Arrays.asList(new Person(123), new Person(456))); @@ -67,7 +68,8 @@ public void testCount2() throws Exception { @Test public void testDeleteId() throws Exception { - try (Context ctx = Context.with(PathElement.of("Kind", 1))) { + + try(Context ctx = Context.with(PathElement.of("Kind", 1))) { // Setup this.repo.deleteAll(); this.repo.save(Arrays.asList(new Person(123), new Person(456))); @@ -82,7 +84,8 @@ public void testDeleteId() throws Exception { @Test public void testDeleteEntity() throws Exception { - try (Context ctx = Context.with(PathElement.of("Kind", 1))) { + + try(Context ctx = Context.with(PathElement.of("Kind", 1))) { // Setup this.repo.deleteAll(); this.repo.save(Arrays.asList(new Person(123), new Person(456))); @@ -97,11 +100,12 @@ public void testDeleteEntity() throws Exception { @Test public void testDeleteEntities() throws Exception { - try (Context ctx = Context.with(PathElement.of("Kind", 1))) { + + try(Context ctx = Context.with(PathElement.of("Kind", 1))) { // Setup this.repo.deleteAll(); this.repo.save( - Arrays.asList(new Person(123), new Person(456), new Person(789))); + Arrays.asList(new Person(123), new Person(456), new Person(789))); // Exercise this.repo.delete(Arrays.asList(new Person(123), new Person(789))); @@ -113,7 +117,8 @@ public void testDeleteEntities() throws Exception { @Test public void testDeleteAll() throws Exception { - try (Context ctx = Context.with(PathElement.of("Kind", 1))) { + + try(Context ctx = Context.with(PathElement.of("Kind", 1))) { // Setup this.repo.deleteAll(); this.repo.save(Arrays.asList(new Person(123), new Person(456))); @@ -128,7 +133,8 @@ public void testDeleteAll() throws Exception { @Test public void testExists1() throws Exception { - try (Context ctx = Context.with(PathElement.of("Kind", 1))) { + + try(Context ctx = Context.with(PathElement.of("Kind", 1))) { // Setup this.repo.deleteAll(); this.repo.save(new Person(123)); @@ -140,7 +146,8 @@ public void testExists1() throws Exception { @Test public void testExists2() throws Exception { - try (Context ctx = Context.with(PathElement.of("Kind", 1))) { + + try(Context ctx = Context.with(PathElement.of("Kind", 1))) { // Setup this.repo.deleteAll(); this.repo.save(new Person(123)); @@ -152,7 +159,8 @@ public void testExists2() throws Exception { @Test public void testFindAll1() throws Exception { - try (Context ctx = Context.with(PathElement.of("Kind", 1))) { + + try(Context ctx = Context.with(PathElement.of("Kind", 1))) { // Setup this.repo.deleteAll(); @@ -163,7 +171,8 @@ public void testFindAll1() throws Exception { @Test public void testFindAll2() throws Exception { - try (Context ctx = Context.with(PathElement.of("Kind", 1))) { + + try(Context ctx = Context.with(PathElement.of("Kind", 1))) { // Setup this.repo.deleteAll(); this.repo.save(Arrays.asList(new Person(123), new Person(456))); @@ -175,7 +184,8 @@ public void testFindAll2() throws Exception { @Test public void testFindAllIds1() throws Exception { - try (Context ctx = Context.with(PathElement.of("Kind", 1))) { + + try(Context ctx = Context.with(PathElement.of("Kind", 1))) { // Setup this.repo.deleteAll(); this.repo.save(Arrays.asList(new Person(123), new Person(456))); @@ -187,20 +197,22 @@ public void testFindAllIds1() throws Exception { @Test public void testFindAllIds2() throws Exception { - try (Context ctx = Context.with(PathElement.of("Kind", 1))) { + + try(Context ctx = Context.with(PathElement.of("Kind", 1))) { // Setup this.repo.deleteAll(); this.repo.save(Arrays.asList(new Person(123), new Person(456))); // Exercise, Verify assertThat(this.repo.findAll(Arrays.asList(123L, 456L)), - contains(new Person(123), new Person(456))); + contains(new Person(123), new Person(456))); } } @Test public void testFindOne1() throws Exception { - try (Context ctx = Context.with(PathElement.of("Kind", 1))) { + + try(Context ctx = Context.with(PathElement.of("Kind", 1))) { // Setup this.repo.deleteAll(); this.repo.save(Arrays.asList(new Person(123), new Person(456))); @@ -212,7 +224,8 @@ public void testFindOne1() throws Exception { @Test public void testFindOne2() throws Exception { - try (Context ctx = Context.with(PathElement.of("Kind", 1))) { + + try(Context ctx = Context.with(PathElement.of("Kind", 1))) { // Setup this.repo.deleteAll(); this.repo.save(Arrays.asList(new Person(123), new Person(456))); @@ -224,7 +237,8 @@ public void testFindOne2() throws Exception { @Test public void testSaveEntity() throws Exception { - try (Context ctx = Context.with(PathElement.of("Kind", 1))) { + + try(Context ctx = Context.with(PathElement.of("Kind", 1))) { // Setup this.repo.deleteAll(); @@ -238,7 +252,8 @@ public void testSaveEntity() throws Exception { @Test public void testSaveEntities() throws Exception { - try (Context ctx = Context.with(PathElement.of("Kind", 1))) { + + try(Context ctx = Context.with(PathElement.of("Kind", 1))) { // Setup this.repo.deleteAll(); @@ -247,13 +262,14 @@ public void testSaveEntities() throws Exception { // Verify assertThat(this.repo.findAll(Arrays.asList(123L, 456L)), - contains(new Person(123), new Person(456))); + contains(new Person(123), new Person(456))); } } @Test public void testQueryMethod_QueryForEntity() throws Exception { - try (Context ctx = Context.with(PathElement.of("Kind", 1))) { + + try(Context ctx = Context.with(PathElement.of("Kind", 1))) { // Setup this.repo.deleteAll(); this.repo.save(Arrays.asList(new Person(123), new Person(456))); @@ -265,66 +281,82 @@ public void testQueryMethod_QueryForEntity() throws Exception { @Test public void testQueryMethod_QueryForEntity_Optional() throws Exception { - try (Context ctx = Context.with(PathElement.of("Kind", 1))) { + + try(Context ctx = Context.with(PathElement.of("Kind", 1))) { // Setup this.repo.deleteAll(); this.repo.save(Arrays.asList(new Person(123), new Person(456))); // Exercise, Verify - assertEquals(new Person(123), this.repo.findFirstById(123L).get()); + assertEquals(new Person(123), this.repo.findFirstById(123L) + .get()); } } @Test public void testQueryMethod_CollectionQuery() throws Exception { - try (Context ctx = Context.with(PathElement.of("Kind", 1))) { + + try(Context ctx = Context.with(PathElement.of("Kind", 1))) { // Setup this.repo.deleteAll(); this.repo.save(Arrays.asList( - new Person(123L, "", "John", "Doe", 0, false), - new Person(456L, "", "Jane", "Doe", 0, false))); + new Person(123L, "", "John", "Doe", 0, false), + new Person(456L, "", "Jane", "Doe", 0, false))); // Exercise, Verify assertEquals( - Arrays.asList(new Person(456L, "", "Jane", "Doe", 0, false)), - this.repo.findByFirstName("Jane")); + Arrays.asList(new Person(456L, "", "Jane", "Doe", 0, false)), + this.repo.findByFirstName("Jane")); } } @Test public void testQueryMethod_StreamQuery() throws Exception { - try (Context ctx = Context.with(PathElement.of("Kind", 1))) { + + try(Context ctx = Context.with(PathElement.of("Kind", 1))) { // Setup this.repo.deleteAll(); this.repo.save(Arrays.asList( - new Person(123L, "", "John", "Lennon", 0, false), - new Person(456L, "", "John", "Mayer", 0, false))); + new Person(123L, "", "John", "Lennon", 0, false), + new Person(456L, "", "John", "Mayer", 0, false))); // Exercise, Verify assertEquals( - Arrays.asList(new Person(456L, "", "John", "Mayer", 0, false)), - this.repo.findByLastName("Mayer").collect(Collectors.toList())); + Arrays.asList(new Person(456L, "", "John", "Mayer", 0, false)), + this.repo.findByLastName("Mayer") + .collect(Collectors.toList())); } } @Test public void testQueryMethod_OrderBy() throws Exception { - try (Context ctx = Context.with(PathElement.of("Kind", 1))) { + + try(Context ctx = Context.with(PathElement.of("Kind", 1))) { // Setup this.repo.deleteAll(); this.repo.save(Arrays.asList( - new Person(1L, "", "Fela", "Kuti", 0, false), - new Person(2L, "", "Tony", "Allen", 0, false), - new Person(3L, "", "Seun", "Kuti", 0, false), - new Person(4L, "", "Femi", "Kuti", 0, false))); + new Person(1L, "", "Fela", "Kuti", 0, false), + new Person(2L, "", "Tony", "Allen", 0, false), + new Person(3L, "", "Seun", "Kuti", 0, false), + new Person(4L, "", "Femi", "Kuti", 0, false))); // Exercise, Verify assertEquals( - Arrays.asList( - new Person(1L, "", "Fela", "Kuti", 0, false), - new Person(4L, "", "Femi", "Kuti", 0, false), - new Person(3L, "", "Seun", "Kuti", 0, false)), - this.repo.findByLastNameOrderByFirstNameAsc("Kuti")); + Arrays.asList( + new Person(1L, "", "Fela", "Kuti", 0, false), + new Person(4L, "", "Femi", "Kuti", 0, false), + new Person(3L, "", "Seun", "Kuti", 0, false)), + this.repo.findByLastNameOrderByFirstNameAsc("Kuti")); + } + } + + @Configuration + public class TestConfig { + + @Bean + public DatastoreOptions datastoreOptions() { + + return DatastoreOptions.getDefaultInstance(); } } }