org.apache.olingo.client.api.ODataClient Java Examples

The following examples show how to use org.apache.olingo.client.api.ODataClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ODataUpdateTests.java    From syndesis with Apache License 2.0 6 votes vote down vote up
private static String queryProperty(String serviceURI, String resourcePath, String keyPredicate, String property) {
    ClientEntity olEntity = null;
    ODataRetrieveResponse<ClientEntity> response = null;
    try {
        URI httpURI = URI.create(serviceURI + FORWARD_SLASH +
                                        resourcePath + OPEN_BRACKET +
                                        QUOTE_MARK + keyPredicate + QUOTE_MARK +
                                        CLOSE_BRACKET);
        ODataClient client = ODataClientFactory.getClient();
        response = client.getRetrieveRequestFactory().getEntityRequest(httpURI).execute();
        assertEquals(HttpStatus.SC_OK, response.getStatusCode());

        olEntity = response.getBody();
        assertNotNull(olEntity);
        return olEntity.getProperty(property).getPrimitiveValue().toString();
    } finally {
        if (response != null) {
            response.close();
        }
    }
}
 
Example #2
Source File: ExpandWithSystemQueryOptionsITCase.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void singleEntityWithExpand() {
  /* A single entity request will be dispatched to a different processor method than entity set request */
  final ODataClient client = getEdmEnabledClient();
  Map<String, Object> keys = new HashMap<String, Object>();
  keys.put("PropertyInt16", 1);
  keys.put("PropertyString", "1");

  final URI uri = client.newURIBuilder(SERVICE_URI).appendEntitySetSegment(ES_TWO_KEY_NAV).appendKeySegment(keys)
      .expandWithOptions(NAV_PROPERTY_ET_KEY_NAV_MANY,
          Collections.singletonMap(QueryOption.FILTER, (Object) "PropertyInt16 lt 2"))
      .build();
  final ODataRetrieveResponse<ClientEntity> response =
          client.getRetrieveRequestFactory().getEntityRequest(uri).execute();
  assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode());

  final ClientEntitySet entitySet =
      response.getBody().getNavigationLink(NAV_PROPERTY_ET_KEY_NAV_MANY).asInlineEntitySet().getEntitySet();
  assertEquals(1, entitySet.getEntities().size());
  assertShortOrInt(1, entitySet.getEntities().get(0).getProperty(PROPERTY_INT16).getPrimitiveValue().toValue());
}
 
Example #3
Source File: ErrorTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void test1OLINGO1102() throws Exception {
  ODataClient odataClient = ODataClientFactory.getClient();
  InputStream entity = getClass().getResourceAsStream("500error." + getSuffix(ContentType.JSON));
  StatusLine statusLine = mock(StatusLine.class);
  when(statusLine.getStatusCode()).thenReturn(500);
  when(statusLine.toString()).thenReturn("Internal Server Error");
  
  ODataClientErrorException exp = (ODataClientErrorException) ODataErrorResponseChecker.
      checkResponse(odataClient, statusLine, entity, "Json");
  assertTrue(exp.getMessage().contains("(500) Internal Server Error"));
  ODataError error = exp.getODataError();
  assertTrue(error.getMessage().startsWith("Internal Server Error"));
  assertEquals(500, Integer.parseInt(error.getCode()));
  assertEquals(2, error.getInnerError().size());
  assertEquals("\"Method does not support entities of specific type\"", error.getInnerError().get("message"));
  assertEquals("\"FaultException\"", error.getInnerError().get("type"));
  assertNull(error.getDetails());
      
}
 
Example #4
Source File: ODataClientTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void searchTest() {
  ODataClient client = ODataClientFactory.getClient();
  assertNotNull(client);
  SearchFactory searchFactory = client.getSearchFactory();
  assertNotNull(searchFactory);
  LiteralSearch literal = (LiteralSearch) searchFactory.literal("test");
  assertNotNull(literal);
  assertEquals("test", literal.build());
  AndSearch and = (AndSearch) searchFactory.and(literal, literal);
  assertNotNull(and);
  assertEquals("(test AND test)", and.build());
  OrSearch or = (OrSearch) searchFactory.or(literal, literal);
  assertNotNull(or);
  assertEquals("(test OR test)", or.build());
  NotSearch not = (NotSearch) searchFactory.not(literal);
  assertNotNull(not);
  assertEquals("NOT (test)", not.build());
}
 
