package org.anderes.edu.client.rest.jersey;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.file.Path;
import java.nio.file.Paths;

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.MediaType;

import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.jsonp.JsonProcessingFeature;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class RecipeRestApiTest {
    
    private final String host = "http://www.anderes.org";
    private File tempFile;

    @Before
    public void setUp() throws Exception {
        System.getProperties().put("http.proxySet", "true");
        System.getProperties().put("http.proxyHost", "localhost");
        System.getProperties().put("http.proxyPort", "8080");
        
        tempFile = File.createTempFile("jsonTest", "json");
    }

    @After
    public void tearDown() throws Exception {
        assertThat(tempFile.delete(), is(true));
    }

    @Test
    public void shouldBeBackupAllRecipe() {
        final JsonArray array = getJsonFromServer(host, "/rest/recipes", JsonArray.class);
        assertThat(array, is(notNullValue()));

        for (int index = 0; index < array.size(); index++) {
            final JsonObject object = array.getJsonObject(index);
            assertThat(object.containsKey("id"), is(true));
        }

    }
    
    @Test
    public void shouldBeOneRecipe() {
        final JsonObject recipe = getJsonFromServer(host, "/rest/recipes/c0e5582e-252f-4e94-8a49-e12b4b047afb", JsonObject.class);
        assertThat(recipe, is(notNullValue()));
        assertThat(recipe.containsKey("id"), is(true));
        assertThat(recipe.getString("id"), is("c0e5582e-252f-4e94-8a49-e12b4b047afb"));
        assertThat(recipe.containsKey("title"), is(true));
        assertThat(recipe.containsKey("preample"), is(true));
        assertThat(recipe.containsKey("noOfPeople"), is(true));
        assertThat(recipe.containsKey("ingredients"), is(true));
        final JsonArray ingredients = recipe.getJsonArray("ingredients");
        assertThat(ingredients, is(notNullValue()));
        assertThat(recipe.containsKey("preparation"), is(true));
        assertThat(recipe.containsKey("tags"), is(true));
        final JsonArray tags = recipe.getJsonArray("tags");
        assertThat(tags, is(notNullValue()));
        assertThat(recipe.containsKey("rating"), is(true));
        assertThat(recipe.containsKey("addingDate"), is(true));
        assertThat(recipe.containsKey("editingDate"), is(true));
        assertThat(recipe.containsKey("image"), is(true));
        System.out.println(recipe);
    }
    
    @Test
    public void shouldBeWriteJsonFile() throws IOException {
        
        // given
        final JsonObjectBuilder builder = Json.createObjectBuilder();
        final JsonObject recipe = builder.add("id", "c0e5582e-252f-4e94-8a49-e12b4b047afc")
                        .add("noOfPeople", "2")
                        .add("ingredients", createIngredients())
                        .add("tags", createTags())
                        .add("title", "Arabische Spaghetti")
                        .add("preparation", "Pouletfleisch in schmale Streifen schneiden und kurz anbraten.")
                        .add("preample", "Dieses Rezept ...")
                        .add("rating", "5")
                        .add("addingDate", "1250959818424")
                        .add("editingDate", "1412436923512")
                        .add("image", "")
                        .build();
        final JsonArray recipes = Json.createArrayBuilder().add(recipe).build();
        final Path path = Paths.get(tempFile.toURI());
        
        // when
        writeJsonToFile(recipes, path);
        
        // then
        assertThat(tempFile.exists(), is(true));
        assertThat(tempFile.getTotalSpace() > 500, is(true));
        
    }
    
    private JsonArray createIngredients() {
        final JsonObjectBuilder objectBuilder = Json.createObjectBuilder();
        final JsonObject ingredient1 = objectBuilder.add("description", "Spaghetti").add("portion", "250g").add("comment", "").build();
        final JsonObject ingredient2 = objectBuilder.add("description", "Sultaninen").add("portion", "2 EL").add("comment", "").build();
        final JsonObject ingredient3 = objectBuilder.add("description", "Salz, Pfeffer und Olivenöl").add("portion", "").add("comment", "").build();
        return Json.createArrayBuilder().add(ingredient1).add(ingredient2).add(ingredient3).build();
    }
    
    private JsonArray createTags() {
        return Json.createArrayBuilder().add("pasta").add("fleisch").build();
    }

    private <T> T getJsonFromServer(final String host, final String path, final Class<T> clazz) {
        final Client client = ClientBuilder.newClient(new ClientConfig().register(JsonProcessingFeature.class));
        return client.target(host).path(path).request(MediaType.APPLICATION_JSON_TYPE).get(clazz);
    }
    
    private void writeJsonToFile(final JsonArray json, final Path path) throws IOException {
        final Writer writer = new FileWriter(path.toFile());
        writer.write(json.toString());
        writer.close();
    }
}

@@ -1489,32 +1489,34 @@
 nArray.class);%0D%0A
+%0D%0A
         assertTh
@@ -1547,26 +1547,24 @@
 e()));%0D%0A
-%0D%0A
         
 for (int
@@ -1559,158 +1559,65 @@
     
-for (int index = 0; index %3C array.size(); index++) %7B%0D%0A            final JsonObject object = array.getJsonObject(index);%0D%0A            assertThat(object
+array.forEach(element -%3E assertThat(((JsonObject)element)
 .con
@@ -1645,22 +1645,10 @@
 ue))
+)
 ;
-%0D%0A        %7D%0D%0A
 %0D%0A  
@@ -2410,32 +2410,324 @@
 NullValue()));%0D%0A
+        ingredients.forEach(i -%3E %7B %0D%0A                assertThat(((JsonObject)i).containsKey(%22description%22), is(true)); %0D%0A                assertThat(((JsonObject)i).containsKey(%22comment%22), is(true));%0D%0A                assertThat(((JsonObject)i).containsKey(%22portion%22), is(true));%0D%0A        %7D);%0D%0A
         assertTh
@@ -2892,24 +2892,94 @@
 y(%22tags%22);%0D%0A
+        ingredients.forEach(t -%3E assertThat(t, is(notNullValue())));%0D%0A
         asse

Test angepasst