Skip to content

Commit 3db02ee

Browse files
committed
updated unit tests
1 parent 882bb18 commit 3db02ee

File tree

10 files changed

+552
-13
lines changed

10 files changed

+552
-13
lines changed

java-client/src/main/java/co/elastic/clients/json/jackson/Jackson3JsonProvider.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import jakarta.json.stream.JsonGeneratorFactory;
3535
import jakarta.json.stream.JsonParser;
3636
import jakarta.json.stream.JsonParserFactory;
37+
import tools.jackson.core.ObjectReadContext;
3738
import tools.jackson.core.json.JsonFactory;
3839

3940
import java.io.InputStream;
@@ -102,17 +103,17 @@ private class ParserFactory implements JsonParserFactory {
102103

103104
@Override
104105
public JsonParser createParser(Reader reader) {
105-
return new Jackson3JsonpParser(jsonFactory.createParser(reader), mapper);
106+
return new Jackson3JsonpParser(mapper.objectMapper().createParser(reader), mapper);
106107
}
107108

108109
@Override
109110
public JsonParser createParser(InputStream in) {
110-
return new Jackson3JsonpParser(jsonFactory.createParser(in), mapper);
111+
return new Jackson3JsonpParser(mapper.objectMapper().createParser(in), mapper);
111112
}
112113

113114
@Override
114115
public JsonParser createParser(InputStream in, Charset charset) {
115-
return new Jackson3JsonpParser(jsonFactory.createParser(new InputStreamReader(in, charset)),
116+
return new Jackson3JsonpParser(mapper.objectMapper().createParser(new InputStreamReader(in, charset)),
116117
mapper);
117118
}
118119

java-client/src/main/java/co/elastic/clients/json/jackson/Jackson3JsonpGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static class Buffering extends Jackson3JsonpGenerator implements Bufferin
5050
private final Jackson3JsonpMapper mapper;
5151

5252
public Buffering(Jackson3JsonpMapper mapper) {
53-
super(new TokenBuffer(mapper.objectMapper()._serializationContext(), false));
53+
super(TokenBuffer.forGeneration()); // TODO probably not
5454
this.mapper = mapper;
5555
}
5656

java-client/src/main/java/co/elastic/clients/json/jackson/Jackson3JsonpMapper.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ private static JsonMapper configure(JsonMapper builder) {
7070
// Accept single objects as collections. This is useful in the context of Elasticsearch since
7171
// Lucene has no concept of multivalued field and fields with a single value will be returned
7272
// as a single object even if other instances of the same field have multiple values.
73-
return builder.rebuild().enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY).build();
73+
return builder.rebuild()
74+
.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
75+
.disable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
76+
.build();
7477
}
7578

7679
@Override
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package co.elastic.clients.json.jackson;
21+
22+
import org.apache.commons.lang3.StringUtils;
23+
import org.junit.jupiter.api.Test;
24+
import tools.jackson.databind.json.JsonMapper;
25+
import tools.jackson.databind.util.TokenBuffer;
26+
27+
import static org.junit.jupiter.api.Assertions.assertEquals;
28+
import static org.junit.jupiter.api.Assertions.assertTrue;
29+
30+
public class Jackson3JsonBufferTest {
31+
32+
private Jackson3JsonpMapper mapper() {
33+
return new Jackson3JsonpMapper(new JsonMapper());
34+
}
35+
36+
@Test
37+
public void testToString_rendersJson() {
38+
TokenBuffer buffer = TokenBuffer.forGeneration();
39+
buffer.writeStartObject();
40+
buffer.writeStringProperty("foo", "bar");
41+
buffer.writeNumberProperty("baz", 42);
42+
buffer.writeEndObject();
43+
44+
Jackson3JsonBuffer jsonBuffer = new Jackson3JsonBuffer(buffer, mapper());
45+
String json = jsonBuffer.toString();
46+
assertTrue(json.contains("\"foo\":\"bar\""));
47+
assertTrue(json.contains("\"baz\":42"));
48+
assertTrue(json.startsWith("{") && json.endsWith("}"));
49+
}
50+
51+
@Test
52+
public void testToString_emptyBuffer() {
53+
TokenBuffer buffer = TokenBuffer.forGeneration();
54+
Jackson3JsonBuffer jsonBuffer = new Jackson3JsonBuffer(buffer, mapper());
55+
String json = jsonBuffer.toString();
56+
assertEquals(StringUtils.EMPTY, json.trim());
57+
}
58+
59+
@Test
60+
public void testToString_invalidBuffer() {
61+
TokenBuffer buffer = TokenBuffer.forGeneration();
62+
63+
// Write some invalid JSON (start object but don't close)
64+
buffer.writeStartObject();
65+
Jackson3JsonBuffer jsonBuffer = new Jackson3JsonBuffer(buffer, mapper());
66+
String json = jsonBuffer.toString();
67+
// Should not throw, but may contain error or partial output
68+
assertTrue(json.startsWith("{"));
69+
}
70+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package co.elastic.clients.json.jackson;
21+
22+
import co.elastic.clients.json.JsonpMapper;
23+
import co.elastic.clients.json.JsonpUtils;
24+
import jakarta.json.JsonObject;
25+
import jakarta.json.JsonValue;
26+
import jakarta.json.stream.JsonParser;
27+
import org.junit.jupiter.api.Assertions;
28+
import org.junit.jupiter.api.Test;
29+
30+
import java.io.StringReader;
31+
import java.util.Map;
32+
33+
public class Jackson3JsonValueParserTest extends Assertions {
34+
35+
public static class Data {
36+
public Map<String, Object> data;
37+
}
38+
39+
@Test
40+
public void testFloatsShouldDeserializeAsFloats() throws Exception {
41+
// When using Jackson to target a map of objects, values with a decimal separator
42+
// should deserialize as a double even if they fit in an int or long.
43+
// See https://github.com/elastic/elasticsearch-java/issues/156
44+
45+
String json = "{\"data\": {\"value\": 1.4778125E7, \"value2\": 1.4778125E7 }}";
46+
JsonpMapper mapper = new Jackson3JsonpMapper();
47+
48+
{
49+
JsonParser parser = mapper.jsonProvider().createParser(new StringReader(json));
50+
Data data = mapper.deserialize(parser, Data.class);
51+
52+
Double d = (Double) data.data.get("value");
53+
assertEquals(1.4778125E7, d, 0.001);
54+
}
55+
56+
{
57+
// Test with buffering used in union resolution
58+
JsonParser parser = mapper.jsonProvider().createParser(new StringReader(json));
59+
parser.next();
60+
JsonObject object = parser.getObject();
61+
62+
// Test equals/hashcode
63+
JsonValue v = object.getJsonObject("data").get("value");
64+
JsonValue v2 = object.getJsonObject("data").get("value2");
65+
66+
assertEquals(v.hashCode(), v2.hashCode());
67+
assertEquals(v, v2);
68+
69+
parser = JsonpUtils.jsonValueParser(object, mapper);
70+
Data data = mapper.deserialize(parser, Data.class);
71+
72+
Double d = (Double) data.data.get("value");
73+
assertEquals(1.4778125E7, d, 0.001);
74+
}
75+
76+
}
77+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package co.elastic.clients.json.jackson;
21+
22+
import jakarta.json.Json;
23+
import jakarta.json.JsonValue;
24+
import jakarta.json.stream.JsonGenerator;
25+
import org.junit.jupiter.api.Assertions;
26+
import org.junit.jupiter.api.Test;
27+
28+
import java.io.StringWriter;
29+
30+
public class Jackson3JsonpGeneratorTest extends Assertions {
31+
32+
@Test
33+
public void testWrite(){
34+
StringWriter sw = new StringWriter();
35+
JsonGenerator generator = new Jackson3JsonpMapper().jsonProvider().createGenerator(sw);
36+
37+
generator.writeStartObject();
38+
39+
// Boolean
40+
generator.write("bool1", true);
41+
generator.writeKey("bool2");
42+
generator.write(false);
43+
44+
// String
45+
generator.write("str1", "foo");
46+
generator.writeKey("str2");
47+
generator.write("bar");
48+
49+
// Integer
50+
generator.write("int1", 42);
51+
generator.writeKey("int2");
52+
generator.write(1337);
53+
54+
// Long
55+
generator.write("long1", 123456789012345L);
56+
generator.writeKey("long2");
57+
generator.write(123456789012345L);
58+
59+
generator.write("double1", 0.001);
60+
generator.writeKey("double2");
61+
generator.write(12345.6789);
62+
63+
// JsonValue
64+
JsonValue jsonValue = Json.createObjectBuilder()
65+
.add("bool", true)
66+
.add("str", "foo")
67+
.add("int", 42)
68+
.add("long", 123456789012345L)
69+
.add("double", 12345.6789)
70+
.build();
71+
72+
generator.write("value", jsonValue);
73+
74+
generator.close();
75+
76+
assertEquals("{" +
77+
"\"bool1\":true," +
78+
"\"bool2\":false," +
79+
"\"str1\":\"foo\"," +
80+
"\"str2\":\"bar\"," +
81+
"\"int1\":42," +
82+
"\"int2\":1337," +
83+
"\"long1\":123456789012345," +
84+
"\"long2\":123456789012345," +
85+
"\"double1\":0.001," +
86+
"\"double2\":12345.6789," +
87+
"\"value\":{\"bool\":true,\"str\":\"foo\",\"int\":42,\"long\":123456789012345,\"double\":12345.6789}" +
88+
"}", sw.toString());
89+
}
90+
}

0 commit comments

Comments
 (0)