DATAMONGO-1819 - Polishing.

Use native field names for NamedMongoScript query instead of relying on metadata-based mapping as NamedMongoScript is considered a simple top-level type. Migrate tests to AssertJ.

Related pull request: #513.
This commit is contained in:
Mark Paluch
2017-11-17 13:48:56 +01:00
parent dc6bbbfd13
commit e0237174ad
3 changed files with 22 additions and 24 deletions

View File

@@ -43,9 +43,10 @@ import com.mongodb.client.MongoDatabase;
/**
* Default implementation of {@link ScriptOperations} capable of saving and executing {@link ServerSideJavaScript}.
*
*
* @author Christoph Strobl
* @author Oliver Gierke
* @author Mark Paluch
* @since 1.7
*/
class DefaultScriptOperations implements ScriptOperations {
@@ -141,7 +142,7 @@ class DefaultScriptOperations implements ScriptOperations {
Assert.hasText(scriptName, "ScriptName must not be null or empty!");
return mongoOperations.exists(query(where("name").is(scriptName)), NamedMongoScript.class, SCRIPT_COLLECTION_NAME);
return mongoOperations.exists(query(where("_id").is(scriptName)), NamedMongoScript.class, SCRIPT_COLLECTION_NAME);
}
/*
@@ -190,7 +191,7 @@ class DefaultScriptOperations implements ScriptOperations {
* Generate a valid name for the {@literal JavaScript}. MongoDB requires an id of type String for scripts. Calling
* scripts having {@link ObjectId} as id fails. Therefore we create a random UUID without {@code -} (as this won't
* work) an prefix the result with {@link #SCRIPT_NAME_PREFIX}.
*
*
* @return
*/
private static String generateScriptName() {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2011 the original author or authors.
* Copyright 2011-2017 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.
@@ -15,8 +15,7 @@
*/
package org.springframework.data.mongodb.core;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import org.bson.Document;
import org.junit.Before;
@@ -35,9 +34,10 @@ import com.mongodb.client.MongoCollection;
/**
* Integration test for {@link MongoTemplate}.
*
*
* @author Oliver Gierke
* @author Thomas Risberg
* @author Mark Paluch
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:template-mapping.xml")
@@ -55,19 +55,17 @@ public class MongoTemplateMappingTests {
}
@Test
public void insertsEntityCorrectly1() throws Exception {
public void insertsEntityCorrectly1() {
addAndRetrievePerson(template1);
checkPersonPersisted(template1);
}
@Test
public void insertsEntityCorrectly2() throws Exception {
public void insertsEntityCorrectly2() {
addAndRetrievePerson(template2);
checkPersonPersisted(template2);
}
private void addAndRetrievePerson(MongoTemplate template) {
@@ -76,15 +74,15 @@ public class MongoTemplateMappingTests {
template.insert(person);
Person result = template.findById(person.getId(), Person.class);
assertThat(result.getFirstName(), is("Oliver"));
assertThat(result.getAge(), is(25));
assertThat(result.getFirstName()).isEqualTo("Oliver");
assertThat(result.getAge()).isEqualTo(25);
}
private void checkPersonPersisted(MongoTemplate template) {
template.execute(Person.class, new CollectionCallback<Object>() {
public Object doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException {
Document document = collection.find(new Document()).first();
assertThat((String) document.get("name"), is("Oliver"));
assertThat((String) document.get("name")).isEqualTo("Oliver");
return null;
}
});

View File

@@ -15,8 +15,7 @@
*/
package org.springframework.data.mongodb.core.mapping;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import java.util.AbstractMap;
import java.util.Collections;
@@ -65,14 +64,14 @@ public class MongoMappingContextUnitTests {
public void doesNotReturnPersistentEntityForMongoSimpleType() {
MongoMappingContext context = new MongoMappingContext();
assertThat(context.getPersistentEntity(DBRef.class), is(nullValue()));
assertThat(context.getPersistentEntity(DBRef.class)).isNull();
}
@Test // DATAMONGO-638
public void doesNotCreatePersistentEntityForAbstractMap() {
MongoMappingContext context = new MongoMappingContext();
assertThat(context.getPersistentEntity(AbstractMap.class), is(nullValue()));
assertThat(context.getPersistentEntity(AbstractMap.class)).isNull();
}
@Test // DATAMONGO-607
@@ -88,7 +87,7 @@ public class MongoMappingContextUnitTests {
});
MongoPersistentEntity<?> entity = context.getRequiredPersistentEntity(Person.class);
assertThat(entity.getRequiredPersistentProperty("firstname").getFieldName(), is("FIRSTNAME"));
assertThat(entity.getRequiredPersistentProperty("firstname").getFieldName()).isEqualTo("FIRSTNAME");
}
@Test // DATAMONGO-607
@@ -119,8 +118,8 @@ public class MongoMappingContextUnitTests {
MongoMappingContext context = new MongoMappingContext();
BasicMongoPersistentEntity<?> pe = context.getRequiredPersistentEntity(ClassWithImplicitId.class);
assertThat(pe, is(not(nullValue())));
assertThat(pe.isIdProperty(pe.getRequiredPersistentProperty("id")), is(true));
assertThat(pe).isNotNull();
assertThat(pe.isIdProperty(pe.getRequiredPersistentProperty("id"))).isTrue();
}
@Test // DATAMONGO-688
@@ -129,8 +128,8 @@ public class MongoMappingContextUnitTests {
MongoMappingContext context = new MongoMappingContext();
BasicMongoPersistentEntity<?> pe = context.getRequiredPersistentEntity(ClassWithExplicitId.class);
assertThat(pe, is(not(nullValue())));
assertThat(pe.isIdProperty(pe.getRequiredPersistentProperty("myId")), is(true));
assertThat(pe).isNotNull();
assertThat(pe.isIdProperty(pe.getRequiredPersistentProperty("myId"))).isTrue();
}
@Test // DATAMONGO-688
@@ -138,7 +137,7 @@ public class MongoMappingContextUnitTests {
MongoMappingContext context = new MongoMappingContext();
BasicMongoPersistentEntity<?> pe = context.getRequiredPersistentEntity(ClassWithExplicitIdAndImplicitId.class);
assertThat(pe, is(not(nullValue())));
assertThat(pe).isNotNull();
}
@Test(expected = MappingException.class) // DATAMONGO-688