/*
 * Copyright 2005-2014 Red Hat, Inc.
 *
 * Red Hat licenses this file to you 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.fabric8.arquillian.server.mock;

import io.fabric8.arquillian.ResourceInjection;
import io.fabric8.arquillian.kubernetes.Constants;
import io.fabric8.kubernetes.api.model.NamespaceBuilder;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodBuilder;
import io.fabric8.kubernetes.api.model.PodListBuilder;
import io.fabric8.kubernetes.api.model.ReplicationControllerListBuilder;
import io.fabric8.kubernetes.api.model.ServiceListBuilder;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.openshift.server.mock.OpenShiftMockServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;

import java.io.IOException;


@RunWith(Suite.class)
@Suite.SuiteClasses(ResourceInjection.class)
public class ServerMockTestSuite {

    private static final OpenShiftMockServer MOCK = new OpenShiftMockServer();

    @BeforeClass
    public static void setUpClass() throws IOException {
        MOCK.expectAndReturnAsJson("/api/v1/namespaces/arquillian", 200, new NamespaceBuilder()
                .withNewMetadata()
                .withName("arquillian")
                .and().build());

        MOCK.expectAndReturnAsJson("/api/v1/namespaces/arquillian/replicationcontrollers", 200, new ReplicationControllerListBuilder()
                .addNewItem()
                .withNewMetadata()
                .withName("repl1")
                .endMetadata()
                .endItem()
                .build());


        MOCK.expectAndReturnAsJson("/api/v1/namespaces/arquillian/pods", 200, new PodListBuilder().addNewItem()
                .withNewMetadata()
                .withName("pod1")
                .endMetadata()
                .endItem()
                .build());

        MOCK.expectAndReturnAsJson("/api/v1/namespaces/arquillian/services", 200, new ServiceListBuilder()
                .addNewItem()
                .withNewMetadata()
                .withName("service1")
                .endMetadata()
                .endItem()
                .build());


        MOCK.init();

        String masterUrl = "http://localhost:" + MOCK.getServer().getPort();
        System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, masterUrl);
        System.setProperty(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY, "arquillian");
        System.setProperty(Constants.NAMESPACE_TO_USE, "arquillian");
        System.setProperty(Constants.NAMESPACE_LAZY_CREATE_ENABLED, "arquillian");
    }

    @AfterClass
    public static void tearDownClass() throws IOException {
        MOCK.getServer().shutdown();
    }
}

@@ -1636,39 +1636,35 @@
  MOCK.expect
-AndReturnAsJson
+().withPath
 (%22/api/v1/na
@@ -1683,18 +1683,28 @@
 uillian%22
-, 
+).andReturn(
 200, new
@@ -1821,32 +1821,41 @@
  .and().build())
+.always()
 ;%0A%0A        MOCK.
@@ -1856,39 +1856,35 @@
  MOCK.expect
-AndReturnAsJson
+().withPath
 (%22/api/v1/na
@@ -1926,18 +1926,28 @@
 rollers%22
-, 
+).andReturn(
 200, new
@@ -2157,32 +2157,41 @@
        .build())
+.always()
 ;%0A%0A%0A        MOCK
@@ -2193,39 +2193,35 @@
  MOCK.expect
-AndReturnAsJson
+().withPath
 (%22/api/v1/na
@@ -2245,18 +2245,28 @@
 an/pods%22
-, 
+).andReturn(
 200, new
@@ -2448,16 +2448,25 @@
 build())
+.always()
 ;%0A%0A     
@@ -2483,23 +2483,19 @@
 pect
-AndReturnAsJson
+().withPath
 (%22/a
@@ -2531,18 +2531,28 @@
 ervices%22
-, 
+).andReturn(
 200, new
@@ -2759,16 +2759,25 @@
 build())
+.always()
 ;%0A%0A%0A    

Align with latest client release.