package org.zalando.nakadi.webservice;

import org.junit.Assert;
import org.junit.Test;
import org.zalando.nakadi.utils.EventTypeTestBuilder;
import org.zalando.nakadi.webservice.utils.NakadiTestUtils;

import static com.jayway.restassured.RestAssured.given;
import static com.jayway.restassured.http.ContentType.JSON;

public class StorageControllerAT extends BaseAT {

    @Test
    public void shouldChangeDefaultStorageWhenRequested() throws Exception {
        given()
                .body("{" +
                        "\"id\": \"default-test\"," +
                        "\"kafka_configuration\": {" +
                        "\"zookeeper_connection\":{" +
                        "\"type\": \"zookeeper\"," +
                        "\"addresses\":[{\"address\":\"zookeeper\", \"port\":2181}]" +
                        "}" +
                        "},\"storage_type\": \"kafka\"}")
                .contentType(JSON).post("/storages");

        NakadiTestUtils.createEventTypeInNakadi(EventTypeTestBuilder.builder().name("event_a").build());
        String storageId = (String) NakadiTestUtils.listTimelines("event_a").get(0).get("storage_id");
        Assert.assertEquals("default", storageId);

        given().contentType(JSON).put("/storages/default/default-test");
        NakadiTestUtils.createEventTypeInNakadi(EventTypeTestBuilder.builder().name("event_b").build());
        storageId = (String) NakadiTestUtils.listTimelines("event_b").get(0).get("storage_id");
        Assert.assertEquals("default-test", storageId);

        // cleanup
        given().contentType(JSON).put("/storages/default/default");
    }
}

@@ -48,43 +48,146 @@
 org.
-junit.Assert;%0Aimport org.junit.Test
+apache.http.HttpStatus;%0Aimport org.junit.After;%0Aimport org.junit.Assert;%0Aimport org.junit.Test;%0Aimport org.zalando.nakadi.domain.EventType
 ;%0Aim
@@ -414,16 +414,194 @@
 pe.JSON;
+%0Aimport static org.junit.Assert.fail;%0Aimport static org.zalando.nakadi.utils.TestUtils.waitFor;%0Aimport static org.zalando.nakadi.webservice.utils.NakadiTestUtils.createEventType;
 %0A%0Apublic
@@ -1220,16 +1220,91 @@
 orages%22)
+%0A                .then()%0A                .statusCode(HttpStatus.SC_CREATED)
 ;%0A%0A     
@@ -1633,130 +1633,325 @@
 st%22)
-;%0A        NakadiTestUtils.createEventTypeInNakadi(EventTypeTestBuilder.builder().name(%22event_b%22).build());%0A       
+%0A                .then()%0A                .statusCode(HttpStatus.SC_OK);%0A%0A        // change of default storage is eventually consistent and relies on zookeeper watcher being processed%0A        waitFor(() -%3E %7B%0A            try %7B%0A                final EventType et = createEventType();%0A                final String
  storage
 Id =
@@ -1938,34 +1938,32 @@
 l String storage
-Id
  = (String) Naka
@@ -1992,17 +1992,20 @@
 nes(
-%22event_b%22
+et.getName()
 ).ge
@@ -2028,32 +2028,40 @@
 e_id%22);%0A        
+     
+   
 Assert.assertEqu
@@ -2091,32 +2091,162 @@
 rage
-Id
 );%0A
-%0A        // cleanup
+            %7D catch (final Exception e) %7B%0A                fail(e.getMessage());%0A            %7D%0A        %7D);%0A    %7D%0A%0A    @After%0A    public void cleanUp() %7B
 %0A   

Fix failing test due to eventual consistency of default storage update