View Javadoc
1   package net.sumaris.core.test;
2   
3   /*-
4    * #%L
5    * SUMARiS:: Test shared
6    * %%
7    * Copyright (C) 2018 - 2019 SUMARiS Consortium
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  import com.google.common.base.Preconditions;
26  import net.sumaris.core.config.SumarisConfiguration;
27  import org.apache.commons.lang3.StringUtils;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  import org.springframework.context.annotation.Bean;
31  import org.springframework.jdbc.datasource.DriverManagerDataSource;
32  
33  import javax.annotation.Resource;
34  import javax.sql.DataSource;
35  
36  /**
37   * @author peck7 on 17/12/2018.
38   *
39   */
40  @org.springframework.boot.test.context.TestConfiguration
41  public abstract class TestConfiguration {
42  
43      /** Logger. */
44      private static final Logger log =
45              LoggerFactory.getLogger(TestConfiguration.class);
46  
47      protected static SumarisConfiguration initConfiguration(String configFileName) {
48          SumarisConfiguration config = SumarisConfiguration.getInstance();
49          if (config == null) {
50              log.info(String.format("Configuration file: %s", configFileName));
51              config = new SumarisConfiguration(configFileName);
52              SumarisConfiguration.setInstance(config);
53          }
54          return config;
55      }
56  
57      @Bean
58      public DataSource dataSource(SumarisConfiguration config) {
59  
60          Preconditions.checkArgument(StringUtils.isNotBlank(config.getJdbcDriver()), "Missing jdbc driver in configuration");
61          Preconditions.checkArgument(StringUtils.isNotBlank(config.getJdbcURL()), "Missing jdbc driver in configuration");
62          Preconditions.checkArgument(StringUtils.isNotBlank(config.getJdbcUsername()), "Missing jdbc username in configuration");
63  
64          log.info(String.format("Database URL: %s", config.getJdbcURL()));
65          log.info(String.format("Database username: %s", config.getJdbcUsername()));
66  
67          // Driver datasource
68          DriverManagerDataSource dataSource = new DriverManagerDataSource();
69          dataSource.setDriverClassName(config.getJdbcDriver());
70          dataSource.setUrl(config.getJdbcURL());
71          dataSource.setUsername(config.getJdbcUsername());
72          dataSource.setPassword(config.getJdbcPassword());
73  
74          if (StringUtils.isNotBlank(config.getJdbcSchema())) {
75              dataSource.setSchema(config.getJdbcSchema());
76          }
77          if (StringUtils.isNotBlank(config.getJdbcCatalog())) {
78              dataSource.setCatalog(config.getJdbcCatalog());
79          }
80          return dataSource;
81      }
82  
83  }