Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
DevAtHeart committed Feb 23, 2021
0 parents commit 2ae74fe
Show file tree
Hide file tree
Showing 39 changed files with 2,074 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.gradle
build
out
.idea
*.iml
.gradle/gradle.properties
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2021 Wholegrain Software, Jimi Steidl <www.wholegrain-software.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
154 changes: 154 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
# Spring MongoDB Test

Spring MongoDB Test is a TestExecutionListener that simplifies seeding the database in spring-data-mongodb integration
tests.

## Dependency

### Gradle

```groovy
testImplementation 'com.wholegrainsoftware:spring-mongodb-test:{latest}'
```

### Maven

```xml

<dependency>
<groupId>com.wholegrainsoftware</groupId>
<artifactId>spring-mongodb-test</artifactId>
<version>{latest}</version>
<scope>test</scope>
</dependency>
```

## Setup

The following lines of code add the `MongoDBTestExecutionListener` to the SpringBootTest lifecycle.

```java
import com.wholegrainsoftware.springmongotest.MongoDBTestExecutionListener;

@SpringBootTest
@TestExecutionListeners(
value = {MongoDBTestExecutionListener.class},
mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS
)
public class MyIntegrationTest {
// ...
}
```

The database will be determined through one of the following properties:

```yaml
spring.data.monogdb.database: my_mongo_database
# or
spring.data.mongodb.uri: mongodb://localhost:27017/my_mongo_database
```
In this case everything will be executed against `my_mongo_database`.

## Tests with Documents

The `@Doc` annotation allows inserting documents into the given database.

A typical test will look like this:

```java
import com.wholegrainsoftware.springmongotest.Doc;
import com.wholegrainsoftware.springmongotest.MongoDBTest;
import org.junit.jupiter.api.Test;
@Doc(collection = "person", files = {"/documents/john-doe.bson"})
public class MyTest extends MyIntegrationTest {
@Test
@MongoDBTest
public void myTest() {
// ...
}
}
```

A bson file, located in `<projectRoot>/src/test/resources/documents/john-doe.bson` could look like this:

```bson
{
_id: ObjectId('6032473edd63e50bd3565171'),
firstName: 'John',
lastName: 'Doe',
_class: 'person'
}
```

For the following entity:

```java
@Document("person")
@TypeAlias("person")
public class Person {
@Id
private ObjectId id;
private String firstName;
private String lastName;
//...
}
```

You can even aggregate multiple `@Doc` annotations into custom annotations like:

```java
import com.wholegrainsoftware.springmongotest.Doc;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Doc(collection = "person", files = {"/documents/john-doe.bson"})
@Doc(collection = "department", files = {"/documents/sales.bson"})
public @interface InsertSalesDepartment {
}
```

## Tests with Files

The `@GridFsFile` annotation allows inserting files into GridFs of the given database.

A test will look like this:

```java
import com.wholegrainsoftware.springmongotest.GridFsFile;
import com.wholegrainsoftware.springmongotest.MongoDBTest;
import org.junit.jupiter.api.Test;
@GridFsFile(id = "60327c7f9189342c201e0e11", filePath = "/files/test.pdf")
public class MyGridFsTest extends MyIntegrationTest {
@Test
@MongoDBTest
public void myTest() {
// ...
}
}
```

## Example

For a more thorough example, please check out the [example](example/spring-example). It contains a functional Spring Boot application
and several tests that showcase this library.

## License

Copyright (C) [Wholegrain Software](https://wholegrain-software.com) 2021

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

[http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0)

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
29 changes: 29 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
plugins {
java
id("org.cadixdev.licenser") version "0.5.0"
}

allprojects {
apply(plugin = "java")
apply(plugin = "org.cadixdev.licenser")

group = "com.wholegrain-software"

java.sourceCompatibility = JavaVersion.VERSION_1_8
java.targetCompatibility = JavaVersion.VERSION_1_8

repositories {
mavenCentral()
}

tasks.withType<Test> {
useJUnitPlatform()
}

license {
header = rootProject.file("LICENSE")
}
}



24 changes: 24 additions & 0 deletions example/spring-example/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
plugins {
id("org.springframework.boot") version "2.4.3"
id("io.spring.dependency-management") version "1.0.11.RELEASE"
}

repositories {
mavenCentral()
maven {
url = uri("https://oss.sonatype.org/content/repositories/snapshots/")
}
}

dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-data-mongodb")

testImplementation("com.wholegrain-software:spring-mongodb-test:0.0.1-SNAPSHOT")
testImplementation("org.testcontainers:mongodb:1.15.2")
testImplementation("org.testcontainers:junit-jupiter:1.15.2")
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(group = "org.junit.vintage")
exclude(group = "org.junit", module = "junit")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2021 Wholegrain Software, Jimi Steidl <www.wholegrain-software.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.wholegrainsoftware.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MongoDbApplication {

public static void main(String[] args) {
SpringApplication.run(MongoDbApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2021 Wholegrain Software, Jimi Steidl <www.wholegrain-software.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.wholegrainsoftware.example.department;

import com.wholegrainsoftware.example.mongodb.Person;
import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.TypeAlias;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


@Document("department")
@TypeAlias("department")
public class Department {
@Id
private ObjectId id;
@DBRef
private List<Person> people = new ArrayList<>();

public Department() {
}

public Department(ObjectId id, List<Person> people) {
this.id = id;
this.people = people;
}

public ObjectId getId() {
return id;
}

public void setId(ObjectId id) {
this.id = id;
}

public List<Person> getPeople() {
return people;
}

public void setPeople(List<Person> people) {
this.people = people;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Department that = (Department) o;
return Objects.equals(id, that.id) && Objects.equals(people, that.people);
}

@Override
public int hashCode() {
return Objects.hash(id, people);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2021 Wholegrain Software, Jimi Steidl <www.wholegrain-software.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.wholegrainsoftware.example.department;

import org.bson.types.ObjectId;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface DepartmentRepository extends MongoRepository<Department, ObjectId> {
}
Loading

0 comments on commit 2ae74fe

Please sign in to comment.