Example #5
Source File: ConformanceITCase.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
/**
 * 9. MAY request entity references in place of entities previously returned in the response (section 11.2.7).
 */
@Test
public void entityNavigationReference() {
  final ODataClient client = getClient();
  final URIBuilder uriBuilder = client.newURIBuilder(SERVICE_URI)
          .appendEntitySetSegment(ES_TWO_PRIM)
          .appendKeySegment(32767)
          .appendNavigationSegment("NavPropertyETAllPrimOne")
          .appendRefSegment();

  ODataEntityRequest<ClientEntity> req = client.getRetrieveRequestFactory().getEntityRequest(uriBuilder.build());

  ODataRetrieveResponse<ClientEntity> res = req.execute();
  assertNotNull(res);

  final ClientEntity entity = res.getBody();
  assertNotNull(entity);
  assertTrue(entity.getId().toASCIIString().endsWith("ESAllPrim(32767)"));
}
 
Example #6
Source File: OAuth2TestITCase.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private void read(final ODataClient client, final ContentType contentType) {
  final URIBuilder uriBuilder =
      client.newURIBuilder(testOAuth2ServiceRootURL).appendEntitySetSegment("Orders").appendKeySegment(8);

  final ODataEntityRequest<ClientEntity> req =
      client.getRetrieveRequestFactory().getEntityRequest(uriBuilder.build());
  req.setFormat(contentType);

  final ODataRetrieveResponse<ClientEntity> res = req.execute();
  assertEquals(200, res.getStatusCode());

  final String etag = res.getETag();
  assertTrue(StringUtils.isNotBlank(etag));

  final ClientEntity order = res.getBody();
  assertEquals(etag, order.getETag());
  assertEquals("Microsoft.Test.OData.Services.ODataWCFService.Order", order.getTypeName().toString());
  assertEquals("Edm.Int32", order.getProperty("OrderID").getPrimitiveValue().getTypeName());
  assertEquals("Edm.DateTimeOffset", order.getProperty("OrderDate").getPrimitiveValue().getTypeName());
  assertEquals("Edm.Duration", order.getProperty("ShelfLife").getPrimitiveValue().getTypeName());
  assertEquals("Collection(Edm.Duration)", order.getProperty("OrderShelfLifes").getCollectionValue().getTypeName());
}
 
Example #7
Source File: ODataTestServer.java    From syndesis with Apache License 2.0 6 votes vote down vote up
public ClientEntity getData(String keyPredicate) {
    String resourcePath = resourcePath() + OPEN_BRACKET + keyPredicate + CLOSE_BRACKET;

    String serviceUri = serviceSSLUri();
    if (serviceUri == null) {
        serviceUri = servicePlainUri();
    }

    ODataClient client = ODataClientFactory.getClient();
    URI customersUri = client.newURIBuilder(serviceUri)
                                                  .appendEntitySetSegment(resourcePath).build();

    ODataRetrieveResponse<ClientEntity> response
        = client.getRetrieveRequestFactory().getEntityRequest(customersUri).execute();

    return response.getBody();
}
 
Example #8
Source File: AbstractRequest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
protected void checkResponse(
        final ODataClient odataClient, final HttpResponse response, final String accept) {

  if (response.getStatusLine().getStatusCode() >= 400) {
    Header contentTypeHeader = response.getEntity() != null ? response.getEntity().getContentType() : null;
    try {
      final ODataRuntimeException exception = ODataErrorResponseChecker.checkResponse(
              odataClient,
              response.getStatusLine(),
              response.getEntity() == null ? null : response.getEntity().getContent(),
                  (contentTypeHeader != null && 
                  contentTypeHeader.getValue().contains(TEXT_CONTENT_TYPE)) ? TEXT_CONTENT_TYPE : accept);
      if (exception != null) {
        if (exception instanceof ODataClientErrorException) {
          ((ODataClientErrorException)exception).setHeaderInfo(response.getAllHeaders());
        }
        throw exception;
      }
    } catch (IOException e) {
      throw new ODataRuntimeException(
              "Received '" + response.getStatusLine() + "' but could not extract error body", e);
    }
  }
}
 
