aboutsummaryrefslogtreecommitdiffstats
path: root/dcae-analytics-it/src/test/java/org/openecomp/dcae/apod/analytics/it/module/IntegrationTestModule.java
blob: c1f8a4e3725e440774070a54f8014c4f39c6b274 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * ===============================LICENSE_START======================================
 *  dcae-analytics
 * ================================================================================
 *    Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *          http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  ============================LICENSE_END===========================================
 */

package org.openecomp.dcae.apod.analytics.it.module;

import com.google.inject.Binder;
import com.google.inject.Module;
import com.google.inject.name.Names;
import org.openecomp.dcae.apod.analytics.common.exception.DCAEAnalyticsRuntimeException;
import org.openecomp.dcae.apod.analytics.it.dmaap.DMaaPMRCreator;
import org.openecomp.dcae.apod.analytics.it.dmaap.DMaaPMRCreatorImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;

/**
 * @author Rajiv Singla . Creation Date: 2/1/2017.
 */
public class IntegrationTestModule implements Module {

    private static final Logger LOG = LoggerFactory.getLogger(IntegrationTestModule.class);

    public static final String ANALYTICS_SYSTEM_VARIABLE_KEY_NAME = "analytics.it.env";
    public static final String DEFAULT_ENVIRONMENT = "dev";
    public static final String ENVIRONMENT_PROPERTIES_FILE_LOCATION = "env";

    @Override
    public void configure(Binder binder) {
        final Properties envProperties = loadPropertiesFile();
        Names.bindProperties(binder, envProperties);
        binder.bind(DMaaPMRCreator.class).to(DMaaPMRCreatorImpl.class);
    }


    /**
     * Load environment specific properties file
     *
     * @return environment properties
     */
    private Properties loadPropertiesFile() {
        final String currentEnvironment = getCurrentEnvironment().toLowerCase();
        final String envPropertiesFileName = currentEnvironment + ".properties";
        final Properties envProperties = new Properties();
        final String fileLocation = ENVIRONMENT_PROPERTIES_FILE_LOCATION + "/" + envPropertiesFileName;
        LOG.info("===>>> EFFECTIVE ENV: {}, EFFECTIVE PROPERTIES FILE: {} <<<====", currentEnvironment, fileLocation);
        try {
            final InputStream propertiesFileInputStream =
                    IntegrationTestModule.class.getClassLoader().getResourceAsStream(fileLocation);
            envProperties.load(propertiesFileInputStream);
        } catch (FileNotFoundException e) {
            final String errorMessage = String.format("Unable to find env properties file: %s.", fileLocation);
            throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e);
        } catch (IOException e) {
            final String errorMessage = String.format("I/O Exception during loading env properties file: %s",
                    fileLocation);
            throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e);
        }

        final Properties systemProperties = System.getProperties();
        for (Object envProperty : envProperties.keySet()) {
            final String systemPropertyValue = systemProperties.getProperty(envProperty.toString());
            if (systemPropertyValue !=  null) {
                LOG.info("Overriding System property name: {} with env property value: {}",
                        envProperty.toString(), systemPropertyValue);
                envProperties.setProperty(envProperty.toString(), systemPropertyValue);
            }
        }

        LOG.info("Printing Effective Environment Properties =============== >>>");
        for (Map.Entry<Object, Object> envPropertyEntry : envProperties.entrySet()) {
            LOG.info("{}={}", envPropertyEntry.getKey(), envPropertyEntry.getValue());
        }

        return envProperties;
    }


    private static String getCurrentEnvironment() {
        // First look in environment variables
        LOG.info("Looking for IT variable name: {} in Environment variables", ANALYTICS_SYSTEM_VARIABLE_KEY_NAME);
        final String itEnvironmentVariable = System.getenv(ANALYTICS_SYSTEM_VARIABLE_KEY_NAME);
        if (itEnvironmentVariable != null) {
            LOG.info("Found value in Environment variables: {} for IT Environment variable", itEnvironmentVariable);
            return itEnvironmentVariable;
        } else {
            LOG.info("Unable to find IT variable name: {} in Environment variable", ANALYTICS_SYSTEM_VARIABLE_KEY_NAME);
        }

        // Second look inside system properties
        LOG.info("Looking for IT variable name: {} in System variables", ANALYTICS_SYSTEM_VARIABLE_KEY_NAME);

        final String itSystemProperty = System.getProperty(ANALYTICS_SYSTEM_VARIABLE_KEY_NAME);
        if (itSystemProperty != null) {
            LOG.info("Found value for System variables: {} in System variable", itSystemProperty);
            return itSystemProperty;
        } else {
            LOG.info("Unable to find IT variable name: {} in System variable", ANALYTICS_SYSTEM_VARIABLE_KEY_NAME);
        }

        // return default enviroment
        LOG.warn("Unable to find IT environment variable. Choosing default environment: {}", DEFAULT_ENVIRONMENT);
        return DEFAULT_ENVIRONMENT;
    }
}