[OLINGO-755] Fixed in UriParseTreeVisitor and added test case

This commit is contained in:
mibo 2015-08-12 15:23:35 +02:00
parent 790ec76011
commit 2ca8e0d9d1
3 changed files with 167 additions and 43 deletions

View File

@ -18,10 +18,6 @@
*/
package org.apache.olingo.server.core.uri.parser;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.antlr.v4.runtime.misc.NotNull;
import org.antlr.v4.runtime.misc.ParseCancellationException;
import org.antlr.v4.runtime.tree.ParseTree;
@ -46,7 +42,9 @@ import org.apache.olingo.commons.api.edm.EdmType;
import org.apache.olingo.commons.api.edm.FullQualifiedName;
import org.apache.olingo.commons.core.edm.primitivetype.EdmPrimitiveTypeFactory;
import org.apache.olingo.server.api.uri.UriInfoKind;
import org.apache.olingo.server.api.uri.UriInfoResource;
import org.apache.olingo.server.api.uri.UriResource;
import org.apache.olingo.server.api.uri.UriResourceEntitySet;
import org.apache.olingo.server.api.uri.UriResourcePartTyped;
import org.apache.olingo.server.api.uri.queryoption.expression.BinaryOperatorKind;
import org.apache.olingo.server.api.uri.queryoption.expression.MethodKind;
@ -185,6 +183,10 @@ import org.apache.olingo.server.core.uri.queryoption.expression.MethodImpl;
import org.apache.olingo.server.core.uri.queryoption.expression.TypeLiteralImpl;
import org.apache.olingo.server.core.uri.queryoption.expression.UnaryImpl;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* UriVisitor
*
@ -1226,9 +1228,43 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor<Object> {
context.contextUriInfo = uriInfoResourceBU;
context.contextExpandItemPath = contextExpandItemPathBU;
// test
validate(uriInfoResourceBU.asUriInfoResource(), expandItem);
//
return expandItem;
}
private void validate(UriInfoResource uriInfoResource, ExpandItemImpl expandItem) {
if(uriInfoResource != null) {
EdmEntityType type = getEntityType(uriInfoResource);
EdmEntityType name = getEntityType(expandItem.getResourcePath());
if(name != null && type != null) {
EdmElement property = type.getProperty(name.getName());
if(! (property instanceof EdmNavigationProperty)) {
throw wrap(new UriParserSemanticException("NavigationProperty '" + name.getName() + "' not found in type '" +
type.getFullQualifiedName().getFullQualifiedNameAsString() + "'",
UriParserSemanticException.MessageKeys.EXPRESSION_PROPERTY_NOT_IN_TYPE,
name.getFullQualifiedName().getFullQualifiedNameAsString(),
type.getFullQualifiedName().getFullQualifiedNameAsString()));
}
}
}
}
private EdmEntityType getEntityType(UriInfoResource test) {
List<UriResource> parts = test.getUriResourceParts();
if (!parts.isEmpty()) {
UriResource lastPart = parts.get(parts.size() - 1);
if (lastPart instanceof UriResourceEntitySet) {
UriResourceEntitySet entitySet = (UriResourceEntitySet) lastPart;
return entitySet.getEntityType();
}
}
return null;
}
@Override
public Object visitExpandPathExtension(final ExpandPathExtensionContext ctx) {
List<SystemQueryOptionImpl> list = new ArrayList<SystemQueryOptionImpl>();

View File

@ -2078,45 +2078,6 @@ public class TestFullResourcePath {
.isCount();
}
/**
* Test for EntitySet and NavigationProperty with same name defined in metadata.
* (related to Olingo issue OLINGO-741)
*/
@Test
public void yetAnotherSmallTest() throws Exception {
TestUriValidator testUri = new TestUriValidator();
Edm mockEdm = Mockito.mock(Edm.class);
EdmEntitySet esCategory = Mockito.mock(EdmEntitySet.class);
EdmEntitySet esProduct = Mockito.mock(EdmEntitySet.class);
EdmEntityType typeCategory = Mockito.mock(EdmEntityType.class);
EdmEntityContainer container = Mockito.mock(EdmEntityContainer.class);
EdmNavigationProperty productsNavigation = Mockito.mock(EdmNavigationProperty.class);
EdmEntityType productsType = Mockito.mock(EdmEntityType.class);
Mockito.when(mockEdm.getEntityContainer(null)).thenReturn(container);
Mockito.when(typeCategory.getName()).thenReturn("Category");
Mockito.when(typeCategory.getNamespace()).thenReturn("NS");
Mockito.when(esCategory.getEntityType()).thenReturn(typeCategory);
Mockito.when(productsNavigation.getName()).thenReturn("Products");
Mockito.when(typeCategory.getProperty("Products")).thenReturn(productsNavigation);
Mockito.when(container.getEntitySet("Category")).thenReturn(esCategory);
Mockito.when(container.getEntitySet("Products")).thenReturn(esProduct);
Mockito.when(productsType.getName()).thenReturn("Products");
Mockito.when(productsType.getNamespace()).thenReturn("NS");
Mockito.when(productsNavigation.getType()).thenReturn(productsType);
// test and verify
testUri.setEdm(mockEdm)
.run("Category", "$expand=Products")
.isKind(UriInfoKind.resource).goPath().goExpand()
.first()
.goPath().first()
.isNavProperty("Products", new FullQualifiedName("NS", "Products"), false)
.isType(new FullQualifiedName("NS", "Products"), false);
Mockito.verifyZeroInteractions(esProduct);
}
@Test
public void runExpand() throws Exception {

View File

@ -0,0 +1,127 @@
/*
* 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.uri.parser;
import org.apache.olingo.commons.api.edm.Edm;
import org.apache.olingo.commons.api.edm.EdmEntityContainer;
import org.apache.olingo.commons.api.edm.EdmEntitySet;
import org.apache.olingo.commons.api.edm.EdmEntityType;
import org.apache.olingo.commons.api.edm.EdmNavigationProperty;
import org.apache.olingo.commons.api.edm.FullQualifiedName;
import org.apache.olingo.server.api.uri.UriInfoKind;
import org.apache.olingo.server.core.uri.testutil.TestUriValidator;
import org.junit.Test;
import org.mockito.Mockito;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
/**
* All Tests which involves the <code>Parser</code> implementation
* (and with that also the <code>UriParseTreeVisitor</code>).
*/
public class ParserTest {
/**
* Test for EntitySet and NavigationProperty with same name defined in metadata.
* (related to Olingo issue OLINGO-741)
*/
@Test
public void parseEntitySetAndNavigationPropertyWithSameName() throws Exception {
TestUriValidator testUri = new TestUriValidator();
Edm mockEdm = Mockito.mock(Edm.class);
EdmEntitySet esCategory = Mockito.mock(EdmEntitySet.class);
EdmEntitySet esProduct = Mockito.mock(EdmEntitySet.class);
EdmEntityType typeCategory = Mockito.mock(EdmEntityType.class);
EdmEntityContainer container = Mockito.mock(EdmEntityContainer.class);
EdmNavigationProperty productsNavigation = Mockito.mock(EdmNavigationProperty.class);
EdmEntityType productsType = Mockito.mock(EdmEntityType.class);
Mockito.when(mockEdm.getEntityContainer(null)).thenReturn(container);
Mockito.when(typeCategory.getName()).thenReturn("Category");
Mockito.when(typeCategory.getNamespace()).thenReturn("NS");
Mockito.when(esCategory.getEntityType()).thenReturn(typeCategory);
Mockito.when(productsNavigation.getName()).thenReturn("Products");
Mockito.when(typeCategory.getProperty("Products")).thenReturn(productsNavigation);
Mockito.when(container.getEntitySet("Category")).thenReturn(esCategory);
Mockito.when(container.getEntitySet("Products")).thenReturn(esProduct);
Mockito.when(productsType.getName()).thenReturn("Products");
Mockito.when(productsType.getNamespace()).thenReturn("NS");
Mockito.when(productsNavigation.getType()).thenReturn(productsType);
// test and verify
testUri.setEdm(mockEdm)
.run("Category", "$expand=Products")
.isKind(UriInfoKind.resource).goPath().goExpand()
.first()
.goPath().first()
.isNavProperty("Products", new FullQualifiedName("NS", "Products"), false)
.isType(new FullQualifiedName("NS", "Products"), false);
Mockito.verifyZeroInteractions(esProduct);
}
/**
* Test for EntitySet with navigation to an not existing NavigationProperty (name)
* but with another EntitySet with this name defined in metadata.
* (related to Olingo issue OLINGO-755)
*/
@Test
public void entitySetWoNavigationButWithEntitySetWithSameName() throws Exception {
TestUriValidator testUri = new TestUriValidator();
Edm mockEdm = Mockito.mock(Edm.class);
EdmEntitySet esCategory = Mockito.mock(EdmEntitySet.class);
EdmEntitySet esProduct = Mockito.mock(EdmEntitySet.class);
EdmEntityType typeCategory = Mockito.mock(EdmEntityType.class);
FullQualifiedName fqnCategory = new FullQualifiedName("NS", "Category");
EdmEntityContainer container = Mockito.mock(EdmEntityContainer.class);
EdmNavigationProperty productsNavigation = Mockito.mock(EdmNavigationProperty.class);
EdmEntityType typeProduct = Mockito.mock(EdmEntityType.class);
FullQualifiedName fqnProduct = new FullQualifiedName("NS", "Products");
Mockito.when(mockEdm.getEntityContainer(null)).thenReturn(container);
Mockito.when(typeCategory.getName()).thenReturn(fqnCategory.getName());
Mockito.when(typeCategory.getNamespace()).thenReturn(fqnCategory.getNamespace());
Mockito.when(typeCategory.getFullQualifiedName()).thenReturn(fqnCategory);
Mockito.when(esCategory.getEntityType()).thenReturn(typeCategory);
Mockito.when(esProduct.getEntityType()).thenReturn(typeProduct);
Mockito.when(productsNavigation.getName()).thenReturn("Products");
Mockito.when(typeCategory.getProperty("Products")).thenReturn(productsNavigation);
Mockito.when(container.getEntitySet("Category")).thenReturn(esCategory);
Mockito.when(container.getEntitySet("Products")).thenReturn(esProduct);
Mockito.when(typeProduct.getName()).thenReturn(fqnProduct.getName());
Mockito.when(typeProduct.getNamespace()).thenReturn(fqnProduct.getNamespace());
Mockito.when(typeProduct.getFullQualifiedName()).thenReturn(fqnProduct);
Mockito.when(productsNavigation.getType()).thenReturn(typeProduct);
try {
// test and verify
testUri.setEdm(mockEdm)
.run("Products", "$expand=Category")
.isKind(UriInfoKind.resource).goPath().goExpand()
.first()
.goPath().first()
.isType(new FullQualifiedName("NS", "Category"), false);
fail("Expected exception was not thrown.");
} catch (Exception e) {
assertEquals("NavigationProperty 'Category' not found in type 'NS.Products'", e.getMessage());
}
}
}