org.apache.olingo.odata2.api.uri.KeyPredicate Java Examples

The following examples show how to use org.apache.olingo.odata2.api.uri.KeyPredicate. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
private Map<String, Object> parseLinkUri(final EdmEntitySet targetEntitySet, final String uriString)
    throws EdmException {
  ODataContext context = getContext();
  final int timingHandle = context.startRuntimeMeasurement("UriParser", "getKeyPredicatesFromEntityLink");

  List<KeyPredicate> key = null;
  try {
    key = UriParser.getKeyPredicatesFromEntityLink(targetEntitySet, uriString,
        context.getPathInfo().getServiceRoot());
  } catch (ODataException e) {
    // We don't understand the link target. This could also be seen as an error.
  }

  context.stopRuntimeMeasurement(timingHandle);

  return key == null ? null : mapKey(key);
}
 
Example #2
Source File: JPAQueryBuilderTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private UriInfo mockURIInfoForEntitySetCount(EdmMapping mapping) throws EdmException {
  
  UriInfo uriInfo = EasyMock.createMock(UriInfo.class);
  List<NavigationSegment> navSegments = new ArrayList<NavigationSegment>();
  EasyMock.expect(uriInfo.getNavigationSegments()).andStubReturn(navSegments);
  EdmEntityType edmEntityType = EasyMock.createMock(EdmEntityType.class);
  EasyMock.expect(edmEntityType.getMapping()).andStubReturn(mapping);
  EdmEntitySet edmEntitySet = EasyMock.createMock(EdmEntitySet.class);
  EasyMock.expect(edmEntitySet.getEntityType()).andStubReturn(edmEntityType);
  EasyMock.expect(uriInfo.getTargetEntitySet()).andStubReturn(edmEntitySet);
  List<KeyPredicate> keyPreds =EasyMock.createMock(ArrayList.class);
  EasyMock.expect(uriInfo.getKeyPredicates()).andStubReturn(keyPreds); 
  OrderByExpression orderbyExpression = mockOrderByExpressions(uriInfo);
  EasyMock.expect(uriInfo.getTop()).andStubReturn(null); 
  EasyMock.expect(uriInfo.getSkip()).andStubReturn(null); 
  EasyMock.replay(edmEntityType, edmEntitySet, uriInfo, keyPreds,orderbyExpression);
  return uriInfo;

}
 
Example #3
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
private Map<String, Object> parseLinkUri(final EdmEntitySet targetEntitySet, final String uriString)
    throws EdmException {
  ODataContext context = getContext();
  final int timingHandle = context.startRuntimeMeasurement("UriParser", "getKeyPredicatesFromEntityLink");

  List<KeyPredicate> key = null;
  try {
    key = UriParser.getKeyPredicatesFromEntityLink(targetEntitySet, uriString,
        context.getPathInfo().getServiceRoot());
  } catch (ODataException e) {
    // We don't understand the link target. This could also be seen as an error.
  }

  context.stopRuntimeMeasurement(timingHandle);

  return key == null ? null : mapKey(key);
}
 
Example #4
Source File: JPQLJoinSelectSingleContextTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
private List<KeyPredicate> createKeyPredicates(final boolean toThrowException) throws EdmException {
  KeyPredicate keyPredicate = EasyMock.createMock(KeyPredicate.class);
  EasyMock.expect(keyPredicate.getLiteral()).andStubReturn("1");
  EdmProperty edmProperty = EasyMock.createMock(EdmProperty.class);
  JPAEdmMappingImpl edmMapping = EasyMock.createMock(JPAEdmMappingImpl.class);
  EasyMock.expect(edmMapping.getJPAType())
  .andStubReturn(null);
  EasyMock.expect(edmMapping.getInternalName()).andStubReturn("soid");
  EasyMock.expect(edmProperty.getMapping()).andStubReturn(edmMapping);
  EasyMock.expect(edmProperty.getName()).andStubReturn("soid");
  EdmSimpleType edmType = EdmSimpleTypeKind.Int32.getEdmSimpleTypeInstance();
  if (toThrowException) {
    EasyMock.expect(edmProperty.getType()).andStubThrow(new EdmException(null));
  } else {
    EasyMock.expect(edmProperty.getType()).andStubReturn(edmType);
  }
  EasyMock.expect(keyPredicate.getProperty()).andStubReturn(edmProperty);

  EasyMock.replay(edmMapping, edmProperty, keyPredicate);
  List<KeyPredicate> keyPredicates = new ArrayList<KeyPredicate>();
  keyPredicates.add(keyPredicate);
  return keyPredicates;
}
 
