/*
 * Copyright 2016 Red Hat Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.apiman.test.integration.ui.tests.clients;

import static io.apiman.test.integration.ui.support.selenide.SelenideUtils.open;

import static com.codeborne.selenide.Condition.visible;
import static org.junit.Assert.assertEquals;

import io.apiman.test.integration.base.AbstractApiTest;
import io.apiman.test.integration.base.clients.AbstractBreakContractTest;
import io.apiman.test.integration.runner.ApimanRunner;
import io.apiman.test.integration.ui.support.selenide.pages.clients.detail.ClientContractsDetailPage;
import io.apiman.manager.api.beans.clients.ClientVersionBean;
import io.apiman.manager.api.beans.summary.ContractSummaryBean;

import java.util.List;

import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * @author jkaspar
 */
@RunWith(ApimanRunner.class)
public class BreakContractIT extends AbstractBreakContractTest {

    private static ClientContractsDetailPage page;

    private static void openPage(ClientVersionBean clientVersion) {
        page = open(ClientContractsDetailPage.class,
            clientVersion.getClient().getOrganization().getId(),
            clientVersion.getClient().getId(),
            clientVersion.getVersion());
    }

    @Test
    public void canBreakContract() throws Exception {
        openPage(unregisteredClientVersion);
        page.breakContract(secondApiVersion.getVersion());

        getContract(unregisteredContractBean, expectedStatusCode(404));
    }

    @Test
    public void shouldRemoveOnlySingleContract() throws Exception {
        List<ContractSummaryBean> contractsBefore = getContracts(unregisteredClientVersion);

        openPage(unregisteredClientVersion);
        page.breakContract(secondApiVersion.getVersion());

        List<ContractSummaryBean> contractsAfter = getContracts(unregisteredClientVersion);
        assertEquals("Unexpected number of contracts", contractsBefore.size() - 1, contractsAfter.size());
    }

    @Test
    public void canBreakAllContracts() throws Exception {
        openPage(unregisteredClientVersion);
        page.breakAllContracts();

        List<ContractSummaryBean> contracts = getContracts(unregisteredClientVersion);
        assertEquals("Unexpected number of contracts", 0, contracts.size());
    }

    // TODO: Should we remove this test?
    @Test
    @Ignore
    public void canNotBreakContractOnRegisteredClientVersion() throws Exception {
        openPage(registeredClientVersion);
        page.breakContractButton(AbstractApiTest.api.getName()).shouldNotBe(visible);
    }

    // TODO: Should we remove this test?
    @Test
    @Ignore
    public void canNotBreakAllContractsOnRegisteredClientVersion() throws Exception {
        openPage(registeredClientVersion);
        page.breakAllContractsButton().shouldNotBe(visible);
    }
}

@@ -866,28 +866,23 @@
 ion.
-base.AbstractApiT
+SuiteProperti
 es
-t
 ;%0Aim
@@ -922,37 +922,19 @@
 ase.
-clients.AbstractBreakContract
+AbstractApi
 Test
@@ -973,27 +973,66 @@
 ion.
-runner.ApimanRunner
+ui.support.selenide.base.clients.AbstractBreakContractTest
 ;%0Aim
@@ -1280,16 +1280,54 @@
 il.List;
+%0Aimport java.util.concurrent.TimeUnit;
 %0A%0Aimport
@@ -1372,97 +1372,35 @@
 st;%0A
-import org.junit.runner.RunWith;%0A%0A/**%0A * @author jkaspar%0A */%0A@RunWith(ApimanRunner.class)
+%0A/**%0A * @author jkaspar%0A */
 %0Apub
@@ -2670,24 +2670,123 @@
 ntracts();%0A%0A
+        TimeUnit.SECONDS.sleep(Integer.valueOf(SuiteProperties.getProperty(%22apiman.test.delay%22)));%0A
         List

Add delay before contract asssertion in BreakContractIT