Added oparent to sdc main
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / config / UserCredentialsFromFile.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.ci.tests.config;
22
23 import org.openecomp.sdc.ci.tests.datatypes.UserCredentials;
24 import org.openecomp.sdc.ci.tests.utils.general.FileHandling;
25 import org.yaml.snakeyaml.Yaml;
26
27 import java.io.File;
28 import java.util.Map;
29
30 public class UserCredentialsFromFile {
31
32     private static final String CREDENTIALS_FILE = "credentials.yaml";
33     private static Map<String, UserCredentials> credentials;
34     private static Yaml yaml = new Yaml();
35
36     private static final UserCredentialsFromFile instance = new UserCredentialsFromFile();
37
38     public static UserCredentialsFromFile getInstance(){
39         return instance;
40     }
41
42     private UserCredentialsFromFile() {
43
44         credentials = null;
45
46         File credentialsFileRemote = new File(FileHandling.getBasePath() + File.separator + "conf" + File.separator + CREDENTIALS_FILE);
47 //              File credentialsFileLocal = new File(FileHandling.getConfFilesPath() + CREDENTIALS_FILE);
48         File credentialsFileLocal = new File(FileHandling.getSdcVnfsPath() + File.separator + "conf"
49                 + File.separator + CREDENTIALS_FILE);
50         File[] credentialFiles = {credentialsFileRemote, credentialsFileLocal};
51         for (File credentialsFile : credentialFiles){
52             if (credentialsFile.exists()){
53                 try {
54                     credentials = (Map<String, UserCredentials>) FileHandling.parseYamlFile(credentialsFile.getAbsolutePath());
55                 } catch (Exception e) {
56                     e.printStackTrace();
57                 }
58                 break;
59             }
60         }
61
62
63     }
64
65     public UserCredentials getUserCredentialsByRole(String userRole) {
66         Map<String, String> credentialsMap = (Map<String, String>) credentials.get(userRole);
67         UserCredentials userCredentials = new UserCredentials();
68         userCredentials.setUserId(credentialsMap.get("username"));
69         userCredentials.setFirstName(credentialsMap.get("firstname"));
70         userCredentials.setLastName(credentialsMap.get("lastname"));
71         userCredentials.setPassword(credentialsMap.get("password"));
72         return  userCredentials;
73     }
74
75 }