Example #5
Source File: JPAQueryBuilderTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private GetEntityUriInfo mockURIInfoWithTopSkip(EdmMapping mapping) throws EdmException {
  UriInfo uriInfo = EasyMock.createMock(UriInfo.class);
  List<NavigationSegment> navSegments = new ArrayList<NavigationSegment>();
  EasyMock.expect(uriInfo.getNavigationSegments()).andStubReturn(navSegments);
  EdmEntityType edmEntityType = EasyMock.createMock(EdmEntityType.class);
  EasyMock.expect(edmEntityType.getMapping()).andStubReturn(mapping);
  EdmEntitySet edmEntitySet = EasyMock.createMock(EdmEntitySet.class);
  EasyMock.expect(edmEntitySet.getEntityType()).andStubReturn(edmEntityType);
  EasyMock.expect(uriInfo.getTargetEntitySet()).andStubReturn(edmEntitySet);
  List<KeyPredicate> keyPreds = EasyMock.createMock(ArrayList.class);
  EasyMock.expect(uriInfo.getKeyPredicates()).andStubReturn(keyPreds); 
  EasyMock.expect(uriInfo.getOrderBy()).andStubReturn(null);
  EasyMock.expect(uriInfo.getTop()).andStubReturn(1);
  EasyMock.expect(uriInfo.getSkip()).andStubReturn(2);
  EdmProperty edmProperty = EasyMock.createMock(EdmProperty.class);
  EasyMock.expect(edmProperty.getMapping()).andStubReturn(mapping);
  EasyMock.expect(edmEntityType.getKeyProperties()).andStubReturn(Arrays.asList(edmProperty));
  EasyMock.expect(uriInfo.getFilter()).andStubReturn(null);
  EasyMock.replay(edmEntityType, edmEntitySet, uriInfo, keyPreds, edmProperty);
  return uriInfo;
}
 
Example #6
Source File: JPQLBuilderFactoryTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
private GetEntityUriInfo getEntityUriInfo() throws EdmException {
  GetEntityUriInfo getEntityView = EasyMock.createMock(GetEntityUriInfo.class);
  EdmEntitySet edmEntitySet = EasyMock.createMock(EdmEntitySet.class);
  EdmEntityType edmEntityType = EasyMock.createMock(EdmEntityType.class);
  EasyMock.expect(edmEntityType.getKeyProperties()).andStubReturn(new ArrayList<EdmProperty>());
  EasyMock.expect(edmEntityType.getMapping()).andStubReturn(null);
  EasyMock.expect(edmEntityType.getName()).andStubReturn("");
  EasyMock.expect(edmEntitySet.getEntityType()).andStubReturn(edmEntityType);
  EasyMock.expect(getEntityView.getSelect()).andStubReturn(null);
  EasyMock.expect(getEntityView.getTargetEntitySet()).andStubReturn(edmEntitySet);
  EdmEntitySet startEdmEntitySet = EasyMock.createMock(EdmEntitySet.class);
  EdmEntityType startEdmEntityType = EasyMock.createMock(EdmEntityType.class);
  EasyMock.expect(startEdmEntityType.getMapping()).andStubReturn(null);
  EasyMock.expect(startEdmEntityType.getName()).andStubReturn("SOHeader");
  EasyMock.expect(startEdmEntitySet.getEntityType()).andStubReturn(startEdmEntityType);
  EasyMock.expect(getEntityView.getStartEntitySet()).andStubReturn(startEdmEntitySet);
  EasyMock.replay(startEdmEntityType, startEdmEntitySet);
  EasyMock.replay(edmEntityType, edmEntitySet);
  EasyMock.expect(getEntityView.getKeyPredicates()).andStubReturn(new ArrayList<KeyPredicate>());
  List<NavigationSegment> navigationSegments = new ArrayList<NavigationSegment>();
  EasyMock.expect(getEntityView.getNavigationSegments()).andStubReturn(navigationSegments);
  EasyMock.replay(getEntityView);
  return getEntityView;
}
 
Example #7
Source File: JPQLJoinSelectSingleStatementBuilderTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
private List<KeyPredicate> createKeyPredicates() throws EdmException {
  KeyPredicate keyPredicate = EasyMock.createMock(KeyPredicate.class);
  EasyMock.expect(keyPredicate.getLiteral()).andStubReturn("1");
  EdmProperty edmProperty = EasyMock.createMock(EdmProperty.class);
  EdmMapping edmMapping = EasyMock.createMock(EdmMapping.class);
  EasyMock.expect(edmMapping.getInternalName()).andStubReturn("soid");
  EasyMock.expect(edmProperty.getMapping()).andStubReturn(edmMapping);
  EdmSimpleType edmType = EasyMock.createMock(EdmSimpleType.class);
  EasyMock.expect(edmProperty.getType()).andStubReturn(edmType);
  EasyMock.expect(keyPredicate.getProperty()).andStubReturn(edmProperty);

  EasyMock.replay(edmType, edmMapping, edmProperty, keyPredicate);
  List<KeyPredicate> keyPredicates = new ArrayList<KeyPredicate>();
  keyPredicates.add(keyPredicate);
  return keyPredicates;
}
 
Example #8
Source File: JPQLJoinContextTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
private List<KeyPredicate> createKeyPredicates() throws EdmException {
  KeyPredicate keyPredicate = EasyMock.createMock(KeyPredicate.class);
  EasyMock.expect(keyPredicate.getLiteral()).andStubReturn("1");
  EdmProperty edmProperty = EasyMock.createMock(EdmProperty.class);
  JPAEdmMappingImpl edmMapping = EasyMock.createMock(JPAEdmMappingImpl.class);
  EasyMock.expect(edmMapping.getJPAType())
  .andStubReturn(null);
  EasyMock.expect(edmMapping.getInternalName()).andStubReturn("soid");
  EasyMock.expect(edmProperty.getMapping()).andStubReturn(edmMapping);
  EasyMock.expect(edmProperty.getName()).andStubReturn("soid");
  EdmSimpleType edmType = EdmSimpleTypeKind.Int32.getEdmSimpleTypeInstance();
  EasyMock.expect(edmProperty.getType()).andStubReturn(edmType);
  EasyMock.expect(keyPredicate.getProperty()).andStubReturn(edmProperty);

  EasyMock.replay(edmMapping, edmProperty, keyPredicate);
  List<KeyPredicate> keyPredicates = new ArrayList<KeyPredicate>();
  keyPredicates.add(keyPredicate);
  return keyPredicates;
}
 
