/*
 * Copyright (C) open knowledge GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package de.openknowledge.jaxrs.reactive.test;

import static javax.ws.rs.client.Entity.entity;
import static org.assertj.core.api.Assertions.assertThat;

import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;

import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.descriptor.api.Descriptors;
import org.jboss.shrinkwrap.descriptor.api.webapp31.WebAppDescriptor;
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
import org.jboss.shrinkwrap.resolver.api.maven.PomEquippedResolveStage;
import org.junit.Test;
import org.junit.runner.RunWith;
import de.openknowledge.jaxrs.reactive.flow.SingleItemPublisher;

@RunAsClient
@RunWith(Arquillian.class)
public class CustomerTest {

  @Deployment
  public static WebArchive deployment() {
    PomEquippedResolveStage pom = Maven.resolver().loadPomFromFile("pom.xml");
    return ShrinkWrap.create(WebArchive.class)
      .addPackage(SingleItemPublisher.class.getPackage())
      .addPackage(Customer.class.getPackage())
      .addAsLibraries(pom.resolve("commons-io:commons-io").withTransitivity().asFile())
      .setWebXML(new StringAsset(Descriptors.create(WebAppDescriptor.class)
        .addDefaultNamespaces()
        .version("3.1")
        .exportAsString()));
  }

  @Test
  public void put(@ArquillianResource URL url) throws URISyntaxException {


    ClientBuilder.newClient().target(url.toURI())
      .path("customers")
      .request()
      .put(entity("[{\"firstName\": \"Joe\", \"lastName\": \"Doe\"}]", MediaType.APPLICATION_JSON_TYPE));

    Response response = ClientBuilder.newClient().target(url.toURI())
      .path("customers")
      .request(MediaType.APPLICATION_JSON)
      .get();


    List<Customer> customers = response.readEntity(new GenericType<List<Customer>>() {});

    Customer customer = new Customer();
    customer.setFirstName("Joe");
    customer.setLastName("Doe");

    assertThat(customers).isEqualTo(List.of(customer));
  }

}

@@ -1639,16 +1639,17 @@
 unWith;%0A
+%0A
 import d
@@ -2341,16 +2341,27 @@
 void put
+OneCustomer
 (@Arquil
@@ -2411,16 +2411,706 @@
 tion %7B%0A%0A
+    ClientBuilder.newClient().target(url.toURI())%0A      .path(%22customers%22)%0A      .request()%0A      .put(entity(%22%5B%7B%5C%22firstName%5C%22: %5C%22John%5C%22, %5C%22lastName%5C%22: %5C%22Doe%5C%22%7D%5D%22, MediaType.APPLICATION_JSON_TYPE));%0A%0A    Response response = ClientBuilder.newClient().target(url.toURI())%0A      .path(%22customers%22)%0A      .request(MediaType.APPLICATION_JSON)%0A      .get();%0A%0A    List%3CCustomer%3E customers = response.readEntity(new GenericType%3CList%3CCustomer%3E%3E() %7B%7D);%0A%0A    Customer john = new Customer();%0A    john.setFirstName(%22John%22);%0A    john.setLastName(%22Doe%22);%0A%0A    assertThat(customers).isEqualTo(List.of(john));%0A  %7D%0A%0A  @Test%0A  public void putCustomers(@ArquillianResource URL url) throws URISyntaxException %7B%0A
 %0A    Cli
@@ -3234,16 +3234,67 @@
 %5C%22: %5C%22Jo
+hn%5C%22, %5C%22lastName%5C%22: %5C%22Doe%5C%22%7D, %7B%5C%22firstName%5C%22: %5C%22Jan
 e%5C%22, %5C%22l
@@ -3505,17 +3505,16 @@
 get();%0A%0A
-%0A
     List
@@ -3609,24 +3609,20 @@
 ustomer 
-customer
+john
  = new C
@@ -3632,32 +3632,28 @@
 omer();%0A    
-customer
+john
 .setFirstNam
@@ -3661,25 +3661,119 @@
 (%22Jo
+hn%22);%0A    john.setLastName(%22Do
 e%22);%0A
+%0A
     
-customer
+Customer jane = new Customer();%0A    jane.setFirstName(%22Jane%22);%0A    jane
 .set
@@ -3838,23 +3838,25 @@
 .of(
-customer
+john, jane
 ));%0A  %7D%0A
 %0A%7D%0A
@@ -3852,11 +3852,10 @@
 ));%0A  %7D%0A
-%0A
 %7D%0A

add test for two customers