package info.metadude.java.library.overpass.test;

import info.metadude.java.library.overpass.ApiModule;
import info.metadude.java.library.overpass.OverpassService;
import info.metadude.java.library.overpass.models.Element;
import info.metadude.java.library.overpass.models.OverpassResponse;
import org.junit.Before;
import org.junit.Test;
import retrofit2.Call;
import retrofit2.Response;

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

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;

public class OverpassServiceTest {

    protected OverpassService streamsService;

    @Before
    public void initStreamService() {
        streamsService = ApiModule.provideOverpassService();
    }

    @Test
    public void testThatRealServerIsReachable() {
        int responseLimit = 13;
        String data = "[out:json];" +
                "node(around:1600,52.516667,13.383333)" +
                "[\"amenity\"=\"post_box\"];" +
                "out qt " + responseLimit + ";";
        Call<OverpassResponse> streamsResponseCall = streamsService.getOverpassResponse(data);

        try {
            Response<OverpassResponse> response = streamsResponseCall.execute();
            if (response.isSuccessful()) {
                OverpassResponse overpassResponse = response.body();
                // Expect at least some data
                assertThat(overpassResponse)
                        .isNotNull();
                List<Element> elements = overpassResponse.elements;
                assertThat(elements)
                        .isNotNull()
                        .isNotEmpty();
                assertThat(elements.size()).isLessThanOrEqualTo(responseLimit);
                for (Element element : elements) {
                    testElement(element);
                }
            } else {
                fail("GetOffers response is not successful.");
            }
        } catch (IOException e) {
            fail("Should not throw " + e);
        }
    }

    private void testElement(Element element) {
        assertThat(element).isNotNull();
        assertThat(element.id).isGreaterThan(0);
        assertThat(element.lat).isGreaterThan(0);
        assertThat(element.lon).isGreaterThan(0);
        assertThat(element.type).isEqualTo("node");
        assertThat(element.tags).isNotNull();
    }

}

@@ -285,16 +285,77 @@
 sponse;%0A
+import info.metadude.java.library.overpass.utils.NodesQuery;%0A
 import o
@@ -371,16 +371,16 @@
 Before;%0A
-
 import o
@@ -490,20 +490,68 @@
 va.util.
-List
+HashMap;%0Aimport java.util.List;%0Aimport java.util.Map
 ;%0A%0Aimpor
@@ -946,37 +946,138 @@
     
+Map%3C
 String
- data = %22%5Bout:json%5D;%22 +
+, String%3E tags = new HashMap%3CString, String%3E() %7B%0A            %7B%0A                put(%22amenity%22, %22post_box%22);%0A            %7D
 %0A   
@@ -1081,24 +1081,27 @@
         
+%7D;%0A
         
 %22node(ar
@@ -1096,36 +1096,113 @@
     
-%22node(around:1600,52.516667,
+NodesQuery nodesQuery = new NodesQuery(%0A                1600,%0A                52.516667,%0A                
 13.3
@@ -1210,12 +1210,9 @@
 3333
-)%22 +
+,
 %0A   
@@ -1228,67 +1228,51 @@
     
-%22%5B%5C%22amenity%5C%22=%5C%22post_box%5C%22%5D;%22 +%0A                %22out qt %22 +
+tags,%0A                true,%0A               
  res
@@ -1285,14 +1285,9 @@
 imit
- + %22;%22
+)
 ;%0A  
@@ -1372,20 +1372,67 @@
 esponse(
-data
+%0A                nodesQuery.getFormattedDataQuery()
 );%0A%0A    

Use NodesQuery in OverpassServiceTest.