Example #9
Source File: HttpExceptionResponseTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Override
public boolean matches(final Object item) {
  if (item instanceof UriInfoImpl) {
    final UriInfoImpl upr = (UriInfoImpl) item;
    final List<KeyPredicate> keyPredicates = upr.getKeyPredicates();
    for (final KeyPredicate keyPredicate : keyPredicates) {
      if (keyLiteral.equals(keyPredicate.getLiteral())) {
        return true;
      }
    }
  }
  return false;
}
 
Example #10
Source File: MediaProcessor.java    From lemonaid with MIT License 5 votes vote down vote up
public Object process(PostUriInfo uriParserResultView, InputStream content, String requestContentType) throws ODataException {
	initialize();
	ODataContext ctx = ODataJPAContextImpl.getContextInThreadLocal();
	HttpServletRequest request = (HttpServletRequest) ctx.getParameter(ODataContext.HTTP_SERVLET_REQUEST_OBJECT);  		
	if (uriParserResultView.getTargetEntitySet().getEntityType().getName().equals("Attachment")) {
		Attachment attachment = new Attachment();
		attachment.setId(UUID.randomUUID().toString());
		attachment.setMimeType(requestContentType);
		attachment.setFileName(request.getHeader("slug"));
		try {
			attachment.setData(IOUtils.toByteArray(content));
		} catch (IOException e) {
			throw new ODataException(e);
		}
		attachment.setFileSize(attachment.getData().length);
		attachment.setLastModified(Calendar.getInstance());
		for (KeyPredicate keyPredicate : uriParserResultView.getKeyPredicates()) {
			if (keyPredicate.getProperty().getName().equals("Id")) {
				attachment.setMentorId(mentorRepository.findOne(keyPredicate.getLiteral()));
			}
		}
		attachmentRepository.save(attachment);
		attachment.setData(null);
		return attachment;
	}
	return null;
}
 
Example #11
Source File: AbstractEntitySet.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
public ODataResponse getEntityMedia(GetMediaResourceUriInfo uri_info,
      ODataSingleProcessor processor) throws ODataException
{
   KeyPredicate startKP = uri_info.getKeyPredicates().get(0);
   T t = Navigator.<T>navigate(uri_info.getStartEntitySet(), startKP,
         uri_info.getNavigationSegments(), null);
   ODataResponse resp = t.getEntityMedia(processor);
   if (resp == null)
   {
      throw new ExpectedException("No stream for entity " + getEntityName());
   }
   return resp;
}
 
Example #12
Source File: AbstractEntitySet.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
public Map<String, Object> getComplexProperty(
      GetComplexPropertyUriInfo uri_info) throws ODataException
{
   KeyPredicate startKP = uri_info.getKeyPredicates().get(0);
   EdmProperty target
         = uri_info.getPropertyPath()
         .get(uri_info.getPropertyPath().size() - 1);
   T t = Navigator.<T>navigate(uri_info.getStartEntitySet(), startKP,
         uri_info.getNavigationSegments(), null);
   return t.getComplexProperty(target.getName());
}
 
Example #13
Source File: ODataAuthorization.java    From lemonaid with MIT License 5 votes vote down vote up
private boolean isOwnProfile(Object uriParserResultView) throws EdmException {
	UriInfo uriInfo = (UriInfo) uriParserResultView;
	if ("Mentors".equals(uriInfo.getTargetEntitySet().getName())) {
		for (KeyPredicate keyPredicate : uriInfo.getKeyPredicates()) {
			if ("Id".equals(keyPredicate.getProperty().getName()) && keyPredicate.getLiteral().equals(getCurrentMentor().getId())) {
				return true;
			}
		}
	}
	return false;
}
 
Example #14
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private static Map<String, Object> mapKey(final List<KeyPredicate> keys) throws EdmException {
  Map<String, Object> keyMap = new HashMap<String, Object>();
  for (final KeyPredicate key : keys) {
    final EdmProperty property = key.getProperty();
    final EdmSimpleType type = (EdmSimpleType) property.getType();
    keyMap.put(property.getName(), type.valueOfString(key.getLiteral(), EdmLiteralKind.DEFAULT, property.getFacets(),
        type.getDefaultType()));
  }
  return keyMap;
}
 
Example #15
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private Object retrieveData(final EdmEntitySet startEntitySet, final List<KeyPredicate> keyPredicates,
    final EdmFunctionImport functionImport, final Map<String, Object> functionImportParameters,
    final List<NavigationSegment> navigationSegments) throws ODataException {
  Object data;
  final Map<String, Object> keys = mapKey(keyPredicates);

  ODataContext context = getContext();
  final int timingHandle = context.startRuntimeMeasurement(getClass().getSimpleName(), "retrieveData");

  try {
    data = functionImport == null ?
        keys.isEmpty() ?
            dataSource.readData(startEntitySet) : dataSource.readData(startEntitySet, keys) :
        dataSource.readData(functionImport, functionImportParameters, keys);

    EdmEntitySet currentEntitySet =
        functionImport == null ? startEntitySet : functionImport.getEntitySet();
    for (NavigationSegment navigationSegment : navigationSegments) {
      data = dataSource.readRelatedData(
          currentEntitySet,
          data,
          navigationSegment.getEntitySet(),
          mapKey(navigationSegment.getKeyPredicates()));
      currentEntitySet = navigationSegment.getEntitySet();
    }
  } finally {
    context.stopRuntimeMeasurement(timingHandle);
  }
  return data;
}
 
