@Test public void whenOptimisticLockRollBackExceptionThenThrowODataJPAProcessorExceptionWithHttpCode412() throws ODataApplicationException, ODataLibraryException { when(response.getStatusCode()).thenReturn(HttpStatusCode.OK.getStatusCode()); when(facade.handleODataRequest(request)).thenReturn(response); doThrow(e).when(transaction).commit(); when(e.getCause()).thenReturn(new OptimisticLockException()); final ODataJPAProcessorException act = assertThrows(ODataJPAProcessorException.class, () -> cut.processChangeSet(facade, requests)); assertEquals(HttpStatusCode.PRECONDITION_FAILED.getStatusCode(), act.getStatusCode()); }
@Override public ODataResponsePart processChangeSet(final BatchFacade facade, final List<ODataRequest> requests) throws ODataApplicationException, ODataLibraryException { final int handle = requestContext.getDebugger().startRuntimeMeasurement(this, "processChangeSet"); final List<ODataResponse> responses = new ArrayList<>(); try { final JPAODataTransaction t = requestContext.getTransactionFactory().createTransaction(); try { for (final ODataRequest request : requests) { final ODataResponse response = facade.handleODataRequest(request); final int statusCode = response.getStatusCode(); if (statusCode < 400) { responses.add(response); } else { t.rollback(); requestContext.getDebugger().stopRuntimeMeasurement(handle); return new ODataResponsePart(response, false); } } requestContext.getCUDRequestHandler().validateChanges(requestContext.getEntityManager()); t.commit(); requestContext.getDebugger().stopRuntimeMeasurement(handle); return new ODataResponsePart(responses, true); } catch (ODataApplicationException e) { t.rollback(); requestContext.getDebugger().stopRuntimeMeasurement(handle); throw e; } catch (ODataLibraryException e) { t.rollback(); requestContext.getDebugger().stopRuntimeMeasurement(handle); throw e; } catch (RollbackException e) { if (e.getCause() instanceof OptimisticLockException) { requestContext.getDebugger().stopRuntimeMeasurement(handle); throw new ODataJPAProcessorException(e.getCause().getCause(), HttpStatusCode.PRECONDITION_FAILED); } requestContext.getDebugger().stopRuntimeMeasurement(handle); throw new ODataJPAProcessorException(e, HttpStatusCode.INTERNAL_SERVER_ERROR); } } catch (ODataJPATransactionException e) { throw new ODataJPAProcessorException(e, HttpStatusCode.NOT_IMPLEMENTED); } }
JPAODataBatchProcessor implements BatchProcessor { @Override public ODataResponsePart processChangeSet(final BatchFacade facade, final List<ODataRequest> requests) throws ODataApplicationException, ODataLibraryException { final int handle = requestContext.getDebugger().startRuntimeMeasurement(this, "processChangeSet"); final List<ODataResponse> responses = new ArrayList<>(); try { final JPAODataTransaction t = requestContext.getTransactionFactory().createTransaction(); try { for (final ODataRequest request : requests) { final ODataResponse response = facade.handleODataRequest(request); final int statusCode = response.getStatusCode(); if (statusCode < 400) { responses.add(response); } else { t.rollback(); requestContext.getDebugger().stopRuntimeMeasurement(handle); return new ODataResponsePart(response, false); } } requestContext.getCUDRequestHandler().validateChanges(requestContext.getEntityManager()); t.commit(); requestContext.getDebugger().stopRuntimeMeasurement(handle); return new ODataResponsePart(responses, true); } catch (ODataApplicationException e) { t.rollback(); requestContext.getDebugger().stopRuntimeMeasurement(handle); throw e; } catch (ODataLibraryException e) { t.rollback(); requestContext.getDebugger().stopRuntimeMeasurement(handle); throw e; } catch (RollbackException e) { if (e.getCause() instanceof OptimisticLockException) { requestContext.getDebugger().stopRuntimeMeasurement(handle); throw new ODataJPAProcessorException(e.getCause().getCause(), HttpStatusCode.PRECONDITION_FAILED); } requestContext.getDebugger().stopRuntimeMeasurement(handle); throw new ODataJPAProcessorException(e, HttpStatusCode.INTERNAL_SERVER_ERROR); } } catch (ODataJPATransactionException e) { throw new ODataJPAProcessorException(e, HttpStatusCode.NOT_IMPLEMENTED); } } }
JPAODataBatchProcessor implements BatchProcessor { @Override public ODataResponsePart processChangeSet(final BatchFacade facade, final List<ODataRequest> requests) throws ODataApplicationException, ODataLibraryException { final int handle = requestContext.getDebugger().startRuntimeMeasurement(this, "processChangeSet"); final List<ODataResponse> responses = new ArrayList<>(); try { final JPAODataTransaction t = requestContext.getTransactionFactory().createTransaction(); try { for (final ODataRequest request : requests) { final ODataResponse response = facade.handleODataRequest(request); final int statusCode = response.getStatusCode(); if (statusCode < 400) { responses.add(response); } else { t.rollback(); requestContext.getDebugger().stopRuntimeMeasurement(handle); return new ODataResponsePart(response, false); } } requestContext.getCUDRequestHandler().validateChanges(requestContext.getEntityManager()); t.commit(); requestContext.getDebugger().stopRuntimeMeasurement(handle); return new ODataResponsePart(responses, true); } catch (ODataApplicationException e) { t.rollback(); requestContext.getDebugger().stopRuntimeMeasurement(handle); throw e; } catch (ODataLibraryException e) { t.rollback(); requestContext.getDebugger().stopRuntimeMeasurement(handle); throw e; } catch (RollbackException e) { if (e.getCause() instanceof OptimisticLockException) { requestContext.getDebugger().stopRuntimeMeasurement(handle); throw new ODataJPAProcessorException(e.getCause().getCause(), HttpStatusCode.PRECONDITION_FAILED); } requestContext.getDebugger().stopRuntimeMeasurement(handle); throw new ODataJPAProcessorException(e, HttpStatusCode.INTERNAL_SERVER_ERROR); } } catch (ODataJPATransactionException e) { throw new ODataJPAProcessorException(e, HttpStatusCode.NOT_IMPLEMENTED); } } JPAODataBatchProcessor(final JPAODataRequestContextAccess requestContext); }
JPAODataBatchProcessor implements BatchProcessor { @Override public ODataResponsePart processChangeSet(final BatchFacade facade, final List<ODataRequest> requests) throws ODataApplicationException, ODataLibraryException { final int handle = requestContext.getDebugger().startRuntimeMeasurement(this, "processChangeSet"); final List<ODataResponse> responses = new ArrayList<>(); try { final JPAODataTransaction t = requestContext.getTransactionFactory().createTransaction(); try { for (final ODataRequest request : requests) { final ODataResponse response = facade.handleODataRequest(request); final int statusCode = response.getStatusCode(); if (statusCode < 400) { responses.add(response); } else { t.rollback(); requestContext.getDebugger().stopRuntimeMeasurement(handle); return new ODataResponsePart(response, false); } } requestContext.getCUDRequestHandler().validateChanges(requestContext.getEntityManager()); t.commit(); requestContext.getDebugger().stopRuntimeMeasurement(handle); return new ODataResponsePart(responses, true); } catch (ODataApplicationException e) { t.rollback(); requestContext.getDebugger().stopRuntimeMeasurement(handle); throw e; } catch (ODataLibraryException e) { t.rollback(); requestContext.getDebugger().stopRuntimeMeasurement(handle); throw e; } catch (RollbackException e) { if (e.getCause() instanceof OptimisticLockException) { requestContext.getDebugger().stopRuntimeMeasurement(handle); throw new ODataJPAProcessorException(e.getCause().getCause(), HttpStatusCode.PRECONDITION_FAILED); } requestContext.getDebugger().stopRuntimeMeasurement(handle); throw new ODataJPAProcessorException(e, HttpStatusCode.INTERNAL_SERVER_ERROR); } } catch (ODataJPATransactionException e) { throw new ODataJPAProcessorException(e, HttpStatusCode.NOT_IMPLEMENTED); } } JPAODataBatchProcessor(final JPAODataRequestContextAccess requestContext); @Override void init(final OData odata, final ServiceMetadata serviceMetadata); @Override void processBatch(final BatchFacade facade, final ODataRequest request, final ODataResponse response); @Override ODataResponsePart processChangeSet(final BatchFacade facade, final List<ODataRequest> requests); }
JPAODataBatchProcessor implements BatchProcessor { @Override public ODataResponsePart processChangeSet(final BatchFacade facade, final List<ODataRequest> requests) throws ODataApplicationException, ODataLibraryException { final int handle = requestContext.getDebugger().startRuntimeMeasurement(this, "processChangeSet"); final List<ODataResponse> responses = new ArrayList<>(); try { final JPAODataTransaction t = requestContext.getTransactionFactory().createTransaction(); try { for (final ODataRequest request : requests) { final ODataResponse response = facade.handleODataRequest(request); final int statusCode = response.getStatusCode(); if (statusCode < 400) { responses.add(response); } else { t.rollback(); requestContext.getDebugger().stopRuntimeMeasurement(handle); return new ODataResponsePart(response, false); } } requestContext.getCUDRequestHandler().validateChanges(requestContext.getEntityManager()); t.commit(); requestContext.getDebugger().stopRuntimeMeasurement(handle); return new ODataResponsePart(responses, true); } catch (ODataApplicationException e) { t.rollback(); requestContext.getDebugger().stopRuntimeMeasurement(handle); throw e; } catch (ODataLibraryException e) { t.rollback(); requestContext.getDebugger().stopRuntimeMeasurement(handle); throw e; } catch (RollbackException e) { if (e.getCause() instanceof OptimisticLockException) { requestContext.getDebugger().stopRuntimeMeasurement(handle); throw new ODataJPAProcessorException(e.getCause().getCause(), HttpStatusCode.PRECONDITION_FAILED); } requestContext.getDebugger().stopRuntimeMeasurement(handle); throw new ODataJPAProcessorException(e, HttpStatusCode.INTERNAL_SERVER_ERROR); } } catch (ODataJPATransactionException e) { throw new ODataJPAProcessorException(e, HttpStatusCode.NOT_IMPLEMENTED); } } JPAODataBatchProcessor(final JPAODataRequestContextAccess requestContext); @Override void init(final OData odata, final ServiceMetadata serviceMetadata); @Override void processBatch(final BatchFacade facade, final ODataRequest request, final ODataResponse response); @Override ODataResponsePart processChangeSet(final BatchFacade facade, final List<ODataRequest> requests);  }