3 import java.io.IOException;
6 import java.util.Objects;
7 import java.util.Scanner;
9 import org.bson.Document;
11 import com.mongodb.client.MongoDatabase;
13 import dst.ass1.jpa.util.Constants;
15 public class DocumentTestData implements IDocumentTestData {
17 private String documentResource;
19 public DocumentTestData() {
20 this("documents.json");
23 public DocumentTestData(String documentResource) {
24 this.documentResource = documentResource;
27 @SuppressWarnings("unchecked")
29 public void insertTestData(MongoDatabase db) throws IOException {
30 URL resource = Objects.requireNonNull(getDocumentsResource());
32 String testDataJson = readFully(resource);
33 List<Document> documents = Document.parse(testDataJson).get("documents", List.class);
34 db.getCollection(Constants.COLL_MATERIAL_DATA).insertMany(documents);
37 private URL getDocumentsResource() {
38 return getClass().getClassLoader().getResource(documentResource);
41 private String readFully(URL resource) throws IOException {
42 try (Scanner scanner = new Scanner(resource.openStream())) {
43 return scanner.useDelimiter("\\Z").next();