summaryrefslogtreecommitdiffstats
path: root/test-apis-ci/src/main/java/org/openecomp/sdc/ci/tests/config/UserCredentialsFromFile.java
blob: 42f06f2bd33443d472399065a16bdfb8bfaccded (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
package org.openecomp.sdc.ci.tests.config;

import org.openecomp.sdc.ci.tests.datatypes.UserCredentials;
import org.openecomp.sdc.ci.tests.utils.general.FileHandling;
import org.yaml.snakeyaml.Yaml;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;

public class UserCredentialsFromFile {

    private static final String CREDENTIALS_FILE = "credentials.yaml";
    private static Map<?, ?> credentials;
    private static Yaml yaml = new Yaml();

    private static UserCredentialsFromFile credentialsFromFile;
//    private UserCredentialsFromFile() {
//
//    }

    public synchronized static UserCredentialsFromFile getInstance() {
        if (credentialsFromFile == null) {
            try {
                credentialsFromFile = new UserCredentialsFromFile();
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return credentialsFromFile;
    }

    private void UserCredentialsFromFile() throws IOException {

        credentials = null;

        File credentialsFileRemote = new File(FileHandling.getBasePath() + File.separator + "conf" + File.separator + CREDENTIALS_FILE);
//		File credentialsFileLocal = new File(FileHandling.getConfFilesPath() + CREDENTIALS_FILE);
        File credentialFile = new File(FileHandling.getSdcVnfsPath() + File.separator + "conf"
                + File.separator + CREDENTIALS_FILE);

        if (false == credentialFile.exists()) {
            throw new RuntimeException("The config file " + credentialFile + " cannot be found.");
        }


        File[] credentialFiles = {credentialsFileRemote, credentialFile};

        for (File credentialsFile : credentialFiles){
            if (credentialsFile.exists()){
                try {
                    credentials = FileHandling.parseYamlFile(credentialsFile.getAbsolutePath());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
        }



    }


    public static UserCredentials getUserCredentialsByRole(String userRole) throws Exception {
        @SuppressWarnings("unchecked")
        Map<String, String> credentialsMap = (Map<String, String>) credentials.get(userRole);
        String user = (String) credentialsMap.get("username");
        String password = (String) credentialsMap.get("password");
        String firstname = (String) credentialsMap.get("firstname");
        String lastname = (String) credentialsMap.get("lastname");

        return new UserCredentials(user, password, firstname, lastname, userRole);
    }

}