package com.telekom.m2m.cot.restsdk.util;

import com.google.gson.JsonObject;
import com.telekom.m2m.cot.restsdk.CloudOfThingsPlatform;
import com.telekom.m2m.cot.restsdk.inventory.ManagedObject;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Properties;

/**
 * Created by Patrick Steinert on 31.01.16.
 */
public class TestHelper {
    public static String TEST_HOST = "";
    public static String TEST_USERNAME = "";
    public static String TEST_PASSWORD = "";
    public static String TEST_TENANT = "";

    private static final SecureRandom random = new SecureRandom();

    static {
        try {
            getPropValues();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static void getPropValues() throws IOException {
        InputStream inputStream;

        try {
            Properties prop = new Properties();
            String propFileName = "test.properties";

            inputStream = TestHelper.class.getClassLoader().getResourceAsStream(propFileName);

            if (inputStream != null) {
                prop.load(inputStream);
            } else {
                throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
            }


            // get the property value and print it out
            TEST_HOST = prop.getProperty("cot.connection.host");
            TEST_USERNAME = prop.getProperty("cot.connection.user");
            TEST_PASSWORD = prop.getProperty("cot.connection.password");
            TEST_TENANT = prop.getProperty("cot.connection.tenant");

            inputStream.close();
        } catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }

    public static String getRandom(int length) {
        return new BigInteger(length * 10, random).toString(length);
    }

    public static ManagedObject createManagedObject(String name) {
        return createManagedObject(name, true);
    }

    public static ManagedObject createManagedObject(String name, boolean isAgent) {
        ManagedObject mo = new ManagedObject();
        mo.setName(name);
        mo.set("c8y_IsDevice", new JsonObject());
        if (isAgent) {
            mo.set("com_cumulocity_model_Agent", new JsonObject());
        }
        return mo;
    }

    public static ManagedObject createRandomManagedObjectInPlatform(CloudOfThingsPlatform cloudOfThingsPlatform, ManagedObject managedObject) {
        return cloudOfThingsPlatform.getInventoryApi().create(managedObject);
    }

    public static ManagedObject createRandomManagedObjectInPlatform(CloudOfThingsPlatform cloudOfThingsPlatform, String name) {
        return cloudOfThingsPlatform.getInventoryApi().create(createManagedObject(name));
    }

    public static void deleteManagedObjectInPlatform(CloudOfThingsPlatform cloudOfThingsPlatform, ManagedObject managedObject) {
        cloudOfThingsPlatform.getInventoryApi().delete(managedObject.getId());
    }

    public static void registerAsChildDevice(CloudOfThingsPlatform cloudOfThingsPlatform, ManagedObject parentDevice, ManagedObject childDevice) {
        cloudOfThingsPlatform.getInventoryApi().registerAsChildDevice(
                parentDevice, childDevice
        );
    }

}

@@ -194,102 +194,8 @@
 t;%0A%0A
-import java.io.FileNotFoundException;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0A
 impo
@@ -219,16 +219,16 @@
 nteger;%0A
+
 import j
@@ -257,37 +257,8 @@
 dom;
-%0Aimport java.util.Properties;
 %0A%0A/*
@@ -768,964 +768,281 @@
 s() 
-throws IOException %7B%0A        InputStream inputStream;%0A%0A        try %7B%0A            Properties prop = new Properties();%0A            String propFileName = %22test.properties%22;%0A%0A            inputStream = TestHelper.class.getClassLoader().getResourceAsStream(propFileName);%0A%0A            if (inputStream != null) %7B%0A                prop.load(inputStream);%0A            %7D else %7B%0A                throw new FileNotFoundException(%22property file '%22 + propFileName + %22' not found in the classpath%22);%0A            %7D%0A%0A%0A            // get the property value and print it out%0A            TEST_HOST = prop.getProperty(%22cot.connection.host%22);%0A            TEST_USERNAME = prop.getProperty(%22cot.connection.user%22);%0A            TEST_PASSWORD = prop.getProperty(%22cot.connection.password%22);%0A            TEST_TENANT = prop.getProperty(%22cot.connection.tenant%22);%0A%0A            inputStream.close();%0A        %7D catch (Exception e) %7B%0A            System.out.println(%22Exception: %22 + e);%0A        %7D
+%7B%0A        TEST_HOST = readFromEnvironment(%22COT_CONNECTION_HOST%22);%0A        TEST_USERNAME = readFromEnvironment(%22COT_CONNECTION_USER%22);%0A        TEST_PASSWORD = readFromEnvironment(%22COT_CONNECTION_PASSWORD%22);%0A        TEST_TENANT = readFromEnvironment(%22COT_CONNECTION_TENANT%22);
 %0A   
@@ -2574,10 +2574,656 @@
 %0A    %7D%0A%0A
+    /**%0A     * Accepts an environment variable name and returns its value.%0A     *%0A     * Fails if the requested variable is not available.%0A     *%0A     * Example:%0A     *%0A     *     String host = readFromEnvironment(%22COT_CONNECTION_HOST%22);%0A     *%0A     * @param name The name of the variable.%0A     * @return The variable value.%0A     */%0A    private static String readFromEnvironment(final String name)%0A    %7B%0A        String value = System.getenv(name);%0A        if (value == null) %7B%0A            throw new RuntimeException(%22Cannot read test configuration: No value found for environment variable '%22 + name + %22'.%22);%0A        %7D%0A        return value;%0A    %7D%0A
 %7D%0A

read integration test configuration from environment variables