View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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  }