package mesosphere.marathon.client;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.hamcrest.Matcher;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import com.squareup.okhttp.mockwebserver.MockResponse;
import com.squareup.okhttp.mockwebserver.rule.MockWebServerRule;

import mesosphere.marathon.client.model.v2.App;
import mesosphere.marathon.client.model.v2.HealthCheck;
import mesosphere.marathon.client.utils.MarathonException;
import okio.Buffer;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.beans.HasPropertyWithValue.hasProperty;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsCollectionContaining.hasItem;

public class MarathonClientTest {

    @Rule
    public final MockWebServerRule server = new MockWebServerRule();

    protected Marathon marathon;

    @Before
    public void before() {
        marathon = MarathonClient.getInstance("http://localhost:" + server.getPort());
    }

    @Test
    public void testGetApp() throws IOException, MarathonException {
        server.enqueue(new MockResponse().setBody(getBufferFromResourceFile("marathon-sample-app.json")));
        App app = marathon.getApp("foo").getApp();
        assertThat(app.getInstances(), equalTo(3));
        assertThat(app.getContainer().getDocker().getImage(), equalTo("group/image"));
        assertThat(app.getHealthChecks().size(), equalTo(3));
        List<HealthCheck> healthChecks = new ArrayList<HealthCheck>(app.getHealthChecks());
        assertThat(healthChecks, (Matcher) hasItem(hasProperty("path", is("/health"))));
    }

    @Test
    public void testUpgradeStrategy() throws IOException, MarathonException {
        server.enqueue(new MockResponse().setBody(getBufferFromResourceFile("marathon-sample-app.json")));
        App app = marathon.getApp("foo").getApp();
        assertThat(app.getUpgradeStrategy().getMinimumHealthCapacity(), equalTo(0.5));
        assertThat(app.getUpgradeStrategy().getMaximumOverCapacity(), equalTo(0.2));
    }

    public Buffer getBufferFromResourceFile(String resourceFileName) throws IOException {
        return new Buffer().readFrom(this.getClass().getResourceAsStream(resourceFileName));
    }

}

@@ -556,23 +556,17 @@
 atchers.
-equalTo
+*
 ;%0Aimport
@@ -2112,24 +2112,1130 @@
 2));%0A    %7D%0A%0A
+    @Test%0A    public void testUpdateApp() throws IOException, MarathonException, InterruptedException %7B%0A        final String appId = %22/product/service/my-app%22;%0A        server.enqueue(new MockResponse().setBody(getBufferFromResourceFile(%22marathon-sample-app.json%22)));%0A        server.enqueue(new MockResponse().setResponseCode(200));%0A%0A        App app = marathon.getApp(appId).getApp();%0A        assertThat(%0A                app,%0A                allOf(hasProperty(%22id%22, is(appId)), hasProperty(%22mem%22, is(256.0)))%0A        );%0A        assertThat(%0A                server.takeRequest(),%0A                allOf(hasProperty(%22method%22, is(%22GET%22)), hasProperty(%22path%22, is(%22/v2/apps%22 + appId)))%0A        );%0A%0A        app.setMem(512.0);%0A        marathon.updateApp(appId, app, true);%0A        assertThat(%0A                server.takeRequest(),%0A                allOf(%0A                        hasProperty(%22method%22, is(%22PUT%22)),%0A                        hasProperty(%22path%22, is(%22/v2/apps%22 + appId + %22?force=true%22)),%0A                        hasProperty(%22utf8Body%22, containsString(%22%5C%22mem%5C%22: 512.0%22))%0A                )%0A        );%0A%0A    %7D%0A%0A
     public B

Test for app update.