Example #16
Source File: ODataJPADefaultProcessorTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private GetEntityUriInfo getEntityUriInfo() {
  GetEntityUriInfo getEntityView = EasyMock.createMock(GetEntityUriInfo.class);
  EdmEntitySet edmEntitySet = EasyMock.createMock(EdmEntitySet.class);
  EdmEntityType edmEntityType = EasyMock.createMock(EdmEntityType.class);
  try {
    EasyMock.expect(getEntityView.getExpand()).andStubReturn(null);
    EasyMock.expect(edmEntityType.getKeyProperties()).andStubReturn(new ArrayList<EdmProperty>());
    EasyMock.expect(edmEntitySet.getEntityType()).andStubReturn(edmEntityType);
    EasyMock.expect(edmEntitySet.getName()).andStubReturn(SALES_ORDER_HEADERS);

    EasyMock.expect(getEntityView.getSelect()).andStubReturn(null);
    EasyMock.expect(getEntityView.getTargetEntitySet()).andStubReturn(edmEntitySet);
    EasyMock.expect(edmEntityType.getPropertyNames()).andStubReturn(getLocalPropertyNames());
    EasyMock.expect(edmEntityType.getProperty(SO_ID)).andStubReturn(getEdmTypedMockedObj(SO_ID));

    EasyMock.expect(edmEntityType.getMapping()).andStubReturn((EdmMapping) getEdmMappingMockedObj(SALES_ORDER));

    EasyMock.expect(edmEntityType.getKind()).andStubReturn(EdmTypeKind.SIMPLE);
    EasyMock.expect(edmEntityType.getNamespace()).andStubReturn(SALES_ORDER_HEADERS);
    EasyMock.expect(edmEntityType.getName()).andStubReturn(SALES_ORDER_HEADERS);
    EasyMock.expect(edmEntityType.hasStream()).andStubReturn(false);
    EasyMock.expect(edmEntityType.getNavigationPropertyNames()).andStubReturn(new ArrayList<String>());
    EasyMock.expect(edmEntityType.getKeyPropertyNames()).andStubReturn(new ArrayList<String>());

    EasyMock.expect(edmEntitySet.getEntityContainer()).andStubReturn(getLocalEdmEntityContainer());

    EasyMock.replay(edmEntityType, edmEntitySet);
    EasyMock.expect(getEntityView.getKeyPredicates()).andStubReturn(new ArrayList<KeyPredicate>());
    List<NavigationSegment> navigationSegments = new ArrayList<NavigationSegment>();
    EasyMock.expect(getEntityView.getNavigationSegments()).andReturn(navigationSegments);
    EasyMock.expect(getEntityView.getStartEntitySet()).andReturn(edmEntitySet);

    EasyMock.replay(getEntityView);
  } catch (EdmException e) {
    fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
  }
  return getEntityView;
}
 
Example #17
Source File: JPQLSelectSingleStatementBuilderTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private void setSpecificProperties(KeyPredicate keyPredicate, EdmProperty kpProperty,  EdmSimpleType edmType) {

    try {
      EasyMock.expect(kpProperty.getType()).andStubReturn(edmType);
      if(EdmSimpleTypeKind.Int32.name().equals(edmType.getName())){
        EasyMock.expect(keyPredicate.getLiteral()).andStubReturn("1");
      }else{
        EasyMock.expect(keyPredicate.getLiteral()).andStubReturn(" MiMe-Id1");
      }
    } catch (EdmException e) {
      fail("this should not happen");
    }
  
  }
 
