Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fix, also fix regexp cast for non string fields #71

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.jupiter-tools</groupId>
<artifactId>spring-test-mongo</artifactId>
<version>0.15</version>
<version>0.16</version>
<packaging>jar</packaging>

<name>spring-test-mongo</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public boolean match(Object originValue, Object comparableValue) {
String cmpValue = (String) comparableValue;
cmpValue = cmpValue.replaceFirst("regex:", "").trim();
Pattern pattern = Pattern.compile(cmpValue);
Matcher matcher = pattern.matcher((String) originValue);
Matcher matcher = pattern.matcher(originValue.toString());
return matcher.matches();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.jupiter.tools.spring.test.mongo.internal.DataSet;
import com.jupiter.tools.spring.test.mongo.internal.exportdata.scanner.DocumentClasses;
import com.jupiter.tools.spring.test.mongo.internal.geo.GeoJsonSerializationModule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.mongodb.core.MongoTemplate;

import java.util.HashMap;
Expand All @@ -25,6 +27,7 @@ public class MongoDataExport implements DataSet {
private final MongoTemplate mongoTemplate;
private final ObjectMapper objectMapper;
private final DocumentClasses documentClasses;
private final Logger log = LoggerFactory.getLogger(MongoDataExport.class);

public MongoDataExport(MongoTemplate mongoTemplate, DocumentClasses documentClasses) {
Guard.check(mongoTemplate != null, InternalException.class, MONGO_TEMPLATE_IS_MANDATORY);
Expand All @@ -40,10 +43,15 @@ public Map<String, List<Map<String, Object>>> read() {
Map<String, List<Map<String, Object>>> map = new HashMap<>();

for (String name : mongoTemplate.getCollectionNames()) {
List<Map<String, Object>> dataSet = getDataSet(name);
if (documentClasses.hasCollection(name)) {

if (!dataSet.isEmpty())
map.put(documentClasses.getDocumentClassName(name), dataSet);
List<Map<String, Object>> dataSet = getDataSet(name);

if (dataSet != null)
map.put(documentClasses.getDocumentClassName(name), dataSet);
} else {
log.warn("Not found document class for collection {}", name);
}
}

return map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ public Class<?> put(String key, Class<?> value) {
public Class<?> get(Object key) {
return super.get(((String) key).toLowerCase());
}

@Override
public boolean containsKey(Object key) {
return super.containsKey(((String) key).toLowerCase());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ public Class<?> getDocumentClass(String collectionName) {
public String getDocumentClassName(String collectionName) {
return documents.get(collectionName).getCanonicalName();
}

public boolean hasCollection(String collectionName) {
return documents.containsKey(collectionName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void testWithEmptyDataBaseAndNotEmptyDataSet() {
Error error = Assertions.assertThrows(Error.class, () -> {
new MongoDbTest(mongoTemplate).expect("/dataset/internal/expected_dataset.json");
});
assertThat(error.getMessage()).contains("Not equal document collections");
assertThat(error.getMessage()).contains("expected 2 but found 0 - com.jupiter.tools.spring.test.mongo.Bar entities");
}

@Test
Expand Down Expand Up @@ -235,6 +235,16 @@ void multipleRegexp() {
// Act
new MongoDbTest(mongoTemplate).expect("/dataset/internal/expect/multiple_regex.json");
}

@Test
@MongoDataSet(cleanBefore = true, cleanAfter = true)
void numberFieldRegexp() {
// Arrange
Foo foo = new Foo("1", new Date(), 1);
mongoTemplate.save(foo);
// Act
new MongoDbTest(mongoTemplate).expect("/dataset/internal/expect/number_field_regex.json");
}
}

@Nested
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import java.nio.file.Files;

import com.jupiter.tools.spring.test.mongo.annotation.ExportMongoDataSet;
import com.jupiter.tools.spring.test.mongo.annotation.MongoDataSet;
import com.jupiter.tools.spring.test.mongo.internal.MongoDbTest;
import lombok.Data;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.junit.ClassRule;
Expand Down Expand Up @@ -62,6 +64,7 @@ private String getExpectedJson() throws IOException {
};

@Test
@MongoDataSet(cleanBefore = true, cleanAfter = true)
@ExportMongoDataSet(outputFile = OUTPUT_FILE_NAME)
public void testExportDataSet() throws Exception {
new MongoDbTest(mongoTemplate).importFrom(INPUT_DATA_SET_FILE);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"com.jupiter.tools.spring.test.mongo.Foo": [
{
"counter": "regex:.+"
}
]
}