#!/bin/bash

source /var/onap_tests/_test_base
source /var/onap/functions

covered_functions=(
"create_configuration_files" "clone_repo" "install_dev_tools"
"configure_bind" "install_java" "install_maven" "install_nodejs" "install_python"
"install_docker" "pull_docker_image" "install_docker_compose" "configure_service"
"start_ODL" "compile_src" "build_docker_image"
)

# TODO(electrocucaracha): Remove/Modify functions that doesn't support proxy settings
if [ -z $http_proxy ] & [ -z $https_proxy ]; then
    covered_functions=(${covered_functions[@]} "docker_openecomp_login" "pull_openecomp_image")
fi

# test_create_configuration_files() - Verify the creation of a configuration files
function test_create_configuration_files {
    create_configuration_files

    asserts_file_exist /opt/config/nexus_docker_repo.txt
    asserts_file_exist /opt/config/nexus_username.txt
    asserts_file_exist /opt/config/nexus_password.txt
    asserts_file_exist /opt/config/openstack_username.txt
    asserts_file_exist /opt/config/tenant_id.txt
    asserts_file_exist /opt/config/dmaap_topic.txt
    asserts_file_exist /opt/config/docker_version.txt
}

# test_docker_openecomp_login() - Verify the proper login to OpenECOMP Docker Hub
function test_docker_openecomp_login {
    docker_openecomp_login
}

# test_pull_openecomp_image() - Verify the addition of a OpenECOMP container image
function test_pull_openecomp_image {
    pull_openecomp_image portalapps ep:1610-1

    asserts_installed_package docker-ce
    asserts_image ep:1610-1

    docker rmi -f ep:1610-1
}

# test_clone_repo() - Verify cloning and pulling source code from repositories
function test_clone_repo {
    clone_repo demo

    asserts_installed_package git
    asserts_file_exist $git_src_folder/demo/LICENSE.TXT
}

# test_install_dev_tools() - Verify the correct installation of developer tools
function test_install_dev_tools {
    install_dev_tools

    asserts_installed_package apt-transport-https
    asserts_installed_package ca-certificates
    asserts_installed_package curl
}

# test_configure_bind() - Verify the correct installation and configuration of bind
function test_configure_bind {
    configure_bind

    asserts_installed_package bind9
    asserts_installed_package bind9utils
    asserts_file_exist /etc/bind/zones/db.simpledemo.openecomp.org
    asserts_file_exist /etc/bind/named.conf.options
    asserts_file_exist /etc/bind/named.conf.local

    rm -rf /etc/bind/
}

# test_install_java() - Verify the correct installation of java
function test_install_java {
    install_java

    asserts_installed_package openjdk-8-jdk
}

# test_install_maven() - Verify the correct installation and configuration of maven
function test_install_maven {
    install_maven

    asserts_installed_package maven3
    asserts_installed_package openjdk-8-jdk
    asserts_file_exist $mvn_conf_file
}

# test_install_nodejs() - Verify the correct installation of NodeJS tools
function test_install_nodejs {
    install_nodejs

    asserts_installed_package nodejs
    asserts_file_exist /usr/bin/npm
}

# test_install_python() - Verify the correct installation of Python
function test_install_python {
    install_python
    asserts_installed_package python2.7
    asserts_installed_package python-dev
    asserts_file_exist /usr/local/bin/tox
}

# test_install_docker() - Verify the correct installation of Docker
function test_install_docker {
    install_docker

    asserts_installed_package docker-ce
}

# test_pull_docker_image() - Verify the correct retrieve of a specific docker image
function test_pull_docker_image {
    local image=attos/dmaap
    pull_docker_image $image

    asserts_image $image

    docker rmi -f $image
}

# test_install_docker_compose() - Verify the correct installation of Docker Compose tool
function test_install_docker_compose {
    install_docker_compose

    asserts_file_exist /opt/docker/docker-compose
}

# test_configure_service() - Verify the correct configuration of a specific init service
function test_configure_service {
    local service=mso

    configure_service $service

    asserts_file_exist /etc/init.d/$service

    rm -rf /etc/init.d/$service
}

# test_start_ODL() - Verify the installation and configuration of OpenDayLight controller
function test_start_ODL {
    start_ODL

    asserts_file_exist /opt/opendaylight/current/bin/start
}

# test_compile_src() - Verify the compilation of java code using maven tools
function test_compile_src {
    local repo=vid/asdcclient
    clone_repo $repo
    compile_src $git_src_folder/$repo

    asserts_file_exist $git_src_folder/$repo/target/asdcclient-1.0.2-SNAPSHOT.jar
}

# test_build_docker_image() - Verify that a docker image is created from source code
function test_build_docker_image {
    clone_repo portal
    compile_src $git_src_folder/portal/ecomp-portal-widget-ms
    build_docker_image $git_src_folder/portal/ecomp-portal-widget-ms

    asserts_image widget-ms

    docker rmi -f widget-ms
}

if [ "$1" != '*' ]; then
    unset covered_functions
    covered_functions=$1
fi
main "${covered_functions[@]}"