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.HashMap;
25  import java.util.Map;
26  import java.util.Properties;
27  import java.util.concurrent.atomic.AtomicReference;
28  import javax.security.auth.login.AppConfigurationEntry;
29  import javax.security.auth.login.Configuration;
30  import org.apache.commons.lang3.StringUtils;
31  import org.apache.curator.test.InstanceSpec;
32  import org.apache.curator.test.TestingServer;
33  import org.apache.zookeeper.server.auth.DigestLoginModule;
34  import org.apache.zookeeper.server.auth.SASLAuthenticationProvider;
35  import org.springframework.context.ApplicationContextInitializer;
36  import org.springframework.context.ConfigurableApplicationContext;
37  
38  public class ZookeeperTestingServer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
39  
40      @Override
41      public void initialize(final ConfigurableApplicationContext ctx) {
42          AtomicReference<Integer> port = new AtomicReference<>();
43          AtomicReference<String> username = new AtomicReference<>();
44          AtomicReference<String> password = new AtomicReference<>();
45          try (InputStream propStream = getClass().getResourceAsStream("/test.properties")) {
46              Properties props = new Properties();
47              props.load(propStream);
48  
49              port.set(Integer.valueOf(StringUtils.substringAfter(props.getProperty("keymaster.address"), ":")));
50              username.set(props.getProperty("keymaster.username"));
51              password.set(props.getProperty("keymaster.password"));
52          } catch (Exception e) {
53              throw new IllegalStateException("Could not load /test.properties", e);
54          }
55  
56          if (AbstractTest.available(port.get())) {
57              Configuration.setConfiguration(new Configuration() {
58  
59                  private final AppConfigurationEntry[] entries = {
60                      new AppConfigurationEntry(
61                      DigestLoginModule.class.getName(),
62                      AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
63                      Map.of("user_" + username.get(), password.get()))
64                  };
65  
66                  @Override
67                  public AppConfigurationEntry[] getAppConfigurationEntry(final String name) {
68                      return entries;
69                  }
70              });
71  
72              Map<String, Object> customProperties = new HashMap<>();
73              customProperties.put("authProvider.1", SASLAuthenticationProvider.class.getName());
74              InstanceSpec spec = new InstanceSpec(null, port.get(), -1, -1, true, 1, -1, -1, customProperties);
75  
76              try {
77                  new TestingServer(spec, true);
78              } catch (Exception e) {
79                  fail(e);
80              }
81          }
82      }
83  }