-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cd091ac
commit 9e0b25f
Showing
107 changed files
with
6,033 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
|
||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>at.ac.tuwien.infosys.dst</groupId> | ||
<artifactId>dst</artifactId> | ||
<version>2023.1</version> | ||
<relativePath>..</relativePath> | ||
</parent> | ||
|
||
<artifactId>ass1-doc</artifactId> | ||
|
||
<packaging>jar</packaging> | ||
|
||
<name>DST :: Assignment 1 :: Document DB</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>at.ac.tuwien.infosys.dst</groupId> | ||
<artifactId>ass1-jpa</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.mongodb</groupId> | ||
<artifactId>mongodb-driver</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>de.flapdoodle.embed</groupId> | ||
<artifactId>de.flapdoodle.embed.mongo</artifactId> | ||
</dependency> | ||
</dependencies> | ||
</project> |
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,14 @@ | ||
package dst.ass1.doc; | ||
|
||
import org.bson.Document; | ||
|
||
import java.util.List; | ||
|
||
public interface IDocumentQuery { | ||
|
||
List<Document> getAverageOpeningHoursOfRestaurants(); | ||
|
||
List<Document> findDocumentsByNameWithinPolygon(String name, List<List<Double>> polygon); | ||
|
||
List<Document> findDocumentsByType(String type); | ||
} |
11 changes: 11 additions & 0 deletions
11
ass1-doc/src/main/java/dst/ass1/doc/IDocumentRepository.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,11 @@ | ||
package dst.ass1.doc; | ||
|
||
import dst.ass1.jpa.model.ILocation; | ||
|
||
import java.util.Map; | ||
|
||
public interface IDocumentRepository { | ||
|
||
void insert(ILocation location, Map<String, Object> locationProperties); | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
ass1-doc/src/main/java/dst/ass1/doc/IDocumentServiceFactory.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,11 @@ | ||
package dst.ass1.doc; | ||
|
||
import com.mongodb.client.MongoDatabase; | ||
|
||
public interface IDocumentServiceFactory { | ||
|
||
IDocumentQuery createDocumentQuery(MongoDatabase db); | ||
|
||
IDocumentRepository createDocumentRepository(); | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
ass1-doc/src/main/java/dst/ass1/doc/impl/DocumentServiceFactory.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,21 @@ | ||
package dst.ass1.doc.impl; | ||
|
||
import com.mongodb.client.MongoDatabase; | ||
import dst.ass1.doc.IDocumentQuery; | ||
import dst.ass1.doc.IDocumentRepository; | ||
import dst.ass1.doc.IDocumentServiceFactory; | ||
|
||
public class DocumentServiceFactory implements IDocumentServiceFactory { | ||
|
||
@Override | ||
public IDocumentQuery createDocumentQuery(MongoDatabase db) { | ||
// TODO | ||
return null; | ||
} | ||
|
||
@Override | ||
public IDocumentRepository createDocumentRepository() { | ||
// TODO | ||
return null; | ||
} | ||
} |
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,45 @@ | ||
package dst.ass1.doc; | ||
|
||
import com.mongodb.client.MongoDatabase; | ||
import dst.ass1.jpa.util.Constants; | ||
import org.bson.Document; | ||
|
||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Scanner; | ||
|
||
public class DocumentTestData implements IDocumentTestData { | ||
|
||
private String documentResource; | ||
|
||
public DocumentTestData() { | ||
this("documents.json"); | ||
} | ||
|
||
public DocumentTestData(String documentResource) { | ||
this.documentResource = documentResource; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public void insertTestData(MongoDatabase db) throws IOException { | ||
URL resource = Objects.requireNonNull(getDocumentsResource()); | ||
|
||
String testDataJson = readFully(resource); | ||
List<Document> documents = Document.parse(testDataJson).get("documents", List.class); | ||
db.getCollection(Constants.COLL_LOCATION_DATA).insertMany(documents); | ||
} | ||
|
||
private URL getDocumentsResource() { | ||
return getClass().getClassLoader().getResource(documentResource); | ||
} | ||
|
||
private String readFully(URL resource) throws IOException { | ||
try (Scanner scanner = new Scanner(resource.openStream())) { | ||
return scanner.useDelimiter("\\Z").next(); | ||
} | ||
} | ||
|
||
} |
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,56 @@ | ||
package dst.ass1.doc; | ||
|
||
import de.flapdoodle.embed.mongo.config.MongodConfig; | ||
import de.flapdoodle.embed.mongo.config.Net; | ||
import de.flapdoodle.embed.mongo.distribution.IFeatureAwareVersion; | ||
import de.flapdoodle.embed.mongo.distribution.Version; | ||
import de.flapdoodle.embed.mongo.tests.MongodForTestsFactory; | ||
import org.junit.rules.ExternalResource; | ||
|
||
import java.io.IOException; | ||
import java.net.BindException; | ||
import java.net.ServerSocket; | ||
|
||
/** | ||
* JUnit rule that creates an in-memory instance of MongoDB using the flapdoodle Embedded MongoDB. | ||
*/ | ||
public class EmbeddedMongo extends ExternalResource { | ||
|
||
private MongodForTestsFactory mongod; | ||
|
||
@Override | ||
protected void before() throws Throwable { | ||
requirePort(); | ||
mongod = new MongodFactory(); // starts process in constructor | ||
} | ||
|
||
@Override | ||
protected void after() { | ||
if (mongod != null) { | ||
mongod.shutdown(); | ||
} | ||
} | ||
|
||
private void requirePort() throws IOException, IllegalStateException { | ||
try (ServerSocket ignore = new ServerSocket(27017)) { | ||
// ignore | ||
} catch (BindException e) { | ||
throw new IllegalStateException("Could not bind port 27017 which is necessary to run the MongoDB tests", e); | ||
} | ||
} | ||
|
||
public static class MongodFactory extends MongodForTestsFactory { | ||
|
||
public MongodFactory() throws IOException { | ||
super(Version.Main.V4_0); | ||
} | ||
|
||
@Override | ||
protected MongodConfig newMongodConfig(IFeatureAwareVersion version) throws IOException { | ||
return MongodConfig.builder() | ||
.net(new Net(27017, false)) | ||
.version(version) | ||
.build(); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
ass1-doc/src/test/java/dst/ass1/doc/IDocumentTestData.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,18 @@ | ||
package dst.ass1.doc; | ||
|
||
import com.mongodb.client.MongoDatabase; | ||
|
||
// DO NOT MODIFY THIS CLASS. | ||
|
||
/** | ||
* The IDocumentTestData interface works like the ITestData as introduced in ass1-jpa only for the {@link MongoService}. | ||
*/ | ||
public interface IDocumentTestData { | ||
/** | ||
* Inserts the data into the given MongoDatabase instance. | ||
* | ||
* @param db the mongo database instance | ||
* @throws Exception if the insertion failed for some reason | ||
*/ | ||
void insertTestData(MongoDatabase db) throws Exception; | ||
} |
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 dst.ass1.doc; | ||
|
||
import dst.ass1.jpa.model.ILocation; | ||
|
||
public class MockLocation implements ILocation { | ||
|
||
private Long id; | ||
private String name; | ||
private Long LocationId; | ||
|
||
|
||
@Override | ||
public Long getId() { | ||
return id; | ||
} | ||
|
||
@Override | ||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public Long getLocationId() { | ||
return LocationId; | ||
} | ||
|
||
@Override | ||
public void setLocationId(Long locationId) { | ||
LocationId = locationId; | ||
} | ||
} |
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,124 @@ | ||
package dst.ass1.doc; | ||
|
||
import com.mongodb.MongoClient; | ||
import com.mongodb.client.MongoCollection; | ||
import com.mongodb.client.MongoDatabase; | ||
import dst.ass1.doc.impl.DocumentServiceFactory; | ||
import dst.ass1.jpa.util.Constants; | ||
import org.bson.Document; | ||
import org.junit.rules.ExternalResource; | ||
|
||
import java.util.Map; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import java.util.stream.StreamSupport; | ||
|
||
// DO NOT MODIFY THIS CLASS. | ||
|
||
/** | ||
* The MongoService class is used as a JUnit rule, that fulfills the same tasks as the ORMService in ass1-jpa, and works | ||
* very similarly. | ||
*/ | ||
public class MongoService extends ExternalResource { | ||
|
||
private IDocumentTestData testData; | ||
private boolean insertTestData; | ||
private boolean clearTestData; | ||
|
||
private MongoClient mongoClient; | ||
private MongoDatabase mongoDatabase; | ||
|
||
private IDocumentServiceFactory documentServiceFactory; | ||
private IDocumentRepository documentRepository; | ||
private IDocumentQuery documentQuery; | ||
|
||
public MongoService() { | ||
this(null, false, true); | ||
} | ||
|
||
public MongoService(IDocumentTestData testData) { | ||
this(testData, true, true); | ||
} | ||
|
||
public MongoService(IDocumentTestData testData, boolean insertTestData, boolean clearTestData) { | ||
this.testData = testData; | ||
this.insertTestData = insertTestData; | ||
this.clearTestData = clearTestData; | ||
} | ||
|
||
public static Stream<Document> stream(MongoCollection<Document> collection) { | ||
return StreamSupport.stream(collection.find().spliterator(), false); | ||
} | ||
|
||
public static <T> Map<T, Document> idMap(MongoCollection<Document> collection, Function<Document, T> idFunction) { | ||
return stream(collection).collect(Collectors.toMap(idFunction, Function.identity())); | ||
} | ||
|
||
public MongoClient getMongoClient() { | ||
return mongoClient; | ||
} | ||
|
||
public MongoDatabase getMongoDatabase() { | ||
return mongoDatabase; | ||
} | ||
|
||
public IDocumentServiceFactory getDocumentServiceFactory() { | ||
return documentServiceFactory; | ||
} | ||
|
||
public IDocumentRepository getDocumentRepository() { | ||
return documentRepository; | ||
} | ||
|
||
public IDocumentQuery getDocumentQuery() { | ||
return documentQuery; | ||
} | ||
|
||
@Override | ||
protected void before() throws Throwable { | ||
setUpMongo(); | ||
|
||
if (insertTestData && testData != null) { | ||
insertData(testData); | ||
} | ||
} | ||
|
||
@Override | ||
protected void after() { | ||
try { | ||
if (clearTestData) { | ||
clearData(); | ||
} | ||
} finally { | ||
tearDownMongo(); | ||
} | ||
} | ||
|
||
private void setUpMongo() { | ||
mongoClient = new MongoClient("127.0.0.1"); | ||
mongoDatabase = mongoClient.getDatabase(Constants.MONGO_DB_NAME); | ||
|
||
documentServiceFactory = new DocumentServiceFactory(); | ||
documentRepository = documentServiceFactory.createDocumentRepository(); | ||
|
||
if (documentRepository == null) { | ||
throw new IllegalStateException("DocumentRepository returned from factory is null"); | ||
} | ||
|
||
documentQuery = documentServiceFactory.createDocumentQuery(mongoDatabase); | ||
} | ||
|
||
private void tearDownMongo() { | ||
mongoClient.close(); | ||
} | ||
|
||
private void insertData(IDocumentTestData testData) throws Exception { | ||
testData.insertTestData(getMongoDatabase()); | ||
} | ||
|
||
private void clearData() { | ||
getMongoDatabase().drop(); | ||
} | ||
|
||
} |
Oops, something went wrong.