DataSourceProcessor extends ODataSingleProcessor implements ODataProcessor { @Override public ODataResponse readEntitySet(final GetEntitySetUriInfo uriInfo, final String contentType) throws ODataException { ArrayList<Object> data = new ArrayList<>(); ReadResult result; try { result = retrieveData(uriInfo, uriInfo.getStartEntitySet(), uriInfo.getKeyPredicates(), uriInfo.getFunctionImport(), mapFunctionParameters(uriInfo.getFunctionImportParameters()), uriInfo.getNavigationSegments()); data.addAll(result.getResult()); } catch (final ODataNotFoundException e) { data.clear(); result = ReadResult.empty(); } final EdmEntitySet entitySet = uriInfo.getTargetEntitySet(); final InlineCount inlineCountType = uriInfo.getInlineCount(); final Integer count = applySystemQueryOptions(entitySet, data, new QueryOptionsHolder(uriInfo), result); ODataContext context = getContext(); String nextLink = null; if (data.size() > SERVER_PAGING_SIZE) { if (uriInfo.getOrderBy() == null && uriInfo.getSkipToken() == null && uriInfo.getSkip() == null && uriInfo.getTop() == null) { sortInDefaultOrder(entitySet, data); } nextLink = context.getPathInfo().getServiceRoot().relativize(context.getPathInfo().getRequestUri()).toString(); nextLink = percentEncodeNextLink(nextLink); nextLink += (nextLink.contains("?") ? "&" : "?") + "$skiptoken=" + getSkipToken(entitySet, data.get(SERVER_PAGING_SIZE)); while (data.size() > SERVER_PAGING_SIZE) { data.remove(SERVER_PAGING_SIZE); } } final EdmEntityType entityType = entitySet.getEntityType(); List<Map<String, Object>> values = new ArrayList<>(); for (final Object entryData : data) { values.add(getStructuralTypeValueMap(entryData, entityType)); } final EntityProviderWriteProperties feedProperties = EntityProviderWriteProperties .serviceRoot(context.getPathInfo().getServiceRoot()) .inlineCountType(inlineCountType) .inlineCount(count) .expandSelectTree(UriParser.createExpandSelectTree(uriInfo.getSelect(), uriInfo.getExpand())) .callbacks(getCallbacks(data, entityType)) .nextLink(nextLink) .build(); final int timingHandle = context.startRuntimeMeasurement("EntityProvider", "writeFeed"); final ODataResponse response = EntityProvider.writeFeed(contentType, entitySet, values, feedProperties); context.stopRuntimeMeasurement(timingHandle); return ODataResponse.fromResponse(response).build(); } DataSourceProcessor(final DataSource dataSource, final ValueAccess valueAccess);  DataSourceProcessor(final DataSource dataSource, final ValueAccess valueAccess, final FunctionSource functionSource); @Override ODataResponse readEntitySet(final GetEntitySetUriInfo uriInfo, final String contentType); @Override ODataResponse countEntitySet(final GetEntitySetCountUriInfo uriInfo, final String contentType); @Override ODataResponse readEntityLinks(final GetEntitySetLinksUriInfo uriInfo, final String contentType); @Override ODataResponse countEntityLinks(final GetEntitySetLinksCountUriInfo uriInfo, final String contentType); @Override ODataResponse readEntity(final GetEntityUriInfo uriInfo, final String contentType); @Override ODataResponse existsEntity(final GetEntityCountUriInfo uriInfo, final String contentType); @Override ODataResponse deleteEntity(final DeleteUriInfo uriInfo, final String contentType); @Override ODataResponse createEntity(final PostUriInfo uriInfo, final InputStream content,
      final String requestContentType, final String contentType); @Override ODataResponse updateEntity(final PutMergePatchUriInfo uriInfo, final InputStream content,
      final String requestContentType, final boolean merge, final String contentType); @Override ODataResponse readEntityLink(final GetEntityLinkUriInfo uriInfo, final String contentType); @Override ODataResponse existsEntityLink(final GetEntityLinkCountUriInfo uriInfo, final String contentType); @Override ODataResponse deleteEntityLink(final DeleteUriInfo uriInfo, final String contentType); @Override ODataResponse createEntityLink(final PostUriInfo uriInfo, final InputStream content,
      final String requestContentType, final String contentType); @Override ODataResponse updateEntityLink(final PutMergePatchUriInfo uriInfo, final InputStream content,
      final String requestContentType, final String contentType); @Override ODataResponse readEntityComplexProperty(final GetComplexPropertyUriInfo uriInfo, final String contentType); @Override ODataResponse readEntitySimpleProperty(final GetSimplePropertyUriInfo uriInfo, final String contentType); @Override ODataResponse readEntitySimplePropertyValue(final GetSimplePropertyUriInfo uriInfo, final String contentType); @Override ODataResponse deleteEntitySimplePropertyValue(final DeleteUriInfo uriInfo, final String contentType); @Override ODataResponse updateEntityComplexProperty(final PutMergePatchUriInfo uriInfo, final InputStream content,
      final String requestContentType, final boolean merge, final String contentType); @Override ODataResponse updateEntitySimpleProperty(final PutMergePatchUriInfo uriInfo, final InputStream content,
      final String requestContentType, final String contentType); @Override ODataResponse updateEntitySimplePropertyValue(final PutMergePatchUriInfo uriInfo, final InputStream content,
      final String requestContentType, final String contentType); @Override ODataResponse readEntityMedia(final GetMediaResourceUriInfo uriInfo, final String contentType); @Override ODataResponse deleteEntityMedia(final DeleteUriInfo uriInfo, final String contentType); @Override ODataResponse updateEntityMedia(final PutMergePatchUriInfo uriInfo, final InputStream content,
      final String requestContentType, final String contentType); @Override ODataResponse executeFunctionImport(final GetFunctionImportUriInfo uriInfo, final String contentType); @Override ODataResponse executeFunctionImportValue(final GetFunctionImportUriInfo uriInfo, final String contentType); @Override ODataResponse executeBatch(final BatchHandler handler, final String contentType, final InputStream content); @Override BatchResponsePart executeChangeSet(final BatchHandler handler, final List<ODataRequest> requests);  }