Example #18
Source File: JPAQueryBuilderTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private DeleteUriInfo mockURIInfoForDeleteAndPut(EdmMapping mapping) throws EdmException {
  UriInfo uriInfo = EasyMock.createMock(UriInfo.class);
  List<NavigationSegment> navSegments = new ArrayList<NavigationSegment>();
  EasyMock.expect(uriInfo.getNavigationSegments()).andStubReturn(navSegments);
  EdmEntityType edmEntityType = EasyMock.createMock(EdmEntityType.class);
  EasyMock.expect(edmEntityType.getMapping()).andStubReturn(mapping);
  EdmEntitySet edmEntitySet = EasyMock.createMock(EdmEntitySet.class);
  EasyMock.expect(edmEntitySet.getEntityType()).andStubReturn(edmEntityType);
  EasyMock.expect(uriInfo.getTargetEntitySet()).andStubReturn(edmEntitySet);
  List<KeyPredicate> keyPreds = new ArrayList<KeyPredicate>();
  
  EdmProperty edmProperty1 = mockEdmProperty(mapping, "Decimal");
  keyPreds.add(mockKeyPredicate(edmProperty1, "1234.7"));
  
  
  EdmProperty edmProperty2 = mockEdmProperty(mapping, "Int64");
  keyPreds.add(mockKeyPredicate(edmProperty2, "1234567899"));
  
  EdmProperty edmProperty3 = mockEdmProperty(mapping, "Double");
  keyPreds.add(mockKeyPredicate(edmProperty3, "12349"));
  
  EdmProperty edmProperty4 = mockEdmProperty(mapping, "Int32");
  keyPreds.add(mockKeyPredicate(edmProperty4, "12349"));
  
  EdmProperty edmProperty5 = mockEdmProperty(mapping, "Single");
  keyPreds.add(mockKeyPredicate(edmProperty5, "12349"));
  
  EdmProperty edmProperty6 = mockEdmProperty(mapping, "SByte");
  keyPreds.add(mockKeyPredicate(edmProperty6, "-123"));
  
  EdmProperty edmProperty7 = mockEdmProperty(mapping, "Binary");
  keyPreds.add(mockKeyPredicate(edmProperty7, getBinaryData()));
  
  EdmProperty edmProperty8 = mockEdmProperty(mapping, "uuid");
  keyPreds.add(mockKeyPredicate(edmProperty8, "56fe79b1-1c88-465b-b309-32bf8b8f6800"));
  
  EasyMock.expect(uriInfo.getKeyPredicates()).andStubReturn(keyPreds); 
  EasyMock.replay(edmEntityType, edmEntitySet, uriInfo);
  return uriInfo;
}
 
Example #19
Source File: JPAQueryBuilderTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
/**
 * @param edmProperty1
 * @return
 */
private KeyPredicate mockKeyPredicate(EdmProperty edmProperty, String value) {
  KeyPredicate keyPredicate = EasyMock.createMock(KeyPredicate.class);
  EasyMock.expect(keyPredicate.getLiteral()).andReturn(value).anyTimes();
  EasyMock.expect(keyPredicate.getProperty()).andReturn(edmProperty).anyTimes();
  EasyMock.replay(keyPredicate);
  return keyPredicate;
}
 
Example #20
Source File: JPAQueryBuilderTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private GetEntityUriInfo mockURIInfoWithTopSkipInline(EdmMapping mapping) throws EdmException {
  UriInfoImpl uriInfo = EasyMock.createMock(UriInfoImpl.class);
  List<NavigationSegment> navSegments = new ArrayList<NavigationSegment>();
  EasyMock.expect(uriInfo.getNavigationSegments()).andStubReturn(navSegments);
  EdmEntityType edmEntityType = EasyMock.createMock(EdmEntityType.class);
  EasyMock.expect(edmEntityType.getMapping()).andStubReturn(mapping);
  EdmEntitySet edmEntitySet = EasyMock.createMock(EdmEntitySet.class);
  EasyMock.expect(edmEntitySet.getEntityType()).andStubReturn(edmEntityType);
  EasyMock.expect(uriInfo.getTargetEntitySet()).andStubReturn(edmEntitySet);
  List<KeyPredicate> keyPreds = EasyMock.createMock(ArrayList.class);
  EasyMock.expect(uriInfo.getKeyPredicates()).andStubReturn(keyPreds); 
  EasyMock.expect(uriInfo.getOrderBy()).andStubReturn(null);
  EasyMock.expect(uriInfo.getTop()).andStubReturn(1);
  EasyMock.expect(uriInfo.getSkip()).andStubReturn(2);
  EasyMock.expect(uriInfo.isCount()).andStubReturn(false);
  EasyMock.expect(uriInfo.getInlineCount()).andStubReturn(InlineCount.ALLPAGES);
  EdmProperty edmProperty = EasyMock.createMock(EdmProperty.class);
  EasyMock.expect(edmProperty.getMapping()).andStubReturn(mapping);
  EasyMock.expect(edmEntityType.getKeyProperties()).andStubReturn(Arrays.asList(edmProperty));
  EasyMock.expect(uriInfo.getFilter()).andStubReturn(null);
  uriInfo.setCount(true);
  EasyMock.expectLastCall().times(1);
  uriInfo.setCount(false);
  EasyMock.expectLastCall().times(1);
  Map<String, String> data = new HashMap<String, String>();
  data.put("count", "5");
  uriInfo.setCustomQueryOptions(data );
  EasyMock.expectLastCall().times(1);
  EasyMock.expect(uriInfo.getCustomQueryOptions()).andStubReturn(data);
  EasyMock.replay(edmEntityType, edmEntitySet, uriInfo, keyPreds, edmProperty);
  return uriInfo;
}
 
