diff --git a/src/main/java/com/arangodb/springframework/repository/SimpleArangoRepository.java b/src/main/java/com/arangodb/springframework/repository/SimpleArangoRepository.java index 0ba99ad3a..8d618392c 100644 --- a/src/main/java/com/arangodb/springframework/repository/SimpleArangoRepository.java +++ b/src/main/java/com/arangodb/springframework/repository/SimpleArangoRepository.java @@ -334,6 +334,7 @@ public long count(final Example example) { final String filter = predicate.length() == 0 ? "" : " FILTER " + predicate; final String query = String.format("FOR e IN %s%s COLLECT WITH COUNT INTO length RETURN length", getCollectionName(), filter); + arangoOperations.collection(domainClass); final ArangoCursor cursor = arangoOperations.query(query, bindVars, null, Long.class); return cursor.next(); } @@ -354,9 +355,9 @@ private ArangoCursor findAllInternal( final Sort sort, @Nullable final Example example, final Map bindVars) { - final String query = String.format("FOR e IN %s %s %s RETURN e", getCollectionName(), buildFilterClause(example, bindVars), buildSortClause(sort, "e")); + arangoOperations.collection(domainClass); return arangoOperations.query(query, bindVars, null, domainClass); } @@ -364,10 +365,9 @@ private ArangoCursor findAllInternal( final Pageable pageable, @Nullable final Example example, final Map bindVars) { - final String query = String.format("FOR e IN %s %s %s RETURN e", getCollectionName(), buildFilterClause(example, bindVars), buildPageableClause(pageable, "e")); - + arangoOperations.collection(domainClass); return arangoOperations.query(query, bindVars, pageable != null && pageable.isPaged() ? new AqlQueryOptions().fullCount(true) : null, domainClass); } diff --git a/src/test/java/com/arangodb/springframework/ArangoMultiTenancyRepositoryTestConfiguration.java b/src/test/java/com/arangodb/springframework/ArangoMultiTenancyRepositoryTestConfiguration.java new file mode 100644 index 000000000..004ecc90f --- /dev/null +++ b/src/test/java/com/arangodb/springframework/ArangoMultiTenancyRepositoryTestConfiguration.java @@ -0,0 +1,70 @@ +/* + * DISCLAIMER + * + * Copyright 2017 ArangoDB GmbH, Cologne, Germany + * + * 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. + * + * Copyright holder is ArangoDB GmbH, Cologne, Germany + */ + +package com.arangodb.springframework; + +import java.util.ArrayList; +import java.util.Collection; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.convert.converter.Converter; + +import com.arangodb.ArangoDB; +import com.arangodb.springframework.annotation.EnableArangoRepositories; +import com.arangodb.springframework.config.ArangoConfiguration; +import com.arangodb.springframework.core.mapping.CustomMappingTest; + +/** + * + * @author Paulo Ferreira + */ +@Configuration +@ComponentScan("com.arangodb.springframework.component") +@EnableArangoRepositories(basePackages = { + "com.arangodb.springframework.repository", + "com.arangodb.springframework.example.polymorphic.repository", + "com.arangodb.springframework.debug.repository"}, + namedQueriesLocation = "classpath*:arango-named-queries-test.properties") +public class ArangoMultiTenancyRepositoryTestConfiguration implements ArangoConfiguration { + + public static final String DB = "spring-test-db"; + + @Override + public ArangoDB.Builder arango() { + return new ArangoDB.Builder(); + } + + @Override + public String database() { + return DB + "#{tenantProvider.getId()}"; + } + + @Override + public Collection> customConverters() { + final Collection> converters = new ArrayList<>(); + converters.add(new CustomMappingTest.CustomVPackReadTestConverter()); + converters.add(new CustomMappingTest.CustomVPackWriteTestConverter()); + converters.add(new CustomMappingTest.CustomDBEntityReadTestConverter()); + converters.add(new CustomMappingTest.CustomDBEntityWriteTestConverter()); + return converters; + } + +} \ No newline at end of file diff --git a/src/test/java/com/arangodb/springframework/core/mapping/MultiTenancyDBLevelRepositoryTest.java b/src/test/java/com/arangodb/springframework/core/mapping/MultiTenancyDBLevelRepositoryTest.java new file mode 100644 index 000000000..750f3e66d --- /dev/null +++ b/src/test/java/com/arangodb/springframework/core/mapping/MultiTenancyDBLevelRepositoryTest.java @@ -0,0 +1,89 @@ +/* + * DISCLAIMER + * + * Copyright 2018 ArangoDB GmbH, Cologne, Germany + * + * 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. + * + * Copyright holder is ArangoDB GmbH, Cologne, Germany + */ + +package com.arangodb.springframework.core.mapping; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +import java.util.Optional; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Example; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.arangodb.springframework.ArangoMultiTenancyRepositoryTestConfiguration; +import com.arangodb.springframework.component.TenantProvider; +import com.arangodb.springframework.core.ArangoOperations; +import com.arangodb.springframework.repository.IdTestRepository; +import com.arangodb.springframework.testdata.IdTestEntity; + +/** + * @author Paulo Ferreira + * + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { ArangoMultiTenancyRepositoryTestConfiguration.class }) +public class MultiTenancyDBLevelRepositoryTest { + + private static final String TENANT00 = "tenant00"; + + @Autowired + protected ArangoOperations template; + + @Autowired + TenantProvider tenantProvider; + + @Autowired + IdTestRepository idTestRepository; + + /* + * For some reason, findAll already creates the collection by itself + */ + + @Test + public void dbFindAll() { + tenantProvider.setId(TENANT00); + assertThat(idTestRepository.findAll().iterator().hasNext(), is(false)); + } + + @Test + public void dbFindOne() { + tenantProvider.setId(TENANT00); + IdTestEntity entity = new IdTestEntity<>(); + entity.setId("MyId"); + Example> example = Example.of(entity); + Optional> result = idTestRepository.findOne(example); + assertThat(result.isPresent(), is(false)); + } + + @Before + @After + public void cleanup() { + tenantProvider.setId(TENANT00); + template.dropDatabase(); + } + +} \ No newline at end of file