Example #9
Source File: PropertyTestITCase.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private void geospatial(final ODataClient client, final ContentType contentType) {
  final URIBuilder uriBuilder = client.newURIBuilder(testStaticServiceRootURL).
      appendEntitySetSegment("People").appendKeySegment(5).appendPropertySegment("Home");
  final ODataPropertyRequest<ClientProperty> req = client.getRetrieveRequestFactory().
      getPropertyRequest(uriBuilder.build());
  req.setFormat(contentType);

  final ClientProperty prop = req.execute().getBody();
  assertNotNull(prop);
  // cast to workaround JDK 6 bug, fixed in JDK 7
  assertEquals("Edm.GeographyPoint", ((ClientValuable) prop).getValue().getTypeName());
}
 
Example #10
Source File: AbstractRequest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
protected void checkRequest(final ODataClient odataClient, final HttpUriRequest request) {
  // If using and Edm enabled client, checks that the cached service root matches the request URI
  if (odataClient instanceof EdmEnabledODataClient
          && !request.getURI().toASCIIString().startsWith(
                  ((EdmEnabledODataClient) odataClient).getServiceRoot())) {

    throw new IllegalArgumentException(
            String.format("The current request URI %s does not match the configured service root %s",
                    request.getURI().toASCIIString(),
                    ((EdmEnabledODataClient) odataClient).getServiceRoot()));
  }
}
 
Example #11
Source File: URIBuilderTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void testDuplicateCustomQueryOption() throws ODataDeserializerException {
  final ODataClient client = ODataClientFactory.getClient();
  final URI uri = client.newURIBuilder(SERVICE_ROOT).appendEntitySetSegment("EntitySet").
      addCustomQueryOption("x", "z").
      addCustomQueryOption("x", "y").build();
  assertEquals("http://host/service/EntitySet?x=y", uri.toASCIIString());
}
 
Example #12
Source File: PropertyTestITCase.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private void _enum(final ODataClient client, final ContentType contentType) {
  final URIBuilder uriBuilder = client.newURIBuilder(testStaticServiceRootURL).
      appendEntitySetSegment("Products").appendKeySegment(5).appendPropertySegment("CoverColors");
  final ODataPropertyRequest<ClientProperty> req = client.getRetrieveRequestFactory().
      getPropertyRequest(uriBuilder.build());
  req.setFormat(contentType);

  final ClientProperty prop = req.execute().getBody();
  assertNotNull(prop);
  // cast to workaround JDK 6 bug, fixed in JDK 7
  assertEquals("Collection(Microsoft.Test.OData.Services.ODataWCFService.Color)",
      ((ClientValuable) prop).getValue().getTypeName());
}
 
Example #13
Source File: URIBuilderTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void test2OLINGO753() throws ODataDeserializerException {
  final ODataClient client = ODataClientFactory.getClient();
  final URI uri = client.newURIBuilder(SERVICE_ROOT).appendOperationCallSegment("functionName").
      filter("paramName eq 1").format("json").count().build();
  final Map<String, ClientValue> parameters = new HashMap<String, ClientValue>();
  final ClientPrimitiveValue value = client.getObjectFactory().
      newPrimitiveValueBuilder().buildString("parameterValue");
  parameters.put("parameterName", value);
  URI newUri = URIUtils.buildFunctionInvokeURI(uri, parameters);
  assertNotNull(newUri);
  assertEquals("http://host/service/functionName(parameterName%3D'parameterValue')"
      + "/%24count?%24filter=paramName%20eq%201&%24format=json", newUri.toASCIIString());
}
 
