|
1 | 1 | /*
|
2 |
| - * Copyright (C) 2017 Julien Viet |
| 2 | + * Copyright (c) 2011-2021 Contributors to the Eclipse Foundation |
3 | 3 | *
|
4 |
| - * Licensed under the Apache License, Version 2.0 (the "License"); |
5 |
| - * you may not use this file except in compliance with the License. |
6 |
| - * You may obtain a copy of the License at |
7 |
| - * |
8 |
| - * http://www.apache.org/licenses/LICENSE-2.0 |
9 |
| - * |
10 |
| - * Unless required by applicable law or agreed to in writing, software |
11 |
| - * distributed under the License is distributed on an "AS IS" BASIS, |
12 |
| - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 |
| - * See the License for the specific language governing permissions and |
14 |
| - * limitations under the License. |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License 2.0 which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| 7 | + * which is available at https://www.apache.org/licenses/LICENSE-2.0. |
15 | 8 | *
|
| 9 | + * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
16 | 10 | */
|
17 | 11 |
|
18 | 12 | package io.vertx.sqlclient.templates;
|
|
28 | 22 | import io.vertx.sqlclient.SqlResult;
|
29 | 23 | import org.junit.Test;
|
30 | 24 |
|
| 25 | +import java.time.Instant; |
31 | 26 | import java.time.LocalDateTime;
|
32 |
| -import java.util.Arrays; |
33 |
| -import java.util.Collections; |
34 |
| -import java.util.HashMap; |
35 |
| -import java.util.List; |
36 |
| -import java.util.Map; |
| 27 | +import java.util.*; |
37 | 28 | import java.util.function.Function;
|
38 | 29 |
|
| 30 | +import static org.junit.Assert.assertEquals; |
| 31 | +import static org.junit.Assert.assertTrue; |
| 32 | + |
39 | 33 | /**
|
40 | 34 | * @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
|
41 | 35 | */
|
@@ -175,4 +169,102 @@ public void testAnemicJson(TestContext ctx) {
|
175 | 169 | ctx.assertEquals("hello world", row.getString(2));
|
176 | 170 | }));
|
177 | 171 | }
|
| 172 | + |
| 173 | + @Test |
| 174 | + public void testInsertJsonObject(TestContext ctx) { |
| 175 | + connection.query("DROP TABLE IF EXISTS distributors").execute(ctx.asyncAssertSuccess(dropped -> { |
| 176 | + connection.query("CREATE TABLE distributors(name VARCHAR(40), attrs JSONB)").execute(ctx.asyncAssertSuccess(created -> { |
| 177 | + |
| 178 | + MyObject value = new MyObject(); |
| 179 | + value.setName("foo"); |
| 180 | + |
| 181 | + Attributes attributes = new Attributes(); |
| 182 | + Instant createdOn = Instant.now(); |
| 183 | + attributes.setCreatedOn(createdOn); |
| 184 | + List<String> stringAttributes = Arrays.asList("foo", "bar", "baz"); |
| 185 | + attributes.setStringAttributes(stringAttributes); |
| 186 | + value.setAttributes(attributes); |
| 187 | + |
| 188 | + SqlTemplate |
| 189 | + .forQuery(connection, "INSERT INTO distributors (name,attrs) VALUES(#{name},#{attributes})") |
| 190 | + .mapFrom(MyObject.class) |
| 191 | + .execute(value, ctx.asyncAssertSuccess(inserted -> { |
| 192 | + connection.query("SELECT name, attrs FROM distributors").execute(ctx.asyncAssertSuccess(rows -> { |
| 193 | + ctx.verify(v -> { |
| 194 | + assertEquals(1, rows.size()); |
| 195 | + Row row = rows.iterator().next(); |
| 196 | + assertEquals("foo", row.getValue("name")); |
| 197 | + Object object = row.getValue("attrs"); |
| 198 | + assertTrue(object instanceof JsonObject); |
| 199 | + JsonObject attrs = (JsonObject) object; |
| 200 | + assertEquals(createdOn, attrs.getInstant("createdOn")); |
| 201 | + assertEquals(stringAttributes, attrs.getJsonArray("stringAttributes").getList()); |
| 202 | + }); |
| 203 | + })); |
| 204 | + })); |
| 205 | + })); |
| 206 | + })); |
| 207 | + } |
| 208 | + |
| 209 | + @SuppressWarnings("unused") |
| 210 | + private static class MyObject { |
| 211 | + |
| 212 | + private String name; |
| 213 | + private Attributes attributes; |
| 214 | + |
| 215 | + public String getName() { |
| 216 | + return name; |
| 217 | + } |
| 218 | + |
| 219 | + public void setName(String name) { |
| 220 | + this.name = name; |
| 221 | + } |
| 222 | + |
| 223 | + public Attributes getAttributes() { |
| 224 | + return attributes; |
| 225 | + } |
| 226 | + |
| 227 | + public void setAttributes(Attributes attributes) { |
| 228 | + this.attributes = attributes; |
| 229 | + } |
| 230 | + |
| 231 | + @Override |
| 232 | + public String toString() { |
| 233 | + return "MyObject{" + |
| 234 | + "name='" + name + '\'' + |
| 235 | + ", attributes=" + attributes + |
| 236 | + '}'; |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + @SuppressWarnings("unused") |
| 241 | + private static class Attributes { |
| 242 | + |
| 243 | + private Instant createdOn; |
| 244 | + private List<String> stringAttributes; |
| 245 | + |
| 246 | + public Instant getCreatedOn() { |
| 247 | + return createdOn; |
| 248 | + } |
| 249 | + |
| 250 | + public void setCreatedOn(Instant createdOn) { |
| 251 | + this.createdOn = createdOn; |
| 252 | + } |
| 253 | + |
| 254 | + public List<String> getStringAttributes() { |
| 255 | + return stringAttributes; |
| 256 | + } |
| 257 | + |
| 258 | + public void setStringAttributes(List<String> stringAttributes) { |
| 259 | + this.stringAttributes = stringAttributes; |
| 260 | + } |
| 261 | + |
| 262 | + @Override |
| 263 | + public String toString() { |
| 264 | + return "Attributes{" + |
| 265 | + "createdOn=" + createdOn + |
| 266 | + ", stringAttributes=" + stringAttributes + |
| 267 | + '}'; |
| 268 | + } |
| 269 | + } |
178 | 270 | }
|
0 commit comments