package se.citerus.cqrs.bookstore.application;

import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.io.Resources;
import org.junit.Ignore;
import se.citerus.cqrs.bookstore.bookcatalog.api.BookDto;
import se.citerus.cqrs.bookstore.infrastructure.JsonSerializer;
import se.citerus.cqrs.bookstore.ordercontext.api.RegisterPublisherContractRequest;

import java.util.List;

import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.io.Resources.getResource;
import static se.citerus.cqrs.bookstore.infrastructure.JsonSerializer.deserialize;

@Ignore
public class TestDataImporter {

  private static final String SERVER_ADDRESS = "http://localhost:8080/service";

  public static void main(String[] args) {
    importBooks();
  }

  private static void importBooks() {

    try {
      TestHttpClient publisherClient = new TestHttpClient(SERVER_ADDRESS + "/publishercontract-requests").init();

      // Add publisher contracts
      String contractsJson = Resources.toString(getResource("se/citerus/cqrs/bookstore/testdata/publishercontracts.json"), UTF_8);
      TypeReference<List<RegisterPublisherContractRequest>> listOfRegisterPublisherRequests = new TypeReference<List<RegisterPublisherContractRequest>>() {
      };
      List<RegisterPublisherContractRequest> publisherContractsRequests = deserialize(contractsJson, listOfRegisterPublisherRequests);

      for (RegisterPublisherContractRequest request : publisherContractsRequests) {
        publisherClient.post(JsonSerializer.serialize(request));
      }
      System.out.println("Imported [" + publisherContractsRequests.size() + "] contracts...");

      TestHttpClient bookClient = new TestHttpClient(SERVER_ADDRESS + "/books").init();

      // Add books
      String booksJson = Resources.toString(getResource("se/citerus/cqrs/bookstore/testdata/books.json"), UTF_8);
      TypeReference<List<BookDto>> listOfCreateBookRequests = new TypeReference<List<BookDto>>() {
      };

      List<BookDto> books = deserialize(booksJson, listOfCreateBookRequests);

      for (BookDto book : books) {
        bookClient.post(JsonSerializer.serialize(book));
      }
      System.out.println("Imported [" + books.size() + "] books...");

    } catch (Exception e) {
      e.printStackTrace();
    }
  }

}

@@ -221,72 +221,8 @@
 to;%0A
-import se.citerus.cqrs.bookstore.infrastructure.JsonSerializer;%0A
 impo
@@ -519,16 +519,97 @@
 rialize;
+%0Aimport static se.citerus.cqrs.bookstore.infrastructure.JsonSerializer.serialize;
 %0A%0A@Ignor
@@ -777,22 +777,152 @@
   import
-Books(
+Contracts(%22se/citerus/cqrs/bookstore/testdata/publishercontracts.json%22);%0A    importBooks(%22se/citerus/cqrs/bookstore/testdata/books.json%22
 );%0A  %7D%0A%0A
@@ -953,19 +953,33 @@
 port
-Books(
+Contracts(String path
 ) %7B%0A
-%0A
     
@@ -1103,41 +1103,8 @@
 );%0A%0A
-      // Add publisher contracts%0A
     
@@ -1163,68 +1163,12 @@
 rce(
-%22se/citerus/cqrs/bookstore/testdata/publishercontracts.json%22
+path
 ), U
@@ -1259,32 +1259,42 @@
 lisherRequests =
+%0A         
  new TypeReferen
@@ -1336,24 +1336,28 @@
 quest%3E%3E() %7B%0A
+    
       %7D;%0A   
@@ -1353,16 +1353,17 @@
      %7D;%0A
+%0A
       Li
@@ -1399,35 +1399,17 @@
 equest%3E 
-publisherContractsR
+r
 equests 
@@ -1526,35 +1526,17 @@
 quest : 
-publisherContractsR
+r
 equests)
@@ -1563,39 +1563,24 @@
 Client.post(
-JsonSerializer.
 serialize(re
@@ -1640,27 +1640,9 @@
 %22 + 
-publisherContractsR
+r
 eque
@@ -1670,24 +1670,149 @@
 acts...%22);%0A%0A
+    %7D catch (Exception e) %7B%0A      e.printStackTrace();%0A    %7D%0A  %7D%0A%0A  private static void importBooks(String path) %7B%0A    try %7B%0A
       TestHt
@@ -1967,55 +1967,12 @@
 rce(
-%22se/citerus/cqrs/bookstore/testdata/books.json%22
+path
 ), U
@@ -2107,20 +2107,23 @@
 ookDto%3E 
-book
+request
 s = dese
@@ -2195,20 +2195,23 @@
  book : 
-book
+request
 s) %7B%0A   
@@ -2231,31 +2231,16 @@
 nt.post(
-JsonSerializer.
 serializ
@@ -2297,20 +2297,23 @@
 ed %5B%22 + 
-book
+request
 s.size()

Split method in two.