Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow to include / exclude write operations on a document (#639)
added flags to repository annotation (@Mongo.Repository) which ...
- Loading branch information
Showing
8 changed files
with
258 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
mongo/test/org/immutables/mongo/fixture/flags/FlagsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,83 @@ | |||
package org.immutables.mongo.fixture.flags; | |||
|
|||
import com.google.common.base.Predicate; | |||
import com.google.common.collect.FluentIterable; | |||
import org.immutables.mongo.fixture.MongoContext; | |||
import org.junit.Rule; | |||
import org.junit.Test; | |||
|
|||
import java.lang.reflect.Method; | |||
import java.lang.reflect.Modifier; | |||
import java.util.Arrays; | |||
import java.util.Set; | |||
|
|||
import static org.immutables.check.Checkers.check; | |||
|
|||
/** | |||
* Ensures read / write / index methods are present / absent from generated repositories after some flags have been | |||
* set using {@link org.immutables.mongo.Mongo.Repository} annotation. | |||
*/ | |||
public class FlagsTest { | |||
|
|||
@Rule | |||
public final MongoContext context = MongoContext.create(); | |||
|
|||
@Test | |||
public void standard() throws Exception { | |||
StandardRepository repo = new StandardRepository(context.setup()); | |||
|
|||
repo.index().withName().ensure().getUnchecked(); | |||
|
|||
repo.findAll().deleteAll().getUnchecked(); | |||
repo.findAll().deleteFirst().getUnchecked(); | |||
repo.findAll().andModifyFirst().setName("foo").update(); | |||
repo.findAll().andReplaceFirst(ImmutableStandard.builder().id("id1").build()).upsert().getUnchecked(); | |||
|
|||
} | |||
|
|||
@Test | |||
public void readonly() throws Exception { | |||
ReadonlyRepository repo = new ReadonlyRepository(context.setup()); | |||
|
|||
// index method should be present. Make it compile time check | |||
repo.index().withName().ensure().getUnchecked(); | |||
|
|||
// ensure write methods are missing | |||
check(methodsByName(ReadonlyRepository.class, "andModifyFirst")).isEmpty(); | |||
check(methodsByName(ReadonlyRepository.class, "andReplaceFirst")).isEmpty(); | |||
check(methodsByName(ReadonlyRepository.class, "update")).isEmpty(); | |||
check(methodsByName(ReadonlyRepository.class, "upsert")).isEmpty(); | |||
check(methodsByName(ReadonlyRepository.class, "insert")).isEmpty(); | |||
|
|||
check(methodsByName(ReadonlyRepository.Finder.class, "deleteFirst")).isEmpty(); | |||
check(methodsByName(ReadonlyRepository.Finder.class, "deleteAll")).isEmpty(); | |||
check(methodsByName(ReadonlyRepository.Finder.class, "delete")).isEmpty(); | |||
check(methodsByName(ReadonlyRepository.Finder.class, "andModifyFirst")).isEmpty(); | |||
check(methodsByName(ReadonlyRepository.Finder.class, "andReplaceFirst")).isEmpty(); | |||
} | |||
|
|||
@Test | |||
public void noIndex() throws Exception { | |||
NoIndexRepository repo = new NoIndexRepository(context.setup()); | |||
|
|||
repo.find(repo.criteria().id("foo")).fetchAll().getUnchecked(); | |||
check(methodsByName(NoIndexRepository.class, "index")).isEmpty(); | |||
} | |||
|
|||
/** | |||
* Gets all (public) methods by name using reflection | |||
*/ | |||
private static Set<Method> methodsByName(Class<?> type, final String name) { | |||
return FluentIterable | |||
.from(Arrays.asList(type.getDeclaredMethods())) | |||
.append(Arrays.asList(type.getMethods())) | |||
.filter(new Predicate<Method>() { | |||
@Override | |||
public boolean apply(Method method) { | |||
return Modifier.isPublic(method.getModifiers()) && method.getName().equals(name); | |||
} | |||
}) | |||
.toSet(); | |||
} | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,36 @@ | |||
package org.immutables.mongo.fixture.flags; | |||
|
|||
import com.google.common.base.Optional; | |||
import org.immutables.mongo.Mongo; | |||
import org.immutables.value.Value; | |||
|
|||
public interface Repo { | |||
|
|||
@Mongo.Id | |||
String id(); | |||
|
|||
Optional<String> name(); | |||
|
|||
@Mongo.Repository | |||
@Value.Immutable | |||
interface Standard extends Repo { | |||
|
|||
} | |||
|
|||
/** | |||
* Repository without delete / andModifyFirst / andReplaceFirst methods. | |||
*/ | |||
@Mongo.Repository(readonly = true) | |||
@Value.Immutable | |||
interface Readonly extends Repo { | |||
|
|||
} | |||
|
|||
|
|||
@Mongo.Repository(index = false, readonly = true) | |||
@Value.Immutable | |||
interface NoIndex extends Repo { | |||
|
|||
} | |||
|
|||
} |
20 changes: 20 additions & 0 deletions
20
mongo/test/org/immutables/mongo/fixture/flags/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,20 @@ | |||
/* | |||
Copyright 2013-2015 Immutables Authors and Contributors | |||
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. | |||
*/ | |||
|
|||
/** | |||
* Tests repositories with various flags (readonly, index etc.) | |||
*/ | |||
package org.immutables.mongo.fixture.flags; |
Oops, something went wrong.