|
1 | 1 | package com.baloise.confluence.digitalsignature; |
2 | 2 |
|
| 3 | +import com.atlassian.bandana.BandanaManager; |
3 | 4 | import org.junit.jupiter.api.Test; |
| 5 | +import org.mockito.Mockito; |
4 | 6 |
|
5 | 7 | import java.io.IOException; |
6 | 8 | import java.io.ObjectInputStream; |
|
9 | 11 | import java.nio.file.Path; |
10 | 12 | import java.nio.file.Paths; |
11 | 13 | import java.util.Date; |
| 14 | +import java.util.HashSet; |
12 | 15 |
|
13 | 16 | import static org.junit.jupiter.api.Assertions.*; |
| 17 | +import static org.mockito.Matchers.any; |
| 18 | +import static org.mockito.Mockito.mock; |
| 19 | +import static org.mockito.Mockito.when; |
14 | 20 |
|
15 | 21 | class SignatureSerialisationTest { |
16 | 22 | 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\"]}"; |
17 | 23 |
|
18 | 24 | @Test |
19 | 25 | void deserialize() throws IOException, ClassNotFoundException { |
20 | | - ObjectInputStream in = new ObjectInputStream(getClass().getResourceAsStream("/signature.ser")); |
21 | | - Signature2 signature = (Signature2) in.readObject(); |
22 | | - in.close(); |
| 26 | + String signatureKey = "signature.a077cdcc5bfcf275fe447ae2c609c1c361331b4e90cb85909582e0d824cbc5b3"; |
23 | 27 |
|
| 28 | + Signature2 signature; |
| 29 | + try(ObjectInputStream in = new ObjectInputStream(getClass().getResourceAsStream("/signature.ser"))) { |
| 30 | + |
| 31 | + HashSet<String> keys = new HashSet<>(); |
| 32 | + keys.add(signatureKey); |
| 33 | + BandanaManager mgr = mock(BandanaManager.class); |
| 34 | + when(mgr.getValue(any(), any())).thenReturn(in.readObject()); |
| 35 | + when(mgr.getKeys(any())).thenReturn(keys); |
| 36 | + |
| 37 | + signature = Signature2.fromBandana(mgr, signatureKey); |
| 38 | + } |
| 39 | + |
| 40 | + assertNotNull(signature); |
24 | 41 | assertAll( |
25 | | - () -> assertEquals("signature.a077cdcc5bfcf275fe447ae2c609c1c361331b4e90cb85909582e0d824cbc5b3", signature.getKey()), |
| 42 | + () -> assertEquals(signatureKey, signature.getKey()), |
26 | 43 | () -> assertEquals("[missing1, missing2]", signature.getMissingSignatures().toString()), |
27 | 44 | () -> assertEquals(1, signature.getSignatures().size()), |
28 | 45 | () -> assertTrue(signature.getSignatures().containsKey("signed1")), |
@@ -52,6 +69,17 @@ void serialize() throws IOException, ClassNotFoundException { |
52 | 69 | // assert the serialization we just wrote can be deserialized |
53 | 70 | ObjectInputStream in = new ObjectInputStream(Files.newInputStream(path)); |
54 | 71 | assertEquals(signature, in.readObject()); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void deserializeHistoricalRecord() throws IOException, ClassNotFoundException { |
| 76 | + Signature signature = new Signature(123L, "body", "title"); |
| 77 | + signature.getNotify().add("notify1"); |
| 78 | + signature.getMissingSignatures().add("missing1"); |
| 79 | + signature.getMissingSignatures().add("missing2"); |
| 80 | + signature.getSignatures().put("signed1", new Date(9999)); |
| 81 | + |
| 82 | + ObjectInputStream in; |
55 | 83 |
|
56 | 84 | // assert the historically serialized class can still be deserialized |
57 | 85 | in = new ObjectInputStream(this.getClass().getResourceAsStream("/signature.ser")); |
|
0 commit comments