ODataWriteProcessorImpl implements ODataWriteProcessor { @Override public ProcessorResult write(ODataRequestContext requestContext, Object entity) throws ODataException { try { WriteMethodHandler methodHandler = getHandler(requestContext); if (methodHandler == null) { return new ProcessorResult(METHOD_NOT_ALLOWED); } return methodHandler.handleWrite(entity); } catch (Exception e) { LOG.error("Couldn't persist or delete given entity '" + entity + "'", e); throw e; } }  @Override ProcessorResult write(ODataRequestContext requestContext, Object entity);  }
@Test public void testUnhandledMethod() throws ODataException, UnsupportedEncodingException { requestContext = createContextWithEntitySet(GET); ProcessorResult result = oDataWriteProcessor.write(requestContext, entity); assertThat(result.getStatus(), is(METHOD_NOT_ALLOWED)); assertThat(result.getData(), is(nullValue())); }
@Test public void testPost() throws Exception { requestContext = createContextWithEntitySet(POST); when(dataSourceFactory.getDataSource(requestContext, entityType)).thenReturn(dataSource); when(dataSource.create(requestContext.getUri(), entity, entityDataModel)).thenReturn(entity); ProcessorResult result = oDataWriteProcessor.write(requestContext, entity); assertThat(result.getStatus(), is(CREATED)); assertThat(result.getData(), is(entity)); verify(dataSource, times(1)).create(requestContext.getUri(), entity, entityDataModel); }
@Test(expected = ODataBadRequestException.class) public void testPostWithNullEntity() throws Exception { requestContext = createContextWithEntitySet(POST); oDataWriteProcessor.write(requestContext, null); }
@Test(expected = ODataBadRequestException.class) public void testPostEntity() throws Exception { requestContext = createContextWithEntity(POST, false); oDataWriteProcessor.write(requestContext, entity); }
@Test public void testDelete() throws Exception { requestContext = createContextForDelete(); when(dataSourceFactory.getDataSource(requestContext, entityType)).thenReturn(dataSource); ProcessorResult result = oDataWriteProcessor.write(requestContext, null); assertThat(result.getStatus(), is(NO_CONTENT)); verify(dataSource, times(1)).delete(requestContext.getUri(), entityDataModel); }
@Test(expected = ODataBadRequestException.class) public void testDeleteWithoutNull() throws Exception { requestContext = createContextForDelete(); ProcessorResult result = oDataWriteProcessor.write(requestContext, entity); assertThat(result.getStatus(), is(OK)); assertThat(result.getData(), is(entity)); }
@Test(expected = ODataBadRequestException.class) public void testPutWithNullEntity() throws ODataException, UnsupportedEncodingException { requestContext = createContextWithEntitySet(PUT); oDataWriteProcessor.write(requestContext, null); }
@Test(expected = ODataBadRequestException.class) public void testPatchWithNullEntity() throws ODataException, UnsupportedEncodingException { requestContext = createContextWithEntitySet(PATCH); oDataWriteProcessor.write(requestContext, null); }
@Test public void testWriteWithPut() throws Exception { requestContext = createContextWithEntity(PUT, false); when(dataSourceFactory.getDataSource(requestContext, entityType)).thenReturn(dataSource); when(dataSource.update(requestContext.getUri(), entity, entityDataModel)).thenReturn(entity); ProcessorResult result = oDataWriteProcessor.write(requestContext, entity); assertThat(result.getStatus(), is(OK)); assertThat(result.getData(), is(entity)); Map<String, String> headers = result.getHeaders(); assertThat(headers.size(), is(1)); assertThat(headers.get("Location"), is("http: }
@Test(expected = ODataBadRequestException.class) public void testPutWithEntitySet() throws ODataException, UnsupportedEncodingException { requestContext = createContextWithEntitySet(PUT); oDataWriteProcessor.write(requestContext, entity); }
@Test(expected = ODataBadRequestException.class) public void testPatchWithEntitySet() throws ODataException, UnsupportedEncodingException { requestContext = createContextWithEntitySet(PATCH); oDataWriteProcessor.write(requestContext, entity); }
@Test public void testWriteWithPutReturnMinimal() throws Exception { requestContext = createContextWithEntity(PUT, true); when(dataSourceFactory.getDataSource(requestContext, entityType)).thenReturn(dataSource); when(dataSource.update(requestContext.getUri(), entity, entityDataModel)).thenReturn(entity); ProcessorResult result = oDataWriteProcessor.write(requestContext, entity); assertThat(result.getStatus(), is(NO_CONTENT)); assertThat(result.getData(), is(nullValue())); Map<String, String> headers = result.getHeaders(); assertThat(headers.size(), is(1)); assertThat(headers.get("Location"), is("http: }
@Test public void testWriteWithPatch() throws Exception { requestContext = createContextWithEntity(PATCH, false); when(dataSourceFactory.getDataSource(requestContext, entityType)).thenReturn(dataSource); when(dataSource.update(requestContext.getUri(), entity, entityDataModel)).thenReturn(entity); ProcessorResult result = oDataWriteProcessor.write(requestContext, entity); assertThat(result.getStatus(), is(OK)); assertThat(result.getData(), is(entity)); Map<String, String> headers = result.getHeaders(); assertThat(headers.size(), is(1)); assertThat(headers.get("Location"), is("http: }
@Test public void testWriteWithPatchReturnMinimal() throws Exception { requestContext = createContextWithEntity(PATCH, true); when(dataSourceFactory.getDataSource(requestContext, entityType)).thenReturn(dataSource); when(dataSource.update(requestContext.getUri(), entity, entityDataModel)).thenReturn(entity); ProcessorResult result = oDataWriteProcessor.write(requestContext, entity); assertThat(result.getStatus(), is(NO_CONTENT)); assertThat(result.getData(), is(nullValue())); Map<String, String> headers = result.getHeaders(); assertThat(headers.size(), is(1)); assertThat(headers.get("Location"), is("http: }
@Test public void testWriteWithUnSupportedHttpMethod() throws ODataException, UnsupportedEncodingException { requestContext = createODataRequestContext(ODataRequest.Method.GET, entityDataModel); lenient().when(dataSourceFactory.getDataSource(requestContext, entity.getClass().getSimpleName())).thenReturn(dataSource); ProcessorResult result = oDataWriteProcessor.write(requestContext, entity); assertThat(result.getStatus(), is(METHOD_NOT_ALLOWED)); }