├── .gitignore
├── .travis.yml
├── README.md
├── build.gradle
├── doc
└── libdoc
│ ├── de.codecentric.robot.mongodblibrary.keywords.MongodbLibrary.html
│ └── de.codecentric.robot.mongodblibrary.keywords.MongodbLibrary.xml
├── generateDocs.cmd
├── generateDocs.launch
├── robot-bin
├── commons-io-1.3.2.jar
├── junit-4.10.jar
├── mongo-java-driver-2.10.0.jar
└── robotframework-2.7.5.jar
├── runSample.cmd
├── runSampleTests.launch
├── runServer.cmd
├── sample
├── data.json
├── dataMultipleRows.json
├── mongodblibrarySample.txt
└── remoteMongodblibrarySample.txt
├── settings.gradle
└── src
├── main
├── java
│ └── de
│ │ └── codecentric
│ │ └── robot
│ │ └── mongodblibrary
│ │ ├── MongodbLibraryException.java
│ │ ├── keywords
│ │ └── MongodbLibrary.java
│ │ └── server
│ │ └── MongodbLibraryRemoteServer.java
└── resources
│ └── de
│ └── codecentric
│ └── robot
│ └── mongodblibrary
│ └── keywords
│ └── MongodbLibrary.properties
└── test
├── data
├── testArray.json
├── testRowSeperated.json
└── testSingleObject.json
└── java
└── de
└── codecentric
└── robot
└── mongodblibrary
└── keywords
└── MongodbLibraryTest.java
/.gitignore:
--------------------------------------------------------------------------------
1 | /.settings
2 | /target
3 | /.classpath
4 | /.project
5 | /.gradle
6 | /build
7 | /bin
8 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: java
2 | jdk:
3 | - openjdk6
4 |
5 | after_script: gradle 'run sample tests'
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Robotframework-Mongodblibrary
2 | =============================
3 |
4 | The **Robot Framework MongoDB Library** is a library for testing MongoDB applications with the Robotframework.
5 | Please see the [Keyword-Documentation](http://mahartma.github.com/robotframework-mongodblibrary/de.codecentric.robot.mongodblibrary.keywords.MongodbLibrary.html) for more information about the provided keywords.
6 |
7 | The library is written in Java using the MongoDB Java Driver.
8 |
9 | Sample test:
10 |
11 | ```
12 | *** Settings ***
13 | Library de.codecentric.robot.mongodblibrary.keywords.MongodbLibrary
14 | Suite Setup Startup Embedded 2.4.4
15 | Suite TearDown Shutdown Embedded
16 | Test Setup Setup MongoDB
17 |
18 | *** Test Cases ***
19 | should insert given document
20 | Insert Document myCollection {say : 'Hello MongoDb!'}
21 | Collection Should Exist myCollection
22 | Document Should Exist myCollection {say : 'Hello MongoDb!'}
23 | should insert data from file
24 | Import Documents myCollection sample/data.json
25 | Collection Should Exist myCollection
26 | Document Should Exist myCollection {name : 'Mike'}
27 | should insert data from file (row-seperated)
28 | Import Documents Row Seperated myCollection sample/dataMultipleRows.json
29 | Collection Should Exist myCollection
30 | Document Should Exist myCollection {name : 'Mike'}
31 | Document Should Exist myCollection {name : 'Tom'}
32 | Document Should Exist myCollection {name : 'Eric'}
33 |
34 | *** Keywords ***
35 | Setup MongoDB
36 | Connect To Server localhost 27017 robotdb1
37 | Drop Database robotdb1
38 | ```
39 |
40 | Dependencies
41 | ------------
42 | - [Robotframework >= 2.7.5 (with Jython)](http://code.google.com/p/robotframework/downloads/list)
43 | - [MongoDB Java Driver >= 2.10.0](http://central.maven.org/maven2/org/mongodb/mongo-java-driver)
44 | - [apache-commons-io 1.3.2](http://search.maven.org/remotecontent?filepath=org/apache/commons/commons-io/1.3.2/commons-io-1.3.2.jar)
45 | - [junit 4.10](http://search.maven.org/remotecontent?filepath=junit/junit/4.10/junit-4.10.jar)
46 | - [MongoDB Server >= 2.2.1](http://www.mongodb.org/downloads)
47 | - [Embedded MongoDB >= 1.31](https://github.com/flapdoodle-oss/embedmongo.flapdoodle.de)
48 |
49 | Install
50 | -------
51 | - download [robotframework-mongodblibrary-0.2.1-with-dependencies.jar](http://mahartma.github.com/robotframework-mongodblibrary/robotframework-mongodblibrary-0.2-with-dependencies.jar)
52 | - start the mongoDB daemon or use the embedded keywords ( _Startup Embedded_, _Shutdown Embedded_ )
53 | - add **robotframework-mongodblibrary-0.2.1-with-dependencies.jar** to the CLASSPATH (see runSample.cmd)
54 | - start the Robot-Tests
55 | - this can also be done by a gradle task:
56 | ```groovy
57 | configurations {
58 | robot
59 | }
60 |
61 | dependencies {
62 | robot files("libs/robotframework-mongodblibrary-0.2.1-with-dependencies.jar")
63 | }
64 |
65 | task(type : JavaExec, 'run tests') {
66 | classpath = configurations.robot
67 | main = 'org.robotframework.RobotFramework'
68 | args = [
69 | '-d',
70 | 'build',
71 | 'sample/mongodblibrarySample.txt'
72 | ]
73 | }
74 | ```
75 |
76 | Remote-Library
77 | --------------
78 | - the library also contains the Remote-Server from the Robotframework for executing the keywords on a dedicated JVM (see [Robot-Remote-Library](http://code.google.com/p/robotframework/wiki/RemoteLibrary))
79 | - it's very useful when you want to use python in the main suite instead of jython
80 | - the server can be started with **java -jar build/libs/robotframework-mongodblibrary-0.2.1-with-dependencies.jar**
81 | - see the example below:
82 |
83 | ```
84 | *** Settings ***
85 | Library Remote http://localhost:8270
86 | Suite Setup Startup Embedded 2.4.4
87 | Suite TearDown Shutdown Embedded
88 | Test Setup Setup MongoDB
89 |
90 | *** Test Cases ***
91 | should insert given document
92 | Insert Document myCollection {say : 'Hello MongoDb!'}
93 | Collection Should Exist myCollection
94 | Document Should Exist myCollection {say : 'Hello MongoDb!'}
95 | should insert data from file
96 | Import Documents myCollection sample/data.json
97 | Collection Should Exist myCollection
98 | Document Should Exist myCollection {name : 'Mike'}
99 | should insert data from file (row-seperated)
100 | Import Documents Row Seperated myCollection sample/dataMultipleRows.json
101 | Collection Should Exist myCollection
102 | Document Should Exist myCollection {name : 'Mike'}
103 | Document Should Exist myCollection {name : 'Tom'}
104 | Document Should Exist myCollection {name : 'Eric'}
105 |
106 | *** Keywords ***
107 | Setup MongoDB
108 | Connect To Server localhost 27017 robotdb1
109 | Drop Database robotdb1
110 | ```
111 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | project.version = '0.2.1'
2 |
3 | repositories { mavenCentral() }
4 |
5 | apply plugin: 'java'
6 | apply plugin: 'eclipse'
7 |
8 | configurations {
9 | robot
10 | docs
11 | }
12 |
13 | dependencies {
14 | compile group: 'junit', name: 'junit', version: "4.10"
15 | compile group: 'de.flapdoodle.embed', name: 'de.flapdoodle.embed.mongo', version: "1.31"
16 | compile group: 'org.apache.commons', name: 'commons-io', version: "1.3.2"
17 | compile group: 'com.github.ombre42', name: 'jrobotremoteserver', version: "2.0"
18 | compile group: 'org.mongodb', name: 'mongo-java-driver', version: "2.10.0"
19 | robot group: 'org.robotframework', name: 'robotframework', version: "2.7.5"
20 | robot group: 'org.robotframework', name: 'javalib-core', version: "1.0.3"
21 | docs files("${System.getProperty('java.home')}/../lib/tools.jar")
22 | }
23 |
24 | task(type : Jar, 'jarWithDependencies') {
25 | classifier = 'with-dependencies'
26 | exclude('META-INF/*.RSA')
27 | exclude('META-INF/*.SF')
28 | from { configurations.compile.collect {
29 | if (!it.isDirectory()) {
30 | zipTree(it)
31 | }
32 | }
33 | }
34 | from { sourceSets.main.output }
35 | manifest { attributes 'Main-Class': 'de.codecentric.robot.mongodblibrary.server.MongodbLibraryRemoteServer' }
36 | }
37 |
38 | processResources {
39 | filter(org.apache.tools.ant.filters.ReplaceTokens,
40 | tokens: [version: project.version])
41 | }
42 |
43 | assemble.dependsOn 'jarWithDependencies'
44 |
45 | task(type : JavaExec, 'runSampleTests') {
46 | classpath = configurations.robot + configurations.compile + sourceSets.main.runtimeClasspath
47 | main = 'org.robotframework.RobotFramework'
48 | args = [
49 | '-d',
50 | 'build',
51 | 'sample/mongodblibrarySample.txt'
52 | ]
53 | }
54 |
55 | task 'generateDocs' << {
56 | tasks['generateHtmlDoc'].exec()
57 | tasks['generateXmlDoc'].exec()
58 | }
59 |
60 | task(type : JavaExec, 'generateHtmldoc') {
61 | classpath = configurations.docs + configurations.robot + configurations.compile + sourceSets.main.runtimeClasspath
62 | main = 'org.python.util.jython'
63 | args = [
64 | '-m',
65 | 'robot.libdoc',
66 | 'src/main/java/de/codecentric/robot/mongodblibrary/keywords/MongodbLibrary.java',
67 | 'doc/libdoc/de.codecentric.robot.mongodblibrary.keywords.MongodbLibrary.html'
68 | ]
69 | }
70 |
71 | task(type : JavaExec, 'generateXmlDoc') {
72 | classpath = configurations.docs + configurations.robot + configurations.compile + sourceSets.main.runtimeClasspath
73 | main = 'org.python.util.jython'
74 | args = [
75 | '-m',
76 | 'robot.libdoc',
77 | '--format',
78 | 'XML',
79 | 'src/main/java/de/codecentric/robot/mongodblibrary/keywords/MongodbLibrary.java',
80 | 'doc/libdoc/de.codecentric.robot.mongodblibrary.keywords.MongodbLibrary.xml'
81 | ]
82 | }
--------------------------------------------------------------------------------
/doc/libdoc/de.codecentric.robot.mongodblibrary.keywords.MongodbLibrary.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
82 |
92 |
114 |
154 |
174 |
186 |
189 |
190 |
191 |
192 |
193 |
194 |
Opening library documentation failed
195 |
196 | - Verify that you have JavaScript enabled in your browser.
197 | - Make sure you are using a modern enough browser. Firefox 3.5, IE 8, or equivalent is required, newer browsers are recommended.
198 | - Check are there messages in your browser's JavaScript error log. Please report the problem if you suspect you have encountered a bug.
199 |
200 |
201 |
202 |
206 |
207 |
232 |
233 |
243 |
244 |
259 |
260 |
269 |
270 |
287 |
294 |
295 |
296 |
297 |
--------------------------------------------------------------------------------
/doc/libdoc/de.codecentric.robot.mongodblibrary.keywords.MongodbLibrary.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | global
5 | no
6 | This library supports mongodb-related testing using the Robot Framework.
7 |
8 |
9 | collectionName
10 |
11 | Fails if the collection does not exist.
12 |
13 | Arguments:
14 | - _collectionName_: the collection which should exist
15 |
16 | Example:
17 | | Collection Should Exist | myCol |
18 |
19 |
20 |
21 | server
22 | port
23 | database
24 |
25 | connects to the given MongoDB-Server
26 |
27 | Arguments:
28 | - _server_: server to connect
29 | - _port_: port to connect
30 | - _database_: database to connect
31 |
32 | Example:
33 | | Connect To Server | localhost | 27017 | robotdb |
34 |
35 |
36 |
37 | collectionName
38 |
39 | Creates a collection with the given name and options.
40 |
41 | Arguments:
42 | - _collectionName_: the name of the collection to create
43 |
44 | Example:
45 | | Create Collection | myCol |
46 |
47 |
48 |
49 | collectionName
50 | options
51 |
52 | Creates a collection with the given name and options.
53 |
54 | Arguments:
55 | - _collectionName_: the name of the collection to create
56 | - _options_: the options of the collection
57 |
58 | Example:
59 | | Create Collection | myCol | {capped:true, size:10000} |
60 |
61 |
62 |
63 | databaseName
64 |
65 | Fails if the database does not exist.
66 |
67 | Arguments:
68 | - _databaseName_: the database which should exist
69 |
70 | Example:
71 | | Database Should Exist | myDatabase |
72 |
73 |
74 |
75 | collectionName
76 | document
77 |
78 | Fails if the given document does not exist in the given collection.
79 |
80 | Arguments:
81 | - _collectionName_: the collection within the document should exist
82 | - _document_: the document which should exist in the given collection
83 |
84 | Example:
85 | | Document Should Exist | myCol | {say : 'Hello MongoDb!'} |
86 |
87 |
88 |
89 | collectionName
90 |
91 | Drops the given collection.
92 |
93 | Arguments:
94 | - _collectionName_: the name of the collection to drop
95 |
96 | Example:
97 | | Drop Collection | myCollection |
98 |
99 |
100 |
101 | databaseName
102 |
103 | A database in MongoDB is a more lightweight construct compared to RDBMS,
104 | so a cleanup after testing can be done quite easily by dropping the whole database.
105 |
106 | Arguments:
107 | - _databaseName_: the name of the database to delete
108 |
109 | Example:
110 | | Drop Database | myDb |
111 |
112 |
113 |
114 | collectionName
115 | keys
116 |
117 | Creates an index on the given collection, the desired fields of the index are given by the keys parameter.
118 |
119 | Arguments:
120 | - _collectionName_: the name of collection
121 | - _keys_: an object with a key set of the fields desired for the index
122 |
123 | Example:
124 | | Ensure Index | myCol | {name : 1, street : 1} |
125 |
126 |
127 |
128 | indexName
129 | collectionName
130 | keys
131 |
132 | Creates an index with given name on the given collection, the desired fields of the index are given by the keys parameter.
133 |
134 | Arguments:
135 | - _indexName_: the name of the index
136 | - _collectionName_: the name of collection
137 | - _keys_: an object with a key set of the fields desired for the index
138 |
139 | Example:
140 | | Ensure Index | myCol | {name : 1, street : 1} |
141 |
142 |
143 |
144 | collectionName
145 | keys
146 | indexName
147 |
148 | Creates an unique index with given name on the given collection, the desired fields of the index are given by the keys parameter.
149 |
150 | Arguments:
151 | - _indexName_: the name of the index
152 | - _collectionName_: the name of collection
153 | - _keys_: an object with a key set of the fields desired for the index
154 |
155 | Example:
156 | | Ensure Index | myCol | {name : 1, street : 1} |
157 |
158 |
159 |
160 | collectionName
161 |
162 | Returns all documents from the given collection.
163 |
164 | Arguments:
165 | - _collectionName_: the name of the collection
166 |
167 | Example:
168 | | Get All Documents | myCol |
169 |
170 |
171 |
172 | collectionName
173 |
174 | Returns the number of documents in the given collection.
175 |
176 | Arguments:
177 | - _collectionName_: the name of the collection
178 |
179 | Example:
180 | | Get Collection Count | myCol |
181 |
182 |
183 |
184 |
185 | Returns the names of the collections from the connected database.
186 |
187 | Example:
188 | | Get Collections |
189 |
190 |
191 |
192 |
193 | Returns the name of the databases from the server.
194 |
195 | Example:
196 | | Get Databases |
197 |
198 |
199 |
200 | collectionName
201 | jsonString
202 |
203 | Finds some documents in the given collection.
204 |
205 | Arguments:
206 | - _collectionName_: the name of the collection
207 | - _jsonString_: the documents to find as JSON
208 |
209 | Example:
210 | | Get Documents | myCol | { age : { $gte: 23 } } |
211 |
212 |
213 |
214 | collectionName
215 | file
216 |
217 | Imports the documents from the given file into the given collection.
218 |
219 | Arguments:
220 | - _collectionName_: the name of the target collection
221 | - _file_: the file that contains the documents
222 |
223 | Example:
224 | | Import Documents | myCol | /data/documents.json |
225 |
226 |
227 |
228 | collectionName
229 | file
230 |
231 | Imports the documents from the given file into the given collection. This keyword reads the data row-based.
232 |
233 | Arguments:
234 | - _collectionName_: the name of the target collection
235 | - _file_: the file that contains the documents
236 |
237 | Example:
238 | | Import Documents Row Seperated | myCol | /data/documents.json |
239 |
240 |
241 |
242 | collectionName
243 | indexName
244 |
245 | Fails if the given index does not exist in the given collection.
246 |
247 | Arguments:
248 | - _collectionName_: the collection within the index should exist
249 | - _index_: the name of the index which should exist in the given collection
250 |
251 | Example:
252 | | Index Should Exist | myCol | a_1_b_1 |
253 |
254 |
255 |
256 | collectionName
257 | jsonString
258 |
259 | Inserts the given document into the given collection.
260 |
261 | Arguments:
262 | - _collectionName_: the name of the target collection
263 | - _jsonString_: the document to persist as JSON
264 |
265 | Example:
266 | | Insert Document | myCollection | {say : 'Hello MongoDB!'} |
267 |
268 |
269 |
270 | collectionName
271 |
272 | Removes all documents in the given collection.
273 |
274 | Arguments:
275 | - _collectionName_: the name of the collection
276 |
277 | Example:
278 | | Remove All Documents | myCol |
279 |
280 |
281 |
282 | collectionName
283 | jsonString
284 |
285 | Removes some documents in the given collection.
286 |
287 | Arguments:
288 | - _collectionName_: the name of the collection
289 | - _jsonString_: the documents to remove as JSON
290 |
291 | Example:
292 | | Remove Documents | myCol | { age : { $gte: 23 } } |
293 |
294 |
295 |
296 |
297 | stops the previously started MongoDB-Server (counter-part to the keywords: `Startup Embedded` and `Startup Embedded On Port`)
298 |
299 | Example:
300 | | Shutdown Embedded |
301 |
302 |
303 |
304 | version
305 |
306 | starts a MongoDB-Server in the given version
307 |
308 | Arguments:
309 | - _version_: MongoDB-Version
310 |
311 | Example:
312 | | Startup Embedded | 2.4.1 |
313 |
314 |
315 |
316 | version
317 | port
318 |
319 | starts a MongoDB-Server in the given version on the given port
320 |
321 | Arguments:
322 | - _version_: MongoDB-Version
323 | - _port_: port to use
324 |
325 | Example:
326 | | Startup Embedded | 2.4.1 | 27042 |
327 |
328 |
329 |
330 | collectionName
331 | queryJsonString
332 | newObjectJsonString
333 |
334 | Updates some documents in the given collection.
335 |
336 | Arguments:
337 | - _collectionName_: the name of the collection
338 | - _queryJsonString_: the documents to update as JSON
339 | - _newObjectJsonString_: the updated document as JSON
340 |
341 | Example:
342 | | Update Documents | myCollection | { age : { $gte: 23 } } | {name : 'Mike', age : 22} |
343 |
344 |
345 |
346 | databaseName
347 |
348 | All following keywords will operate on the selected database. Same as use
349 | dbname with the Mongo shell. If the database does not exist, it will be
350 | create as soon as needed.
351 |
352 | Arguments:
353 | - _databaseName_: the name of the database
354 |
355 | Example:
356 | | Use Database | myDb |
357 |
358 |
359 |
--------------------------------------------------------------------------------
/generateDocs.cmd:
--------------------------------------------------------------------------------
1 | del %~dp0%doc\libdoc\*.html
2 | del %~dp0%doc\libdoc\*.xml
3 | java -cp robot-bin/robotframework-2.7.5.jar;%JAVA_HOME%\lib\tools.jar org.python.util.jython -m robot.libdoc src/main/java/de/codecentric/robot/mongodblibrary/keywords/MongodbLibrary.java doc/libdoc/de.codecentric.robot.mongodblibrary.keywords.MongodbLibrary.html
4 | java -cp robot-bin/robotframework-2.7.5.jar;%JAVA_HOME%\lib\tools.jar org.python.util.jython -m robot.libdoc --format XML src/main/java/de/codecentric/robot/mongodblibrary/keywords/MongodbLibrary.java doc/libdoc/de.codecentric.robot.mongodblibrary.keywords.MongodbLibrary.xml
--------------------------------------------------------------------------------
/generateDocs.launch:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/robot-bin/commons-io-1.3.2.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mahartma/robotframework-mongodblibrary/821b25f045d2fb6fadb81a66c4f8305d2d905d69/robot-bin/commons-io-1.3.2.jar
--------------------------------------------------------------------------------
/robot-bin/junit-4.10.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mahartma/robotframework-mongodblibrary/821b25f045d2fb6fadb81a66c4f8305d2d905d69/robot-bin/junit-4.10.jar
--------------------------------------------------------------------------------
/robot-bin/mongo-java-driver-2.10.0.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mahartma/robotframework-mongodblibrary/821b25f045d2fb6fadb81a66c4f8305d2d905d69/robot-bin/mongo-java-driver-2.10.0.jar
--------------------------------------------------------------------------------
/robot-bin/robotframework-2.7.5.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mahartma/robotframework-mongodblibrary/821b25f045d2fb6fadb81a66c4f8305d2d905d69/robot-bin/robotframework-2.7.5.jar
--------------------------------------------------------------------------------
/runSample.cmd:
--------------------------------------------------------------------------------
1 | REM ======= LOCAL =======
2 | java -jar robot-bin/robotframework-2.7.5.jar -P build/libs/robotframework-mongodblibrary-0.2.1-with-dependencies.jar -d target sample/mongodblibrarySample.txt
3 | REM ======= REMOTE =======
4 | REM java -jar robot-bin/robotframework-2.7.5.jar -d target sample/remoteMongodblibrarySample.txt
--------------------------------------------------------------------------------
/runSampleTests.launch:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/runServer.cmd:
--------------------------------------------------------------------------------
1 | REM ======= starting Remote-Library =======
2 | java -jar build/libs/robotframework-mongodblibrary-0.2.1-with-dependencies.jar --port 8270
--------------------------------------------------------------------------------
/sample/data.json:
--------------------------------------------------------------------------------
1 | {name : 'Mike'}
--------------------------------------------------------------------------------
/sample/dataMultipleRows.json:
--------------------------------------------------------------------------------
1 | {name : 'Mike', age : 22}
2 | {name : 'Tom', age : 25}
3 | {name : 'Eric', age : 40}
--------------------------------------------------------------------------------
/sample/mongodblibrarySample.txt:
--------------------------------------------------------------------------------
1 | *** Settings ***
2 | Library de.codecentric.robot.mongodblibrary.keywords.MongodbLibrary
3 | Library Collections
4 | Suite Setup Startup Embedded 2.4.4
5 | Suite TearDown Shutdown Embedded
6 | Test Setup Setup MongoDB
7 |
8 | *** Test Cases ***
9 | should insert given document
10 | Insert Document myCollection {say : 'Hello MongoDb!'}
11 | Collection Should Exist myCollection
12 | Document Should Exist myCollection {say : 'Hello MongoDb!'}
13 | should insert data from file
14 | Import Documents myCollection sample/data.json
15 | Collection Should Exist myCollection
16 | Document Should Exist myCollection {name : 'Mike'}
17 | should insert data from file (row-seperated)
18 | Import Documents Row Seperated myCollection sample/dataMultipleRows.json
19 | Collection Should Exist myCollection
20 | Document Should Exist myCollection {name : 'Mike', age : 22}
21 | Document Should Exist myCollection {name : 'Tom', age : 25}
22 | Document Should Exist myCollection {name : 'Eric', age : 40}
23 | ${allDocuments}= Get All Documents myCollection
24 | ${document1}= Get From List ${allDocuments} 0
25 | ${document1Age}= Get From Dictionary ${document1} age
26 | ${expectedAge}= Convert To Integer 22
27 | Should Be Equal ${expectedAge} ${document1Age} age isn't 22
28 | *** Keywords ***
29 | Setup MongoDB
30 | Connect To Server localhost 27020 robotdb1
31 | Drop Database robotdb1
--------------------------------------------------------------------------------
/sample/remoteMongodblibrarySample.txt:
--------------------------------------------------------------------------------
1 | *** Settings ***
2 | Library Remote http://localhost:8270
3 | Suite Setup Startup Embedded 2.4.4
4 | Suite TearDown Shutdown Embedded
5 | Test Setup Setup MongoDB
6 |
7 | *** Test Cases ***
8 | should insert given document
9 | Insert Document myCollection {say : 'Hello MongoDb!'}
10 | Collection Should Exist myCollection
11 | Document Should Exist myCollection {say : 'Hello MongoDb!'}
12 | should insert data from file
13 | Import Documents myCollection sample/data.json
14 | Collection Should Exist myCollection
15 | Document Should Exist myCollection {name : 'Mike'}
16 | should insert data from file (row-seperated)
17 | Import Documents Row Seperated myCollection sample/dataMultipleRows.json
18 | Collection Should Exist myCollection
19 | Document Should Exist myCollection {name : 'Mike'}
20 | Document Should Exist myCollection {name : 'Tom'}
21 | Document Should Exist myCollection {name : 'Eric'}
22 |
23 | *** Keywords ***
24 | Setup MongoDB
25 | Connect To Server localhost 27020 robotdb1
26 | Drop Database robotdb1
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | rootProject.name = 'robotframework-mongodblibrary'
--------------------------------------------------------------------------------
/src/main/java/de/codecentric/robot/mongodblibrary/MongodbLibraryException.java:
--------------------------------------------------------------------------------
1 | package de.codecentric.robot.mongodblibrary;
2 |
3 | /**
4 | *
5 | *
6 | * @author Max Hartmann
7 | *
8 | */
9 | public class MongodbLibraryException extends RuntimeException {
10 |
11 | private static final long serialVersionUID = 42L;
12 |
13 | public MongodbLibraryException() {
14 | super();
15 | }
16 |
17 | public MongodbLibraryException(String message, Throwable cause) {
18 | super(message, cause);
19 | }
20 |
21 | public MongodbLibraryException(String message) {
22 | super(message);
23 | }
24 |
25 | public MongodbLibraryException(Throwable cause) {
26 | super(cause);
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/src/main/java/de/codecentric/robot/mongodblibrary/keywords/MongodbLibrary.java:
--------------------------------------------------------------------------------
1 | package de.codecentric.robot.mongodblibrary.keywords;
2 |
3 | import static com.mongodb.util.JSON.parse;
4 | import static de.flapdoodle.embed.process.runtime.Network.localhostIsIPv6;
5 | import static java.lang.Integer.parseInt;
6 | import static org.junit.Assert.assertTrue;
7 | import static org.junit.Assert.fail;
8 |
9 | import java.io.FileReader;
10 | import java.io.IOException;
11 | import java.net.UnknownHostException;
12 | import java.util.ArrayList;
13 | import java.util.Iterator;
14 | import java.util.List;
15 | import java.util.Map;
16 | import java.util.ResourceBundle;
17 | import java.util.Set;
18 |
19 | import org.apache.commons.io.IOUtils;
20 |
21 | import com.mongodb.BasicDBList;
22 | import com.mongodb.BasicDBObject;
23 | import com.mongodb.DB;
24 | import com.mongodb.DBObject;
25 | import com.mongodb.MongoClient;
26 | import com.mongodb.util.JSON;
27 |
28 | import de.codecentric.robot.mongodblibrary.MongodbLibraryException;
29 | import de.flapdoodle.embed.mongo.MongodExecutable;
30 | import de.flapdoodle.embed.mongo.MongodStarter;
31 | import de.flapdoodle.embed.mongo.config.MongodConfig;
32 | import de.flapdoodle.embed.process.distribution.GenericVersion;
33 |
34 | /**
35 | * This library supports mongodb-related testing using the Robot Framework.
36 | */
37 | public class MongodbLibrary {
38 |
39 | public static final String VERSION = ResourceBundle.
40 | getBundle(MongodbLibrary.class.getName()).getString("lib.version");
41 |
42 | public static final String ROBOT_LIBRARY_SCOPE = "GLOBAL";
43 |
44 | private static final int MONGO_DEFAULT_PORT = 27020;
45 |
46 | private MongoClient mongoClient;
47 | private DB db;
48 | private MongodExecutable mongodExecutable;
49 |
50 | /**
51 | * starts a MongoDB-Server in the given version
52 | *
53 | * Arguments:
54 | * - _version_: MongoDB-Version
55 | *
56 | * Example:
57 | * | Startup Embedded | 2.4.1 |
58 | */
59 | public void startupEmbedded(String version) throws IOException {
60 | MongodConfig mongodConfig = new MongodConfig(new GenericVersion(version), MONGO_DEFAULT_PORT, localhostIsIPv6());
61 | MongodStarter runtime = MongodStarter.getDefaultInstance();
62 | mongodExecutable = runtime.prepare(mongodConfig);
63 | mongodExecutable.start();
64 | }
65 |
66 | /**
67 | * starts a MongoDB-Server in the given version on the given port
68 | *
69 | * Arguments:
70 | * - _version_: MongoDB-Version
71 | * - _port_: port to use
72 | *
73 | * Example:
74 | * | Startup Embedded | 2.4.1 | 27042 |
75 | */
76 | public void startupEmbeddedOnPort(String version, String port) throws IOException {
77 | MongodConfig mongodConfig = new MongodConfig(new GenericVersion(version), parseInt(port), localhostIsIPv6());
78 | MongodStarter runtime = MongodStarter.getDefaultInstance();
79 | mongodExecutable = runtime.prepare(mongodConfig);
80 | mongodExecutable.start();
81 | }
82 |
83 | /**
84 | * stops the previously started MongoDB-Server (counter-part to the keywords: `Startup Embedded` and `Startup Embedded On Port`)
85 | *
86 | * Example:
87 | * | Shutdown Embedded |
88 | */
89 | public void shutdownEmbedded() {
90 | if (mongodExecutable != null) {
91 | mongodExecutable.stop();
92 | }
93 | }
94 |
95 | /**
96 | * connects to the given MongoDB-Server
97 | *
98 | * Arguments:
99 | * - _server_: server to connect
100 | * - _port_: port to connect
101 | * - _database_: database to connect
102 | *
103 | * Example:
104 | * | Connect To Server | localhost | 27017 | robotdb |
105 | */
106 | public void connectToServer(String server, String port, String database) {
107 | try {
108 | mongoClient = new MongoClient(server, parseInt(port));
109 | db = mongoClient.getDB(database);
110 | } catch (UnknownHostException e) {
111 | throw new MongodbLibraryException("error connecting mongodb", e);
112 | }
113 | }
114 |
115 | /**
116 | * Inserts the given document into the given collection.
117 | *
118 | * Arguments:
119 | * - _collectionName_: the name of the target collection
120 | * - _jsonString_: the document to persist as JSON
121 | *
122 | * Example:
123 | * | Insert Document | myCollection | {say : 'Hello MongoDB!'} |
124 | */
125 | public void insertDocument(String collectionName,
126 | String jsonString) {
127 | db.getCollection(collectionName).insert(
128 | (DBObject) parse(jsonString));
129 | }
130 |
131 | /**
132 | * Updates some documents in the given collection.
133 | *
134 | * Arguments:
135 | * - _collectionName_: the name of the collection
136 | * - _queryJsonString_: the documents to update as JSON
137 | * - _newObjectJsonString_: the updated document as JSON
138 | *
139 | * Example:
140 | * | Update Documents | myCollection | { age : { $gte: 23 } } | {name : 'Mike', age : 22} |
141 | */
142 | public void updateDocuments(String collectionName,
143 | String queryJsonString, String newObjectJsonString) {
144 | DBObject queryJson = (DBObject) parse(queryJsonString);
145 | DBObject newObjectJson = (DBObject) parse(newObjectJsonString);
146 | db.getCollection(collectionName).update(queryJson, newObjectJson, false, true);
147 | }
148 |
149 | /**
150 | * Imports the documents from the given file into the given collection.
151 | *
152 | * Arguments:
153 | * - _collectionName_: the name of the target collection
154 | * - _file_: the file that contains the documents
155 | *
156 | * Example:
157 | * | Import Documents | myCol | /data/documents.json |
158 | */
159 | public void importDocuments(String collectionName, String file) {
160 | try {
161 | String documents = IOUtils.toString(new FileReader(file));
162 | DBObject dbObject = (DBObject) parse(documents);
163 | if (dbObject instanceof BasicDBList) {
164 | Iterator