forked from eventuate-clients/eventuate-client-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
eventuate-clients#9: Enhancing the Eventuate Java Client According GD…
…PR. Adding tests.
- Loading branch information
Showing
7 changed files
with
234 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
...te/javaclient/spring/autoconfiguration/AbstractEventuateAggregateStoreEncryptionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package io.eventuate.javaclient.spring.autoconfiguration; | ||
|
||
import io.eventuate.*; | ||
import io.eventuate.encryption.EncryptionKey; | ||
import io.eventuate.encryption.EventDataEncryptor; | ||
import io.eventuate.encryption.NoEncryptionKeyProvidedException; | ||
import io.vertx.core.json.Json; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public abstract class AbstractEventuateAggregateStoreEncryptionTest { | ||
|
||
private static String hexRegExp = "^[0-9a-fA-F]+$"; | ||
|
||
private static final String idKey = "1"; | ||
private static final String key = "Super strong password"; | ||
private static final String salt = "47b4033642e911e8842f0ed5f89f718b"; | ||
|
||
private static final String testEventData = "Some secret data"; | ||
private static final String testEventDataUpdated = "Some secret data (Updated)"; | ||
|
||
protected static final EncryptionKey encryptionKey = new EncryptionKey(idKey, key, salt); | ||
|
||
protected static final EventDataEncryptor eventDataEncryptor = new EventDataEncryptor(); | ||
|
||
@Test | ||
public void testSaveEncrypted() throws Exception { | ||
EntityIdAndVersion entityIdAndVersion = save(testEventData); | ||
|
||
NoEncryptionKeyProvidedException noEncryptionKeyProvidedException = null; | ||
|
||
try { | ||
find(entityIdAndVersion, false); | ||
} catch (NoEncryptionKeyProvidedException e) { | ||
noEncryptionKeyProvidedException = e; | ||
} | ||
|
||
Assert.assertNotNull(noEncryptionKeyProvidedException); | ||
|
||
String data = noEncryptionKeyProvidedException.getEncryptedEventData().getData(); | ||
|
||
Assert.assertNotNull(data); | ||
Assert.assertFalse(data.trim().isEmpty()); | ||
Assert.assertTrue(data.matches(hexRegExp)); | ||
Assert.assertEquals(Json.encode(new SomeEvent(testEventData)), eventDataEncryptor.decrypt(encryptionKey, data)); | ||
|
||
Assert.assertFalse(testEventData.matches(hexRegExp)); | ||
} | ||
|
||
@Test | ||
public void testSaveAndFind() throws Exception { | ||
EntityIdAndVersion entityIdAndVersion = save(testEventData); | ||
|
||
EntityWithMetadata<SomeAggregate> entityEntityWithMetadata = find(entityIdAndVersion, true); | ||
|
||
Assert.assertEquals(1, entityEntityWithMetadata.getEvents().size()); | ||
Assert.assertEquals(testEventData, ((SomeEvent)entityEntityWithMetadata.getEvents().get(0).getEvent()).getData()); | ||
} | ||
|
||
@Test | ||
public void testUpdateAndFind() throws Exception { | ||
EntityIdAndVersion entityIdAndVersion = save(testEventData); | ||
|
||
entityIdAndVersion = update(entityIdAndVersion, testEventDataUpdated); | ||
|
||
EntityWithMetadata<SomeAggregate> entityEntityWithMetadata = find(entityIdAndVersion, true); | ||
|
||
Assert.assertEquals(2, entityEntityWithMetadata.getEvents().size()); | ||
Assert.assertEquals(testEventDataUpdated, | ||
((SomeEvent)entityEntityWithMetadata.getEvents().get(1).getEvent()).getData()); | ||
} | ||
|
||
protected abstract EntityIdAndVersion save(String data) throws Exception; | ||
protected abstract EntityIdAndVersion update(EntityIdAndVersion entityIdAndVersion, String data) throws Exception; | ||
protected abstract EntityWithMetadata<SomeAggregate> find(EntityIdAndVersion entityIdAndVersion, boolean encrypted) throws Exception; | ||
|
||
public static class SomeAggregate implements Aggregate<SomeAggregate> { | ||
@Override | ||
public SomeAggregate applyEvent(Event event) { | ||
return this; | ||
} | ||
} | ||
|
||
public static class SomeEvent implements Event { | ||
private String data; | ||
|
||
public SomeEvent() { | ||
} | ||
|
||
public SomeEvent(String data) { | ||
this.data = data; | ||
} | ||
|
||
public String getData() { | ||
return data; | ||
} | ||
|
||
public void setData(String data) { | ||
this.data = data; | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
.../eventuate/javaclient/spring/autoconfiguration/EventuateAggregateStoreEncryptionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package io.eventuate.javaclient.spring.autoconfiguration; | ||
|
||
import io.eventuate.*; | ||
import io.eventuate.encryption.NoEncryptionKeyProvidedException; | ||
import io.eventuate.javaclient.saasclient.EventuateAggregateStoreBuilder; | ||
import org.junit.Assert; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.boot.test.IntegrationTest; | ||
import org.springframework.boot.test.SpringApplicationConfiguration; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
|
||
import java.util.Collections; | ||
import java.util.Optional; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@SpringApplicationConfiguration(classes = AutoConfigurationIntegrationTestConfiguration.class) | ||
@IntegrationTest | ||
public class EventuateAggregateStoreEncryptionTest extends AbstractEventuateAggregateStoreEncryptionTest { | ||
|
||
private io.eventuate.EventuateAggregateStore aggregateStore = EventuateAggregateStoreBuilder.defaultFromEnv(); | ||
|
||
@Override | ||
protected EntityIdAndVersion save(String data) throws Exception { | ||
return aggregateStore.save(SomeAggregate.class, | ||
Collections.singletonList(new SomeEvent(data)), | ||
new SaveOptions().withEncryptionKey(encryptionKey)).get(); | ||
} | ||
|
||
@Override | ||
protected EntityIdAndVersion update(EntityIdAndVersion entityIdAndVersion, String data) throws Exception { | ||
return aggregateStore.update(SomeAggregate.class, | ||
entityIdAndVersion, | ||
Collections.singletonList(new SomeEvent(data)), | ||
new UpdateOptions().withEncryptionKey(encryptionKey)).get(); | ||
} | ||
|
||
@Override | ||
protected EntityWithMetadata<SomeAggregate> find(EntityIdAndVersion entityIdAndVersion, | ||
boolean encrypted) throws Exception { | ||
try { | ||
return aggregateStore.find(SomeAggregate.class, | ||
entityIdAndVersion.getEntityId(), | ||
encrypted ? Optional.of(new FindOptions().withEncryptionKey(encryptionKey)) : Optional.empty()).get(); | ||
} catch (ExecutionException e) { | ||
Assert.assertTrue(e.getCause() instanceof NoEncryptionKeyProvidedException); | ||
throw (NoEncryptionKeyProvidedException) e.getCause(); | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...ntuate/javaclient/spring/autoconfiguration/SyncEventuateAggregateStoreEncryptionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.eventuate.javaclient.spring.autoconfiguration; | ||
|
||
import io.eventuate.*; | ||
import io.eventuate.javaclient.saasclient.EventuateAggregateStoreBuilder; | ||
import io.eventuate.sync.EventuateAggregateStore; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.boot.test.IntegrationTest; | ||
import org.springframework.boot.test.SpringApplicationConfiguration; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
|
||
import java.util.Collections; | ||
import java.util.Optional; | ||
|
||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@SpringApplicationConfiguration(classes = AutoConfigurationIntegrationTestConfiguration.class) | ||
@IntegrationTest | ||
public class SyncEventuateAggregateStoreEncryptionTest extends AbstractEventuateAggregateStoreEncryptionTest { | ||
|
||
private EventuateAggregateStore aggregateStore = EventuateAggregateStoreBuilder.standard().buildSync(); | ||
|
||
@Override | ||
protected EntityIdAndVersion save(String data) { | ||
return aggregateStore.save(SomeAggregate.class, | ||
Collections.singletonList(new SomeEvent(data)), | ||
new SaveOptions().withEncryptionKey(encryptionKey)); | ||
} | ||
|
||
@Override | ||
protected EntityIdAndVersion update(EntityIdAndVersion entityIdAndVersion, String data) { | ||
return aggregateStore.update(SomeAggregate.class, | ||
entityIdAndVersion, | ||
Collections.singletonList(new SomeEvent(data)), | ||
new UpdateOptions().withEncryptionKey(encryptionKey)); | ||
} | ||
|
||
@Override | ||
protected EntityWithMetadata<SomeAggregate> find(EntityIdAndVersion entityIdAndVersion, boolean encrypted) { | ||
return aggregateStore.find(SomeAggregate.class, | ||
entityIdAndVersion.getEntityId(), | ||
encrypted ? Optional.of(new FindOptions().withEncryptionKey(encryptionKey)) : Optional.empty()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
...e-client-java/src/main/java/io/eventuate/encryption/NoEncryptionKeyProvidedException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.eventuate.encryption; | ||
|
||
public class NoEncryptionKeyProvidedException extends RuntimeException { | ||
private EncryptedEventData encryptedEventData; | ||
|
||
public NoEncryptionKeyProvidedException(EncryptedEventData encryptedEventData) { | ||
this.encryptedEventData = encryptedEventData; | ||
} | ||
|
||
public EncryptedEventData getEncryptedEventData() { | ||
return encryptedEventData; | ||
} | ||
} |