Example #21
Source File: JPAQueryBuilderTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private UriInfo mockURIInfo(EdmMapping mapping) throws EdmException {
  
  UriInfo uriInfo = EasyMock.createMock(UriInfo.class);
  List<NavigationSegment> navSegments = new ArrayList<NavigationSegment>();
  EasyMock.expect(uriInfo.getNavigationSegments()).andStubReturn(navSegments);
  EdmEntityType edmEntityType = EasyMock.createMock(EdmEntityType.class);
  EasyMock.expect(edmEntityType.getMapping()).andStubReturn(mapping);
  EdmEntitySet edmEntitySet = EasyMock.createMock(EdmEntitySet.class);
  EasyMock.expect(edmEntitySet.getEntityType()).andStubReturn(edmEntityType);
  EasyMock.expect(uriInfo.getTargetEntitySet()).andStubReturn(edmEntitySet);
  List<KeyPredicate> keyPreds =EasyMock.createMock(ArrayList.class);
  EdmProperty edmProperty = EasyMock.createMock(EdmProperty.class);
  EasyMock.expect(edmProperty.getMapping()).andStubReturn(mapping);
  KeyPredicate keyPredicate = EasyMock.createMock(KeyPredicate.class);
  EasyMock.expect(keyPredicate.getLiteral()).andReturn("Id").anyTimes();
  EasyMock.expect(keyPredicate.getProperty()).andReturn(edmProperty).anyTimes();
  EasyMock.expect(keyPreds.size()).andStubReturn(1);
  Iterator<KeyPredicate> keyPredicateitr =  EasyMock.createMock(Iterator.class);
  EasyMock.expect(keyPredicateitr.next()).andStubReturn(keyPredicate);
  EasyMock.expect(keyPredicateitr.hasNext()).andStubReturn(true);
  EasyMock.expect(keyPreds.iterator()).andStubReturn(keyPredicateitr);
  EasyMock.expect(keyPreds.isEmpty()).andStubReturn(false);
  EasyMock.expect(keyPreds.get(0)).andStubReturn(keyPredicate);
  EasyMock.expect(uriInfo.getKeyPredicates()).andStubReturn(keyPreds); 
  EasyMock.replay(edmEntityType, edmEntitySet, uriInfo, keyPredicate, keyPreds, edmProperty);
  return uriInfo;

}
 
Example #22
Source File: JPAQueryBuilderTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private UriInfo mockURIInfoForEntityCount(EdmMapping mapping) throws EdmException {
  
  EdmEntityType edmEntityType = EasyMock.createMock(EdmEntityType.class);
  UriInfo uriInfo = EasyMock.createMock(UriInfo.class);
  EdmEntitySet navEntitySet = EasyMock.createMock(EdmEntitySet.class);
  EdmEntityType navEntityType = EasyMock.createMock(EdmEntityType.class);
  List<NavigationSegment> navSegments = mockNavigationSegments(edmEntityType, 
      uriInfo, navEntitySet, navEntityType);
  EasyMock.expect(uriInfo.getNavigationSegments()).andStubReturn(navSegments);
  
  EasyMock.expect(edmEntityType.getMapping()).andStubReturn(mapping);
  EdmEntitySet edmEntitySet = EasyMock.createMock(EdmEntitySet.class);
  EasyMock.expect(edmEntitySet.getEntityType()).andStubReturn(edmEntityType);
  EasyMock.expect(uriInfo.getStartEntitySet()).andStubReturn(edmEntitySet);
  EasyMock.expect(uriInfo.getTargetEntitySet()).andStubReturn(navEntitySet);
  EasyMock.expect(navEntitySet.getEntityType()).andStubReturn(navEntityType);
  EasyMock.expect(navEntityType.getMapping()).andStubReturn((EdmMapping) mockNavEdmMappingForProperty());
  List<KeyPredicate> keyPreds = new ArrayList<KeyPredicate>();
  EdmProperty edmProperty = mockEdmProperty(mapping, "String");
  keyPreds.add(mockKeyPredicate(edmProperty, "Id"));
  EasyMock.expect(uriInfo.getKeyPredicates()).andStubReturn(keyPreds); 
  EasyMock.expect(uriInfo.getTop()).andStubReturn(null); 
  EasyMock.expect(uriInfo.getSkip()).andStubReturn(null); 
  OrderByExpression orderbyExpression = mockOrderByExpressions(uriInfo);
  EasyMock.replay(edmEntityType, edmEntitySet, uriInfo,  
      navEntitySet, orderbyExpression);
  return uriInfo;

}
 
Example #23
Source File: UriParserTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void getKeyFromLink() throws Exception {
  final EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Teams");
  final EdmEntitySet entitySet2 = edm.getEntityContainer("Container2").getEntitySet("Photos");
  final String serviceRoot = "http://service.com/example.svc/";
  final UriParserImpl parser = new UriParserImpl(null);

  List<KeyPredicate> key = parser.getKeyFromEntityLink(entitySet, "Teams('1')", null);
  assertEquals(1, key.size());
  assertEquals(entitySet.getEntityType().getKeyProperties().get(0), key.get(0).getProperty());
  assertEquals("1", key.get(0).getLiteral());

  key = parser.getKeyFromEntityLink(entitySet, "Teams(Id='2')", URI.create(serviceRoot));
  assertEquals("2", key.get(0).getLiteral());

  key = parser.getKeyFromEntityLink(entitySet, serviceRoot + "Teams('3')", URI.create(serviceRoot));
  assertEquals("3", key.get(0).getLiteral());

  key = parser.getKeyFromEntityLink(entitySet2, "Container2.Photos(Id=4,Type='test')", null);
  assertEquals(2, key.size());

  wrongGetKey(entitySet, "someContainer.Teams('5')", null, UriNotMatchingException.CONTAINERNOTFOUND);
  wrongGetKey(entitySet, "Employees('6')/ne_Team", null, UriNotMatchingException.MATCHPROBLEM);
  wrongGetKey(entitySet, "Teams()", null, UriSyntaxException.ENTITYSETINSTEADOFENTITY);
  wrongGetKey(entitySet, "Teams('8')/Id", null, UriNotMatchingException.MATCHPROBLEM);
  wrongGetKey(entitySet, "Rooms('9')", null, UriNotMatchingException.NOTFOUND);
  wrongGetKey(entitySet, "anotherServiceRoot/Teams('10')", serviceRoot, UriNotMatchingException.NOTFOUND);
  wrongGetKey(entitySet2, "Photos(Id=11,Type='test')", null, UriNotMatchingException.CONTAINERNOTFOUND);
  wrongGetKey(entitySet2, "anotherContainer.Photos(Id=12,Type='test')", null,
      UriNotMatchingException.CONTAINERNOTFOUND);
}
 
