-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from antkorwin/feature/export-dataset-1
Feature/export dataset
- Loading branch information
Showing
30 changed files
with
1,077 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/main/java/com/antkorwin/springtestmongo/annotation/ExportMongoDataSet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
22
src/main/java/com/antkorwin/springtestmongo/internal/DataSet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/antkorwin/springtestmongo/internal/DataSetExport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/antkorwin/springtestmongo/internal/DataSetImport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/com/antkorwin/springtestmongo/internal/ExportFile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/com/antkorwin/springtestmongo/internal/ImportFile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/antkorwin/springtestmongo/internal/JsonExport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/com/antkorwin/springtestmongo/internal/JsonImport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
src/main/java/com/antkorwin/springtestmongo/internal/MongoDataExport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.