Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Commit

Permalink
Add integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Puneetha17 committed Jan 11, 2019
1 parent d7f6dd8 commit 5d5797d
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@
*/
package tech.pegasys.pantheon.orion;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import tech.pegasys.pantheon.orion.types.ReceiveContent;
import tech.pegasys.pantheon.orion.types.ReceiveResponse;
import tech.pegasys.pantheon.orion.types.SendContent;
import tech.pegasys.pantheon.orion.types.SendResponse;

import java.io.IOException;

import org.junit.BeforeClass;
Expand Down Expand Up @@ -45,7 +49,8 @@
@Ignore
public class OrionTest {

private static String PUBLIC_KEY = "<replace_me>";
private static String PUBLIC_KEY = "<update_with_contents_of_foo.pub>";
private static String PAYLOAD = "SGVsbG8sIFdvcmxkIQ==";
private static Orion orion;
private static Orion broken;

Expand All @@ -64,20 +69,27 @@ public void testUpCheck() throws IOException {
}

@Test(expected = IOException.class)
public void whenUpCheckFailsReturnFalse() throws IOException {
public void whenUpCheckFailsThrows() throws IOException {
broken.upCheck();
}

@Test
public void testSend() throws IOException {
SendContent sc = new SendContent("SGVsbG8sIFdvcmxkIQ==", PUBLIC_KEY, new String[] {PUBLIC_KEY});

System.out.println("LcF7I+UnR2XBdSxZesiYE/lTtxVfFeY4EvL9fDXb0Uo=".length());
SendContent sc = new SendContent(PAYLOAD, PUBLIC_KEY, new String[] {PUBLIC_KEY});
SendResponse sr = orion.send(sc);

assertThat(
"LcF7I+UnR2XBdSxZesiYE/lTtxVfFeY4EvL9fDXb0Uo=".length(), is(orion.send(sc).length()));
// example "LcF7I+UnR2XBdSxZesiYE/lTtxVfFeY4EvL9fDXb0Uo=".length() is 44
assertEquals(44, sr.getKey().length());
}

@Test
public void testReceive() {}
public void testReceive() throws IOException {
SendContent sc = new SendContent(PAYLOAD, PUBLIC_KEY, new String[] {PUBLIC_KEY});
SendResponse sr = orion.send(sc);

ReceiveContent rc = new ReceiveContent(sr.getKey(), PUBLIC_KEY);
ReceiveResponse rr = orion.receive(rc);

assertEquals(PAYLOAD, rr.getPayload());
}
}
20 changes: 13 additions & 7 deletions orion/src/main/java/tech/pegasys/pantheon/orion/Orion.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
*/
package tech.pegasys.pantheon.orion;

import tech.pegasys.pantheon.orion.types.ReceiveContent;
import tech.pegasys.pantheon.orion.types.ReceiveResponse;
import tech.pegasys.pantheon.orion.types.SendContent;
import tech.pegasys.pantheon.orion.types.SendResponse;

import java.io.IOException;

import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -25,7 +30,7 @@

public class Orion {
private static final MediaType JSON = MediaType.parse("application/json");
private static final ObjectMapper om = new ObjectMapper();
private static final ObjectMapper objectMapper = new ObjectMapper();
private static final Logger LOG = LogManager.getLogger();

private String url;
Expand All @@ -47,22 +52,23 @@ public Boolean upCheck() throws IOException {
}
}

public String send(final SendContent content) throws IOException {
return executePost("/send", om.writeValueAsString(content));
public SendResponse send(final SendContent content) throws IOException {
return executePost("/send", objectMapper.writeValueAsString(content), SendResponse.class);
}

public String receive(final ReceiveContent content) throws IOException {
return executePost("/receive", om.writeValueAsString(content));
public ReceiveResponse receive(final ReceiveContent content) throws IOException {
return executePost("/receive", objectMapper.writeValueAsString(content), ReceiveResponse.class);
}

private String executePost(final String path, final String content) throws IOException {
private <T> T executePost(final String path, final String content, final Class<T> responseType)
throws IOException {
OkHttpClient client = new OkHttpClient();

RequestBody body = RequestBody.create(JSON, content);
Request request = new Request.Builder().url(url + path).post(body).build();

try (Response response = client.newCall(request).execute()) {
return response.body().string();
return objectMapper.readValue(response.body().string(), responseType);
} catch (IOException e) {
LOG.error("Orion failed to execute ", path);
throw new IOException("Failed to execute post", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.pantheon.orion;
package tech.pegasys.pantheon.orion.types;

public class ReceiveContent {
private String key;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.pantheon.orion;
package tech.pegasys.pantheon.orion.types;

public class SendContent {
private String payload;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2019 ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.pantheon.orion.types;

public class ReceiveResponse {

String payload;

public String getPayload() {
return payload;
}

public void setPayload(final String payload) {
this.payload = payload;
}

public ReceiveResponse(final String payload) {
this.payload = payload;
}

public ReceiveResponse() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2019 ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.pantheon.orion.types;

public class SendResponse {
String key;

public String getKey() {
return key;
}

public void setKey(final String key) {
this.key = key;
}

public SendResponse(final String key) {
this.key = key;
}

public SendResponse() {}
}

0 comments on commit 5d5797d

Please sign in to comment.