[OLINGO-1114] Minor test fix

This commit is contained in:
Michael Bolz 2019-11-26 07:57:47 +01:00
parent 820b462f49
commit 35e2302576
1 changed files with 45 additions and 51 deletions

View File

@ -5,7 +5,7 @@
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@ -15,7 +15,11 @@
*/
package org.apache.olingo.client.core.serialization;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.io.StringWriter;
import org.apache.olingo.client.api.ODataClient;
import org.apache.olingo.client.api.domain.ClientEntity;
import org.apache.olingo.client.api.domain.ClientObjectFactory;
@ -23,57 +27,47 @@ import org.apache.olingo.client.api.serialization.ODataSerializerException;
import org.apache.olingo.client.core.ODataClientFactory;
import org.apache.olingo.commons.api.edm.FullQualifiedName;
import org.apache.olingo.commons.api.format.ContentType;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Test;
public class JsonSerializerTest {
@Test
public void testClientEntityJSONWithNull() throws ODataSerializerException {
String expectedJson = "{\"@odata.type\":\"#test.testClientEntity\","
+ "\"testInt32@odata.type\":\"Int32\","
+ "\"testInt32\":12,"
+ "\"testInt32Null@odata.type\":\"Int32\""
+ ",\"testInt32Null\":null,"
+ "\"testString@odata.type\":\"String\","
+ "\"testString\":\"testString\","
+ "\"testStringNull@odata.type\":\"String\","
+ "\"testStringNull\":null}";
ODataClient odataClient = ODataClientFactory.getClient();
ClientObjectFactory objFactory = odataClient.getObjectFactory();
ClientEntity clientEntity = objFactory.newEntity(new FullQualifiedName("test", "testClientEntity"));
clientEntity.getProperties().add(
objFactory.newPrimitiveProperty(
"testInt32",
objFactory.newPrimitiveValueBuilder().buildInt32(12)
)
);
clientEntity.getProperties().add(
objFactory.newPrimitiveProperty(
"testInt32Null",
objFactory.newPrimitiveValueBuilder().buildInt32(null)
)
);
clientEntity.getProperties().add(
objFactory.newPrimitiveProperty(
"testString",
objFactory.newPrimitiveValueBuilder().buildString("testString")
)
);
clientEntity.getProperties().add(
objFactory.newPrimitiveProperty(
"testStringNull",
objFactory.newPrimitiveValueBuilder().buildString(null)
)
);
JsonSerializer jsonSerializer = new JsonSerializer(false, ContentType.JSON);
StringWriter writer = new StringWriter();
jsonSerializer.write(writer, odataClient.getBinder().getEntity(clientEntity));
assertThat(writer.toString(), is(expectedJson));
}
@Test
public void testClientEntityJSONWithNull() throws ODataSerializerException {
String expectedJson = "{\"@odata.type\":\"#test.testClientEntity\","
+ "\"testInt32@odata.type\":\"Int32\","
+ "\"testInt32\":12,"
+ "\"testInt32Null@odata.type\":\"Int32\""
+ ",\"testInt32Null\":null,"
+ "\"testString@odata.type\":\"String\","
+ "\"testString\":\"testString\","
+ "\"testStringNull@odata.type\":\"String\","
+ "\"testStringNull\":null}";
ODataClient odataClient = ODataClientFactory.getClient();
ClientObjectFactory objFactory = odataClient.getObjectFactory();
ClientEntity clientEntity = objFactory.newEntity(new FullQualifiedName("test", "testClientEntity"));
clientEntity.getProperties().add(
objFactory.newPrimitiveProperty(
"testInt32",
objFactory.newPrimitiveValueBuilder().buildInt32(12)));
clientEntity.getProperties().add(
objFactory.newPrimitiveProperty(
"testInt32Null",
objFactory.newPrimitiveValueBuilder().buildInt32(null)));
clientEntity.getProperties().add(
objFactory.newPrimitiveProperty(
"testString",
objFactory.newPrimitiveValueBuilder().buildString("testString")));
clientEntity.getProperties().add(
objFactory.newPrimitiveProperty(
"testStringNull",
objFactory.newPrimitiveValueBuilder().buildString(null)));
JsonSerializer jsonSerializer = new JsonSerializer(false, ContentType.JSON_FULL_METADATA);
StringWriter writer = new StringWriter();
jsonSerializer.write(writer, odataClient.getBinder().getEntity(clientEntity));
assertThat(writer.toString(), is(expectedJson));
}
}