2 * ===============================LICENSE_START======================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============================LICENSE_END===========================================
21 package org.openecomp.dcae.apod.analytics.it.module;
23 import com.google.inject.Binder;
24 import com.google.inject.Module;
25 import com.google.inject.name.Names;
26 import org.openecomp.dcae.apod.analytics.common.exception.DCAEAnalyticsRuntimeException;
27 import org.openecomp.dcae.apod.analytics.it.dmaap.DMaaPMRCreator;
28 import org.openecomp.dcae.apod.analytics.it.dmaap.DMaaPMRCreatorImpl;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import java.io.FileNotFoundException;
33 import java.io.IOException;
34 import java.io.InputStream;
36 import java.util.Properties;
39 * @author Rajiv Singla . Creation Date: 2/1/2017.
41 public class IntegrationTestModule implements Module {
43 private static final Logger LOG = LoggerFactory.getLogger(IntegrationTestModule.class);
45 public static final String ANALYTICS_SYSTEM_VARIABLE_KEY_NAME = "analytics.it.env";
46 public static final String DEFAULT_ENVIRONMENT = "dev";
47 public static final String ENVIRONMENT_PROPERTIES_FILE_LOCATION = "env";
50 public void configure(Binder binder) {
51 final Properties envProperties = loadPropertiesFile();
52 Names.bindProperties(binder, envProperties);
53 binder.bind(DMaaPMRCreator.class).to(DMaaPMRCreatorImpl.class);
58 * Load environment specific properties file
60 * @return environment properties
62 private Properties loadPropertiesFile() {
63 final String currentEnvironment = getCurrentEnvironment().toLowerCase();
64 final String envPropertiesFileName = currentEnvironment + ".properties";
65 final Properties envProperties = new Properties();
66 final String fileLocation = ENVIRONMENT_PROPERTIES_FILE_LOCATION + "/" + envPropertiesFileName;
67 LOG.info("===>>> EFFECTIVE ENV: {}, EFFECTIVE PROPERTIES FILE: {} <<<====", currentEnvironment, fileLocation);
69 final InputStream propertiesFileInputStream =
70 IntegrationTestModule.class.getClassLoader().getResourceAsStream(fileLocation);
71 envProperties.load(propertiesFileInputStream);
72 } catch (FileNotFoundException e) {
73 final String errorMessage = String.format("Unable to find env properties file: %s.", fileLocation);
74 throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e);
75 } catch (IOException e) {
76 final String errorMessage = String.format("I/O Exception during loading env properties file: %s",
78 throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e);
81 final Properties systemProperties = System.getProperties();
82 for (Object envProperty : envProperties.keySet()) {
83 final String systemPropertyValue = systemProperties.getProperty(envProperty.toString());
84 if (systemPropertyValue != null) {
85 LOG.info("Overriding System property name: {} with env property value: {}",
86 envProperty.toString(), systemPropertyValue);
87 envProperties.setProperty(envProperty.toString(), systemPropertyValue);
91 LOG.info("Printing Effective Environment Properties =============== >>>");
92 for (Map.Entry<Object, Object> envPropertyEntry : envProperties.entrySet()) {
93 LOG.info("{}={}", envPropertyEntry.getKey(), envPropertyEntry.getValue());
100 private static String getCurrentEnvironment() {
101 // First look in environment variables
102 LOG.info("Looking for IT variable name: {} in Environment variables", ANALYTICS_SYSTEM_VARIABLE_KEY_NAME);
103 final String itEnvironmentVariable = System.getenv(ANALYTICS_SYSTEM_VARIABLE_KEY_NAME);
104 if (itEnvironmentVariable != null) {
105 LOG.info("Found value in Environment variables: {} for IT Environment variable", itEnvironmentVariable);
106 return itEnvironmentVariable;
108 LOG.info("Unable to find IT variable name: {} in Environment variable", ANALYTICS_SYSTEM_VARIABLE_KEY_NAME);
111 // Second look inside system properties
112 LOG.info("Looking for IT variable name: {} in System variables", ANALYTICS_SYSTEM_VARIABLE_KEY_NAME);
114 final String itSystemProperty = System.getProperty(ANALYTICS_SYSTEM_VARIABLE_KEY_NAME);
115 if (itSystemProperty != null) {
116 LOG.info("Found value for System variables: {} in System variable", itSystemProperty);
117 return itSystemProperty;
119 LOG.info("Unable to find IT variable name: {} in System variable", ANALYTICS_SYSTEM_VARIABLE_KEY_NAME);
122 // return default enviroment
123 LOG.warn("Unable to find IT environment variable. Choosing default environment: {}", DEFAULT_ENVIRONMENT);
124 return DEFAULT_ENVIRONMENT;