package com.afrozaar.wordpress.wpapi.v2;

import static org.assertj.core.api.Assertions.assertThat;

import com.afrozaar.wordpress.wpapi.v2.model.Post;
import com.afrozaar.wordpress.wpapi.v2.request.SearchRequest;
import com.afrozaar.wordpress.wpapi.v2.util.ClientConfig;
import com.afrozaar.wordpress.wpapi.v2.util.ClientFactory;

import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author johan
 */
public class ClientLiveTest {

    private static final Logger LOG = LoggerFactory.getLogger(ClientLiveTest.class);
    static String yamlConfig;
    static ClientConfig clientConfig;

    @BeforeClass
    public static void init() throws UnknownHostException {
        yamlConfig = String.format("/config/%s-test.yaml", InetAddress.getLocalHost().getHostName());
        clientConfig = ClientConfig.load(ClientConfig.class.getResourceAsStream(yamlConfig));
    }

    @Test
    public void posts() {
        final Client client = ClientFactory.fromConfig(clientConfig);

        final String EXPECTED = String.format("%s%s/posts", clientConfig.getWordpress().getBaseUrl(), Client.CONTEXT);

        final PagedResponse<Post> postPagedResponse = client.fetchPosts(SearchRequest.posts());

        postPagedResponse.debug();

        assertThat(postPagedResponse.hasNext()).isTrue();
        assertThat(postPagedResponse.getPrevious().isPresent()).isFalse();
        assertThat(postPagedResponse.getSelf()).isEqualTo(EXPECTED);

        PagedResponse<Post> next = client.get(postPagedResponse, resp -> resp.getNext().get());

        next.debug();

        while (next.hasNext()) {
            next = client.get(next, response -> response.getNext().get());
            next.debug();
        }

    }

    @Test
    public void testGetPost() {
        final Client client = ClientFactory.fromConfig(clientConfig);

        final Post post = client.getPost(3629);

        assertThat(post).isNotNull();

        LOG.debug("post = {}", post);
    }

    @Test
    public void searchWithFilterParametersForInvalidAuthor_shouldReturnEmptyList() {
        final Client client = ClientFactory.fromConfig(clientConfig);

        SearchRequest search = SearchRequest.Builder.aSearchRequest().withParam("filter[author]", "999").build();

        final PagedResponse<Post> postPagedResponse = client.fetchPosts(search);

        assertThat(postPagedResponse.getList()).isEmpty();
    }

    @Test
    public void searchWithFilterParametersForValidAuthor_shouldReturnPopulatedList() {

    }
}

@@ -645,16 +645,17 @@
 class);%0A
+%0A
     stat
@@ -713,16 +713,42 @@
 tConfig;
+%0A    static Client client;
 %0A%0A    @B
@@ -1022,67 +1022,11 @@
     
-%7D%0A%0A    @Test%0A    public void posts() %7B%0A        final Client
+   
  cli
@@ -1063,32 +1063,74 @@
 (clientConfig);%0A
+    %7D%0A%0A    @Test%0A    public void posts() %7B
 %0A        final S
@@ -1887,78 +1887,8 @@
 () %7B
-%0A        final Client client = ClientFactory.fromConfig(clientConfig);
 %0A%0A  
@@ -2109,32 +2109,33 @@
 ptyList() %7B%0A
+%0A
         
 final Client
@@ -2126,70 +2126,16 @@
     
-final Client client = ClientFactory.fromConfig(clientConfig);%0A
+// given
 %0A   
@@ -2242,24 +2242,40 @@
 ).build();%0A%0A
+        // when%0A
         fina
@@ -2340,24 +2340,40 @@
 s(search);%0A%0A
+        // then%0A
         asse
@@ -2519,21 +2519,326 @@
 tedList() %7B%0A
+        // given%0A        SearchRequest search = SearchRequest.Builder.aSearchRequest().withParam(%22filter%5Bauthor%5D%22, %221%22).build();%0A%0A        // when%0A        final PagedResponse%3CPost%3E postPagedResponse = client.fetchPosts(search);%0A%0A        // then%0A        assertThat(postPagedResponse.getList()).isNotEmpty();
 %0A    %7D%0A%7D%0A

Add test to search for known author's posts.