1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.sra;
20
21 import static org.junit.jupiter.api.Assertions.fail;
22
23 import java.io.InputStream;
24 import java.util.Map;
25 import java.util.Properties;
26 import org.apache.commons.lang3.StringUtils;
27 import org.apache.commons.lang3.mutable.Mutable;
28 import org.apache.commons.lang3.mutable.MutableObject;
29 import org.apache.curator.test.InstanceSpec;
30 import org.apache.curator.test.TestingServer;
31 import org.apache.zookeeper.server.auth.DigestAuthenticationProvider;
32 import org.springframework.context.ApplicationContextInitializer;
33 import org.springframework.context.ConfigurableApplicationContext;
34
35 public class ZookeeperTestingServer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
36
37 @Override
38 public void initialize(final ConfigurableApplicationContext ctx) {
39 Mutable<Integer> port = new MutableObject<>();
40 Mutable<String> username = new MutableObject<>();
41 Mutable<String> password = new MutableObject<>();
42 try (InputStream propStream = getClass().getResourceAsStream("/test.properties")) {
43 Properties props = new Properties();
44 props.load(propStream);
45
46 port.setValue(Integer.valueOf(StringUtils.substringAfter(props.getProperty("keymaster.address"), ":")));
47 username.setValue(props.getProperty("keymaster.username"));
48 password.setValue(props.getProperty("keymaster.password"));
49 } catch (Exception e) {
50 throw new IllegalStateException("Could not load /test.properties", e);
51 }
52
53 if (AbstractTest.available(port.get())) {
54 try {
55 System.setProperty(
56 "zookeeper.DigestAuthenticationProvider.superDigest",
57 DigestAuthenticationProvider.generateDigest(username.get() + ":" + password.get()));
58 InstanceSpec spec = new InstanceSpec(null, port.get(), -1, -1, true, 1, -1, -1, Map.of());
59 new TestingServer(spec, true);
60 } catch (Exception e) {
61 fail(e);
62 }
63 }
64 }
65 }