new ui sanity docker
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / config / UserCredentialsFromFile.java
1 package org.openecomp.sdc.ci.tests.config;
2
3 import org.openecomp.sdc.ci.tests.datatypes.UserCredentials;
4 import org.openecomp.sdc.ci.tests.utils.general.FileHandling;
5 import org.yaml.snakeyaml.Yaml;
6
7 import java.io.File;
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.nio.file.Files;
11 import java.nio.file.Paths;
12 import java.util.Map;
13
14 public class UserCredentialsFromFile {
15
16     private static final String CREDENTIALS_FILE = "credentials.yaml";
17     private static Map<?, ?> credentials;
18     private static Yaml yaml = new Yaml();
19
20     private static UserCredentialsFromFile credentialsFromFile;
21 //    private UserCredentialsFromFile() {
22 //
23 //    }
24
25     public synchronized static UserCredentialsFromFile getInstance() {
26         if (credentialsFromFile == null) {
27             try {
28                 credentialsFromFile = new UserCredentialsFromFile();
29             } catch (Exception e) {
30                 e.printStackTrace();
31                 return null;
32             }
33         }
34         return credentialsFromFile;
35     }
36
37     private void UserCredentialsFromFile() throws IOException {
38
39         credentials = null;
40
41         File credentialsFileRemote = new File(FileHandling.getBasePath() + File.separator + "conf" + File.separator + CREDENTIALS_FILE);
42 //              File credentialsFileLocal = new File(FileHandling.getConfFilesPath() + CREDENTIALS_FILE);
43         File credentialFile = new File(FileHandling.getSdcVnfsPath() + File.separator + "conf"
44                 + File.separator + CREDENTIALS_FILE);
45
46         if (false == credentialFile.exists()) {
47             throw new RuntimeException("The config file " + credentialFile + " cannot be found.");
48         }
49
50
51         File[] credentialFiles = {credentialsFileRemote, credentialFile};
52
53         for (File credentialsFile : credentialFiles){
54             if (credentialsFile.exists()){
55                 try {
56                     credentials = FileHandling.parseYamlFile(credentialsFile.getAbsolutePath());
57                 } catch (Exception e) {
58                     e.printStackTrace();
59                 }
60                 break;
61             }
62         }
63
64
65
66     }
67
68
69     public static UserCredentials getUserCredentialsByRole(String userRole) throws Exception {
70         @SuppressWarnings("unchecked")
71         Map<String, String> credentialsMap = (Map<String, String>) credentials.get(userRole);
72         String user = (String) credentialsMap.get("username");
73         String password = (String) credentialsMap.get("password");
74         String firstname = (String) credentialsMap.get("firstname");
75         String lastname = (String) credentialsMap.get("lastname");
76
77         return new UserCredentials(user, password, firstname, lastname, userRole);
78     }
79
80 }