[OLINGO-916] Changed guid key predicate parsing

This commit is contained in:
mibo 2016-03-26 21:46:02 +01:00
parent 838ca234e4
commit 18b126742e
5 changed files with 54 additions and 15 deletions

View File

@ -18,7 +18,6 @@
*/
package org.apache.olingo.server.core;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
@ -47,12 +46,9 @@ import org.apache.olingo.server.api.serializer.RepresentationType;
import org.apache.olingo.server.api.serializer.SerializerException;
import org.apache.olingo.server.api.uri.UriInfo;
import org.apache.olingo.server.api.uri.queryoption.FormatOption;
import org.apache.olingo.server.api.uri.queryoption.QueryOption;
import org.apache.olingo.server.api.uri.queryoption.SystemQueryOption;
import org.apache.olingo.server.api.uri.queryoption.SystemQueryOptionKind;
import org.apache.olingo.server.core.debug.ServerCoreDebugger;
import org.apache.olingo.server.core.uri.parser.Parser;
import org.apache.olingo.server.core.uri.parser.UriDecoder;
import org.apache.olingo.server.core.uri.parser.UriParserException;
import org.apache.olingo.server.core.uri.parser.UriParserSemanticException;
import org.apache.olingo.server.core.uri.parser.UriParserSyntaxException;

View File