Example #24
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private static Map<String, Object> mapKey(final List<KeyPredicate> keys) throws EdmException {
  Map<String, Object> keyMap = new HashMap<String, Object>();
  for (final KeyPredicate key : keys) {
    final EdmProperty property = key.getProperty();
    final EdmSimpleType type = (EdmSimpleType) property.getType();
    keyMap.put(property.getName(), type.valueOfString(key.getLiteral(), EdmLiteralKind.DEFAULT, property.getFacets(),
        type.getDefaultType()));
  }
  return keyMap;
}
 
Example #25
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private Object retrieveData(final EdmEntitySet startEntitySet, final List<KeyPredicate> keyPredicates,
    final EdmFunctionImport functionImport, final Map<String, Object> functionImportParameters,
    final List<NavigationSegment> navigationSegments) throws ODataException {
  Object data;
  final Map<String, Object> keys = mapKey(keyPredicates);

  ODataContext context = getContext();
  final int timingHandle = context.startRuntimeMeasurement(getClass().getSimpleName(), "retrieveData");

  try {
    data = functionImport == null ?
        keys.isEmpty() ?
            dataSource.readData(startEntitySet) : dataSource.readData(startEntitySet, keys) :
        dataSource.readData(functionImport, functionImportParameters, keys);

    EdmEntitySet currentEntitySet =
        functionImport == null ? startEntitySet : functionImport.getEntitySet();
    for (NavigationSegment navigationSegment : navigationSegments) {
      data = dataSource.readRelatedData(
          currentEntitySet,
          data,
          navigationSegment.getEntitySet(),
          mapKey(navigationSegment.getKeyPredicates()));
      currentEntitySet = navigationSegment.getEntitySet();
    }
  } finally {
    context.stopRuntimeMeasurement(timingHandle);
  }
  return data;
}
 
Example #26
Source File: EntityTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private UriInfo mockUriResult(final String entitySetName, final String keyName, final String keyValue)
    throws EdmException {
  EdmProperty keyProperty = mock(EdmProperty.class);
  when(keyProperty.getName()).thenReturn(keyName);
  when(keyProperty.getType()).thenReturn(EdmSimpleTypeKind.String.getEdmSimpleTypeInstance());
  when(keyProperty.isSimple()).thenReturn(true);
  EdmMapping mapping = mock(EdmMapping.class);
  when(mapping.getInternalName()).thenReturn("getId");
  when(keyProperty.getMapping()).thenReturn(mapping);

  KeyPredicate key = mock(KeyPredicate.class);
  when(key.getProperty()).thenReturn(keyProperty);
  when(key.getLiteral()).thenReturn(keyValue);

  ArrayList<KeyPredicate> keys = new ArrayList<KeyPredicate>();
  keys.add(key);

  EdmEntityType entityType = mock(EdmEntityType.class);
  when(entityType.getName()).thenReturn(entitySetName);
  when(entityType.getProperty(keyProperty.getName())).thenReturn(keyProperty);
  when(entityType.getPropertyNames()).thenReturn(Arrays.asList(keyName));

  EdmEntitySet entitySet = mock(EdmEntitySet.class);
  when(entitySet.getName()).thenReturn(entitySetName);
  when(entitySet.getEntityType()).thenReturn(entityType);

  EdmEntityContainer entityContainer = mock(EdmEntityContainer.class);
  when(entityContainer.isDefaultEntityContainer()).thenReturn(true);
  when(entitySet.getEntityContainer()).thenReturn(entityContainer);

  UriInfo uriResult = mock(UriInfo.class);
  when(uriResult.getStartEntitySet()).thenReturn(entitySet);
  when(uriResult.getTargetEntitySet()).thenReturn(entitySet);
  when(uriResult.getKeyPredicates()).thenReturn(keys);
  return uriResult;
}
 
