Skip to content

Commit

Permalink
Merge pull request #3 from antkorwin/feature/export-dataset-1
Browse files Browse the repository at this point in the history
Feature/export dataset
  • Loading branch information
antkorwin authored Dec 7, 2018
2 parents c2791b7 + 0c0ccdb commit 1a0abe3
Show file tree
Hide file tree
Showing 30 changed files with 1,077 additions and 22 deletions.
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.antkorwin</groupId>
<artifactId>spring-test-mongo</artifactId>
<version>0.3-SNAPSHOT</version>
<version>0.5-SNAPSHOT</version>
<packaging>jar</packaging>

<name>spring-test-mongo</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class MongoPopulator {
* @param mongoTemplate connection to mongo database
* @param dataSetFileName path to the json dataset
*/
@Deprecated
public static void populate(MongoTemplate mongoTemplate, String dataSetFileName) {

Guard.check(dataSetFileName != null, InternalException.class, DATASET_FILE_NAME_IS_MANDATORY);
Expand Down Expand Up @@ -79,7 +80,8 @@ private static void internalPopulate(MongoTemplate mongoTemplate, Map<String, Ob
}


private static void populateOneDocumentCollection(MongoTemplate mongoTemplate, Class<?> documentClassType,
private static void populateOneDocumentCollection(MongoTemplate mongoTemplate,
Class<?> documentClassType,
List<?> recordCollection) {
recordCollection.forEach(document -> {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.antkorwin.springtestmongo.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Created on 03.12.2018.
*
* You can use this annotation in tests to
* generate files with data sets.
* All document collections from current database
* will be store in export file, after test execution.
*
* @author Korovin Anatoliy
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ExportMongoDataSet {

/**
* @return path to the export file
*/
String outputFile();
}
22 changes: 22 additions & 0 deletions src/main/java/com/antkorwin/springtestmongo/internal/DataSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.antkorwin.springtestmongo.internal;

import java.util.List;
import java.util.Map;

/**
* Read a map from some kind of source
*
* @author Korovin Anatoliy
*/
interface DataSet {

/**
* Read the data set from some kind of source
*
* @return map with the data set,
* map looks like this:
* "org.package....FirstDocument" : [FirstDocument doc1, FirstDocument doc2, FirstDocument doc3],
* "org.package....SecondDocument" : [SecondDocument Doc1, SecondDocument Doc2, SecondDocument Doc3]
*/
Map<String, List<?>> read();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.antkorwin.springtestmongo.internal;

/**
* Export data from MongoDb --> {@link DataSet}
*
* @author Korovin Anatoliy
*/
interface DataSetExport {

/**
* export data set from MongoDb to the {@link DataSet} instance
*
* @return {@link DataSet}
*/
DataSet export();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.antkorwin.springtestmongo.internal;

/**
* Import data to MongoDb from {@link DataSet}
*
* @author Korovin Anatoliy
*/
interface DataSetImport {

/**
* Import {@link DataSet} to the MongoDb
*
* @param dataSet source data set
*/
void importFrom(DataSet dataSet);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.antkorwin.springtestmongo.internal;

import com.antkorwin.commonutils.exceptions.InternalException;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
* Save text in a file.
*
* @author Korovin Anatoliy
*/
class ExportFile {

private final Text text;

ExportFile(Text text) {
this.text = text;
}

/**
* save text in a file, if the file does not exist
* then it will create with all folders in the path.
*
* @param fileName path to file
*/
public void write(String fileName) {
String textData = this.text.read();
try {
Path path = Paths.get(fileName);
Files.createDirectories(path.getParent());
Files.write(path, textData.getBytes());
}
catch (IOException e) {
e.printStackTrace();
throw new InternalException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.antkorwin.springtestmongo.internal;

import com.antkorwin.commonutils.exceptions.InternalException;
import com.antkorwin.commonutils.validation.Guard;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import static com.antkorwin.springtestmongo.errorinfo.MongoDbErrorInfo.READ_DATASETS_FILE_ERROR;
import static sun.management.AgentConfigurationError.FILE_NOT_FOUND;

/**
* Load text from a file.
*
* @author Korovin Anatoliy
*/
class ImportFile implements Text {

private final String fileName;

ImportFile(String fileName) {
this.fileName = fileName;
}

@Override
public String read() {

InputStream inputStream = getResourceStream();
Guard.check(inputStream != null, InternalException.class, FILE_NOT_FOUND);

try {
return IOUtils.toString(inputStream, StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
throw new InternalException(READ_DATASETS_FILE_ERROR, e);
}
}

private InputStream getResourceStream() {
return ImportFile.class.getClass()
.getResourceAsStream(this.fileName);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.antkorwin.springtestmongo.internal;

import com.antkorwin.commonutils.exceptions.InternalException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

/**
* Convert object to {@link Text} in JSON format.
*
* @author Korovin Anatoliy
*/
class JsonExport implements Text {

private final DataSet dataSet;
private final ObjectMapper objectMapper;

JsonExport(DataSet dataSet) {
this.objectMapper = new ObjectMapper();
this.objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
this.dataSet = dataSet;
}

@Override
public String read() {
try {
return objectMapper.writeValueAsString(dataSet.read());
} catch (JsonProcessingException e) {
e.printStackTrace();
throw new InternalException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.antkorwin.springtestmongo.internal;

import com.antkorwin.commonutils.exceptions.InternalException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
* Convert a {@link Text} (in JSON format) to the {@link DataSet}
*
* @author Korovin Anatoliy
*/
class JsonImport implements DataSet {

private final Text text;
private final ObjectMapper objectMapper;

JsonImport(Text text) {
this.objectMapper = new ObjectMapper();
this.objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
this.text = text;
}

@Override
public Map<String, List<?>> read() {

String content = text.read();
try {
return objectMapper.readValue(content, new TypeReference<Map<String, List<?>>>() {});
} catch (IOException e) {
e.printStackTrace();
throw new InternalException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.antkorwin.springtestmongo.internal;

import com.antkorwin.commonutils.exceptions.InternalException;
import com.antkorwin.commonutils.validation.Guard;
import org.bson.Document;
import org.springframework.data.mongodb.core.MongoTemplate;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.antkorwin.springtestmongo.errorinfo.MongoDbErrorInfo.MONGO_TEMPLATE_IS_MANDATORY;

/**
* Export a data from MongoDb to {@link DataSet}
*
* @author Korovin Anatoliy
*/
class MongoDataExport implements DataSet {

private final MongoTemplate mongoTemplate;

MongoDataExport(MongoTemplate mongoTemplate) {
Guard.check(mongoTemplate != null, InternalException.class, MONGO_TEMPLATE_IS_MANDATORY);
this.mongoTemplate = mongoTemplate;
}

@Override
public Map<String, List<?>> read() {
Map<String, List<?>> map = new HashMap<>();

for (String name : mongoTemplate.getCollectionNames()) {
map.put(getEntityClassName(name), getDataSet(name));
}

return map;
}

private List<?> getDataSet(String collectionName) {

Document first = mongoTemplate.getCollection(collectionName)
.find(Document.class)
.first();
if (first == null) {
return Collections.emptyList();
}

try {
String className = (String) first.get("_class");
Class<?> aClass = Class.forName(className);
return mongoTemplate.findAll(aClass);
}
catch (ClassNotFoundException e) {
e.printStackTrace();
throw new InternalException(e);
}
}

private String getEntityClassName(String collectionName) {

Document first = mongoTemplate.getCollection(collectionName)
.find(Document.class)
.first();

if (first == null) return collectionName;

Object classType = first.get("_class");
return (classType != null) ? (String) classType : collectionName;
}
}
Loading

0 comments on commit 1a0abe3

Please sign in to comment.