You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by sk...@apache.org on 2014/02/14 10:05:27 UTC

[07/50] [abbrv] git commit: [OLINGO-117] prepare for TDD

[OLINGO-117] prepare for TDD


Project: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/commit/255fcc05
Tree: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/tree/255fcc05
Diff: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/diff/255fcc05

Branch: refs/heads/ODataServlet
Commit: 255fcc05ad0f2ee0cb25175d420bc442fd9ffd0e
Parents: f9d2a01
Author: Stephan Klevenz <st...@klaeff.de>
Authored: Fri Jan 17 18:04:28 2014 +0100
Committer: Stephan Klevenz <st...@klaeff.de>
Committed: Fri Jan 17 18:04:28 2014 +0100

----------------------------------------------------------------------
 .../odata2/fit/client/ClientDeltaResponse.java  |  84 +++++++++++
 .../olingo/odata2/fit/client/util/Client.java   | 140 +++++++++++++++++++
 .../odata2/fit/client/util/HttpException.java   |  19 +++
 3 files changed, 243 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/255fcc05/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/client/ClientDeltaResponse.java
----------------------------------------------------------------------
diff --git a/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/client/ClientDeltaResponse.java b/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/client/ClientDeltaResponse.java
new file mode 100644
index 0000000..6c8ef5b
--- /dev/null
+++ b/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/client/ClientDeltaResponse.java
@@ -0,0 +1,84 @@
+package org.apache.olingo.odata2.fit.client;
+
+import static org.junit.Assert.*;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.*;
+
+import java.io.IOException;
+import java.net.Proxy;
+import java.util.List;
+
+import org.apache.olingo.odata2.api.ODataService;
+import org.apache.olingo.odata2.api.commons.HttpStatusCodes;
+import org.apache.olingo.odata2.api.edm.Edm;
+import org.apache.olingo.odata2.api.edm.EdmEntitySetInfo;
+import org.apache.olingo.odata2.api.edm.provider.EdmProvider;
+import org.apache.olingo.odata2.api.ep.feed.ODataFeed;
+import org.apache.olingo.odata2.api.exception.ODataException;
+import org.apache.olingo.odata2.api.processor.ODataResponse;
+import org.apache.olingo.odata2.api.processor.ODataSingleProcessor;
+import org.apache.olingo.odata2.api.processor.part.MetadataProcessor;
+import org.apache.olingo.odata2.api.uri.info.GetMetadataUriInfo;
+import org.apache.olingo.odata2.core.processor.ODataSingleProcessorService;
+import org.apache.olingo.odata2.fit.client.util.Client;
+import org.apache.olingo.odata2.fit.client.util.HttpException;
+import org.apache.olingo.odata2.ref.edm.ScenarioEdmProvider;
+import org.apache.olingo.odata2.testutil.fit.AbstractFitTest;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+
+public class ClientDeltaResponse extends AbstractFitTest {
+
+  private Client client;
+
+  @Before
+  @Override
+  public void before() {
+    super.before();
+    try {
+      client = new Client(getEndpoint().toASCIIString());
+    } catch (Exception e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  private class StubProcessor extends ODataSingleProcessor {
+    
+  }
+  
+  @Override
+  protected ODataService createService() throws ODataException {
+    EdmProvider provider = new ScenarioEdmProvider();
+    ODataSingleProcessor processor = new StubProcessor();
+    
+     return new ODataSingleProcessorService(provider, processor);
+  }
+
+  @Test
+  public void dummy() throws Exception {}
+
+  @Test
+  public void testEdm() throws Exception {
+    Edm edm = client.getEdm();
+    assertNotNull(edm);
+    assertNotNull(edm.getDefaultEntityContainer());
+
+    System.out.println(edm.getDefaultEntityContainer().getName());
+  }
+
+  @Test
+  public void testEntitySets() throws Exception {
+    List<EdmEntitySetInfo> sets = client.getEntitySets();
+    assertNotNull(sets);
+    assertEquals(6, sets.size());
+  }
+
+  @Test
+//  @Ignore
+  public void testEntity() throws Exception {
+    ODataFeed feed = client.readFeed("Container1", "Rooms", "application/atom+xml");
+    assertNotNull(feed);
+  }
+
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/255fcc05/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/client/util/Client.java
----------------------------------------------------------------------
diff --git a/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/client/util/Client.java b/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/client/util/Client.java
new file mode 100644
index 0000000..ef68b58
--- /dev/null
+++ b/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/client/util/Client.java
@@ -0,0 +1,140 @@
+package org.apache.olingo.odata2.fit.client.util;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.InetSocketAddress;
+import java.net.Proxy;
+import java.net.URL;
+import java.util.List;
+
+import org.apache.commons.codec.binary.Base64;
+import org.apache.olingo.odata2.api.commons.HttpStatusCodes;
+import org.apache.olingo.odata2.api.edm.Edm;
+import org.apache.olingo.odata2.api.edm.EdmEntityContainer;
+import org.apache.olingo.odata2.api.edm.EdmEntitySetInfo;
+import org.apache.olingo.odata2.api.ep.EntityProvider;
+import org.apache.olingo.odata2.api.ep.EntityProviderReadProperties;
+import org.apache.olingo.odata2.api.ep.feed.ODataFeed;
+import org.apache.olingo.odata2.api.exception.ODataException;
+
+public class Client {
+
+  public static final String APPLICATION_JSON = "application/json";
+  public static final String APPLICATION_XML = "application/xml";
+  public static final String APPLICATION_ATOM_XML = "application/atom+xml";
+
+  public static final String METADATA = "$metadata";
+
+  private String serviceUrl;
+  private Proxy.Type protocol;
+  private String proxy;
+  private int port;
+  private boolean useProxy;
+  private String username;
+  private String password;
+  private boolean useAuthentication;
+
+  private Edm edm;
+
+  public Client(String serviceUrl, Proxy.Type protocol, String proxy, int port) throws IOException, ODataException, HttpException {
+    this.serviceUrl = serviceUrl;
+    this.protocol = protocol;
+    this.proxy = proxy;
+    this.port = port;
+    this.useProxy = true;
+    this.useAuthentication = false;
+
+    edm = getEdmInternal();
+  }
+
+  public Client(String serviceUrl) throws IOException, ODataException, HttpException {
+    this.serviceUrl = serviceUrl;
+    this.useProxy = false;
+    this.useAuthentication = false;
+
+    edm = getEdmInternal();
+  }
+
+  public Client(String serviceUrl, Proxy.Type protocol, String proxy, int port, String username, String password) throws IOException, ODataException, HttpException {
+    this.serviceUrl = serviceUrl;
+    this.protocol = protocol;
+    this.proxy = proxy;
+    this.port = port;
+    this.useProxy = true;
+    this.username = username;
+    this.password = password;
+    this.useAuthentication = true;
+
+    edm = getEdmInternal();
+  }
+
+  public Client(String serviceUrl, String username, String password) throws IOException, ODataException, HttpException {
+    this.serviceUrl = serviceUrl;
+    this.useProxy = false;
+    this.username = username;
+    this.password = password;
+    this.useAuthentication = true;
+
+    edm = getEdmInternal();
+  }
+
+  private Edm getEdmInternal() throws IOException, ODataException, HttpException {
+    HttpURLConnection connection = connect(METADATA, APPLICATION_XML, "GET");
+    edm = EntityProvider.readMetadata((InputStream) connection.getContent(), false);
+    return edm;
+  }
+
+  private void checkStatus(HttpURLConnection connection) throws IOException, HttpException {
+    if (400 <= connection.getResponseCode() && connection.getResponseCode() <= 599) {
+      HttpStatusCodes httpStatusCode = HttpStatusCodes.fromStatusCode(connection.getResponseCode());
+      throw new HttpException(httpStatusCode, httpStatusCode.getStatusCode() + " " + httpStatusCode.toString());
+    }
+  }
+
+  public Edm getEdm() {
+    return edm;
+  }
+
+  public List<EdmEntitySetInfo> getEntitySets() throws ODataException {
+    return edm.getServiceMetadata().getEntitySetInfos();
+  }
+
+  public ODataFeed readFeed(String entityContainerName, String entitySetName, String contentType) throws IOException, ODataException, HttpException {
+    EdmEntityContainer entityContainer = edm.getEntityContainer(entityContainerName);
+    String relativeUri;
+    if (entityContainer.isDefaultEntityContainer()) {
+      relativeUri = entitySetName;
+    } else {
+      relativeUri = entityContainerName + "." + entitySetName;
+    }
+
+    InputStream content = (InputStream) connect(relativeUri, contentType, "GET").getContent();
+    return EntityProvider.readFeed(contentType, entityContainer.getEntitySet(entitySetName), content, EntityProviderReadProperties.init().build());
+  }
+
+  private HttpURLConnection connect(String relativeUri, String contentType, String httpMethod) throws IOException, HttpException {
+    URL url = new URL(serviceUrl + relativeUri);
+    HttpURLConnection connection;
+    if (useProxy) {
+      Proxy proxy = new Proxy(protocol, new InetSocketAddress(this.proxy, port));
+      connection = (HttpURLConnection) url.openConnection(proxy);
+    } else {
+      connection = (HttpURLConnection) url.openConnection();
+    }
+    connection.setRequestMethod(httpMethod);
+    connection.setRequestProperty("Accept", contentType);
+
+    if (useAuthentication) {
+      String authorization = "Basic ";
+      authorization += new String(Base64.encodeBase64((this.username + ":" + this.password).getBytes()));
+      connection.setRequestProperty("Authorization", authorization);
+    }
+
+    connection.connect();
+
+    checkStatus(connection);
+
+    return connection;
+  }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/255fcc05/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/client/util/HttpException.java
----------------------------------------------------------------------
diff --git a/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/client/util/HttpException.java b/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/client/util/HttpException.java
new file mode 100644
index 0000000..cb35db1
--- /dev/null
+++ b/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/client/util/HttpException.java
@@ -0,0 +1,19 @@
+package org.apache.olingo.odata2.fit.client.util;
+
+import org.apache.olingo.odata2.api.commons.HttpStatusCodes;
+
+public class HttpException extends Exception {
+
+  private static final long serialVersionUID = 1L;
+
+  private HttpStatusCodes httpStatusCode;
+
+  public HttpException(HttpStatusCodes httpStatusCode, String message) {
+    super(message);
+    this.httpStatusCode = httpStatusCode;
+  }
+
+  public HttpStatusCodes getHttpStatusCode() {
+    return httpStatusCode;
+  }
+}
\ No newline at end of file