package de.ddb.pdc;

import de.ddb.pdc.metadata.DdbApiUrls;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.TestRestTemplate;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.web.client.RestTemplate;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Main.class)
@WebAppConfiguration
@IntegrationTest("server.port:0")
public class PdcIntegrationTest {

  @Value("http://localhost:${local.server.port}")
  private String urlPrefix;

  @Value("${ddb.apikey}")
  private String dbbApiKey;

  @Autowired
  private RestTemplate serverRestTemplate;
  private MockRestServiceServer mockDdbApi;

  @Before
  public void setUp() {
    mockDdbApi = MockRestServiceServer.createServer(serverRestTemplate);
  }

  @Test
  public void search() throws Exception {
    String ddbUrl = DdbApiUrls.searchUrl("goethe", 3, dbbApiKey);
    String ddbResponse = loadJsonReponse("/ddb_search/goethe3");
    mockDdbApi.expect(requestTo(ddbUrl))
        .andRespond(withSuccess(ddbResponse, MediaType.APPLICATION_JSON));

    RestTemplate client = new TestRestTemplate();
    String url = urlPrefix + "/search?q=goethe&max=3";
    List results = client.getForObject(url, List.class);

    assertEquals(3, results.size());

    Map result1 = (Map) results.get(0);
    assertEquals("TNPFDKO2VDGBZ72RWC6RKDNZYZQZP3XK", result1.get("id"));
    assertEquals("Goethe", result1.get("title"));
    assertEquals("Druckgraphik", result1.get("subtitle"));
    assertEquals(
        "https://www.deutsche-digitale-bibliothek.de/binary/TNPFDKO2VDGBZ72RWC6RKDNZYZQZP3XK/list/1.jpg",
        result1.get("imageUrl"));

    Map result2 = (Map) results.get(1);
    assertEquals("ILL7O3BSG7ZXIZMX6RT23AR7FDT3NUPK", result2.get("id"));
    assertEquals("Goethe", result2.get("title"));
    assertEquals("Zeichnung", result2.get("subtitle"));
    assertEquals(
        "https://www.deutsche-digitale-bibliothek.de/binary/ILL7O3BSG7ZXIZMX6RT23AR7FDT3NUPK/list/1.jpg",
        result2.get("imageUrl"));

    Map result3 = (Map) results.get(2);
    assertEquals("4XUDMRWXBMC7O3FPLATAK2HLCMLBBO22", result3.get("id"));
    assertEquals("Cornelie Goethe", result3.get("title"));
    assertEquals("Druckgraphik", result3.get("subtitle"));
    assertEquals(
        "https://www.deutsche-digitale-bibliothek.de/binary/4XUDMRWXBMC7O3FPLATAK2HLCMLBBO22/list/1.jpg",
        result3.get("imageUrl"));
  }

  private String loadJsonReponse(String path) throws Exception {
    InputStream stream = getClass().getResourceAsStream(path + ".json");
    InputStreamReader streamReader = new InputStreamReader(stream);
    try (BufferedReader bufferedReader = new BufferedReader(streamReader)) {
      return bufferedReader.readLine();
    }
  }
}

@@ -78,16 +78,41 @@
 Before;%0A
+import org.junit.Ignore;%0A
 import o
@@ -122,24 +122,24 @@
 junit.Test;%0A
-
 import org.j
@@ -1550,16 +1550,100 @@
 dbApi;%0A%0A
+  private final String DDB_DOMAIN = %22https://www.deutsche-digitale-bibliothek.de%22;%0A%0A
   @Befor
@@ -2241,24 +2241,67 @@
 s.size());%0A%0A
+    String totalUrl;%0A    String urlSearch;%0A
     Map resu
@@ -2518,74 +2518,21 @@
     
-assertEquals(%0A        %22https://www.deutsche-digitale-bibliothek.de
+urlSearch = %22
 /bin
@@ -2575,33 +2575,91 @@
 /list/1.jpg%22
-,
+;
 %0A    
-   
+totalUrl = DDB_DOMAIN + urlSearch;%0A    assertEquals(totalUrl,
  result1.get
@@ -2901,74 +2901,21 @@
     
-assertEquals(%0A        %22https://www.deutsche-digitale-bibliothek.de
+urlSearch = %22
 /bin
@@ -2958,33 +2958,91 @@
 /list/1.jpg%22
-,
+;
 %0A    
-   
+totalUrl = DDB_DOMAIN + urlSearch;%0A    assertEquals(totalUrl,
  result2.get
@@ -3296,74 +3296,22 @@
     
-assertEquals(%0A        %22https://www.deutsche-digitale-bibliothek.de
+urlSearch =  %22
 /bin
@@ -3362,17 +3362,75 @@
 jpg%22
-,
+;
 %0A    
-   
+totalUrl = DDB_DOMAIN + urlSearch;%0A    assertEquals(totalUrl,
  res
@@ -3776,24 +3776,24 @@
 readLine();%0A
-
     %7D%0A  %7D%0A%7D%0A
@@ -3790,10 +3790,406 @@
   %7D%0A  %7D%0A
+%0A%0A  @Ignore%0A  @Test%0A  public void searchItem() throws Exception %7B%0A    String ddbUrl = DdbApiUrls.itemAipUrl(%223%22, dbbApiKey);%0A    String ddbResponse = loadJsonReponse(%22/pdc/3%22);%0A    mockDdbApi.expect(requestTo(ddbUrl))%0A            .andRespond(withSuccess(ddbResponse, MediaType.APPLICATION_JSON));%0A    RestTemplate client = new TestRestTemplate();%0A    String url = urlPrefix + %22/search?q=1%22;%0A%0A  %7D%0A
 %7D%0A

Refactor Test