Enclave refactoring (#1684)

* Factorise metrics code for KeyValueStorage database

- introduce `MonitorableKeyValueStorage`
- factorise code
- remove metrics instanciation in `RocksDbKeyValueStorage` and `ColumnarRocksDbKeyValueStorage`

* Rename class

* refactor Enclave

- use final modifiers when appropriate to comply with Pantheon global policy
- use `assertj` assertions in tests
- don't use local variables when not necessary
- check exception message for `upCheck` method

Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>
pull/2/head
Abdelhamid Bakhta 5 years ago committed by GitHub
parent 65fb548b8d
commit 3c7e684c2b
  1. 4
      enclave/build.gradle
  2. 88
      enclave/src/integration-test/java/tech/pegasys/pantheon/enclave/EnclaveTest.java
  3. 25
      enclave/src/main/java/tech/pegasys/pantheon/enclave/Enclave.java

@ -7,8 +7,10 @@ dependencies {
testImplementation 'junit:junit'
testImplementation project(':testutil')
// integration test dependacies.
// integration test dependencies.
integrationTestImplementation 'junit:junit'
integrationTestImplementation 'org.assertj:assertj-core'
integrationTestImplementation project(':testutil')
integrationTestImplementation 'net.consensys:orion'
}

@ -13,8 +13,8 @@
package tech.pegasys.pantheon.enclave;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.junit.Assert.assertTrue;
import tech.pegasys.orion.testutil.OrionTestHarness;
@ -24,7 +24,6 @@ import tech.pegasys.pantheon.enclave.types.DeletePrivacyGroupRequest;
import tech.pegasys.pantheon.enclave.types.PrivacyGroup;
import tech.pegasys.pantheon.enclave.types.ReceiveRequest;
import tech.pegasys.pantheon.enclave.types.ReceiveResponse;
import tech.pegasys.pantheon.enclave.types.SendRequest;
import tech.pegasys.pantheon.enclave.types.SendRequestLegacy;
import tech.pegasys.pantheon.enclave.types.SendRequestPantheon;
import tech.pegasys.pantheon.enclave.types.SendResponse;
@ -72,68 +71,65 @@ public class EnclaveTest {
@Test
public void testSendAndReceive() throws Exception {
List<String> publicKeys = testHarness.getPublicKeys();
SendRequest sc =
new SendRequestLegacy(PAYLOAD, publicKeys.get(0), Lists.newArrayList(publicKeys.get(0)));
SendResponse sr = enclave.send(sc);
ReceiveRequest rc = new ReceiveRequest(sr.getKey(), publicKeys.get(0));
ReceiveResponse rr = enclave.receive(rc);
assertEquals(PAYLOAD, new String(rr.getPayload(), UTF_8));
assertNotNull(rr.getPrivacyGroupId());
final List<String> publicKeys = testHarness.getPublicKeys();
final SendResponse sr =
enclave.send(
new SendRequestLegacy(
PAYLOAD, publicKeys.get(0), Lists.newArrayList(publicKeys.get(0))));
final ReceiveResponse rr = enclave.receive(new ReceiveRequest(sr.getKey(), publicKeys.get(0)));
assertThat(rr).isNotNull();
assertThat(new String(rr.getPayload(), UTF_8)).isEqualTo(PAYLOAD);
assertThat(rr.getPrivacyGroupId()).isNotNull();
}
@Test
public void testSendWithPrivacyGroupAndReceive() throws Exception {
List<String> publicKeys = testHarness.getPublicKeys();
final List<String> publicKeys = testHarness.getPublicKeys();
CreatePrivacyGroupRequest privacyGroupRequest =
final CreatePrivacyGroupRequest privacyGroupRequest =
new CreatePrivacyGroupRequest(publicKeys.toArray(new String[0]), publicKeys.get(0), "", "");
PrivacyGroup privacyGroupResponse = enclave.createPrivacyGroup(privacyGroupRequest);
SendRequest sc =
new SendRequestPantheon(
PAYLOAD, publicKeys.get(0), privacyGroupResponse.getPrivacyGroupId());
SendResponse sr = enclave.send(sc);
final PrivacyGroup privacyGroupResponse = enclave.createPrivacyGroup(privacyGroupRequest);
ReceiveRequest rc = new ReceiveRequest(sr.getKey(), publicKeys.get(0));
ReceiveResponse rr = enclave.receive(rc);
assertEquals(PAYLOAD, new String(rr.getPayload(), UTF_8));
assertNotNull(rr.getPrivacyGroupId());
final SendResponse sr =
enclave.send(
new SendRequestPantheon(
PAYLOAD, publicKeys.get(0), privacyGroupResponse.getPrivacyGroupId()));
final ReceiveResponse rr = enclave.receive(new ReceiveRequest(sr.getKey(), publicKeys.get(0)));
assertThat(rr).isNotNull();
assertThat(new String(rr.getPayload(), UTF_8)).isEqualTo(PAYLOAD);
assertThat(rr.getPrivacyGroupId()).isNotNull();
}
@Test
public void testCreateAndDeletePrivacyGroup() throws Exception {
List<String> publicKeys = testHarness.getPublicKeys();
String name = "testName";
String description = "testDesc";
CreatePrivacyGroupRequest privacyGroupRequest =
final List<String> publicKeys = testHarness.getPublicKeys();
final String name = "testName";
final String description = "testDesc";
final CreatePrivacyGroupRequest privacyGroupRequest =
new CreatePrivacyGroupRequest(
publicKeys.toArray(new String[0]), publicKeys.get(0), name, description);
PrivacyGroup privacyGroupResponse = enclave.createPrivacyGroup(privacyGroupRequest);
assertNotNull(privacyGroupResponse.getPrivacyGroupId());
assertEquals(name, privacyGroupResponse.getName());
assertEquals(description, privacyGroupResponse.getDescription());
assertEquals(PrivacyGroup.Type.PANTHEON, privacyGroupResponse.getType());
final PrivacyGroup privacyGroupResponse = enclave.createPrivacyGroup(privacyGroupRequest);
DeletePrivacyGroupRequest deletePrivacyGroupRequest =
new DeletePrivacyGroupRequest(privacyGroupResponse.getPrivacyGroupId(), publicKeys.get(0));
assertThat(privacyGroupResponse.getPrivacyGroupId()).isNotNull();
assertThat(privacyGroupResponse.getName()).isEqualTo(name);
assertThat(privacyGroupResponse.getDescription()).isEqualTo(description);
assertThat(privacyGroupResponse.getType()).isEqualByComparingTo(PrivacyGroup.Type.PANTHEON);
String response = enclave.deletePrivacyGroup(deletePrivacyGroupRequest);
final String response =
enclave.deletePrivacyGroup(
new DeletePrivacyGroupRequest(
privacyGroupResponse.getPrivacyGroupId(), publicKeys.get(0)));
assertEquals(response, privacyGroupResponse.getPrivacyGroupId());
assertThat(privacyGroupResponse.getPrivacyGroupId()).isEqualTo(response);
}
@Test(expected = IOException.class)
public void whenUpCheckFailsThrows() throws IOException {
Enclave broken = new Enclave(URI.create("http://null"));
broken.upCheck();
@Test
public void whenUpCheckFailsThrows() {
final Throwable thrown = catchThrowable(() -> new Enclave(URI.create("http://null")).upCheck());
assertThat(thrown).isInstanceOf(IOException.class);
assertThat(thrown).hasMessageContaining("Failed to perform upcheck");
}
}

@ -47,9 +47,9 @@ public class Enclave {
this.client = new OkHttpClient();
}
public Boolean upCheck() throws IOException {
String url = enclaveUri.resolve("/upcheck").toString();
Request request = new Request.Builder().url(url).get().build();
public boolean upCheck() throws IOException {
final String url = enclaveUri.resolve("/upcheck").toString();
final Request request = new Request.Builder().url(url).get().build();
try (Response response = client.newCall(request).execute()) {
return response.isSuccessful();
@ -60,29 +60,26 @@ public class Enclave {
}
public SendResponse send(final SendRequest content) throws Exception {
Request request = buildPostRequest(JSON, content, "/send");
return executePost(request, SendResponse.class);
return executePost(buildPostRequest(JSON, content, "/send"), SendResponse.class);
}
public ReceiveResponse receive(final ReceiveRequest content) throws Exception {
Request request = buildPostRequest(ORION, content, "/receive");
return executePost(request, ReceiveResponse.class);
return executePost(buildPostRequest(ORION, content, "/receive"), ReceiveResponse.class);
}
public PrivacyGroup createPrivacyGroup(final CreatePrivacyGroupRequest content) throws Exception {
Request request = buildPostRequest(JSON, content, "/privacyGroupId");
return executePost(request, PrivacyGroup.class);
return executePost(buildPostRequest(JSON, content, "/privacyGroupId"), PrivacyGroup.class);
}
public String deletePrivacyGroup(final DeletePrivacyGroupRequest content) throws Exception {
Request request = buildPostRequest(JSON, content, "/deletePrivacyGroupId");
return executePost(request, String.class);
return executePost(buildPostRequest(JSON, content, "/deletePrivacyGroupId"), String.class);
}
private Request buildPostRequest(
final MediaType mediaType, final Object content, final String endpoint) throws Exception {
RequestBody body = RequestBody.create(mediaType, objectMapper.writeValueAsString(content));
String url = enclaveUri.resolve(endpoint).toString();
final RequestBody body =
RequestBody.create(mediaType, objectMapper.writeValueAsString(content));
final String url = enclaveUri.resolve(endpoint).toString();
return new Request.Builder().url(url).post(body).build();
}
@ -91,7 +88,7 @@ public class Enclave {
if (response.isSuccessful()) {
return objectMapper.readValue(response.body().string(), responseType);
} else {
ErrorResponse errorResponse =
final ErrorResponse errorResponse =
objectMapper.readValue(response.body().string(), ErrorResponse.class);
throw new EnclaveException(errorResponse.getError());
}

Loading…
Cancel
Save