public void createEntity( final String serviceRootURL, final ODataPubFormat format, final ODataEntity original, final String entitySetName, final String contentType, final String prefer) { final URIBuilder uriBuilder = client.getURIBuilder(serviceRootURL); uriBuilder.appendEntitySetSegment(entitySetName); final ODataEntityCreateRequest createReq = client.getCUDRequestFactory().getEntityCreateRequest(uriBuilder.build(), original); createReq.setFormat(format); createReq.setContentType(contentType); createReq.setPrefer(prefer); final ODataEntityCreateResponse createRes = createReq.execute(); if (prefer.equals("return-no-content")) { assertEquals(204, createRes.getStatusCode()); } else { assertEquals(201, createRes.getStatusCode()); assertEquals("Created", createRes.getStatusMessage()); assertTrue(createRes.getHeader("DataServiceVersion").contains("3.0;")); final ODataEntity created = createRes.getBody(); assertNotNull(created); } }
public ODataEntity validateEntities( final String serviceRootURL, final ODataPubFormat format, final ODataEntity original, final int actualObjectId, final Collection<String> expands, final String entitySetName) { final URIBuilder uriBuilder = client .getURIBuilder(serviceRootURL) .appendEntityTypeSegment(entitySetName) .appendKeySegment(actualObjectId); // search expanded if (expands != null) { for (String expand : expands) { uriBuilder.expand(expand); } } final ODataEntityRequest req = client.getRetrieveRequestFactory().getEntityRequest(uriBuilder.build()); req.setFormat(format); final ODataRetrieveResponse<ODataEntity> res = req.execute(); assertEquals(200, res.getStatusCode()); final ODataEntity actual = res.getBody(); assertNotNull(actual); validateLinks(original.getAssociationLinks(), actual.getAssociationLinks()); validateLinks(original.getEditMediaLinks(), actual.getEditMediaLinks()); validateLinks(original.getNavigationLinks(), actual.getNavigationLinks()); // validate equalities of properties checkProperties(original.getProperties(), actual.getProperties()); return actual; }
@Test public void createNoMultipleKey() { final ODataPubFormat format = ODataPubFormat.JSON_FULL_METADATA; final String contentType = "application/json;odata=fullmetadata"; final String prefer = "return-content"; try { final ODataEntity message = ODataObjectFactory.newEntity( "Microsoft.Test.OData.Services.AstoriaDefaultService.Message"); message.addProperty( ODataObjectFactory.newPrimitiveProperty( "MessageId", client .getPrimitiveValueBuilder() .setText(String.valueOf(25)) .setType(EdmSimpleType.Int32) .build())); message.addProperty( ODataObjectFactory.newPrimitiveProperty( "FromUsername", client .getPrimitiveValueBuilder() .setText("user") .setType(EdmSimpleType.String) .build())); message.addProperty( ODataObjectFactory.newPrimitiveProperty( "ToUsername", client .getPrimitiveValueBuilder() .setValue("usernameabc") .setType(EdmSimpleType.String) .build())); message.addProperty( ODataObjectFactory.newPrimitiveProperty( "Subject", client .getPrimitiveValueBuilder() .setValue("Subject of message") .setType(EdmSimpleType.String) .build())); message.addProperty( ODataObjectFactory.newPrimitiveProperty( "Body", client .getPrimitiveValueBuilder() .setValue("Body Content") .setType(EdmSimpleType.String) .build())); message.addProperty( ODataObjectFactory.newPrimitiveProperty( "IsRead", client .getPrimitiveValueBuilder() .setValue(false) .setType(EdmSimpleType.Boolean) .build())); final URIBuilder builder = client.getURIBuilder(testDefaultServiceRootURL).appendEntitySetSegment("Message"); final ODataEntityCreateRequest req = client.getCUDRequestFactory().getEntityCreateRequest(builder.build(), message); req.setFormat(format); req.setContentType(contentType); req.setPrefer(prefer); final ODataEntityCreateResponse res = req.execute(); assertNotNull(res); assertEquals(201, res.getStatusCode()); } catch (Exception e) { fail(e.getMessage()); } }