BatchMethodHandler { public List<ProcessorResult> handleWrite() throws ODataException { LOG.info("Handling transactional operations per each odata request."); List<ProcessorResult> resultList = new ArrayList<>(); try { for (ChangeSetEntity changeSetEntity : changeSetEntities) { ODataRequestContext odataRequestContext = changeSetEntity.getRequestContext(); ODataUri requestUri = odataRequestContext.getUri(); ODataRequest.Method method = odataRequestContext.getRequest().getMethod(); ProcessorResult result = null; if (method == ODataRequest.Method.POST) { result = handlePOST(odataRequestContext, requestUri, changeSetEntity); } else if (method == ODataRequest.Method.PUT || method == ODataRequest.Method.PATCH) { result = handlePutAndPatch(odataRequestContext, requestUri, changeSetEntity); } else if (method == ODataRequest.Method.DELETE) { result = handleDelete(odataRequestContext, requestUri, changeSetEntity); } resultList.add(result); } commitTransactions(); } catch (ODataException e) { LOG.error("Transaction could not be processed, rolling back", e); rollbackTransactions(); throw e; } return resultList; } BatchMethodHandler(ODataRequestContext requestContext, DataSourceFactory dataSourceFactory,
                              List<ChangeSetEntity> changeSetEntries); List<ProcessorResult> handleWrite();  }
@Test(expected = ODataException.class) public void testFailWithDbError() throws Exception { stubForTesting(getEntity()); TransactionalDataSource trxDataSourceMock = mock(TransactionalDataSource.class); when(trxDataSourceMock.create(any(ODataUri.class), any(), any(EntityDataModel.class))) .thenThrow(new ODataDataSourceException("something went wrong with db")); when(dataSourceMock.startTransaction()).thenReturn(trxDataSourceMock); EntityDataModel entityDataModel = getEntityDataModel(); getPostMethodHandler(entityDataModel, getEntity()).handleWrite(); }
@Test public void testSuccess() throws Exception { stubForTesting(getEntity()); TransactionalDataSource trxDataSourceMock = mock(TransactionalDataSource.class); when(trxDataSourceMock.create(any(ODataUri.class), any(), any(EntityDataModel.class))).thenReturn(getEntity()); when(dataSourceMock.startTransaction()).thenReturn(trxDataSourceMock); EntityDataModel entityDataModel = getEntityDataModel(); getPostMethodHandler(entityDataModel, getEntity()).handleWrite(); }