@Test public void clientEscaping() { final String filter = getClient() .getFilterFactory() .eq( getClient().getFilterFactory().getArgFactory().property("PropertyString"), getClient() .getFilterFactory() .getArgFactory() .literal("First Resource - positive values")) .build(); final URI uri = getClient() .newURIBuilder(SERVICE_URI) .appendEntitySetSegment(ES_ALL_PRIM) .filter(filter) .build(); ODataEntitySetRequest<ClientEntitySet> request = getClient().getRetrieveRequestFactory().getEntitySetRequest(uri); setCookieHeader(request); final ODataRetrieveResponse<ClientEntitySet> response = request.execute(); saveCookieHeader(response); assertEquals(1, response.getBody().getEntities().size()); ClientEntity clientEntity = response.getBody().getEntities().get(0); assertShortOrInt( 32767, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); }
@Test public void testMissingEntity() { // Update an existing entity, use a URI to a not existing entity // Perform the request to a single navigation property and a collection navigation property as // well. // Expected: Not Found (404) final ODataClient client = getClient(); final URI entityURI = client .newURIBuilder(SERVICE_URI) .appendEntitySetSegment(ES_KEY_NAV) .appendKeySegment(1) .build(); final ClientObjectFactory of = client.getObjectFactory(); // Request to single (non collection) navigation property ClientEntity entity = of.newEntity(ET_KEY_NAV); final ClientLink navLinkOne = of.newEntityNavigationLink( NAV_PROPERTY_ET_KEY_NAV_ONE, client .newURIBuilder(SERVICE_URI) .appendEntitySetSegment(ES_KEY_NAV) .appendKeySegment(42) .build()); entity.addLink(navLinkOne); try { client .getCUDRequestFactory() .getEntityUpdateRequest(entityURI, UpdateType.PATCH, entity) .execute(); fail(); } catch (ODataClientErrorException e) { assertEquals(HttpStatusCode.NOT_FOUND.getStatusCode(), e.getStatusLine().getStatusCode()); } // Request to collection navigation propetry entity = of.newEntity(ET_KEY_NAV); final ClientLink navLinkMany = of.newEntitySetNavigationLink( NAV_PROPERTY_ET_KEY_NAV_MANY, client .newURIBuilder(SERVICE_URI) .appendEntitySetSegment(ES_KEY_NAV) .appendKeySegment(3) .build()); entity.addLink(navLinkMany); try { client .getCUDRequestFactory() .getEntityUpdateRequest(entityURI, UpdateType.PATCH, entity) .execute(); } catch (ODataClientErrorException e) { assertEquals(HttpStatusCode.NOT_FOUND.getStatusCode(), e.getStatusLine().getStatusCode()); } }
@Test public void numericPromotionToInt64() { ODataRetrieveResponse<ClientEntitySet> result = sendRequest(ES_ALL_PRIM, "PropertyInt64 eq 0"); assertEquals(1, result.getBody().getEntities().size()); ClientEntity clientEntity = result.getBody().getEntities().get(0); assertShortOrInt(0, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); }
@Test public void stringProperty() { ODataRetrieveResponse<ClientEntitySet> result = sendRequest(ES_TWO_KEY_NAV, "PropertyString eq '2'"); assertEquals(1, result.getBody().getEntities().size()); ClientEntity clientEntity = result.getBody().getEntities().get(0); assertEquals("2", clientEntity.getProperty("PropertyString").getPrimitiveValue().toValue()); }
@Test public void dateSubDate() { ODataRetrieveResponse<ClientEntitySet> response = sendRequest(ES_ALL_PRIM, "PropertyDuration eq 2012-12-04 sub 2012-12-04"); assertEquals(1, response.getBody().getEntities().size()); final ClientEntity clientEntity = response.getBody().getEntities().get(0); assertShortOrInt(0, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); }
@Test public void binaryIntegerOperations() { ODataRetrieveResponse<ClientEntitySet> result = sendRequest(ES_TWO_KEY_NAV, "PropertyInt16 add 1 eq (1 sub 3) div 2 mul 3 add 7"); assertEquals(1, result.getBody().getEntities().size()); ClientEntity clientEntity = result.getBody().getEntities().get(0); assertShortOrInt(3, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); assertEquals("1", clientEntity.getProperty("PropertyString").getPrimitiveValue().toValue()); }
@Test public void minuteFunctionDateTimeOffset() { ODataRetrieveResponse<ClientEntitySet> result = sendRequest(ES_ALL_PRIM, "minute(PropertyDateTimeOffset) eq 17"); assertEquals(1, result.getBody().getEntities().size()); ClientEntity clientEntity = result.getBody().getEntities().get(0); assertShortOrInt( -32768, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); }
@Test public void secondFunctionTimeOfDay() { ODataRetrieveResponse<ClientEntitySet> response = sendRequest(ES_ALL_PRIM, "second(PropertyTimeOfDay) eq 14"); assertEquals(1, response.getBody().getEntities().size()); ClientEntity clientEntity = response.getBody().getEntities().get(0); assertShortOrInt( -32768, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); }
@Test public void toUpper() { ODataRetrieveResponse<ClientEntitySet> result = sendRequest(ES_ALL_PRIM, "contains(PropertyString,concat(toupper('f'),'irst'))"); assertEquals(1, result.getBody().getEntities().size()); ClientEntity clientEntity = result.getBody().getEntities().get(0); assertShortOrInt( 32767, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); }
@Test public void trim() { ODataRetrieveResponse<ClientEntitySet> result = sendRequest(ES_ALL_PRIM, "trim(substring(PropertyString,0,6)) eq 'First'"); assertEquals(1, result.getBody().getEntities().size()); ClientEntity clientEntity = result.getBody().getEntities().get(0); assertShortOrInt( 32767, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); }
@Test public void dateTimeOffsetLiteral() { ODataRetrieveResponse<ClientEntitySet> result = sendRequest(ES_ALL_PRIM, "PropertyDateTimeOffset eq 2012-12-03T07:16:23Z"); assertEquals(1, result.getBody().getEntities().size()); ClientEntity clientEntity = result.getBody().getEntities().get(0); assertShortOrInt( 32767, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); }
@Test public void decimalDiv() { ODataRetrieveResponse<ClientEntitySet> result = sendRequest(ES_ALL_PRIM, "PropertyDouble eq 0 sub (358000 div 2)"); assertEquals(1, result.getBody().getEntities().size()); ClientEntity clientEntity = result.getBody().getEntities().get(0); assertShortOrInt( -32768, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); }
@Test public void timeOfDayLiteral() { ODataRetrieveResponse<ClientEntitySet> result = sendRequest(ES_ALL_PRIM, "PropertyTimeOfDay eq 03:26:05"); assertEquals(1, result.getBody().getEntities().size()); ClientEntity clientEntity = result.getBody().getEntities().get(0); assertShortOrInt( 32767, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); }
@Test public void dateAddDuration() { ODataRetrieveResponse<ClientEntitySet> response = sendRequest(ES_ALL_PRIM, "PropertyDate eq 2012-12-01 add duration'P1DT27H16M23S'"); assertEquals(1, response.getBody().getEntities().size()); final ClientEntity clientEntity = response.getBody().getEntities().get(0); assertShortOrInt( 32767, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); }
@Test public void durationSubDuration() { ODataRetrieveResponse<ClientEntitySet> response = sendRequest(ES_ALL_PRIM, "PropertyDuration sub duration'PT2S' eq duration'PT4S'"); assertEquals(1, response.getBody().getEntities().size()); final ClientEntity clientEntity = response.getBody().getEntities().get(0); assertShortOrInt( 32767, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); }
@Test public void indexOf() { ODataRetrieveResponse<ClientEntitySet> result = sendRequest(ES_ALL_PRIM, "indexof(PropertyString,'positive') eq 17"); assertEquals(1, result.getBody().getEntities().size()); ClientEntity clientEntity = result.getBody().getEntities().get(0); assertShortOrInt( 32767, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); }
@Test public void badRequestInChangeSet() { /* * A bad request (status code >= 400) without "continue on error prefer header" in a changeset * should return a single response with Content-Type: application/http * * See: * OData Version 4.0 Part 1: Protocol Plus Errata 01 * 11.7.4 Responding to a Batch Request * * When a request within a change set fails, the change set response is not represented using * the multipart/mixed media type. Instead, a single response, using the application/http media type * and a Content-Transfer-Encoding header with a value of binary, is returned that applies to all requests * in the change set and MUST be formatted according to the Error Handling defined * for the particular response format. */ // Try to create entity, with invalid type ClientObjectFactory factory = getFactory(); final ClientEntity entity = factory.newEntity(ES_NOT_AVAILABLE); entity .getProperties() .add( factory.newPrimitiveProperty( PROPERTY_STRING, factory.newPrimitiveValueBuilder().buildString("1"))); final ODataBatchRequest batchRequest = getClient().getBatchRequestFactory().getBatchRequest(SERVICE_URI); final BatchManager payloadManager = batchRequest.payloadManager(); final ODataChangeset changeset = payloadManager.addChangeset(); final URI targetURI = getClient() .newURIBuilder(SERVICE_URI) .appendEntitySetSegment(ES_NOT_AVAILABLE_NAME) .build(); final ODataEntityCreateRequest<ClientEntity> createRequest = getClient().getCUDRequestFactory().getEntityCreateRequest(targetURI, entity); changeset.addRequest(createRequest); final ODataBatchResponse response = payloadManager.getResponse(); assertEquals(HttpStatusCode.ACCEPTED.getStatusCode(), response.getStatusCode()); // Check response items final Iterator<ODataBatchResponseItem> responseBodyIter = response.getBody(); assertTrue(responseBodyIter.hasNext()); final ODataBatchResponseItem changeSetResponse = responseBodyIter.next(); assertTrue(changeSetResponse.isChangeset()); assertTrue(changeSetResponse.hasNext()); final ODataResponse updateResponse = changeSetResponse.next(); assertTrue(changeSetResponse.isBreaking()); assertEquals(HttpStatusCode.NOT_FOUND.getStatusCode(), updateResponse.getStatusCode()); assertContentType(updateResponse.getContentType()); }
@Test public void round() { ODataRetrieveResponse<ClientEntitySet> result = sendRequest(ES_TWO_KEY_NAV, "PropertyInt16 eq round(2.5)"); assertEquals(1, result.getBody().getEntities().size()); ClientEntity clientEntity = result.getBody().getEntities().get(0); assertShortOrInt(3, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); assertEquals("1", clientEntity.getProperty("PropertyString").getPrimitiveValue().toValue()); result = sendRequest(ES_TWO_KEY_NAV, "PropertyInt16 eq round(2.4)"); assertEquals(1, result.getBody().getEntities().size()); clientEntity = result.getBody().getEntities().get(0); assertShortOrInt(2, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); assertEquals("1", clientEntity.getProperty("PropertyString").getPrimitiveValue().toValue()); result = sendRequest(ES_TWO_KEY_NAV, "PropertyInt16 eq round(2.6)"); assertEquals(1, result.getBody().getEntities().size()); clientEntity = result.getBody().getEntities().get(0); assertShortOrInt(3, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); assertEquals("1", clientEntity.getProperty("PropertyString").getPrimitiveValue().toValue()); result = sendRequest(ES_TWO_KEY_NAV, "PropertyInt16 eq round(3.1)"); assertEquals(1, result.getBody().getEntities().size()); clientEntity = result.getBody().getEntities().get(0); assertShortOrInt(3, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); assertEquals("1", clientEntity.getProperty("PropertyString").getPrimitiveValue().toValue()); }
@Test public void int64Literal() { long value = Integer.MAX_VALUE + 1L; ODataRetrieveResponse<ClientEntitySet> result = sendRequest(ES_ALL_PRIM, "PropertyInt64 gt " + value); assertEquals(1, result.getBody().getEntities().size()); ClientEntity clientEntity = result.getBody().getEntities().get(0); assertShortOrInt( 32767, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); }
@Test public void doubleLiteral() { Double value = -17900000000000000000.0; ODataRetrieveResponse<ClientEntitySet> result = sendRequest(ES_ALL_PRIM, "PropertyDouble le " + value.toString()); assertEquals(1, result.getBody().getEntities().size()); ClientEntity clientEntity = result.getBody().getEntities().get(0); assertShortOrInt( 32767, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); }
@Test public void substringStartAndEndGiven() { ODataRetrieveResponse<ClientEntitySet> result = sendRequest( ES_ALL_PRIM, "substring(PropertyString,length('First') add 1,8) eq ('Resource')"); assertEquals(1, result.getBody().getEntities().size()); ClientEntity clientEntity = result.getBody().getEntities().get(0); assertShortOrInt( 32767, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); }
@Test public void notOperator() { ODataRetrieveResponse<ClientEntitySet> result = sendRequest(ES_TWO_KEY_NAV, "not (PropertyInt16 eq 1)"); assertEquals(2, result.getBody().getEntities().size()); ClientEntity clientEntity = result.getBody().getEntities().get(0); assertShortOrInt(2, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); assertEquals("1", clientEntity.getProperty("PropertyString").getPrimitiveValue().toValue()); clientEntity = result.getBody().getEntities().get(1); assertShortOrInt(3, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); assertEquals("1", clientEntity.getProperty("PropertyString").getPrimitiveValue().toValue()); }
@Test public void unaryMinusOperatorDecimal() { ODataRetrieveResponse<ClientEntitySet> result = sendRequest(ES_TWO_KEY_NAV, "PropertyInt16 gt -2.0 add --3.0"); assertEquals(2, result.getBody().getEntities().size()); ClientEntity clientEntity = result.getBody().getEntities().get(0); assertShortOrInt(2, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); assertEquals("1", clientEntity.getProperty("PropertyString").getPrimitiveValue().toValue()); clientEntity = result.getBody().getEntities().get(1); assertShortOrInt(3, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); assertEquals("1", clientEntity.getProperty("PropertyString").getPrimitiveValue().toValue()); }
@Test public void binaryOperationIntegerDecimalWithPromotion() { String filterString = "" + "PropertyInt16 mod 2 eq " // Choose mod 2 == 1 => { 1, 3, .. } + "(((5 sub 1) div 5) " // Integer Division 4 / 5 == 0 + "add 1) " // 0 + 1 = 1 + "and " + "PropertyComp/PropertyInt16 eq " // Complex Property + "5.5 mul 2"; // Single * Int16 => Single => Int16 eq Single => Single eq Single ODataRetrieveResponse<ClientEntitySet> result = sendRequest(ES_TWO_KEY_NAV, filterString); assertEquals(3, result.getBody().getEntities().size()); ClientEntity clientEntity = result.getBody().getEntities().get(0); assertShortOrInt(1, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); assertEquals("1", clientEntity.getProperty("PropertyString").getPrimitiveValue().toValue()); assertShortOrInt( 11, clientEntity .getProperty("PropertyComp") .getComplexValue() .get("PropertyInt16") .getPrimitiveValue() .toValue()); clientEntity = result.getBody().getEntities().get(1); assertShortOrInt(1, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); assertEquals("2", clientEntity.getProperty("PropertyString").getPrimitiveValue().toValue()); assertShortOrInt( 11, clientEntity .getProperty("PropertyComp") .getComplexValue() .get("PropertyInt16") .getPrimitiveValue() .toValue()); clientEntity = result.getBody().getEntities().get(2); assertShortOrInt(3, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); assertEquals("1", clientEntity.getProperty("PropertyString").getPrimitiveValue().toValue()); assertShortOrInt( 11, clientEntity .getProperty("PropertyComp") .getComplexValue() .get("PropertyInt16") .getPrimitiveValue() .toValue()); }
@Test public void endsWith() { ODataRetrieveResponse<ClientEntitySet> result = sendRequest(ES_ALL_PRIM, "endswith(PropertyString,'values')"); assertEquals(2, result.getBody().getEntities().size()); ClientEntity clientEntity = result.getBody().getEntities().get(0); assertShortOrInt( 32767, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); clientEntity = result.getBody().getEntities().get(1); assertShortOrInt( -32768, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); }
@Test public void fractionalsecondsDateTimeOffset() { ODataRetrieveResponse<ClientEntitySet> response = sendRequest( ES_COMP_ALL_PRIM, "fractionalseconds(PropertyComp/PropertyDateTimeOffset) eq 0.1234567"); assertEquals(2, response.getBody().getEntities().size()); ClientEntity clientEntity = response.getBody().getEntities().get(0); assertShortOrInt( 32767, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); assertEquals("W/\"32767\"", clientEntity.getETag()); clientEntity = response.getBody().getEntities().get(1); assertShortOrInt(0, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); assertEquals("W/\"0\"", clientEntity.getETag()); }
@Test public void booleanLiteral() { ODataRetrieveResponse<ClientEntitySet> response = sendRequest(ES_ALL_PRIM, "PropertyBoolean eq false"); assertEquals(2, response.getBody().getEntities().size()); ClientEntity clientEntity = response.getBody().getEntities().get(0); assertShortOrInt( -32768, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); clientEntity = response.getBody().getEntities().get(1); assertShortOrInt(0, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); response = sendRequest(ES_ALL_PRIM, "PropertyBoolean eq true"); assertEquals(1, response.getBody().getEntities().size()); clientEntity = response.getBody().getEntities().get(0); assertShortOrInt( 32767, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); }
@Test public void numericBinaryOperationWithNullValues() { // Create new Entries final String filterString = "PropertyString eq null"; ClientEntity entity = getFactory().newEntity(new FullQualifiedName(SERVICE_NAMESPACE, "ETAllPrim")); entity .getProperties() .add( getFactory() .newPrimitiveProperty( "PropertyInt16", getFactory().newPrimitiveValueBuilder().buildInt16((short) 1))); entity.addLink( getFactory() .newEntityNavigationLink( "NavPropertyETTwoPrimOne", getClient() .newURIBuilder(SERVICE_URI) .appendEntitySetSegment("ESTwoPrim") .appendKeySegment(32766) .build())); final URI uri = getClient().newURIBuilder(SERVICE_URI).appendEntitySetSegment(ES_ALL_PRIM).build(); ODataEntityCreateResponse<ClientEntity> createResponse = getClient().getCUDRequestFactory().getEntityCreateRequest(uri, entity).execute(); ODataRetrieveResponse<ClientEntitySet> filterResponse = sendRequest( ES_ALL_PRIM, filterString, createResponse.getHeader(HttpHeader.SET_COOKIE).iterator().next()); assertEquals(1, filterResponse.getBody().getEntities().size()); }
@Test public void substringStartGiven() { ODataRetrieveResponse<ClientEntitySet> result = sendRequest( ES_TWO_KEY_NAV, "substring(PropertyComp/PropertyComp/PropertyString,6) eq 'Value'"); assertEquals(4, result.getBody().getEntities().size()); ClientEntity clientEntity = result.getBody().getEntities().get(0); assertShortOrInt(1, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); assertEquals("1", clientEntity.getProperty("PropertyString").getPrimitiveValue().toValue()); clientEntity = result.getBody().getEntities().get(1); assertShortOrInt(1, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); assertEquals("2", clientEntity.getProperty("PropertyString").getPrimitiveValue().toValue()); clientEntity = result.getBody().getEntities().get(2); assertShortOrInt(2, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); assertEquals("1", clientEntity.getProperty("PropertyString").getPrimitiveValue().toValue()); clientEntity = result.getBody().getEntities().get(3); assertShortOrInt(3, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); assertEquals("1", clientEntity.getProperty("PropertyString").getPrimitiveValue().toValue()); }
@Test public void booleanOperator() { ODataRetrieveResponse<ClientEntitySet> result = sendRequest(ES_TWO_KEY_NAV, "PropertyString eq '2' and PropertyInt16 eq 1"); assertEquals(1, result.getBody().getEntities().size()); ClientEntity clientEntity = result.getBody().getEntities().get(0); assertShortOrInt(1, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); assertEquals("2", clientEntity.getProperty("PropertyString").getPrimitiveValue().toValue()); result = sendRequest(ES_TWO_KEY_NAV, "PropertyString eq '2' or PropertyInt16 eq 1"); assertEquals(2, result.getBody().getEntities().size()); clientEntity = result.getBody().getEntities().get(0); assertShortOrInt(1, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); assertEquals("1", clientEntity.getProperty("PropertyString").getPrimitiveValue().toValue()); clientEntity = result.getBody().getEntities().get(1); assertShortOrInt(1, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue()); assertEquals("2", clientEntity.getProperty("PropertyString").getPrimitiveValue().toValue()); result = sendRequest( ES_MIX_ENUM_DEF_COLL_COMP, "PropertyEnumString eq Namespace1_Alias.ENString'String2'"); assertTrue(result.getBody().getEntities().isEmpty()); }