Example #14
Source File: EntityReferencesITCase.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void createMissingNavigationProperty() throws Exception {
  final ODataClient client = getClient();
  final URI uri = client.newURIBuilder(SERVICE_URI).appendEntitySetSegment(ES_KEY_NAV).appendRefSegment().build();
  final URI ref = client.newURIBuilder(SERVICE_URI).appendEntitySetSegment(ES_KEY_NAV).appendKeySegment(1).build();
  
  try {
    client.getCUDRequestFactory().getReferenceAddingRequest(new URI(SERVICE_URI), uri, ref).execute();
  } catch (ODataClientErrorException e) {
    assertEquals(HttpStatusCode.BAD_REQUEST.getStatusCode(), e.getStatusLine().getStatusCode());
  }
}
 
Example #15
Source File: AbstractODataStreamedRequest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor.
 *
 * @param odataClient client instance getting this request
 * @param method OData request HTTP method.
 * @param uri OData request URI.
 */
public AbstractODataStreamedRequest(final ODataClient odataClient,
        final HttpMethod method, final URI uri) {

  super(odataClient, method, uri);
  setAccept(ContentType.APPLICATION_OCTET_STREAM.toContentTypeString());
  setContentType(ContentType.APPLICATION_OCTET_STREAM.toContentTypeString());
}
 
Example #16
Source File: ExpandWithSystemQueryOptionsITCase.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void count() throws Exception{
  final ODataClient client = getEdmEnabledClient();
  Map<QueryOption, Object> options = new EnumMap<QueryOption, Object>(QueryOption.class);
  options.put(QueryOption.SELECT, "PropertyInt16");
  options.put(QueryOption.COUNT, true);

  final URI uri =
      client.newURIBuilder(SERVICE_URI).appendEntitySetSegment(ES_TWO_KEY_NAV).expandWithOptions(
          NAV_PROPERTY_ET_TWO_KEY_NAV_MANY, options).addQueryOption(QueryOption.SELECT,
              "PropertyInt16,PropertyString").build();
     
  final ODataRetrieveResponse<ClientEntitySet> response =
      client.getRetrieveRequestFactory().getEntitySetRequest(uri).execute();

  final List<ClientEntity> entities = response.getBody().getEntities();
  assertEquals(4, entities.size());

  for (final ClientEntity entity : entities) {
    final Object propInt16 = entity.getProperty(PROPERTY_INT16).getPrimitiveValue().toValue();
    final Object propString = entity.getProperty(PROPERTY_STRING).getPrimitiveValue().toValue();
    final ClientEntitySet entitySet =
        entity.getNavigationLink(NAV_PROPERTY_ET_TWO_KEY_NAV_MANY).asInlineEntitySet().getEntitySet();

    if ((propInt16.equals(1) ||propInt16.equals((short)1)) && propString.equals("1")) {
      assertEquals(Integer.valueOf(2), entitySet.getCount());
    } else if ((propInt16.equals(1) ||propInt16.equals((short)1)) && propString.equals("2")) {
      assertEquals(Integer.valueOf(1), entitySet.getCount());
    } else if ((propInt16.equals(2) ||propInt16.equals((short)2)) && propString.equals("1")) {
      assertEquals(Integer.valueOf(1), entitySet.getCount());
    } else if ((propInt16.equals(3) ||propInt16.equals((short)3)) && propString.equals("1")) {
      assertEquals(Integer.valueOf(0), entitySet.getCount());
    } else {
      fail();
    }
  }
}
 