@Test public void readTop() throws Exception { GetEntitySetUriInfo uriInfo = createMockedUriInfo("Rooms"); Mockito.when(uriInfo.getTop()).thenReturn(5); List<Room> results = createRooms(1, 10); ReadResult<Room> readResult = ReadResult.forResult(results).build(); Mockito.when(mockedDataSource.readData(Mockito.any(EdmEntitySet.class), Mockito.any(ReadOptions.class))) .thenReturn((ReadResult) readResult); ODataResponse result = dataSourceProcessor.readEntitySet(uriInfo, "application/json"); StringHelper.Stream resultStream = StringHelper.toStream(result.getEntityAsStream()); List parsedResults = JsonHelper.getResults(resultStream.asString()); Assert.assertEquals(5, parsedResults.size()); }
@Test public void optimizedReadTop() throws Exception { GetEntitySetUriInfo uriInfo = createMockedUriInfo("Rooms"); Mockito.when(uriInfo.getTop()).thenReturn(5); List<Room> results = createRooms(1, 10); ReadResult<Room> readResult = ReadResult.forResult(results).topApplied().build(); Mockito.when(mockedDataSource.readData(Mockito.any(EdmEntitySet.class), Mockito.any(ReadOptions.class))) .thenReturn((ReadResult) readResult); ODataResponse result = dataSourceProcessor.readEntitySet(uriInfo, "application/json"); StringHelper.Stream resultStream = StringHelper.toStream(result.getEntityAsStream()); List parsedResults = JsonHelper.getResults(resultStream.asString()); Assert.assertEquals(10, parsedResults.size()); }
@Test public void readOrder() throws Exception { GetEntitySetUriInfo uriInfo = createMockedUriInfo("Rooms"); Edm edm = EdmMock.createMockEdm(); OrderByExpression exp = UriParserImpl.parseOrderBy(edm, edm.getEntityType("RefScenario", "Room"), "Name"); Mockito.when(uriInfo.getOrderBy()).thenReturn(exp); List<Room> results = createRooms(1, 10); Collections.reverse(results); ReadResult<Room> readResult = ReadResult.forResult(results).build(); Mockito.when(mockedDataSource.readData(Mockito.any(EdmEntitySet.class), Mockito.any(ReadOptions.class))) .thenReturn((ReadResult)readResult); ODataResponse result = dataSourceProcessor.readEntitySet(uriInfo, "application/json"); StringHelper.Stream resultStream = StringHelper.toStream(result.getEntityAsStream()); List<LinkedTreeMap<?, ?>> parsedResults = JsonHelper.getResults(resultStream.asString()); Assert.assertEquals(10, parsedResults.size()); Assert.assertEquals("Room with id: 1", parsedResults.get(0).get("Name")); Assert.assertEquals("Room with id: 9", parsedResults.get(9).get("Name")); }
@Test public void optimizedReadOrder() throws Exception { GetEntitySetUriInfo uriInfo = createMockedUriInfo("Rooms"); Edm edm = EdmMock.createMockEdm(); OrderByExpression exp = UriParserImpl.parseOrderBy(edm, edm.getEntityType("RefScenario", "Room"), "Name"); Mockito.when(uriInfo.getOrderBy()).thenReturn(exp); List<Room> results = createRooms(1, 10); Collections.shuffle(results); ReadResult<Room> readResult = ReadResult.forResult(results).orderApplied().build(); Mockito.when(mockedDataSource.readData(Mockito.any(EdmEntitySet.class), Mockito.any(ReadOptions.class))) .thenReturn((ReadResult)readResult); ODataResponse result = dataSourceProcessor.readEntitySet(uriInfo, "application/json"); StringHelper.Stream resultStream = StringHelper.toStream(result.getEntityAsStream()); List<LinkedTreeMap<?, ?>> parsedResults = JsonHelper.getResults(resultStream.asString()); Assert.assertEquals(10, parsedResults.size()); Assert.assertEquals("Room with id: 1", parsedResults.get(0).get("Name")); Assert.assertEquals("Room with id: 9", parsedResults.get(9).get("Name")); }
@Test public void readFilter() throws Exception { GetEntitySetUriInfo uriInfo = createMockedUriInfo("Rooms"); Edm edm = EdmMock.createMockEdm(); FilterExpression exp = UriParserImpl.parseFilter(edm, edm.getEntityType("RefScenario", "Room"), "Version gt 105"); Mockito.when(uriInfo.getFilter()).thenReturn(exp); List<Room> results = createRooms(1, 10); ReadResult<Room> readResult = ReadResult.forResult(results).build(); Mockito.when(mockedDataSource.readData(Mockito.any(EdmEntitySet.class), Mockito.any(ReadOptions.class))) .thenReturn((ReadResult)readResult); ODataResponse result = dataSourceProcessor.readEntitySet(uriInfo, "application/json"); StringHelper.Stream resultStream = StringHelper.toStream(result.getEntityAsStream()); List<LinkedTreeMap<?, ?>> parsedResults = JsonHelper.getResults(resultStream.asString()); Assert.assertEquals(5, parsedResults.size()); Assert.assertEquals("Room with id: 10", parsedResults.get(0).get("Name")); Assert.assertEquals("Room with id: 9", parsedResults.get(4).get("Name")); }
@Test public void readOptimizedFilter() throws Exception { GetEntitySetUriInfo uriInfo = createMockedUriInfo("Rooms"); Edm edm = EdmMock.createMockEdm(); FilterExpression exp = UriParserImpl.parseFilter(edm, edm.getEntityType("RefScenario", "Room"), "Version gt 105"); Mockito.when(uriInfo.getFilter()).thenReturn(exp); List<Room> results = createRooms(1, 10); ReadResult<Room> readResult = ReadResult.forResult(results).filterApplied().build(); Mockito.when(mockedDataSource.readData(Mockito.any(EdmEntitySet.class), Mockito.any(ReadOptions.class))) .thenReturn((ReadResult)readResult); ODataResponse result = dataSourceProcessor.readEntitySet(uriInfo, "application/json"); StringHelper.Stream resultStream = StringHelper.toStream(result.getEntityAsStream()); List<LinkedTreeMap<?, ?>> parsedResults = JsonHelper.getResults(resultStream.asString()); Assert.assertEquals(10, parsedResults.size()); Assert.assertEquals("Room with id: 1", parsedResults.get(0).get("Name")); Assert.assertEquals("Room with id: 9", parsedResults.get(9).get("Name")); }