Skip to content

Commit

Permalink
mongo insert document to collection
Browse files Browse the repository at this point in the history
  • Loading branch information
twcn committed Oct 20, 2018
1 parent 932a2d3 commit 10a0bea
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ before_install:
- sudo service mysql restart
- mysql -e 'CREATE DATABASE pandaria;' -u root -ppassword

before_script:
- mongo mydb_test --eval 'db.createUser({user:"root",pwd:"password",roles:["readWrite"]});'

script:
"./gradlew clean build -x signArchives"

services:
- mysql
- mongodb

addons:
apt:
Expand Down
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ subprojects {
json_schema_validator = '1.9.2'

json_unit = '2.0.0.RC4'

mongo = '3.8.2'
}

task javadocJar(type: Jar) {
Expand Down
8 changes: 8 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,11 @@ services:
environment:
MYSQL_DATABASE: pandaria
MYSQL_ROOT_PASSWORD: password

pandaria-mongo:
image: mongo:4.0.3
ports:
- "27017:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: password
37 changes: 37 additions & 0 deletions pandaria-mongo/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
ext {
archivesBaseName = "pandaria-mongo"
}

dependencies {
compile(
project(":pandaria-core"),
"org.mongodb:mongodb-driver:${mongo}"
)

testCompile(
"io.cucumber:cucumber-junit:${cucumber}",

"com.github.dreamhead:moco-core:${moco}",
"com.github.dreamhead:moco-runner:${moco}",
"com.github.dreamhead:moco-junit:${moco}"
)
}

configurations {
cucumberRuntime {
extendsFrom testRuntime
}
}

task cucumber() {
dependsOn assemble, compileTestJava
doLast {
javaexec {
main = "cucumber.api.cli.Main"
classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
args = ['--plugin', 'pretty', '--glue', 'com.github.jakimli.pandaria', 'src/test/resources']
}
}
}

build.dependsOn tasks.cucumber
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.github.jakimli.pandaria.domain;

import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import static com.mongodb.client.MongoClients.create;

@Component
public class MongoClient {

private MongoDatabase database;

public MongoClient(
@Value("${mongo.db.name}") String name,
@Value("${mongo.connection.string}") String connection) {
database = create(connection).getDatabase(name);
}

public void insert(String collection, String document) {
MongoCollection<Document> collect = database.getCollection(collection);

if (collect == null) {
database.createCollection(collection);
collect = database.getCollection(collection);
}

collect.insertOne(Document.parse(document));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.github.jakimli.pandaria.steps;

import com.github.jakimli.pandaria.domain.MongoClient;
import cucumber.api.java.en.When;
import org.springframework.beans.factory.annotation.Autowired;

public class MongoSteps {

@Autowired
MongoClient mongo;

@When("^collection: '([^\"]*)' insert:$")
public void insert(String collection, String document) {
mongo.insert(collection, document);
}
}
2 changes: 2 additions & 0 deletions pandaria-mongo/src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mongo.db.name=test
mongo.connection.string=mongodb://root:password@localhost:27017
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Feature: simple mongo insert
insert document into mongo collection

Background:
* dir: features/mongo

Scenario: insert a simple document

* collection: 'users' insert:
"""
{"user": "jakim"}
"""
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ rootProject.name = 'pandaria'

include 'pandaria-core'
include 'pandaria-db'
include 'pandaria-mongo'
include 'doc'

0 comments on commit 10a0bea

Please sign in to comment.