[JAVA-32759] Split-or-move-spring-boot-persistence-mongodb (#16392)

This commit is contained in:
vunamtien
2024-04-14 22:04:50 +07:00
committed by GitHub
parent 2685d31fd1
commit 684ee81ed1
18 changed files with 65 additions and 31 deletions
@@ -0,0 +1,73 @@
package com.baeldung.mongodb.dbref;
import com.baeldung.mongodb.dbref.model.Person;
import com.baeldung.mongodb.dbref.model.Pet;
import com.baeldung.mongodb.dbref.repository.PersonRepository;
import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DBObject;
import com.mongodb.DBRef;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
@RunWith(SpringRunner.class)
@SpringBootTest
@DirtiesContext
public class DbRefIntegrationTest {
@Autowired
PersonRepository personRepository;
@Autowired
private MongoTemplate mongoTemplate;
@Test
public void givenPetsAndPersonInDatabase_whenListPersons_thenReferenceIsFetched() {
// given
DBObject catInDatabase = BasicDBObjectBuilder.start()
.add("name", "Loki")
.get();
DBObject dogInDatabase = BasicDBObjectBuilder.start()
.add("name", "Max")
.get();
mongoTemplate.save(catInDatabase, "Cat");
mongoTemplate.save(dogInDatabase, "Dog");
List<DBRef> petsReference = new ArrayList<DBRef>();
petsReference.add(new DBRef("Cat", catInDatabase.get("_id")));
petsReference.add(new DBRef("Dog", dogInDatabase.get("_id")));
DBObject personInDatabase = BasicDBObjectBuilder.start()
.add("name", "Bob")
.add("pets", petsReference)
.get();
mongoTemplate.save(personInDatabase, "Person");
// when
List<Person> persons = personRepository.findAll();
// then
assertThat(persons).hasSize(1);
Person person = persons.get(0);
assertEquals("Bob", person.getName());
List<Pet> pets = person.getPets();
assertThat(pets).hasSize(2);
assertThat(pets).anyMatch(pet -> "Loki".equals(pet.getName()));
assertThat(pets).anyMatch(pet -> "Max".equals(pet.getName()));
}
}
@@ -0,0 +1,57 @@
package com.baeldung.zoneddatetime;
import com.baeldung.zoneddatetime.config.MongoConfig;
import com.baeldung.zoneddatetime.model.Action;
import com.baeldung.zoneddatetime.repository.ActionRepository;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
/**
*
* This test requires:
* * mongodb instance running on the environment
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MongoConfig.class)
public class ActionRepositoryLiveTest {
@Autowired
private MongoOperations mongoOps;
@Autowired
private ActionRepository actionRepository;
@Before
public void setup() {
if (!mongoOps.collectionExists(Action.class)) {
mongoOps.createCollection(Action.class);
}
}
@Test
public void givenSavedAction_TimeIsRetrievedCorrectly() {
String id = "testId";
ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC);
actionRepository.save(new Action(id, "click-action", now));
Action savedAction = actionRepository.findById(id).get();
Assert.assertEquals(now.withNano(0), savedAction.getTime().withNano(0));
}
@After
public void tearDown() {
mongoOps.dropCollection(Action.class);
}
}
@@ -0,0 +1 @@
spring.mongodb.embedded.version=4.4.9