Example #17
Source File: ExpandWithSystemQueryOptionsITCase.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void countOnly() throws Exception {
  final ODataClient client = getEdmEnabledClient();
  Map<QueryOption, Object> options = new EnumMap<QueryOption, Object>(QueryOption.class);

  final URI uri =
      client.newURIBuilder(SERVICE_URI).appendEntitySetSegment(ES_TWO_KEY_NAV).expandWithOptions(
          NAV_PROPERTY_ET_TWO_KEY_NAV_MANY, false, true, options).addQueryOption(QueryOption.SELECT,
              "PropertyInt16,PropertyString").build();
  final ODataRetrieveResponse<ClientEntitySet> response =
      client.getRetrieveRequestFactory().getEntitySetRequest(uri).execute();

  final List<ClientEntity> entities = response.getBody().getEntities();
  assertEquals(4, entities.size());

  for (final ClientEntity entity : entities) {
    final Object propInt16 = entity.getProperty(PROPERTY_INT16).getPrimitiveValue().toValue();
    final Object propString = entity.getProperty(PROPERTY_STRING).getPrimitiveValue().toValue();
    final ClientEntitySet entitySet =
        entity.getNavigationLink(NAV_PROPERTY_ET_TWO_KEY_NAV_MANY).asInlineEntitySet().getEntitySet();

    if ((propInt16.equals(1) ||propInt16.equals((short)1)) && propString.equals("1")) {
      assertEquals(Integer.valueOf(2), entitySet.getCount());
    } else if ((propInt16.equals(1) ||propInt16.equals((short)1)) && propString.equals("2")) {
      assertEquals(Integer.valueOf(1), entitySet.getCount());
    } else if ((propInt16.equals(2) ||propInt16.equals((short)2)) && propString.equals("1")) {
      assertEquals(Integer.valueOf(1), entitySet.getCount());
    } else if ((propInt16.equals(3) ||propInt16.equals((short)3)) && propString.equals("1")) {
      assertEquals(Integer.valueOf(0), entitySet.getCount());
    } else {
      fail();
    }
  }
}
 
Example #18
Source File: AbstractODataResponse.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
public AbstractODataResponse(
    final ODataClient odataClient, final HttpClient httpclient, final HttpResponse res) {

  this.odataClient = odataClient;
  this.httpClient = httpclient;
  this.res = res;
  if (res != null) {
    initFromHttpResponse(res);
  }
}
 
Example #19
Source File: JsonSerializerTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void testClientEntityJSONWithNull() throws ODataSerializerException {
  String expectedJson = "{\"@odata.type\":\"#test.testClientEntity\","
      + "\"[email protected]\":\"Int32\","
      + "\"testInt32\":12,"
      + "\"[email protected]\":\"Int32\""
      + ",\"testInt32Null\":null,"
      + "\"[email protected]\":\"String\","
      + "\"testString\":\"testString\","
      + "\"[email protected]\":\"String\","
      + "\"testStringNull\":null}";

  ODataClient odataClient = ODataClientFactory.getClient();
  ClientObjectFactory objFactory = odataClient.getObjectFactory();
  ClientEntity clientEntity = objFactory.newEntity(new FullQualifiedName("test", "testClientEntity"));

  clientEntity.getProperties().add(
      objFactory.newPrimitiveProperty(
          "testInt32",
          objFactory.newPrimitiveValueBuilder().buildInt32(12)));
  clientEntity.getProperties().add(
      objFactory.newPrimitiveProperty(
          "testInt32Null",
          objFactory.newPrimitiveValueBuilder().buildInt32(null)));
  clientEntity.getProperties().add(
      objFactory.newPrimitiveProperty(
          "testString",
          objFactory.newPrimitiveValueBuilder().buildString("testString")));
  clientEntity.getProperties().add(
      objFactory.newPrimitiveProperty(
          "testStringNull",
          objFactory.newPrimitiveValueBuilder().buildString(null)));

  JsonSerializer jsonSerializer = new JsonSerializer(false, ContentType.JSON_FULL_METADATA);

  StringWriter writer = new StringWriter();
  jsonSerializer.write(writer, odataClient.getBinder().getEntity(clientEntity));
  assertThat(writer.toString(), is(expectedJson));
}
 
Example #20
Source File: ConformanceITCase.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
/**
 * 2. MUST specify OData-Version (section 8.1.5) and Content-Type (section 8.1.1) in any request with a payload.
 */
