import time
from pnc_cli import utils
from pnc_cli.swagger_client.apis.buildconfigurations_api import BuildconfigurationsApi
from pnc_cli.swagger_client.apis.runningbuildrecords_api import RunningbuildrecordsApi

configs_api = BuildconfigurationsApi(utils.get_api_client())
running_api = RunningbuildrecordsApi(utils.get_api_client())


def test_get_all():
    # start a build so that a build is running
    # need to run a legitimate build, create a buildconfiguration that will start running
    configs_api.trigger(id=1)
    running_builds = running_api.get_all(page_size=1000000).content
    time.sleep(5)
    assert running_builds is not None


def test_get_specific():
    #same as above
    configs_api.trigger(id=1)
    running_build = running_api.get_specific(id=1)
    time.sleep(5)
    assert running_build is not None

import time

import pytest

from pnc_cli import buildconfigurations
from pnc_cli import utils
from test import testutils
from pnc_cli.swagger_client.apis.buildconfigurations_api import BuildconfigurationsApi
from pnc_cli.swagger_client.apis.runningbuildrecords_api import RunningbuildrecordsApi


configs_api = BuildconfigurationsApi(utils.get_api_client())
running_api = RunningbuildrecordsApi(utils.get_api_client())


@pytest.fixture(scope='function')
def new_config(request):
    created_bc = configs_api.create_new(
        body=buildconfigurations.create_build_conf_object(name=testutils.gen_random_name(),
                                                          project_id=1,
                                                          environment_id=1,
                                                          build_status="UNKNOWN",
                                                          build_script='mvn clean install',
                                                          product_version_ids=[1],
                                                          scm_repo_url='https://github.com/thauser/simple-maven-build-pnc.git')).content

    def teardown():
        existing = [x.id for x in configs_api.get_all(page_size=10000000).content]
        if created_bc.id in existing:
            configs_api.delete_specific(id=created_bc.id)

    request.addfinalizer(teardown)
    return created_bc


def test_get_all(new_config):
    # start a build so that a build is running
    # need to run a legitimate build, create a buildconfiguration that will start running
    configs_api.trigger(id=new_config.id, rebuild_all=False)
    running_builds = running_api.get_all(page_size=1000).content
    assert running_builds is not None


def test_get_specific(new_config):
    # same as above
    triggered_build = configs_api.trigger(id=new_config.id).content
    running_build = running_api.get_specific(id=triggered_build.id)
    assert running_build is not None

fix runningbuilds API tests to pass.