[OLINGO-499][OLINGO-501] Serialization of complex collections works properly

Signed-off-by: Michael Bolz <michael.bolz@sap.com>
This commit is contained in:
Christian Holzer 2014-12-02 13:57:52 +01:00 committed by Michael Bolz
parent 2318ffad20
commit 5f18ea2e09
3 changed files with 138 additions and 1 deletions

View File

@ -370,7 +370,7 @@ public class ODataJsonSerializer implements ODataSerializer {
writeComplexValue(type, ((LinkedComplexValue) value).getValue(), selectedPaths, json);
break;
case COLLECTION_COMPLEX:
writeComplexValue(type, property.asComplex(), selectedPaths, json);
writeComplexValue(type, ((Property) value).asComplex(), selectedPaths, json);
break;
default:
throw new SerializerException("Property type not yet supported!",

View File

@ -0,0 +1,66 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); 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
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.olingo.server.core.serializer.json;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.List;
import org.apache.olingo.commons.api.ODataException;
import org.apache.olingo.commons.api.edm.EdmComplexType;
import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind;
import org.apache.olingo.commons.api.edm.FullQualifiedName;
import org.apache.olingo.server.api.edm.provider.ComplexType;
import org.apache.olingo.server.api.edm.provider.EdmProvider;
import org.apache.olingo.server.api.edm.provider.NavigationProperty;
import org.apache.olingo.server.api.edm.provider.Property;
import org.apache.olingo.server.core.edm.provider.EdmComplexTypeImpl;
import org.apache.olingo.server.core.edm.provider.EdmProviderImpl;
public class ComplexTypeHelper {
public static EdmComplexType createType() throws ODataException {
EdmProvider provider = mock(EdmProvider.class);
EdmProviderImpl edm = new EdmProviderImpl(provider);
FullQualifiedName baseName = new FullQualifiedName("namespace", "BaseTypeName");
ComplexType baseComplexType = new ComplexType();
List<Property> baseProperties = new ArrayList<Property>();
baseProperties.add(new Property().setName("prop1").setType(EdmPrimitiveTypeKind.String.getFullQualifiedName()));
List<NavigationProperty> baseNavigationProperties = new ArrayList<NavigationProperty>();
baseNavigationProperties.add(new NavigationProperty().setName("nav1"));
baseComplexType.setName("BaseTypeName").setAbstract(false).setOpenType(false).setProperties(baseProperties)
.setNavigationProperties(baseNavigationProperties);
when(provider.getComplexType(baseName)).thenReturn(baseComplexType);
FullQualifiedName name = new FullQualifiedName("namespace", "typeName");
ComplexType complexType = new ComplexType().setBaseType(baseName);
List<Property> properties = new ArrayList<Property>();
properties.add(new Property().setName("prop2").setType(EdmPrimitiveTypeKind.String.getFullQualifiedName()));
List<NavigationProperty> navigationProperties = new ArrayList<NavigationProperty>();
navigationProperties.add(new NavigationProperty().setName("nav2"));
complexType.setName("BaseTypeName").setAbstract(false).setOpenType(false).setProperties(properties)
.setNavigationProperties(navigationProperties);
when(provider.getComplexType(name)).thenReturn(complexType);
return EdmComplexTypeImpl.getInstance(edm, name, complexType);
}
}

View File

@ -0,0 +1,71 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); 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
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.olingo.server.core.serializer.json;
import static org.junit.Assert.assertEquals;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.olingo.commons.api.ODataException;
import org.apache.olingo.commons.api.data.ContextURL;
import org.apache.olingo.commons.api.data.Property;
import org.apache.olingo.commons.api.data.ValueType;
import org.apache.olingo.commons.api.format.ODataFormat;
import org.apache.olingo.commons.core.data.PropertyImpl;
import org.apache.olingo.server.api.serializer.ComplexSerializerOptions;
import org.junit.Test;
public class ODataJsonSerializerTest {
@Test
public void testCollectionComplex() throws ODataException, IOException {
final List<Property> col = new ArrayList<Property>();
col.add(new PropertyImpl(null, "ComplexOne", ValueType.COMPLEX, getValues(1)));
col.add(new PropertyImpl(null, "ComplexTwo", ValueType.COMPLEX, getValues(2)));
final Property complexCollection = new PropertyImpl(null, "ComplexCol", ValueType.COLLECTION_COMPLEX, col);
final ODataJsonSerializer serializer = new ODataJsonSerializer(ODataFormat.APPLICATION_JSON);
final ComplexSerializerOptions options = ComplexSerializerOptions.with()
.contextURL(ContextURL.with().selectList("ComplexCollection").build()).build();
final InputStream in = serializer.complexCollection(ComplexTypeHelper.createType(), complexCollection, options);
final BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
if (line.contains("value")) {
assertEquals("{\"@odata.context\":\"$metadata(ComplexCollection)\",\"value\":"
+ "[{\"prop1\":\"test1\",\"prop2\":\"test11\"},{\"prop1\":\"test2\",\"prop2\":\"test22\"}]}", line);
}
}
}
private List<Property> getValues(int i) {
final List<Property> values = new ArrayList<Property>();
values.add(new PropertyImpl(null, "prop1", ValueType.PRIMITIVE, "test" + i));
values.add(new PropertyImpl(null, "prop2", ValueType.PRIMITIVE, "test" + i + i));
return values;
}
}