All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.duracloud.unittestdb.util.StorageAccountTestUtil Maven / Gradle / Ivy

There is a newer version: 3.7.4
Show newest version
/*
 * The contents of this file are subject to the license and copyright
 * detailed in the LICENSE and NOTICE files at the root of the source
 * tree and available online at
 *
 *     http://duracloud.org/license/
 */
package org.duracloud.unittestdb.util;

import org.duracloud.common.model.Credential;
import org.duracloud.common.model.DuraCloudUserType;
import org.duracloud.common.util.EncryptionUtil;
import org.duracloud.common.web.RestHttpHelper;
import org.duracloud.common.web.RestHttpHelper.HttpResponse;
import org.duracloud.storage.domain.StorageProviderType;
import org.duracloud.unittestdb.UnitTestDatabaseUtil;
import org.duracloud.unittestdb.domain.ResourceType;

/**
 * Provides utilities for testing with storage accounts.
 *
 * @author Bill Branan
 */
public class StorageAccountTestUtil {

    private Credential rootCredential;

    public HttpResponse initializeDurastore(String host, String port, String context)
        throws Exception {
        String baseURL = "http://" + host + ":" + port + "/" + context;
        String storesURL =  baseURL + "/stores";

        String xml = buildTestAccountXml();
        RestHttpHelper restHelper = new RestHttpHelper(getRootCredential());
        return restHelper.post(storesURL, xml, null);
    }

    private String buildTestAccountXml() throws Exception {
        StringBuilder xml = new StringBuilder();
        xml.append("");

        UnitTestDatabaseUtil dbUtil = new UnitTestDatabaseUtil();
        int acctId = 0;
        for(StorageProviderType type : StorageProviderType.values()) {
            Credential cred = null;
            try {
                cred = dbUtil.findCredentialForResource(ResourceType.fromStorageProviderType(
                    type));
            } catch (Exception e) {
                if(type.equals(StorageProviderType.TEST_RETRY) ||
                   type.equals(StorageProviderType.TEST_VERIFY_CREATE) ||
                   type.equals(StorageProviderType.TEST_VERIFY_DELETE)) {
                    cred = new Credential("", "");
                } else {
                    // No credentials available for provider type - skip
                    continue;
                }
            }
            if(cred != null) {
                ++acctId;
                EncryptionUtil encryptUtil = new EncryptionUtil();
                String encUsername = encryptUtil.encrypt(cred.getUsername());
                String encPassword = encryptUtil.encrypt(cred.getPassword());

                xml.append("");
                xml.append(""+acctId+"");
                xml.append("");
                xml.append(type.name());
                xml.append("");
                xml.append("");
                xml.append(""+encUsername+"");
                xml.append(""+encPassword+"");
                xml.append("");
                xml.append("");
            }
        }

        xml.append("");
        return xml.toString();
    }

    public Credential getRootCredential() throws Exception {
        if (null == rootCredential) {
            UnitTestDatabaseUtil dbUtil = new UnitTestDatabaseUtil();
            ResourceType rootUser = ResourceType.fromDuraCloudUserType(
                DuraCloudUserType.ROOT);
            rootCredential = dbUtil.findCredentialForResource(rootUser);
        }
        return rootCredential;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy