Java tutorial
/* * Copyright 2014 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 * * 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 com.carlomicieli.jtrains.infrastructure.mongo; import com.carlomicieli.jtrains.test.AbstractJongoRepositoryJUnit4Tests; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import org.bson.types.ObjectId; import org.jongo.Jongo; import org.jongo.MongoCollection; import org.junit.Before; import org.junit.Test; import javax.validation.Validator; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; import java.util.stream.Stream; import static org.assertj.core.api.Assertions.assertThat; /** * @author Carlo Micieli */ public class JongoRepositoryTests extends AbstractJongoRepositoryJUnit4Tests { @Before public void beforeTest() { MongoCollection myObjs = jongo().getCollection("myobjs"); myObjs.insert(MyObj.of(objectId("507c7f79bcf86cd7994f6c0d"), "one", 1)); myObjs.insert(MyObj.of(objectId("507c7f79bcf86cd7994f6c0e"), "two", 2)); myObjs.insert(MyObj.of(objectId("507c7f79bcf86cd7994f6c0f"), "three", 3)); } @Test public void shouldReturnCollectionName() { DocumentsRepository repo = repo(jongo()); assertThat(repo.getCollectionName()).isEqualTo("myobjs"); } @Test public void shouldFindDocuments() { Stream<MyObj> resultStream = repo(jongo()).find(myObjs -> myObjs.find("{'value': 2}")); List<MyObj> results = resultStream.collect(Collectors.toList()); assertThat(results).hasSize(1); } @Test public void shouldFindOneDocument() { Optional<MyObj> result = repo(jongo()).findOne(myObjs -> myObjs.findOne("{'value': 2}")); assertThat(result.orElse(null)).isNotNull(); } @Test public void shouldFindOneDocumentById() { Optional<MyObj> result = repo(jongo()).findById(objectId("507c7f79bcf86cd7994f6c0e")); assertThat(result.orElse(null)).isNotNull(); } @Test public void shouldSaveNewDocuments() { MyObj newObj = MyObj.of(objectId("507c7f79bcf86cd7994f6c0a"), "four", 4); repo(jongo()).save(newObj); long count = count(jongo(), "myobjs", objectId("507c7f79bcf86cd7994f6c0a")); assertThat(count).isEqualTo(1); } @Test public void shouldDeleteDocuments() { MyObj o = MyObj.of(objectId("507c7f79bcf86cd7994f6c0f"), "three", 3); repo(jongo()).delete(o, MyObj::getId); long count = count(jongo(), "myobjs", objectId("507c7f79bcf86cd7994f6c0f")); assertThat(count).isEqualTo(0); } private static class MyObj { ObjectId _id; String name; int value; public ObjectId getId() { return _id; } @JsonCreator MyObj(@JsonProperty("_id") ObjectId _id, @JsonProperty("name") String name, @JsonProperty("value") int value) { this._id = _id; this.name = name; this.value = value; } static MyObj of(ObjectId id, String name, int value) { return new MyObj(id, name, value); } } private DocumentsRepository repo(Jongo jongo) { return new DocumentsRepository(jongo); } private static class DocumentsRepository extends AbstractJongoRepository<MyObj> { public DocumentsRepository(Jongo jongo) { super(jongo, MyObj.class); } @Override protected Optional<Validator> getValidator() { return Optional.empty(); } } }