@Test
public void checkClientWithPayloadHeader() {
  assumeTrue("json conformance test with content type", isJson());

  ClientEntity newEntity = getFactory().newEntity(ET_ALL_PRIM);
  newEntity.getProperties().add(getFactory().newPrimitiveProperty(PROPERTY_INT64,
      getFactory().newPrimitiveValueBuilder().buildInt64((long) 42)));
  final ODataClient client = getClient();
  newEntity.addLink(getFactory().newEntityNavigationLink(NAV_PROPERTY_ET_TWO_PRIM_ONE,
      client.newURIBuilder(SERVICE_URI)
          .appendEntitySetSegment(ES_TWO_PRIM)
          .appendKeySegment(32766)
          .build()));

  final ODataEntityCreateRequest<ClientEntity> createRequest = client.getCUDRequestFactory().getEntityCreateRequest(
      client.newURIBuilder(SERVICE_URI).appendEntitySetSegment(ES_ALL_PRIM).build(),
      newEntity);
  assertNotNull(createRequest);
  createRequest.setFormat(contentType);
  // check for OData-Version
  assertEquals(ODATA_MAX_VERSION_NUMBER, createRequest.getHeader(HttpHeader.ODATA_VERSION));

  // check for Content-Type
  assertEquals(
      ContentType.APPLICATION_JSON.toContentTypeString(),
      createRequest.getHeader(HttpHeader.CONTENT_TYPE));
  assertEquals(
      ContentType.APPLICATION_JSON.toContentTypeString(),
      createRequest.getContentType());

  final ODataEntityCreateResponse<ClientEntity> createResponse = createRequest.execute();

  assertEquals(HttpStatusCode.CREATED.getStatusCode(), createResponse.getStatusCode());
}
 
Example #21
Source File: URIBuilderTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void test6OLINGO753() throws ODataDeserializerException {
  final ODataClient client = ODataClientFactory.getClient();
  final URI uri = client.newURIBuilder(SERVICE_ROOT).
      appendOperationCallSegment("functionName").count().filter("PropertyString eq '1'").build();
  final Map<String, ClientValue> parameters = new HashMap<String, ClientValue>();
  URI newUri = URIUtils.buildFunctionInvokeURI(uri, parameters);
  assertNotNull(newUri);
  assertEquals("http://host/service/functionName()"
      + "/%24count?%24filter=PropertyString%20eq%20'1'", newUri.toASCIIString());
}
 
Example #22
Source File: ConformanceITCase.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
/**
 * 7. MUST generate PATCH requests for updates, if the client supports updates (section 11.4.3).
 **/
@Test
public void patchEntityRequestForUpdates() {
  final ODataClient client = getClient();
  final ClientEntity patch = client.getObjectFactory().newEntity(ET_TWO_PRIM);     
  final URI uri = client.newURIBuilder(SERVICE_URI)
          .appendEntitySetSegment(ES_TWO_PRIM)
          .appendKeySegment(32766)
          .build();
  patch.setEditLink(uri);
  final String newString = "Seconds: (" + System.currentTimeMillis() + ")";
  patch.getProperties().add(client.getObjectFactory().newPrimitiveProperty("PropertyString",
      client.getObjectFactory().newPrimitiveValueBuilder().buildString(newString)));

  final ODataEntityUpdateRequest<ClientEntity> req =            
      client.getCUDRequestFactory().getEntityUpdateRequest(UpdateType.PATCH, patch);
  assertNotNull(req);
  assertEquals(ODATA_MAX_VERSION_NUMBER, req.getHeader(HttpHeader.ODATA_MAX_VERSION));
  
  final ODataEntityUpdateResponse<ClientEntity> res = req.execute();
  assertNotNull(res);
  assertEquals(200, res.getStatusCode());   
          
  final ClientEntity entity = res.getBody();
  assertNotNull(entity);
  assertEquals(newString, entity.getProperty("PropertyString").getPrimitiveValue().toString());
}
 
Example #23
Source File: XMLMetadataRequestImpl.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private XMLMetadataResponseImpl(final ODataClient odataClient, final HttpClient httpClient,
    final HttpResponse res, final XMLMetadata metadata) {

  super(odataClient, httpClient, null);
  initFromHttpResponse(res);
  this.metadata = metadata;
}
 
