[OLINGO-62] more tests for EDM providers
Change-Id: I95bc3475385d4124c3251837ed763bb82ff5b63a Signed-off-by: Christian Amend <chrisam@apache.org>
This commit is contained in:
parent
7955eadf02
commit
ae46250b03
|
@ -0,0 +1,81 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
* 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.odata4.commons.core.edm.provider;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import org.apache.olingo.odata4.commons.api.edm.EdmBindingTarget;
|
||||||
|
import org.apache.olingo.odata4.commons.api.edm.EdmEntityContainer;
|
||||||
|
import org.apache.olingo.odata4.commons.api.edm.EdmEntitySet;
|
||||||
|
import org.apache.olingo.odata4.commons.api.edm.EdmEntityType;
|
||||||
|
import org.apache.olingo.odata4.commons.api.edm.provider.EdmProvider;
|
||||||
|
import org.apache.olingo.odata4.commons.api.edm.provider.EntityContainerInfo;
|
||||||
|
import org.apache.olingo.odata4.commons.api.edm.provider.EntitySet;
|
||||||
|
import org.apache.olingo.odata4.commons.api.edm.provider.EntityType;
|
||||||
|
import org.apache.olingo.odata4.commons.api.edm.provider.FullQualifiedName;
|
||||||
|
import org.apache.olingo.odata4.commons.api.edm.provider.NavigationPropertyBinding;
|
||||||
|
import org.apache.olingo.odata4.commons.api.edm.provider.PropertyRef;
|
||||||
|
import org.apache.olingo.odata4.commons.api.edm.provider.Target;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class EdmEntitySetImplTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void entitySet() throws Exception {
|
||||||
|
EdmProvider provider = mock(EdmProvider.class);
|
||||||
|
EdmProviderImpl edm = new EdmProviderImpl(provider);
|
||||||
|
|
||||||
|
final FullQualifiedName typeName = new FullQualifiedName("ns", "entityType");
|
||||||
|
final EntityType entityTypeProvider = new EntityType()
|
||||||
|
.setName(typeName.getName())
|
||||||
|
.setKey(Arrays.asList(new PropertyRef().setPropertyName("Id")));
|
||||||
|
when(provider.getEntityType(typeName)).thenReturn(entityTypeProvider);
|
||||||
|
|
||||||
|
final FullQualifiedName containerName = new FullQualifiedName("ns", "container");
|
||||||
|
final EntityContainerInfo containerInfo = new EntityContainerInfo().setContainerName(containerName);
|
||||||
|
when(provider.getEntityContainerInfo(containerName)).thenReturn(containerInfo);
|
||||||
|
final EdmEntityContainer entityContainer = new EdmEntityContainerImpl(edm, provider, containerInfo);
|
||||||
|
|
||||||
|
final String entitySetName = "entitySet";
|
||||||
|
final EntitySet entitySetProvider = new EntitySet()
|
||||||
|
.setName(entitySetName)
|
||||||
|
.setType(typeName)
|
||||||
|
.setIncludeInServiceDocument(true)
|
||||||
|
.setNavigationPropertyBindings(Arrays.asList(
|
||||||
|
new NavigationPropertyBinding().setPath("path")
|
||||||
|
.setTarget(new Target().setEntityContainer(containerName).setTargetName(entitySetName))));
|
||||||
|
when(provider.getEntitySet(containerName, entitySetName)).thenReturn(entitySetProvider);
|
||||||
|
|
||||||
|
final EdmEntitySet entitySet = new EdmEntitySetImpl(edm, entityContainer, entitySetProvider);
|
||||||
|
assertEquals(entitySetName, entityContainer.getEntitySet(entitySetName).getName());
|
||||||
|
assertEquals(entitySetName, entitySet.getName());
|
||||||
|
final EdmEntityType entityType = entitySet.getEntityType();
|
||||||
|
assertEquals(typeName.getNamespace(), entityType.getNamespace());
|
||||||
|
assertEquals(typeName.getName(), entityType.getName());
|
||||||
|
assertEquals(entityContainer, entitySet.getEntityContainer());
|
||||||
|
assertNull(entitySet.getRelatedBindingTarget(null));
|
||||||
|
final EdmBindingTarget target = entitySet.getRelatedBindingTarget("path");
|
||||||
|
assertEquals(entitySetName, target.getName());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,84 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
* 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.odata4.commons.core.edm.provider;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
|
import org.apache.olingo.odata4.commons.api.edm.EdmEntityContainer;
|
||||||
|
import org.apache.olingo.odata4.commons.api.edm.EdmFunction;
|
||||||
|
import org.apache.olingo.odata4.commons.api.edm.EdmFunctionImport;
|
||||||
|
import org.apache.olingo.odata4.commons.api.edm.provider.EdmProvider;
|
||||||
|
import org.apache.olingo.odata4.commons.api.edm.provider.EntityContainerInfo;
|
||||||
|
import org.apache.olingo.odata4.commons.api.edm.provider.FullQualifiedName;
|
||||||
|
import org.apache.olingo.odata4.commons.api.edm.provider.Function;
|
||||||
|
import org.apache.olingo.odata4.commons.api.edm.provider.FunctionImport;
|
||||||
|
import org.apache.olingo.odata4.commons.api.edm.provider.Parameter;
|
||||||
|
import org.apache.olingo.odata4.commons.api.edm.provider.ReturnType;
|
||||||
|
import org.apache.olingo.odata4.commons.core.edm.primitivetype.EdmPrimitiveTypeKind;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class EdmFunctionImportImplTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void functionImport() throws Exception {
|
||||||
|
EdmProvider provider = mock(EdmProvider.class);
|
||||||
|
EdmProviderImpl edm = new EdmProviderImpl(provider);
|
||||||
|
|
||||||
|
final FullQualifiedName functionName = new FullQualifiedName("ns", "function");
|
||||||
|
final Function functionProvider = new Function()
|
||||||
|
.setName(functionName.getName())
|
||||||
|
.setParameters(Collections.<Parameter> emptyList())
|
||||||
|
.setBound(false)
|
||||||
|
.setComposable(false)
|
||||||
|
.setReturnType(new ReturnType().setType(EdmPrimitiveTypeKind.Boolean.getFullQualifiedName()));
|
||||||
|
when(provider.getFunctions(functionName)).thenReturn(Arrays.asList(functionProvider));
|
||||||
|
|
||||||
|
final FullQualifiedName containerName = new FullQualifiedName("ns", "container");
|
||||||
|
final EntityContainerInfo containerInfo = new EntityContainerInfo().setContainerName(containerName);
|
||||||
|
when(provider.getEntityContainerInfo(containerName)).thenReturn(containerInfo);
|
||||||
|
final EdmEntityContainer entityContainer = new EdmEntityContainerImpl(edm, provider, containerInfo);
|
||||||
|
|
||||||
|
final String functionImportName = "functionImport";
|
||||||
|
final FunctionImport functionImportProvider = new FunctionImport()
|
||||||
|
.setName(functionImportName)
|
||||||
|
.setFunction(functionName)
|
||||||
|
.setIncludeInServiceDocument(true);
|
||||||
|
when(provider.getFunctionImport(containerName, functionImportName)).thenReturn(functionImportProvider);
|
||||||
|
|
||||||
|
final EdmFunctionImport functionImport =
|
||||||
|
new EdmFunctionImportImpl(edm, "test", entityContainer, functionImportProvider);
|
||||||
|
assertEquals(functionImportName, entityContainer.getFunctionImport(functionImportName).getName());
|
||||||
|
assertEquals("test", functionImport.getName());
|
||||||
|
final EdmFunction function = functionImport.getFunction(Collections.<String> emptyList());
|
||||||
|
assertEquals(functionName.getNamespace(), function.getNamespace());
|
||||||
|
assertEquals(functionName.getName(), function.getName());
|
||||||
|
assertFalse(function.isBound());
|
||||||
|
assertFalse(function.isComposable());
|
||||||
|
assertEquals(EdmPrimitiveTypeKind.Boolean.getEdmPrimitiveTypeInstance(), function.getReturnType().getType());
|
||||||
|
assertEquals(entityContainer, functionImport.getEntityContainer());
|
||||||
|
assertNull(functionImport.getReturnedEntitySet());
|
||||||
|
}
|
||||||
|
}
|
|
@ -22,9 +22,11 @@ import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertNull;
|
import static org.junit.Assert.assertNull;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import org.apache.olingo.odata4.commons.api.edm.EdmException;
|
||||||
import org.apache.olingo.odata4.commons.api.edm.EdmPrimitiveType;
|
import org.apache.olingo.odata4.commons.api.edm.EdmPrimitiveType;
|
||||||
import org.apache.olingo.odata4.commons.api.edm.EdmProperty;
|
import org.apache.olingo.odata4.commons.api.edm.EdmProperty;
|
||||||
import org.apache.olingo.odata4.commons.api.edm.EdmType;
|
import org.apache.olingo.odata4.commons.api.edm.EdmType;
|
||||||
|
@ -40,89 +42,114 @@ import org.junit.Test;
|
||||||
|
|
||||||
public class EdmPropertyImplTest {
|
public class EdmPropertyImplTest {
|
||||||
|
|
||||||
public void getTypeReturnsPrimitiveType() {
|
@Test
|
||||||
EdmProviderImpl edm = new EdmProviderImpl(mock(EdmProvider.class));
|
public void getTypeReturnsPrimitiveType() {
|
||||||
Property propertyProvider = new Property();
|
EdmProviderImpl edm = new EdmProviderImpl(mock(EdmProvider.class));
|
||||||
propertyProvider.setType(EdmPrimitiveTypeKind.Binary.getFullQualifiedName());
|
Property propertyProvider = new Property();
|
||||||
final EdmProperty property = new EdmPropertyImpl(edm, propertyProvider);
|
propertyProvider.setType(EdmPrimitiveTypeKind.Binary.getFullQualifiedName());
|
||||||
assertTrue(property.isPrimitive());
|
final EdmProperty property = new EdmPropertyImpl(edm, propertyProvider);
|
||||||
final EdmType type = property.getType();
|
assertTrue(property.isPrimitive());
|
||||||
assertEquals(EdmTypeKind.PRIMITIVE, type.getKind());
|
final EdmType type = property.getType();
|
||||||
assertEquals(EdmPrimitiveType.EDM_NAMESPACE, type.getNamespace());
|
assertEquals(EdmTypeKind.PRIMITIVE, type.getKind());
|
||||||
assertEquals(EdmPrimitiveTypeKind.Binary.toString(), type.getName());
|
assertEquals(EdmPrimitiveType.EDM_NAMESPACE, type.getNamespace());
|
||||||
}
|
assertEquals(EdmPrimitiveTypeKind.Binary.toString(), type.getName());
|
||||||
|
}
|
||||||
|
|
||||||
public void getTypeReturnsComplexType() throws Exception {
|
@Test
|
||||||
EdmProvider provider = mock(EdmProvider.class);
|
public void getTypeReturnsComplexType() throws Exception {
|
||||||
EdmProviderImpl edm = new EdmProviderImpl(provider);
|
EdmProvider provider = mock(EdmProvider.class);
|
||||||
final FullQualifiedName complexTypeName = new FullQualifiedName("ns", "complex");
|
EdmProviderImpl edm = new EdmProviderImpl(provider);
|
||||||
ComplexType complexTypeProvider = new ComplexType();
|
final FullQualifiedName complexTypeName = new FullQualifiedName("ns", "complex");
|
||||||
when(provider.getComplexType(complexTypeName)).thenReturn(complexTypeProvider);
|
ComplexType complexTypeProvider = new ComplexType();
|
||||||
Property propertyProvider = new Property();
|
when(provider.getComplexType(complexTypeName)).thenReturn(complexTypeProvider);
|
||||||
propertyProvider.setType(complexTypeName);
|
Property propertyProvider = new Property();
|
||||||
final EdmProperty property = new EdmPropertyImpl(edm, propertyProvider);
|
propertyProvider.setType(complexTypeName);
|
||||||
assertFalse(property.isCollection());
|
final EdmProperty property = new EdmPropertyImpl(edm, propertyProvider);
|
||||||
assertFalse(property.isPrimitive());
|
assertFalse(property.isCollection());
|
||||||
final EdmType type = property.getType();
|
assertFalse(property.isPrimitive());
|
||||||
assertEquals(EdmTypeKind.COMPLEX, type.getKind());
|
final EdmType type = property.getType();
|
||||||
assertEquals("ns", type.getNamespace());
|
assertEquals(EdmTypeKind.COMPLEX, type.getKind());
|
||||||
assertEquals("complex", type.getName());
|
assertEquals("ns", type.getNamespace());
|
||||||
}
|
assertEquals("complex", type.getName());
|
||||||
@Test
|
}
|
||||||
public void getTypeReturnsEnumType() throws Exception {
|
|
||||||
EdmProvider provider = mock(EdmProvider.class);
|
@Test
|
||||||
EdmProviderImpl edm = new EdmProviderImpl(provider);
|
public void getTypeReturnsEnumType() throws Exception {
|
||||||
final FullQualifiedName enumTypeName = new FullQualifiedName("ns", "enum");
|
EdmProvider provider = mock(EdmProvider.class);
|
||||||
EnumType enumTypeProvider = new EnumType();
|
EdmProviderImpl edm = new EdmProviderImpl(provider);
|
||||||
when(provider.getEnumType(enumTypeName)).thenReturn(enumTypeProvider);
|
final FullQualifiedName enumTypeName = new FullQualifiedName("ns", "enum");
|
||||||
Property propertyProvider = new Property();
|
EnumType enumTypeProvider = new EnumType();
|
||||||
propertyProvider.setType(enumTypeName);
|
when(provider.getEnumType(enumTypeName)).thenReturn(enumTypeProvider);
|
||||||
final EdmProperty property = new EdmPropertyImpl(edm, propertyProvider);
|
Property propertyProvider = new Property();
|
||||||
assertFalse(property.isCollection());
|
propertyProvider.setType(enumTypeName);
|
||||||
assertFalse(property.isPrimitive());
|
final EdmProperty property = new EdmPropertyImpl(edm, propertyProvider);
|
||||||
final EdmType type = property.getType();
|
assertFalse(property.isCollection());
|
||||||
assertEquals(EdmTypeKind.ENUM, type.getKind());
|
assertFalse(property.isPrimitive());
|
||||||
assertEquals("ns", type.getNamespace());
|
final EdmType type = property.getType();
|
||||||
assertEquals("enum", type.getName());
|
assertEquals(EdmTypeKind.ENUM, type.getKind());
|
||||||
}
|
assertEquals("ns", type.getNamespace());
|
||||||
|
assertEquals("enum", type.getName());
|
||||||
@Test
|
}
|
||||||
public void getTypeReturnsTypeDefinition() throws Exception {
|
|
||||||
EdmProvider provider = mock(EdmProvider.class);
|
@Test
|
||||||
EdmProviderImpl edm = new EdmProviderImpl(provider);
|
public void getTypeReturnsTypeDefinition() throws Exception {
|
||||||
final FullQualifiedName typeName = new FullQualifiedName("ns", "definition");
|
EdmProvider provider = mock(EdmProvider.class);
|
||||||
TypeDefinition typeProvider = new TypeDefinition().setUnderlyingType(new FullQualifiedName("Edm", "String"));
|
EdmProviderImpl edm = new EdmProviderImpl(provider);
|
||||||
when(provider.getTypeDefinition(typeName)).thenReturn(typeProvider);
|
final FullQualifiedName typeName = new FullQualifiedName("ns", "definition");
|
||||||
Property propertyProvider = new Property();
|
TypeDefinition typeProvider = new TypeDefinition().setUnderlyingType(new FullQualifiedName("Edm", "String"));
|
||||||
propertyProvider.setType(typeName);
|
when(provider.getTypeDefinition(typeName)).thenReturn(typeProvider);
|
||||||
final EdmProperty property = new EdmPropertyImpl(edm, propertyProvider);
|
Property propertyProvider = new Property();
|
||||||
assertFalse(property.isPrimitive());
|
propertyProvider.setType(typeName);
|
||||||
final EdmType type = property.getType();
|
final EdmProperty property = new EdmPropertyImpl(edm, propertyProvider);
|
||||||
assertEquals(EdmTypeKind.DEFINITION, type.getKind());
|
assertFalse(property.isPrimitive());
|
||||||
assertEquals("ns", type.getNamespace());
|
final EdmType type = property.getType();
|
||||||
assertEquals("definition", type.getName());
|
assertEquals(EdmTypeKind.DEFINITION, type.getKind());
|
||||||
}
|
assertEquals("ns", type.getNamespace());
|
||||||
|
assertEquals("definition", type.getName());
|
||||||
@Test
|
}
|
||||||
public void facets() {
|
|
||||||
EdmProviderImpl edm = new EdmProviderImpl(mock(EdmProvider.class));
|
@Test(expected = EdmException.class)
|
||||||
Property propertyProvider = new Property();
|
public void getTypeReturnsWrongType() throws Exception {
|
||||||
propertyProvider.setType(EdmPrimitiveTypeKind.String.getFullQualifiedName());
|
EdmProvider provider = mock(EdmProvider.class);
|
||||||
propertyProvider.setPrecision(42);
|
EdmProviderImpl edm = new EdmProviderImpl(provider);
|
||||||
propertyProvider.setScale(12);
|
final Property propertyProvider = new Property()
|
||||||
propertyProvider.setMaxLength(128);
|
.setType(new FullQualifiedName("ns", "wrong"));
|
||||||
propertyProvider.setUnicode(true);
|
final EdmProperty property = new EdmPropertyImpl(edm, propertyProvider);
|
||||||
propertyProvider.setNullable(false);
|
property.getType();
|
||||||
propertyProvider.setDefaultValue("x");
|
fail();
|
||||||
final EdmProperty property = new EdmPropertyImpl(edm, propertyProvider);
|
}
|
||||||
assertTrue(property.isPrimitive());
|
|
||||||
assertNull(property.getMapping());
|
@Test(expected = IllegalArgumentException.class)
|
||||||
assertNull(property.getMimeType());
|
public void getTypeReturnsNoTypeKind() throws Exception {
|
||||||
assertEquals(Integer.valueOf(42), property.getPrecision());
|
EdmProvider provider = mock(EdmProvider.class);
|
||||||
assertEquals(Integer.valueOf(12), property.getScale());
|
EdmProviderImpl edm = new EdmProviderImpl(provider);
|
||||||
assertEquals(Integer.valueOf(128), property.getMaxLength());
|
final Property propertyProvider = new Property()
|
||||||
assertTrue(property.isUnicode());
|
.setType(new FullQualifiedName(EdmPrimitiveType.EDM_NAMESPACE, "type"));
|
||||||
assertFalse(property.isNullable());
|
final EdmProperty property = new EdmPropertyImpl(edm, propertyProvider);
|
||||||
assertEquals("x", property.getDefaultValue());
|
property.getType();
|
||||||
}
|
fail();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void facets() {
|
||||||
|
EdmProviderImpl edm = new EdmProviderImpl(mock(EdmProvider.class));
|
||||||
|
Property propertyProvider = new Property();
|
||||||
|
propertyProvider.setType(EdmPrimitiveTypeKind.String.getFullQualifiedName());
|
||||||
|
propertyProvider.setPrecision(42);
|
||||||
|
propertyProvider.setScale(12);
|
||||||
|
propertyProvider.setMaxLength(128);
|
||||||
|
propertyProvider.setUnicode(true);
|
||||||
|
propertyProvider.setNullable(false);
|
||||||
|
propertyProvider.setDefaultValue("x");
|
||||||
|
final EdmProperty property = new EdmPropertyImpl(edm, propertyProvider);
|
||||||
|
assertTrue(property.isPrimitive());
|
||||||
|
assertNull(property.getMapping());
|
||||||
|
assertNull(property.getMimeType());
|
||||||
|
assertEquals(Integer.valueOf(42), property.getPrecision());
|
||||||
|
assertEquals(Integer.valueOf(12), property.getScale());
|
||||||
|
assertEquals(Integer.valueOf(128), property.getMaxLength());
|
||||||
|
assertTrue(property.isUnicode());
|
||||||
|
assertFalse(property.isNullable());
|
||||||
|
assertEquals("x", property.getDefaultValue());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,122 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
* 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.odata4.commons.core.edm.provider;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import org.apache.olingo.odata4.commons.api.edm.EdmBindingTarget;
|
||||||
|
import org.apache.olingo.odata4.commons.api.edm.EdmEntityContainer;
|
||||||
|
import org.apache.olingo.odata4.commons.api.edm.EdmEntityType;
|
||||||
|
import org.apache.olingo.odata4.commons.api.edm.EdmException;
|
||||||
|
import org.apache.olingo.odata4.commons.api.edm.EdmSingleton;
|
||||||
|
import org.apache.olingo.odata4.commons.api.edm.provider.EdmProvider;
|
||||||
|
import org.apache.olingo.odata4.commons.api.edm.provider.EntityContainerInfo;
|
||||||
|
import org.apache.olingo.odata4.commons.api.edm.provider.EntityType;
|
||||||
|
import org.apache.olingo.odata4.commons.api.edm.provider.FullQualifiedName;
|
||||||
|
import org.apache.olingo.odata4.commons.api.edm.provider.NavigationPropertyBinding;
|
||||||
|
import org.apache.olingo.odata4.commons.api.edm.provider.PropertyRef;
|
||||||
|
import org.apache.olingo.odata4.commons.api.edm.provider.Singleton;
|
||||||
|
import org.apache.olingo.odata4.commons.api.edm.provider.Target;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class EdmSingletonImplTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void singleton() throws Exception {
|
||||||
|
EdmProvider provider = mock(EdmProvider.class);
|
||||||
|
EdmProviderImpl edm = new EdmProviderImpl(provider);
|
||||||
|
|
||||||
|
final FullQualifiedName typeName = new FullQualifiedName("ns", "entityType");
|
||||||
|
final EntityType entityTypeProvider = new EntityType()
|
||||||
|
.setName(typeName.getName())
|
||||||
|
.setKey(Arrays.asList(new PropertyRef().setPropertyName("Id")));
|
||||||
|
when(provider.getEntityType(typeName)).thenReturn(entityTypeProvider);
|
||||||
|
|
||||||
|
final FullQualifiedName containerName = new FullQualifiedName("ns", "container");
|
||||||
|
final EntityContainerInfo containerInfo = new EntityContainerInfo().setContainerName(containerName);
|
||||||
|
when(provider.getEntityContainerInfo(containerName)).thenReturn(containerInfo);
|
||||||
|
final EdmEntityContainer entityContainer = new EdmEntityContainerImpl(edm, provider, containerInfo);
|
||||||
|
|
||||||
|
final String singletonName = "singleton";
|
||||||
|
final Singleton singletonProvider = new Singleton()
|
||||||
|
.setName(singletonName)
|
||||||
|
.setType(typeName)
|
||||||
|
.setNavigationPropertyBindings(Arrays.asList(
|
||||||
|
new NavigationPropertyBinding().setPath("path")
|
||||||
|
.setTarget(new Target().setEntityContainer(containerName).setTargetName(singletonName))));
|
||||||
|
when(provider.getSingleton(containerName, singletonName)).thenReturn(singletonProvider);
|
||||||
|
|
||||||
|
final EdmSingleton singleton = new EdmSingletonImpl(edm, entityContainer, singletonProvider);
|
||||||
|
assertEquals(singletonName, entityContainer.getSingleton(singletonName).getName());
|
||||||
|
assertEquals(singletonName, singleton.getName());
|
||||||
|
final EdmEntityType entityType = singleton.getEntityType();
|
||||||
|
assertEquals(typeName.getNamespace(), entityType.getNamespace());
|
||||||
|
assertEquals(typeName.getName(), entityType.getName());
|
||||||
|
assertEquals(entityContainer, singleton.getEntityContainer());
|
||||||
|
assertNull(singleton.getRelatedBindingTarget(null));
|
||||||
|
final EdmBindingTarget target = singleton.getRelatedBindingTarget("path");
|
||||||
|
assertEquals(singletonName, target.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = EdmException.class)
|
||||||
|
public void wrongTarget() throws Exception {
|
||||||
|
EdmProvider provider = mock(EdmProvider.class);
|
||||||
|
EdmProviderImpl edm = new EdmProviderImpl(provider);
|
||||||
|
|
||||||
|
final FullQualifiedName containerName = new FullQualifiedName("ns", "container");
|
||||||
|
final EntityContainerInfo containerInfo = new EntityContainerInfo().setContainerName(containerName);
|
||||||
|
when(provider.getEntityContainerInfo(containerName)).thenReturn(containerInfo);
|
||||||
|
|
||||||
|
final String singletonName = "singleton";
|
||||||
|
final Singleton singletonProvider = new Singleton()
|
||||||
|
.setNavigationPropertyBindings(Arrays.asList(
|
||||||
|
new NavigationPropertyBinding().setPath("path")
|
||||||
|
.setTarget(new Target().setEntityContainer(containerName).setTargetName("wrong"))));
|
||||||
|
when(provider.getSingleton(containerName, singletonName)).thenReturn(singletonProvider);
|
||||||
|
|
||||||
|
final EdmSingleton singleton = new EdmSingletonImpl(edm, null, singletonProvider);
|
||||||
|
singleton.getRelatedBindingTarget("path");
|
||||||
|
fail();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = EdmException.class)
|
||||||
|
public void wrongTargetContainer() throws Exception {
|
||||||
|
EdmProvider provider = mock(EdmProvider.class);
|
||||||
|
EdmProviderImpl edm = new EdmProviderImpl(provider);
|
||||||
|
|
||||||
|
final FullQualifiedName containerName = new FullQualifiedName("ns", "container");
|
||||||
|
final String singletonName = "singleton";
|
||||||
|
final Singleton singletonProvider = new Singleton()
|
||||||
|
.setNavigationPropertyBindings(Arrays.asList(
|
||||||
|
new NavigationPropertyBinding().setPath("path")
|
||||||
|
.setTarget(new Target().setEntityContainer(new FullQualifiedName("ns", "wrongContainer"))
|
||||||
|
.setTargetName(singletonName))));
|
||||||
|
when(provider.getSingleton(containerName, singletonName)).thenReturn(singletonProvider);
|
||||||
|
|
||||||
|
final EdmSingleton singleton = new EdmSingletonImpl(edm, null, singletonProvider);
|
||||||
|
singleton.getRelatedBindingTarget("path");
|
||||||
|
fail();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue