package de.ddb.pdc.metadata;

import org.junit.Before;
import org.junit.Test;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

public class MetaFetcherImplTest {

  private MetaFetcherImpl fetcher;
  private RestTemplate rest;

  @Before
  public void setUp() {
    rest = mock(RestTemplate.class);
    fetcher = new MetaFetcherImpl(rest, "authkey");
  }

  @Test
  public void search() {
    SearchResultItem resultItem = mock(SearchResultItem.class);
    when(resultItem.getId()).thenReturn("abcde");
    when(resultItem.getTitle()).thenReturn("Titel");
    when(resultItem.getSubtitle()).thenReturn("Untertitel");
    when(resultItem.getThumbnail()).thenReturn("/thumbnail.jpg");

    ArrayList<SearchResultItem> resultItems = new ArrayList<>();
    resultItems.add(resultItem);
    SearchResults results = mock(SearchResults.class);
    when(results.getResultItems()).thenReturn(resultItems);

    String url = "https://api.deutsche-digitale-bibliothek.de/search?oauth_consumer_key=authkey&sort=RELEVANCE&rows=10&query=Titel";
    when(rest.getForObject(url, SearchResults.class)).thenReturn(results);

    DDBItem[] items = fetcher.searchForItems("Titel", 10);
    assertEquals(1, items.length);
    assertEquals("abcde", items[0].getId());
    assertEquals("Titel", items[0].getTitle());
    assertEquals("Untertitel", items[0].getSubtitle());
    assertEquals("https://www.deutsche-digitale-bibliothek.de/thumbnail.jpg",
        items[0].getImageUrl());
  }
}

@@ -151,16 +151,102 @@
 rayList;
+%0Aimport java.util.Calendar;%0Aimport java.util.GregorianCalendar;%0Aimport java.util.List;
 %0A%0Aimport
@@ -1649,10 +1649,2016 @@
 ));%0A  %7D%0A
+  %0A  @Test%0A  public void fetch() %7B%0A    EntitiesResultItem eri = mock(EntitiesResultItem.class);%0A    when(eri.getName()).thenReturn(%22Johann Wolfgang von Goethe%22);%0A    when(eri.getPlaceOfBirth()).thenReturn(%22Frankfurt am Main%22);%0A    when(eri.getYearOfBirth()).thenReturn(1749);%0A    when(eri.getYearOfDeath()).thenReturn(1832);%0A%0A    EntitiesResult er = mock(EntitiesResult.class);%0A    when(er.getResultItem()).thenReturn(eri);%0A    %0A    RDFItem rdf = mock(RDFItem.class);%0A    String authorId = %22http://d-nb.info/gnd/118540238%22;%0A    List%3CString%3E authorIds = new ArrayList%3C%3E();%0A    authorIds.add(authorId);%0A    when(rdf.getAuthorIds()).thenReturn(authorIds);%0A    when(rdf.getInstitution()).thenReturn(%22Deutsche Digitale Bibliothek%22);%0A    when(rdf.getPublishYear()).thenReturn(1946);%0A    %0A    EDMItem edm = mock(EDMItem.class);%0A    when(edm.getRdf()).thenReturn(rdf);%0A    %0A    ItemAipResult result = mock(ItemAipResult.class);%0A    when(result.getRDFItem()).thenReturn(rdf);%0A    %0A    %0A    String url = %22https://api.deutsche-digitale-bibliothek.de/items/itemId/aip?oauth_consumer_key=authkey%22;%0A    String url2 = %22https://api.deutsche-digitale-bibliothek.de/entities?query=id:%5C%22http://d-nb.info/gnd/118540238%5C%22&oauth_consumer_key=authkey%22;%0A    %0A    when(rest.getForObject(url, ItemAipResult.class)).thenReturn(result);%0A    %0A    when(rest.getForObject(url2, EntitiesResult.class)).thenReturn(er);%0A    %0A    DDBItem ddbItem = fetcher.fetchMetadata(%22itemId%22);%0A    %0A    assertEquals(%22Deutsche Digitale Bibliothek%22,ddbItem.getInstitute());%0A    assertEquals(1946,ddbItem.getPublishedYear().get(Calendar.YEAR));%0A    assertEquals(%22http://d-nb.info/gnd/118540238%22, ddbItem.getAuthors().get(0).getDnbId());%0A    Author author = ddbItem.getAuthors().get(0);%0A    assertEquals(%22Johann Wolfgang von Goethe%22,author.getName());%0A    assertEquals(1749, author.getYearOfBirth().get(Calendar.YEAR));%0A    assertEquals(1832, author.getYearOfDeath().get(Calendar.YEAR));%0A    assertEquals(%22Frankfurt am Main%22, author.getPlaceOfBirth());%0A  %7D %0A
 %7D%0A

add testmethod for fetch Metadata