|
| 1 | +package org.vertx.mods.mongo.test.integration.java; |
| 2 | +/* |
| 3 | + * Copyright 2013 Red Hat, Inc. |
| 4 | + * |
| 5 | + * Red Hat licenses this file to you under the Apache License, version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with the |
| 7 | + * License. You may obtain a copy of the License at: |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | + * License for the specific language governing permissions and limitations |
| 15 | + * under the License. |
| 16 | + * |
| 17 | + * @author <a href="http://tfox.org">Tim Fox</a> |
| 18 | + */ |
| 19 | + |
| 20 | +import com.mongodb.BasicDBObject; |
| 21 | +import com.mongodb.DBObject; |
| 22 | +import org.bson.types.Binary; |
| 23 | +import org.junit.Test; |
| 24 | +import org.vertx.java.core.Handler; |
| 25 | +import org.vertx.java.core.eventbus.Message; |
| 26 | +import org.vertx.java.core.json.JsonArray; |
| 27 | +import org.vertx.java.core.json.JsonObject; |
| 28 | +import org.vertx.mods.MongoUtil; |
| 29 | +import org.vertx.testtools.VertxAssert; |
| 30 | + |
| 31 | +import java.util.ArrayList; |
| 32 | +import java.util.Date; |
| 33 | +import java.util.List; |
| 34 | + |
| 35 | +import static org.vertx.testtools.VertxAssert.assertEquals; |
| 36 | +import static org.vertx.testtools.VertxAssert.testComplete; |
| 37 | + |
| 38 | +public class MongoTypesTest extends PersistorTestParent { |
| 39 | + |
| 40 | + @Override |
| 41 | + protected JsonObject getConfig() { |
| 42 | + JsonObject config = super.getConfig(); |
| 43 | + config.putBoolean("use_mongo_types", true); |
| 44 | + return config; |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testCommand() throws Exception { |
| 49 | + JsonObject ping = new JsonObject() |
| 50 | + .putString("action", "command") |
| 51 | + .putString("command", "{ping:1}"); |
| 52 | + |
| 53 | + eb.send(ADDRESS, ping, new Handler<Message<JsonObject>>() { |
| 54 | + public void handle(Message<JsonObject> reply) { |
| 55 | + Number ok = reply.body() |
| 56 | + .getObject("result") |
| 57 | + .getNumber("ok"); |
| 58 | + |
| 59 | + assertEquals(1.0, ok); |
| 60 | + testComplete(); |
| 61 | + } |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testDate() throws Exception { |
| 68 | + Date date = new Date(); |
| 69 | + insertTypedData(date); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testByteArray() throws Exception { |
| 74 | + byte[] data = new byte[]{1, 2, 3}; |
| 75 | + insertTypedData(data); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testArrayList() throws Exception { |
| 80 | + List data = new ArrayList(); |
| 81 | + data.add(1); |
| 82 | + data.add(2); |
| 83 | + data.add(new BasicDBObject("foo", "bar")); |
| 84 | + data.add(4); |
| 85 | + insertTypedData(data); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testEmbeddedDoc() throws Exception { |
| 90 | + BasicDBObject y = new BasicDBObject("y", 3); |
| 91 | + BasicDBObject data = new BasicDBObject("x", y); |
| 92 | + insertTypedData(data); |
| 93 | + } |
| 94 | + |
| 95 | + private void insertTypedData(final Object data) { |
| 96 | + deleteAll(new Handler<Message<JsonObject>>() { |
| 97 | + public void handle(Message<JsonObject> reply) { |
| 98 | + final String testValue = "{\"testKey\" : \"testValue\"}"; |
| 99 | + final DBObject obj = MongoUtil.convertJsonToBson(testValue); |
| 100 | + obj.put("data", data); |
| 101 | + |
| 102 | + JsonObject docWithDate = MongoUtil.convertBsonToJson(obj); |
| 103 | + |
| 104 | + JsonObject json = new JsonObject() |
| 105 | + .putString("collection", COLLECTION) |
| 106 | + .putString("action", "save").putObject("document", docWithDate); |
| 107 | + |
| 108 | + eb.send(ADDRESS, json, new Handler<Message<JsonObject>>() { |
| 109 | + public void handle(Message<JsonObject> reply) { |
| 110 | + assertEquals(reply.body().toString(), "ok", reply.body().getString("status")); |
| 111 | + |
| 112 | + assertStored(testValue, data, obj); |
| 113 | + } |
| 114 | + }); |
| 115 | + } |
| 116 | + }); |
| 117 | + |
| 118 | + } |
| 119 | + |
| 120 | + private void assertStored(String testValue, final Object data, final DBObject sentObject) { |
| 121 | + JsonObject matcher = new JsonObject(testValue); |
| 122 | + JsonObject query = new JsonObject() |
| 123 | + .putString("collection", COLLECTION) |
| 124 | + .putString("action", "find") |
| 125 | + .putObject("matcher", matcher); |
| 126 | + |
| 127 | + eb.send(ADDRESS, query, new Handler<Message<JsonObject>>() { |
| 128 | + public void handle(Message<JsonObject> reply) { |
| 129 | + assertEquals(reply.body().toString(), "ok", reply.body().getString("status")); |
| 130 | + JsonArray results = reply.body().getArray("results"); |
| 131 | + |
| 132 | + if (results.size() > 0) { |
| 133 | + JsonObject result = results.get(0); |
| 134 | + DBObject dbObj = MongoUtil.convertJsonToBson(result); |
| 135 | + dbObj.removeField("_id"); |
| 136 | + Object storedData = dbObj.get("data"); |
| 137 | + if (storedData instanceof Binary) { |
| 138 | + VertxAssert.assertArrayEquals((byte[]) data, ((Binary) storedData).getData()); |
| 139 | + } else { |
| 140 | + VertxAssert.assertEquals(sentObject, dbObj); |
| 141 | + } |
| 142 | + testComplete(); |
| 143 | + } else { |
| 144 | + VertxAssert.fail("Stored object not found in DB"); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + ); |
| 150 | + } |
| 151 | +} |
| 152 | + |
0 commit comments