Skip to content

Commit a686255

Browse files
Create collection on find (#196)
Co-authored-by: Paulo Ferreira <paulo.ferreira25@gmail.com>
1 parent 6c354f9 commit a686255

File tree

3 files changed

+162
-3
lines changed

3 files changed

+162
-3
lines changed

src/main/java/com/arangodb/springframework/repository/SimpleArangoRepository.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ public <S extends T> long count(final Example<S> example) {
334334
final String filter = predicate.length() == 0 ? "" : " FILTER " + predicate;
335335
final String query = String.format("FOR e IN %s%s COLLECT WITH COUNT INTO length RETURN length",
336336
getCollectionName(), filter);
337+
arangoOperations.collection(domainClass);
337338
final ArangoCursor<Long> cursor = arangoOperations.query(query, bindVars, null, Long.class);
338339
return cursor.next();
339340
}
@@ -354,20 +355,19 @@ private <S extends T> ArangoCursor<T> findAllInternal(
354355
final Sort sort,
355356
@Nullable final Example<S> example,
356357
final Map<String, Object> bindVars) {
357-
358358
final String query = String.format("FOR e IN %s %s %s RETURN e", getCollectionName(),
359359
buildFilterClause(example, bindVars), buildSortClause(sort, "e"));
360+
arangoOperations.collection(domainClass);
360361
return arangoOperations.query(query, bindVars, null, domainClass);
361362
}
362363

363364
private <S extends T> ArangoCursor<T> findAllInternal(
364365
final Pageable pageable,
365366
@Nullable final Example<S> example,
366367
final Map<String, Object> bindVars) {
367-
368368
final String query = String.format("FOR e IN %s %s %s RETURN e", getCollectionName(),
369369
buildFilterClause(example, bindVars), buildPageableClause(pageable, "e"));
370-
370+
arangoOperations.collection(domainClass);
371371
return arangoOperations.query(query, bindVars,
372372
pageable != null && pageable.isPaged() ? new AqlQueryOptions().fullCount(true) : null, domainClass);
373373
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* DISCLAIMER
3+
*
4+
* Copyright 2017 ArangoDB GmbH, Cologne, Germany
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
*/
20+
21+
package com.arangodb.springframework;
22+
23+
import java.util.ArrayList;
24+
import java.util.Collection;
25+
26+
import org.springframework.context.annotation.ComponentScan;
27+
import org.springframework.context.annotation.Configuration;
28+
import org.springframework.core.convert.converter.Converter;
29+
30+
import com.arangodb.ArangoDB;
31+
import com.arangodb.springframework.annotation.EnableArangoRepositories;
32+
import com.arangodb.springframework.config.ArangoConfiguration;
33+
import com.arangodb.springframework.core.mapping.CustomMappingTest;
34+
35+
/**
36+
*
37+
* @author Paulo Ferreira
38+
*/
39+
@Configuration
40+
@ComponentScan("com.arangodb.springframework.component")
41+
@EnableArangoRepositories(basePackages = {
42+
"com.arangodb.springframework.repository",
43+
"com.arangodb.springframework.example.polymorphic.repository",
44+
"com.arangodb.springframework.debug.repository"},
45+
namedQueriesLocation = "classpath*:arango-named-queries-test.properties")
46+
public class ArangoMultiTenancyRepositoryTestConfiguration implements ArangoConfiguration {
47+
48+
public static final String DB = "spring-test-db";
49+
50+
@Override
51+
public ArangoDB.Builder arango() {
52+
return new ArangoDB.Builder();
53+
}
54+
55+
@Override
56+
public String database() {
57+
return DB + "#{tenantProvider.getId()}";
58+
}
59+
60+
@Override
61+
public Collection<Converter<?, ?>> customConverters() {
62+
final Collection<Converter<?, ?>> converters = new ArrayList<>();
63+
converters.add(new CustomMappingTest.CustomVPackReadTestConverter());
64+
converters.add(new CustomMappingTest.CustomVPackWriteTestConverter());
65+
converters.add(new CustomMappingTest.CustomDBEntityReadTestConverter());
66+
converters.add(new CustomMappingTest.CustomDBEntityWriteTestConverter());
67+
return converters;
68+
}
69+
70+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* DISCLAIMER
3+
*
4+
* Copyright 2018 ArangoDB GmbH, Cologne, Germany
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
*/
20+
21+
package com.arangodb.springframework.core.mapping;
22+
23+
import static org.hamcrest.Matchers.is;
24+
import static org.junit.Assert.assertThat;
25+
26+
import java.util.Optional;
27+
28+
import org.junit.After;
29+
import org.junit.Before;
30+
import org.junit.Test;
31+
import org.junit.runner.RunWith;
32+
import org.springframework.beans.factory.annotation.Autowired;
33+
import org.springframework.data.domain.Example;
34+
import org.springframework.test.context.ContextConfiguration;
35+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
36+
37+
import com.arangodb.springframework.ArangoMultiTenancyRepositoryTestConfiguration;
38+
import com.arangodb.springframework.component.TenantProvider;
39+
import com.arangodb.springframework.core.ArangoOperations;
40+
import com.arangodb.springframework.repository.IdTestRepository;
41+
import com.arangodb.springframework.testdata.IdTestEntity;
42+
43+
/**
44+
* @author Paulo Ferreira
45+
*
46+
*/
47+
@RunWith(SpringJUnit4ClassRunner.class)
48+
@ContextConfiguration(classes = { ArangoMultiTenancyRepositoryTestConfiguration.class })
49+
public class MultiTenancyDBLevelRepositoryTest {
50+
51+
private static final String TENANT00 = "tenant00";
52+
53+
@Autowired
54+
protected ArangoOperations template;
55+
56+
@Autowired
57+
TenantProvider tenantProvider;
58+
59+
@Autowired
60+
IdTestRepository<String> idTestRepository;
61+
62+
/*
63+
* For some reason, findAll already creates the collection by itself
64+
*/
65+
66+
@Test
67+
public void dbFindAll() {
68+
tenantProvider.setId(TENANT00);
69+
assertThat(idTestRepository.findAll().iterator().hasNext(), is(false));
70+
}
71+
72+
@Test
73+
public void dbFindOne() {
74+
tenantProvider.setId(TENANT00);
75+
IdTestEntity<String> entity = new IdTestEntity<>();
76+
entity.setId("MyId");
77+
Example<IdTestEntity<String>> example = Example.of(entity);
78+
Optional<IdTestEntity<String>> result = idTestRepository.findOne(example);
79+
assertThat(result.isPresent(), is(false));
80+
}
81+
82+
@Before
83+
@After
84+
public void cleanup() {
85+
tenantProvider.setId(TENANT00);
86+
template.dropDatabase();
87+
}
88+
89+
}

0 commit comments

Comments
 (0)