@ -28,7 +28,6 @@ import org.apache.olingo.server.api.deserializer.batch.BatchDeserializerExceptio
import org.apache.olingo.server.api.deserializer.batch.BatchRequestPart;
import org.apache.olingo.server.api.deserializer.batch.ODataResponsePart;
import org.apache.olingo.server.api.processor.BatchProcessor;
import org.apache.olingo.server.core.ODataHandlerImpl;
import org.apache.olingo.server.core.deserializer.batch.BatchParserCommon;
public class BatchFacadeImpl implements BatchFacade {

View File

@ -29,7 +29,6 @@ import org.apache.olingo.server.api.deserializer.batch.BatchDeserializerExceptio
import org.apache.olingo.server.api.deserializer.batch.BatchRequestPart;
import org.apache.olingo.server.api.deserializer.batch.ODataResponsePart;
import org.apache.olingo.server.api.processor.BatchProcessor;
import org.apache.olingo.server.core.ODataHandlerImpl;
import org.apache.olingo.server.core.batchhandler.referenceRewriting.BatchReferenceRewriter;
public class BatchPartHandler {

View File

@ -306,15 +306,12 @@ public class ParserHelper {
}
}
if (tokenizer.next(TokenKind.ODataIdentifier)) {
if(tokenizer.next(TokenKind.GuidValue)) {
keys.add(parseSimpleKey(tokenizer, edm, referringType, aliases, keyPropertyRefs, referencedNames, true));
} else if (tokenizer.next(TokenKind.ODataIdentifier)) {
keys.addAll(compoundKey(tokenizer, edmEntityType, edm, referringType, aliases));
} else if (keyPropertyRefs.size() - referencedNames.size() == 1) {
for (final EdmKeyPropertyRef candidate : keyPropertyRefs) {
if (referencedNames.get(candidate.getName()) == null) {
keys.add(simpleKey(tokenizer, candidate, edm, referringType, aliases));
break;
}
}
keys.add(parseSimpleKey(tokenizer, edm, referringType, aliases, keyPropertyRefs, referencedNames, false));
} else {
throw new UriParserSemanticException(
"Expected " + (keyPropertyRefs.size() -referencedNames.size()) + " key predicates but found one.",
@ -349,14 +346,35 @@ public class ParserHelper {
}
}
private static UriParameter parseSimpleKey(final UriTokenizer tokenizer, final Edm edm, final EdmType referringType,
final Map<String, AliasQueryOption> aliases,
final List<EdmKeyPropertyRef> keyPropertyRefs,
final Map<String, String> referencedNames, final boolean tokenConsumed)
throws UriParserException, UriValidationException {
for (final EdmKeyPropertyRef candidate : keyPropertyRefs) {
if (referencedNames.get(candidate.getName()) == null) {
return simpleKey(tokenizer, candidate, edm, referringType, aliases, tokenConsumed);
}
}
throw new UriParserSemanticException("No suitable key found.",
UriParserSemanticException.MessageKeys.WRONG_NUMBER_OF_KEY_PROPERTIES,
"0", String.valueOf(keyPropertyRefs.size()));
}
private static UriParameter simpleKey(UriTokenizer tokenizer, final EdmKeyPropertyRef edmKeyPropertyRef,
final Edm edm, final EdmType referringType, final Map<String, AliasQueryOption> aliases)
final Edm edm, final EdmType referringType,
final Map<String, AliasQueryOption> aliases, final boolean tokenConsumed)
throws UriParserException, UriValidationException {
final EdmProperty edmProperty = edmKeyPropertyRef == null ? null : edmKeyPropertyRef.getProperty();
final EdmPrimitiveType primitiveType = edmProperty == null ? null : (EdmPrimitiveType) edmProperty.getType();
final boolean nullable = edmProperty != null && edmProperty.isNullable();
if (nextPrimitiveTypeValue(tokenizer, primitiveType, nullable)) {
boolean primitiveTypeAvailable = tokenConsumed;
if(!tokenConsumed) {
primitiveTypeAvailable = nextPrimitiveTypeValue(tokenizer, primitiveType, nullable);
}
if (primitiveTypeAvailable) {
final String literalValue = tokenizer.getText();
ParserHelper.requireNext(tokenizer, TokenKind.CLOSE);
return createUriParameter(edmProperty, edmKeyPropertyRef.getName(), literalValue, edm, referringType, aliases);

View File

@ -43,6 +43,33 @@ import static org.junit.Assert.fail;
*/
public class ParserTest {
@Test
public void keyPropertyGuid() throws Exception {
final String entitySetName = "ESGuid";
final String keyPropertyName = "a";
EdmProperty keyProperty = Mockito.mock(EdmProperty.class);
Mockito.when(keyProperty.getType())
.thenReturn(OData.newInstance().createPrimitiveTypeInstance(EdmPrimitiveTypeKind.Guid));
EdmKeyPropertyRef keyPropertyRef = Mockito.mock(EdmKeyPropertyRef.class);
Mockito.when(keyPropertyRef.getName()).thenReturn(keyPropertyName);
Mockito.when(keyPropertyRef.getProperty()).thenReturn(keyProperty);
EdmEntityType entityType = Mockito.mock(EdmEntityType.class);
Mockito.when(entityType.getKeyPredicateNames()).thenReturn(Collections.singletonList(keyPropertyName));
Mockito.when(entityType.getKeyPropertyRefs()).thenReturn(Collections.singletonList(keyPropertyRef));
EdmEntitySet entitySet = Mockito.mock(EdmEntitySet.class);
Mockito.when(entitySet.getName()).thenReturn(entitySetName);
Mockito.when(entitySet.getEntityType()).thenReturn(entityType);
EdmEntityContainer container = Mockito.mock(EdmEntityContainer.class);
Mockito.when(container.getEntitySet(entitySetName)).thenReturn(entitySet);
Edm mockedEdm = Mockito.mock(Edm.class);
Mockito.when(mockedEdm.getEntityContainer()).thenReturn(container);
new TestUriValidator().setEdm(mockedEdm)
.run("ESGuid(f89dee73-af9f-4cd4-b330-db93c25ff3c7)")
.goPath()
.at(0).isEntitySet(entitySetName)
.at(0).isKeyPredicate(0, keyPropertyName, "f89dee73-af9f-4cd4-b330-db93c25ff3c7");
}
@Test
public void navPropertySameNameAsEntitySet() throws Exception {
final String namespace = "namespace";