Skip to content

Commit 44b4ea4

Browse files
committed
#82: Tests, error handling of Signature's Bandana access and minor cleanup
1 parent 757fdfd commit 44b4ea4

File tree

5 files changed

+85
-43
lines changed

5 files changed

+85
-43
lines changed

src/main/java/com/baloise/confluence/digitalsignature/DigitalSignatureMacro.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@
2929

3030
import java.io.UnsupportedEncodingException;
3131
import java.net.URLEncoder;
32+
import java.nio.charset.StandardCharsets;
3233
import java.security.InvalidParameterException;
3334
import java.util.*;
3435

3536
import static com.atlassian.confluence.renderer.radeox.macros.MacroUtils.defaultVelocityContext;
3637
import static com.atlassian.confluence.security.ContentPermission.*;
37-
import static com.atlassian.confluence.setup.bandana.ConfluenceBandanaContext.GLOBAL_CONTEXT;
3838
import static com.atlassian.confluence.util.velocity.VelocityUtils.getRenderedTemplate;
3939
import static java.util.Arrays.asList;
4040
import static java.util.stream.Collectors.toList;
@@ -296,7 +296,9 @@ private Signature sync(Signature signature, Set<String> signers) {
296296
save = true;
297297
}
298298

299-
if (save) save(loaded);
299+
if (save) {
300+
save(loaded);
301+
}
300302
} else {
301303
signature.setMissingSignatures(signers);
302304
save(signature);
@@ -346,7 +348,7 @@ protected String getMailto(Collection<UserProfile> profiles, String subject, boo
346348

347349
public String urlEncode(String string) {
348350
try {
349-
return URLEncoder.encode(string, "UTF-8");
351+
return URLEncoder.encode(string, StandardCharsets.UTF_8.name());
350352
} catch (UnsupportedEncodingException e) {
351353
throw new IllegalStateException(e);
352354
}

src/main/java/com/baloise/confluence/digitalsignature/Signature.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,8 @@ public Signature(long pageId, String body, String title) {
4141

4242
public static boolean isPetitionMode(Set<String> userGroups) {
4343
return userGroups != null
44-
&& userGroups.size() == 1
45-
&& userGroups.iterator().next().trim().equals("*");
46-
}
47-
48-
String serialize() {
49-
return GSON.toJson(this, Signature.class);
44+
&& userGroups.size() == 1
45+
&& userGroups.iterator().next().trim().equals("*");
5046
}
5147

5248
static Signature deserialize(String serialization) {
@@ -57,7 +53,7 @@ public static Signature fromBandana(BandanaManager mgr, String key) {
5753
Object value = mgr.getValue(GLOBAL_CONTEXT, key);
5854

5955
if (value == null) {
60-
return null;
56+
throw new IllegalArgumentException("Value is null in Bandana???");
6157
}
6258

6359
if (value instanceof Signature) {
@@ -76,8 +72,7 @@ public static Signature fromBandana(BandanaManager mgr, String key) {
7672
}
7773
}
7874

79-
log.error("Could not deserialize {} value from Bandana", value.getClass().getName());
80-
return null;
75+
throw new IllegalArgumentException(String.format("Could not deserialize %s value from Bandana. Please clear the plugin-cache and reboot confluence. (https://github.com/baloise/digital-signature/issues/82)", value));
8176
}
8277

8378
public static void toBandana(BandanaManager mgr, String key, Signature sig) {
@@ -88,6 +83,10 @@ public static void toBandana(BandanaManager mgr, Signature sig) {
8883
toBandana(mgr, sig.getKey(), sig);
8984
}
9085

86+
String serialize() {
87+
return GSON.toJson(this, Signature.class);
88+
}
89+
9190
public String getHash() {
9291
if (hash == null) {
9392
hash = getKey().replace("signature.", "");

src/main/java/com/baloise/confluence/digitalsignature/rest/DigitalSignatureService.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,13 @@ public class DigitalSignatureService {
6565
private final ContextHelper contextHelper = new ContextHelper();
6666
private final transient Markdown markdown = new Markdown();
6767

68-
public DigitalSignatureService(
69-
@ComponentImport BandanaManager bandanaManager,
70-
@ComponentImport SettingsManager settingsManager,
71-
@ComponentImport UserManager userManager,
72-
@ComponentImport LocalNotificationService notificationService,
73-
@ComponentImport MailServerManager mailServerManager,
74-
@ComponentImport PageManager pageManager,
75-
@ComponentImport I18nResolver i18nResolver) {
68+
public DigitalSignatureService(@ComponentImport BandanaManager bandanaManager,
69+
@ComponentImport SettingsManager settingsManager,
70+
@ComponentImport UserManager userManager,
71+
@ComponentImport LocalNotificationService notificationService,
72+
@ComponentImport MailServerManager mailServerManager,
73+
@ComponentImport PageManager pageManager,
74+
@ComponentImport I18nResolver i18nResolver) {
7675
this.bandanaManager = bandanaManager;
7776
this.settingsManager = settingsManager;
7877
this.userManager = userManager;
Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
package com.baloise.confluence.digitalsignature;
22

3-
43
import org.junit.jupiter.api.Test;
54

6-
import java.io.FileOutputStream;
75
import java.io.IOException;
86
import java.io.ObjectInputStream;
97
import java.io.ObjectOutputStream;
8+
import java.nio.file.Files;
9+
import java.nio.file.Path;
10+
import java.nio.file.Paths;
1011
import java.util.Date;
1112

1213
import static org.junit.jupiter.api.Assertions.*;
1314

14-
1515
class SignatureSerialisationTest {
16+
public static final String SIG_JSON = "{\"key\":\"signature.a077cdcc5bfcf275fe447ae2c609c1c361331b4e90cb85909582e0d824cbc5b3\",\"hash\":\"a077cdcc5bfcf275fe447ae2c609c1c361331b4e90cb85909582e0d824cbc5b3\",\"pageId\":123,\"title\":\"title\",\"body\":\"body\",\"maxSignatures\":-1,\"visibilityLimit\":-1,\"signatures\":{\"signed1\":\"Jan 1, 1970, 1:00:09 AM\"},\"missingSignatures\":[\"missing1\",\"missing2\"],\"notify\":[\"notify1\"]}";
17+
1618
@Test
1719
void deserialize() throws IOException, ClassNotFoundException {
1820
ObjectInputStream in = new ObjectInputStream(getClass().getResourceAsStream("/signature.ser"));
@@ -24,7 +26,13 @@ void deserialize() throws IOException, ClassNotFoundException {
2426
() -> assertEquals("[missing1, missing2]", signature.getMissingSignatures().toString()),
2527
() -> assertEquals(1, signature.getSignatures().size()),
2628
() -> assertTrue(signature.getSignatures().containsKey("signed1")),
27-
() -> assertEquals(9999, signature.getSignatures().get("signed1").getTime())
29+
() -> assertEquals(9999, signature.getSignatures().get("signed1").getTime()),
30+
31+
// assert we can still read the old gson serialization
32+
() -> assertEquals(signature, Signature.deserialize(SIG_JSON)),
33+
34+
// assert that deserialization of the serialization results in the original Signature
35+
() -> assertEquals(signature, Signature.deserialize(signature.serialize()))
2836
);
2937
}
3038

@@ -35,11 +43,18 @@ void serialize() throws IOException, ClassNotFoundException {
3543
signature.getMissingSignatures().add("missing1");
3644
signature.getMissingSignatures().add("missing2");
3745
signature.getSignatures().put("signed1", new Date(9999));
38-
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("src/test/resources/signature-test.ser"));
39-
out.writeObject(signature);
40-
out.close();
4146

42-
ObjectInputStream in = new ObjectInputStream(this.getClass().getResourceAsStream("/signature.ser"));
47+
Path path = Paths.get("src/test/resources/signature-test.ser");
48+
try(ObjectOutputStream out = new ObjectOutputStream(Files.newOutputStream(path))) {
49+
out.writeObject(signature);
50+
}
51+
52+
// assert the serialization we just wrote can be deserialized
53+
ObjectInputStream in = new ObjectInputStream(Files.newInputStream(path));
54+
assertEquals(signature, in.readObject());
55+
56+
// assert the historically serialized class can still be deserialized
57+
in = new ObjectInputStream(this.getClass().getResourceAsStream("/signature.ser"));
4358
assertEquals(signature, in.readObject());
4459
}
4560
}

src/test/java/com/baloise/confluence/digitalsignature/SignatureTest.java

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package com.baloise.confluence.digitalsignature;
22

33
import com.atlassian.bandana.BandanaManager;
4+
import com.atlassian.bandana.DefaultBandanaManager;
45
import org.junit.jupiter.api.Nested;
56
import org.junit.jupiter.api.Test;
7+
import org.mockito.ArgumentCaptor;
68

7-
import java.util.Set;
9+
import java.util.Collections;
810

911
import static org.junit.jupiter.api.Assertions.assertEquals;
1012
import static org.junit.jupiter.api.Assertions.assertNull;
1113
import static org.mockito.Matchers.any;
12-
import static org.mockito.Mockito.mock;
13-
import static org.mockito.Mockito.when;
14+
import static org.mockito.Mockito.*;
1415

1516
class SignatureTest {
1617
@Nested
@@ -28,8 +29,8 @@ void serialize_empty() {
2829
void serialize_initializedObject() {
2930
Signature signature = new Signature(42L, "body text", "title text");
3031
signature.sign("max.mustermann");
31-
signature.setMissingSignatures(Set.of("max.muster"));
32-
signature.setNotify(Set.of("max.meier"));
32+
signature.setMissingSignatures(Collections.singleton("max.muster"));
33+
signature.setNotify(Collections.singleton("max.meier"));
3334

3435
String json = signature.serialize();
3536

@@ -46,8 +47,8 @@ void deserialize_empty() {
4647
void serializeAndDeserialize() {
4748
Signature signature = new Signature(42L, "body text", "title text");
4849
signature.sign("max.mustermann");
49-
signature.setMissingSignatures(Set.of("max.muster"));
50-
signature.setNotify(Set.of("max.meier"));
50+
signature.setMissingSignatures(Collections.singleton("max.muster"));
51+
signature.setNotify(Collections.singleton("max.meier"));
5152

5253
String json = signature.serialize();
5354

@@ -60,25 +61,51 @@ void serializeAndDeserialize() {
6061

6162
@Nested
6263
class BandanaWrapperTest {
64+
private final BandanaManager bandana = mock(DefaultBandanaManager.class);
6365
private final Signature signature = new Signature(1, "test", "title");
64-
private final BandanaManager bandana = mock(BandanaManager.class);
66+
67+
@Test
68+
void toBandanaFromBandana_readAsWritten() {
69+
ArgumentCaptor<String> stringCapator = ArgumentCaptor.forClass(String.class);
70+
ArgumentCaptor<Object> objectCapator = ArgumentCaptor.forClass(Object.class);
71+
72+
String key = signature.getKey();
73+
assertNull(Signature.fromBandana(bandana, key), "Should not be there yet.");
74+
75+
doNothing().when(bandana).setValue(any(), stringCapator.capture(), objectCapator.capture());
76+
when(bandana.getKeys(any())).thenReturn(Collections.singletonList(key));
77+
78+
Signature.toBandana(bandana, signature);
79+
assertEquals(key, stringCapator.getValue());
80+
assertEquals(signature.serialize(), objectCapator.getValue());
81+
82+
when(bandana.getValue(any(), any())).thenCallRealMethod();
83+
when(bandana.getValue(any(), eq(key), eq(true))).thenReturn(signature);
84+
assertEquals(signature, Signature.fromBandana(bandana, signature.getKey()));
85+
}
6586

6687
@Test
6788
void fromBandana_signature_signature() {
68-
when(bandana.getValue(any(), any())).thenReturn(signature.serialize());
89+
String key = signature.getKey();
90+
assertNull(Signature.fromBandana(bandana, key), "Should not be there yet.");
6991

70-
Signature readSignature = Signature.fromBandana(bandana, null);
92+
when(bandana.getKeys(any())).thenReturn(Collections.singletonList(key));
93+
when(bandana.getValue(any(), any())).thenCallRealMethod();
94+
when(bandana.getValue(any(), eq(key), eq(true))).thenReturn(signature);
7195

72-
assertEquals(signature, readSignature);
96+
assertEquals(signature, Signature.fromBandana(bandana, signature.getKey()));
7397
}
7498

7599
@Test
76-
void fromBandana_string_signatur() {
77-
when(bandana.getValue(any(), any())).thenReturn(signature);
100+
void fromBandana_string_signature() {
101+
String key = signature.getKey();
102+
assertNull(Signature.fromBandana(bandana, key), "Should not be there yet.");
78103

79-
Signature readSignature = Signature.fromBandana(bandana, null);
104+
when(bandana.getKeys(any())).thenReturn(Collections.singletonList(key));
105+
when(bandana.getValue(any(), any())).thenCallRealMethod();
106+
when(bandana.getValue(any(), eq(key), eq(true))).thenReturn(signature.serialize());
80107

81-
assertEquals(signature, readSignature);
108+
assertEquals(signature, Signature.fromBandana(bandana, signature.getKey()));
82109
}
83110
}
84111
}

0 commit comments

Comments
 (0)