Example #27
Source File: UriParserTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void getKeyFromLink() throws Exception {
  final EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Teams");
  final EdmEntitySet entitySet2 = edm.getEntityContainer("Container2").getEntitySet("Photos");
  final String serviceRoot = "http://service.com/example.svc/";
  final UriParserImpl parser = new UriParserImpl(null);

  List<KeyPredicate> key = parser.getKeyFromEntityLink(entitySet, "Teams('1')", null);
  assertEquals(1, key.size());
  assertEquals(entitySet.getEntityType().getKeyProperties().get(0), key.get(0).getProperty());
  assertEquals("1", key.get(0).getLiteral());

  key = parser.getKeyFromEntityLink(entitySet, "Teams(Id='2')", URI.create(serviceRoot));
  assertEquals("2", key.get(0).getLiteral());

  key = parser.getKeyFromEntityLink(entitySet, serviceRoot + "Teams('3')", URI.create(serviceRoot));
  assertEquals("3", key.get(0).getLiteral());

  key = parser.getKeyFromEntityLink(entitySet2, "Container2.Photos(Id=4,Type='test')", null);
  assertEquals(2, key.size());

  wrongGetKey(entitySet, "someContainer.Teams('5')", null, UriNotMatchingException.CONTAINERNOTFOUND);
  wrongGetKey(entitySet, "Employees('6')/ne_Team", null, UriNotMatchingException.MATCHPROBLEM);
  wrongGetKey(entitySet, "Teams()", null, UriSyntaxException.ENTITYSETINSTEADOFENTITY);
  wrongGetKey(entitySet, "Teams('8')/Id", null, UriNotMatchingException.MATCHPROBLEM);
  wrongGetKey(entitySet, "Rooms('9')", null, UriNotMatchingException.NOTFOUND);
  wrongGetKey(entitySet, "anotherServiceRoot/Teams('10')", serviceRoot, UriNotMatchingException.NOTFOUND);
  wrongGetKey(entitySet2, "Photos(Id=11,Type='test')", null, UriNotMatchingException.CONTAINERNOTFOUND);
  wrongGetKey(entitySet2, "anotherContainer.Photos(Id=12,Type='test')", null,
      UriNotMatchingException.CONTAINERNOTFOUND);
}
 
Example #28
Source File: MediaProcessor.java    From lemonaid with MIT License 5 votes vote down vote up
public ODataResponse getMediaEntity(final GetMediaResourceUriInfo uriInfo, final String contentType) throws ODataException {
	initialize();
	if (uriInfo.getTargetEntitySet().getEntityType().getName().equals("Attachment")) {
		for (KeyPredicate keyPredicate : uriInfo.getKeyPredicates()) {
			if (keyPredicate.getProperty().getName().equals("Id")) {
				Attachment attachment = attachmentRepository.findOne(keyPredicate.getLiteral());
				return ODataResponse
						.fromResponse(EntityProvider.writeBinary(attachment.getMimeType(), attachment.getData()))
						.header("Content-Disposition", "attachment ;filename=\"" + attachment.getFileName() + "\"")
						.build();
			}
		}
	}
	throw new ODataNotFoundException(ODataNotFoundException.COMMON);
}
 
Example #29
Source File: NetworkEntitySet.java    From DataHubSystem with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public AbstractEntity getEntity(KeyPredicate kp)
{
   Integer key = Integer.parseInt(kp.getLiteral());
   return (new NetworkMap()).get(key);
}
 
Example #30
Source File: JPAQueryBuilderTest.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private UriInfo mockURIInfoWithMultipleKeyPredicates(EdmMapping mapping) throws EdmException {
  
  UriInfo uriInfo = EasyMock.createMock(UriInfo.class);
  List<NavigationSegment> navSegments = new ArrayList<NavigationSegment>();
  EasyMock.expect(uriInfo.getNavigationSegments()).andStubReturn(navSegments);
  EdmEntityType edmEntityType = EasyMock.createMock(EdmEntityType.class);
  EasyMock.expect(edmEntityType.getMapping()).andStubReturn(mapping);
  EdmEntitySet edmEntitySet = EasyMock.createMock(EdmEntitySet.class);
  EasyMock.expect(edmEntitySet.getEntityType()).andStubReturn(edmEntityType);
  EasyMock.expect(uriInfo.getTargetEntitySet()).andStubReturn(edmEntitySet);
  
  List<KeyPredicate> keyPreds = new ArrayList<KeyPredicate>();
  EdmProperty edmProperty1 = mockEdmProperty(mapping, "DateTime");
  keyPreds.add(mockKeyPredicate(edmProperty1, "2012-10-31T18:31:00"));
  
  EdmProperty edmProperty2 = mockEdmProperty(mapping, "Time");
  keyPreds.add(mockKeyPredicate(edmProperty2, "PT0H0M23S"));
  
  EdmProperty edmProperty3 = mockEdmProperty((EdmMapping) mockMappingWithType("Character"), "String");
  keyPreds.add(mockKeyPredicate(edmProperty3, "A"));
  
  EdmProperty edmProperty4 = mockEdmProperty((EdmMapping) mockMappingWithType("char"), "String");
  keyPreds.add(mockKeyPredicate(edmProperty4, "A"));
  
  EdmProperty edmProperty5 = mockEdmProperty((EdmMapping) mockMappingWithType("characterArray"), "String");
  keyPreds.add(mockKeyPredicate(edmProperty5, "ABC"));
  
  EdmProperty edmProperty6 = mockEdmProperty((EdmMapping) mockMappingWithType("charArray"), "String");
  keyPreds.add(mockKeyPredicate(edmProperty6, "ABC"));
  
  EdmProperty edmProperty7 = mockEdmProperty(mapping, "String");
  keyPreds.add(mockKeyPredicate(edmProperty7, "ABC"));
  
  EdmProperty edmProperty8 = mockEdmProperty((EdmMapping) mockMappingWithType("uuid"), "uuid");
  keyPreds.add(mockKeyPredicate(edmProperty8, "56fe79b1-1c88-465b-b309-33bf8b8f6800"));
  
  EasyMock.expect(uriInfo.getKeyPredicates()).andStubReturn(keyPreds); 
  EasyMock.replay(edmEntityType, edmEntitySet, uriInfo);
  return uriInfo;

}