Example #24
Source File: URIBuilderTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void test8OLINGO753() throws ODataDeserializerException {
  final ODataClient client = ODataClientFactory.getClient();
  final URI uri = client.newURIBuilder(SERVICE_ROOT).appendEntitySetSegment("EntitySet").
      appendOperationCallSegment("functionName").filter("PropertyString eq '1'").
      appendNavigationSegment("NavSeg").appendActionCallSegment("ActionName").count().build();
  final Map<String, ClientValue> parameters = new HashMap<String, ClientValue>();
  final ClientPrimitiveValue value = client.getObjectFactory().
      newPrimitiveValueBuilder().buildString("parameterValue");
  parameters.put("parameterName", value);
  URI newUri = URIUtils.buildFunctionInvokeURI(uri, parameters);
  assertNotNull(newUri);
  assertEquals("http://host/service/EntitySet/functionName(parameterName%3D'parameterValue')/NavSeg/ActionName"
      + "/%24count?%24filter=PropertyString%20eq%20'1'", newUri.toASCIIString());
}
 
Example #25
Source File: URIBuilderTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomQueryOptionWithFilter() throws ODataDeserializerException {
  final ODataClient client = ODataClientFactory.getClient();
  final URI uri = client.newURIBuilder(SERVICE_ROOT).appendEntitySetSegment("EntitySet").
      filter("PropertyString eq '1'").
      addCustomQueryOption("x", "y").build();
  assertEquals("http://host/service/EntitySet?%24filter=PropertyString%20eq%20'1'&x=y", 
      uri.toASCIIString());
}
 
Example #26
Source File: BasicITCase.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void updateEntityWithComplex() throws Exception {
  ClientEntity newEntity = getFactory().newEntity(ET_KEY_NAV);
  newEntity.getProperties().add(getFactory().newComplexProperty("PropertyCompCompNav", null));
  // The following properties must not be null
  newEntity.getProperties().add(getFactory().newPrimitiveProperty(PROPERTY_STRING,
      getFactory().newPrimitiveValueBuilder().buildString("Test")));
  newEntity.getProperties().add(
      getFactory().newComplexProperty("PropertyCompTwoPrim",
          getFactory().newComplexValue(SERVICE_NAMESPACE+".CTTwoPrim")
              .add(getFactory().newPrimitiveProperty(
                  PROPERTY_INT16,
                  getFactory().newPrimitiveValueBuilder().buildInt16((short) 1)))
              .add(getFactory().newPrimitiveProperty(
                  PROPERTY_STRING,
                  getFactory().newPrimitiveValueBuilder().buildString("Test2")))));

  ODataClient client = getClient();
  final URI uri = client.newURIBuilder(SERVICE_URI).appendEntitySetSegment(ES_KEY_NAV).appendKeySegment(1).build();
  final ODataEntityUpdateRequest<ClientEntity> request = client.getCUDRequestFactory().getEntityUpdateRequest(
      uri, UpdateType.REPLACE, newEntity);
  final ODataEntityUpdateResponse<ClientEntity> response = request.execute();
  assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode());

  // Check that the complex-property hierarchy is still there and that all primitive values are now null.
  final ClientEntity entity = response.getBody();
  assertNotNull(entity);
  final ClientComplexValue complex = entity.getProperty("PropertyCompCompNav").getComplexValue()
      .get("PropertyCompNav").getComplexValue();
  assertNotNull(complex);
  final ClientProperty property = complex.get(PROPERTY_INT16);
  assertNotNull(property);
  assertNull(property.getPrimitiveValue().toValue());
}
 
