View Javadoc
1   /*
2    * Copyright 2023 Adrian Herscu
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package dev.aherscu.qa.jgiven.jdbc;
17  
18  import static dev.aherscu.qa.testing.utils.StringUtilsExtensions.*;
19  
20  import java.util.concurrent.*;
21  
22  import javax.annotation.concurrent.*;
23  
24  import org.apache.commons.configuration.*;
25  import org.apache.commons.configuration.AbstractConfiguration;
26  
27  import com.zaxxer.hikari.*;
28  
29  import dev.aherscu.qa.jgiven.jdbc.utils.dbutils.*;
30  import dev.aherscu.qa.testing.utils.config.BaseConfiguration;
31  import lombok.*;
32  import lombok.extern.slf4j.*;
33  
34  /**
35   * Represents the configuration parameters for tests.
36   *
37   * @author aherscu
38   */
39  @ThreadSafe
40  @Slf4j
41  public final class TestConfiguration extends BaseConfiguration {
42      private static final ConcurrentMap<String, StreamingQueryRunner> queryRunners =
43          new ConcurrentHashMap<>();
44  
45      static {
46          // IMPORTANT: this makes all property values to be parsed as
47          // as list if commas are found inside
48          AbstractConfiguration
49              .setDefaultListDelimiter(COMMA.charAt(0));
50      }
51  
52      /**
53       * Loads the specified configurations.
54       *
55       * @param configurations
56       *            the additional configurations; might be null or empty
57       */
58      public TestConfiguration(final Configuration... configurations) {
59          super(configurations);
60      }
61  
62      public StreamingQueryRunner queryRunnerFor(final String id) {
63          log.trace("configuring query runner {}", id);
64          return queryRunners.computeIfAbsent(id, _id -> {
65              val dataSource = new HikariDataSource();
66              dataSource.setJdbcUrl(datasourceString(_id, "url"));
67              return new StreamingQueryRunner(dataSource);
68          });
69      }
70  
71      private int datasourceInt(String id, String name) {
72          return getInt("datasource" + DOT + id + DOT + name);
73      }
74  
75      private String datasourceString(String id, String name) {
76          return getString("datasource" + DOT + id + DOT + name);
77      }
78  
79  }