package de.knoppiks.hap.client;

import de.knoppiks.hap.client.model.Form;
import de.knoppiks.hap.client.model.Link;
import org.junit.Test;

import java.net.URI;
import java.util.Map;

import static com.cognitect.transit.TransitFactory.Format.JSON;
import static com.cognitect.transit.TransitFactory.keyword;
import static com.google.common.collect.ImmutableMap.of;
import static com.google.common.truth.Truth.assertThat;
import static java.lang.System.getProperty;
import static org.apache.http.impl.client.HttpClients.createDefault;

/**
 * This test uses <a href="https://github.com/alexanderkiel/hap-todo">https://github.com/alexanderkiel/hap-todo</a>.
 *
 * @author <a href="mailto:jwagner@knoppiks.de">Jonas Wagner</a>
 */
public class TodoIT {

    private static final HapClient CLIENT = Hap.createClient(createDefault(), JSON);
    private static final URI TEST_URL = URI.create(getProperty("test.url"));
    private static final String CREATE_ITEM = "todo/create-item";
    private static final String ITEM_STATE = "todo/item-state";

    @Test
    public void fetchServiceDocument() throws Exception {
        HapEntity entity = CLIENT.fetch(TEST_URL);

        Map<?, ?> data = (Map<?, ?>) entity.getData();
        assertThat(data).containsKey(keyword("name"));
        assertThat(data).containsKey(keyword("version"));

        assertThat(entity.getForm(keyword(CREATE_ITEM))).isPresent();
        assertThat(entity.getForm(keyword(CREATE_ITEM)).get().getTitle()).hasValue("Create Item");
    }

    @Test
    public void createAndDelete() throws Exception {
        HapEntity serviceDoc = CLIENT.fetch(TEST_URL);

        assertThat(serviceDoc.getForm(keyword(CREATE_ITEM))).isPresent();
        Form form = serviceDoc.getForm(keyword(CREATE_ITEM)).get();
        URI itemUri = CLIENT.create(RequestBuilders.create(form).put(keyword("label"), "label-222612"));

        HapEntity item = CLIENT.fetch(itemUri);
        assertThat((Map<?, ?>) item.getData()).containsEntry(keyword("label"), "label-222612");

        CLIENT.delete(itemUri);
    }

    @Test
    public void createUpdateAndDelete() throws Exception {
        HapEntity serviceDoc = CLIENT.fetch(TEST_URL);

        assertThat(serviceDoc.getForm(keyword(CREATE_ITEM))).isPresent();
        Form form = serviceDoc.getForm(keyword(CREATE_ITEM)).get();
        URI itemUri = CLIENT.create(RequestBuilders.create(form).put(keyword("label"), "label-222612"));

        HapEntity item = CLIENT.fetch(itemUri);

        assertThat(item.getLinks(keyword(ITEM_STATE))).isNotEmpty();
        Link itemStateUri = item.getLinks(keyword(ITEM_STATE)).get(0);

        HapEntity itemState = CLIENT.fetch(itemStateUri);

        CLIENT.update(itemState.updateData(of(keyword("state"), keyword("completed"))));

        CLIENT.delete(itemUri);
    }
}

@@ -110,16 +110,59 @@
 l.Link;%0A
+import de.knoppiks.hap.client.model.Query;%0A
 import o
@@ -176,16 +176,16 @@
 t.Test;%0A
-
 %0Aimport 
@@ -1079,16 +1079,136 @@
 -state%22;
+%0A    private static final String FILTER_ITEM = %22todo/filter-item%22;%0A    private static final String ITEMS = %22todo/items%22;
 %0A%0A    @T
@@ -2077,24 +2077,24 @@
 h(itemUri);%0A
-
         asse
@@ -2209,32 +2209,957 @@
 temUri);%0A    %7D%0A%0A
+    @Test%0A    public void createQueryAndDelete() throws Exception %7B%0A        HapEntity serviceDoc = CLIENT.fetch(TEST_URL);%0A        String label = %220460941b-2114-4622-8f23-a85639f39278%22;%0A%0A        assertThat(serviceDoc.getForm(keyword(CREATE_ITEM))).isPresent();%0A        Form form = serviceDoc.getForm(keyword(CREATE_ITEM)).get();%0A        URI itemUri = CLIENT.create(RequestBuilders.create(form).put(keyword(%22label%22), label));%0A%0A        assertThat(serviceDoc.getQuery(keyword(FILTER_ITEM))).isPresent();%0A        Query filter = serviceDoc.getQuery(keyword(FILTER_ITEM)).get();%0A        HapEntity filtered = CLIENT.execute(RequestBuilders.query(filter).put(keyword(%22label%22), label));%0A        assertThat(filtered.getEmbedded(keyword(ITEMS))).isNotEmpty();%0A%0A        HapEntity item = CLIENT.fetch(itemUri);%0A        assertThat((Map%3C?, ?%3E) item.getData()).containsEntry(keyword(%22label%22), label);%0A%0A        CLIENT.delete(itemUri);%0A    %7D%0A%0A
     @Test%0A    pu

Add filter query to TodoIT