Example #27
Source File: BasicITCase.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void createEntity() throws Exception {
  ClientEntity newEntity = getFactory().newEntity(ET_ALL_PRIM);
  newEntity.getProperties().add(getFactory().newPrimitiveProperty(PROPERTY_INT64,
      getFactory().newPrimitiveValueBuilder().buildInt64((long) 42)));
  final ODataClient client = getClient();
  newEntity.addLink(getFactory().newEntityNavigationLink(NAV_PROPERTY_ET_TWO_PRIM_ONE,
      client.newURIBuilder(SERVICE_URI)
          .appendEntitySetSegment(ES_TWO_PRIM)
          .appendKeySegment(32766)
          .build()));

  final ODataEntityCreateRequest<ClientEntity> createRequest = client.getCUDRequestFactory().getEntityCreateRequest(
      client.newURIBuilder(SERVICE_URI).appendEntitySetSegment(ES_ALL_PRIM).build(),
      newEntity);
  assertNotNull(createRequest);
  final ODataEntityCreateResponse<ClientEntity> createResponse = createRequest.execute();
  assertNotNull(createRequest.getHttpRequest());
  assertNotNull(((AbstractODataBasicRequest)createRequest).getPayload());
  assertEquals(HttpStatusCode.CREATED.getStatusCode(), createResponse.getStatusCode());
  assertEquals(SERVICE_URI + ES_ALL_PRIM + "(1)", createResponse.getHeader(HttpHeader.LOCATION).iterator().next());
  final ClientEntity createdEntity = createResponse.getBody();
  assertNotNull(createdEntity);
  final ClientProperty property1 = createdEntity.getProperty(PROPERTY_INT64);
  assertNotNull(property1);
  assertShortOrInt(42, property1.getPrimitiveValue().toValue());
  final ClientProperty property2 = createdEntity.getProperty(PROPERTY_DECIMAL);
  assertNotNull(property2);
  assertNull(property2.getPrimitiveValue().toValue());
}
 
Example #28
Source File: ErrorTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithNull() throws Exception {
  ODataClient odataClient = ODataClientFactory.getClient();
  StatusLine statusLine = mock(StatusLine.class);
  when(statusLine.getStatusCode()).thenReturn(500);
  when(statusLine.toString()).thenReturn("Internal Server Error");
      
  ODataRuntimeException exp = ODataErrorResponseChecker.
      checkResponse(odataClient, statusLine, null, "Json");
  assertTrue(exp.getMessage().startsWith("Internal Server Error"));
}
 
Example #29
Source File: AsyncSupportITCase.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void getBatchRequest() throws Exception {
  ODataClient client = getClient();
  final ODataBatchRequest request = client.getBatchRequestFactory().getBatchRequest(SERVICE_URI);
  request.setPrefer(PreferenceName.RESPOND_ASYNC + "; " + TEC_ASYNC_SLEEP + "=1");
  ODataBatchableRequest getRequest = appendGetRequest(client, "ESAllPrim", 32767, false);
  AsyncBatchRequestWrapper asyncRequest =
      client.getAsyncRequestFactory().getAsyncBatchRequestWrapper(request);
  asyncRequest.addRetrieve(getRequest);
  AsyncResponseWrapper<ODataBatchResponse> asyncResponse = asyncRequest.execute();
  assertTrue(asyncResponse.isPreferenceApplied());
  assertFalse(asyncResponse.isDone());

  waitTillDone(asyncResponse, 3);

  final ODataBatchResponse response = asyncResponse.getODataResponse();
  final ODataBatchResponseItem item = response.getBody().next();
  @SuppressWarnings("unchecked")
  final ODataRetrieveResponse<ClientEntity> firstResponse = (ODataRetrieveResponse<ClientEntity>) item.next();
  assertEquals(HttpStatusCode.OK.getStatusCode(), firstResponse.getStatusCode());
  assertEquals(3, firstResponse.getHeaderNames().size());
  assertEquals("4.0", firstResponse.getHeader(HttpHeader.ODATA_VERSION).iterator().next());

  final ClientEntity entity = firstResponse.getBody();
  assertShortOrInt(32767, entity.getProperty("PropertyInt16").getPrimitiveValue().toValue());
  assertEquals("First Resource - positive values",
      entity.getProperty("PropertyString").getPrimitiveValue().toValue());
}
 
Example #30
Source File: ODataMetaDataExtension.java    From syndesis with Apache License 2.0 5 votes vote down vote up
private static Edm requestEdm(Map<String, Object> parameters, String serviceUrl) {
    ODataClient client = ODataClientFactory.getClient();
    HttpClientFactory factory = ODataUtil.newHttpFactory(parameters);
    client.getConfiguration().setHttpClientFactory(factory);

    EdmMetadataRequest request = client.getRetrieveRequestFactory().getMetadataRequest(serviceUrl);
    ODataRetrieveResponse<Edm> response = request.execute();

    if (response.getStatusCode() != 200) {
        throw new IllegalStateException("Metatdata response failure. Return code: " + response.getStatusCode());
    }

    return response.getBody();
}