Skip to content

Commit

Permalink
#82: fixes tests and adds documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Tiliavir committed Sep 10, 2023
1 parent 5201325 commit f16b769
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
import java.util.Set;
import java.util.TreeSet;

/**
* This class is deprecated and should no longer be used except for downwards compatibility, i.e. reading values from
* Bandana that were written with an older version.
* <br />
* Use @{@link com.baloise.confluence.digitalsignature.Signature2} instead.
*/
@Deprecated
public class Signature implements Serializable, Cloneable {

Expand Down Expand Up @@ -199,6 +205,6 @@ public boolean hasMissingSignatures() {

@Override
public Signature clone() throws CloneNotSupportedException{
return (Signature) super.clone();
return (Signature) super.clone();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.baloise.confluence.digitalsignature;

import com.atlassian.bandana.BandanaManager;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.io.IOException;
import java.io.ObjectInputStream;
Expand All @@ -9,20 +11,35 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;
import java.util.HashSet;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class SignatureSerialisationTest {
public static final String SIG_JSON = "{\"key\":\"signature.a077cdcc5bfcf275fe447ae2c609c1c361331b4e90cb85909582e0d824cbc5b3\",\"hash\":\"a077cdcc5bfcf275fe447ae2c609c1c361331b4e90cb85909582e0d824cbc5b3\",\"pageId\":123,\"title\":\"title\",\"body\":\"body\",\"maxSignatures\":-1,\"visibilityLimit\":-1,\"signatures\":{\"signed1\":\"1970-01-01T01:00:09CET\"},\"missingSignatures\":[\"missing1\",\"missing2\"],\"notify\":[\"notify1\"]}";

@Test
void deserialize() throws IOException, ClassNotFoundException {
ObjectInputStream in = new ObjectInputStream(getClass().getResourceAsStream("/signature.ser"));
Signature2 signature = (Signature2) in.readObject();
in.close();
String signatureKey = "signature.a077cdcc5bfcf275fe447ae2c609c1c361331b4e90cb85909582e0d824cbc5b3";

Signature2 signature;
try(ObjectInputStream in = new ObjectInputStream(getClass().getResourceAsStream("/signature.ser"))) {

HashSet<String> keys = new HashSet<>();
keys.add(signatureKey);
BandanaManager mgr = mock(BandanaManager.class);
when(mgr.getValue(any(), any())).thenReturn(in.readObject());
when(mgr.getKeys(any())).thenReturn(keys);

signature = Signature2.fromBandana(mgr, signatureKey);
}

assertNotNull(signature);
assertAll(
() -> assertEquals("signature.a077cdcc5bfcf275fe447ae2c609c1c361331b4e90cb85909582e0d824cbc5b3", signature.getKey()),
() -> assertEquals(signatureKey, signature.getKey()),
() -> assertEquals("[missing1, missing2]", signature.getMissingSignatures().toString()),
() -> assertEquals(1, signature.getSignatures().size()),
() -> assertTrue(signature.getSignatures().containsKey("signed1")),
Expand Down Expand Up @@ -52,6 +69,17 @@ void serialize() throws IOException, ClassNotFoundException {
// assert the serialization we just wrote can be deserialized
ObjectInputStream in = new ObjectInputStream(Files.newInputStream(path));
assertEquals(signature, in.readObject());
}

@Test
void deserializeHistoricalRecord() throws IOException, ClassNotFoundException {
Signature signature = new Signature(123L, "body", "title");
signature.getNotify().add("notify1");
signature.getMissingSignatures().add("missing1");
signature.getMissingSignatures().add("missing2");
signature.getSignatures().put("signed1", new Date(9999));

ObjectInputStream in;

// assert the historically serialized class can still be deserialized
in = new ObjectInputStream(this.getClass().getResourceAsStream("/signature.ser"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ void serializeAndDeserialize() {
class BandanaWrapperTest {
private final BandanaManager bandana = mock(DefaultBandanaManager.class);
private final Signature2 signature = new Signature2(1, "test", "title");
private final Signature signatureOld = new Signature(1, "test", "title");

@Test
void toBandanaFromBandana_readAsWritten() {
Expand All @@ -80,7 +81,7 @@ void toBandanaFromBandana_readAsWritten() {
assertEquals(signature.serialize(), objectCapator.getValue());

when(bandana.getValue(any(), any())).thenCallRealMethod();
when(bandana.getValue(any(), eq(key), eq(true))).thenReturn(signature);
when(bandana.getValue(any(), eq(key), eq(true))).thenReturn(signature.serialize());
assertEquals(signature, Signature2.fromBandana(bandana, signature.getKey()));
}

Expand All @@ -91,7 +92,7 @@ void fromBandana_signature_signature() {

when(bandana.getKeys(any())).thenReturn(Collections.singletonList(key));
when(bandana.getValue(any(), any())).thenCallRealMethod();
when(bandana.getValue(any(), eq(key), eq(true))).thenReturn(signature);
when(bandana.getValue(any(), eq(key), eq(true))).thenReturn(signatureOld);

assertEquals(signature, Signature2.fromBandana(bandana, signature.getKey()));
}
Expand Down

0 comments on commit f16b769

Please sign in to comment.