// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package test;

import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.HttpResponse;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

public class UnitTest {
    private static String normalizeXML(String xml) {
        String res = xml;
        res = res.replaceAll("<\\?xml [^>]+\\?>", "");
        res = res.replaceAll(" xmlns:[a-z]+=\"http://www.w3.org/2001/XMLSchema-instance\"", "");
        res = res.replaceAll(" xmlns:[a-z]+=\"http://www.w3.org/2001/XMLSchema\"", "");
        res = res.replace("\r", "");
        res = res.replace("\n", "");
        res = res.replaceAll(">[ ]+<", "><");
        return res;
    }
    private static void testGetOne(String urlprefix, int f1, String acctyp) throws IOException {
        HttpGet request = new HttpGet(urlprefix + "/t1/" + f1);
        request.setHeader("Accept", acctyp);
        HttpClient client = HttpClientBuilder.create().build();
        HttpResponse response = client.execute(request);
        if(response.getStatusLine().getStatusCode() != 200) System.out.printf("getOne status = %d\n", response.getStatusLine().getStatusCode());
        String contyp = response.getFirstHeader("Content-Type").getValue().split(";")[0];
        if(!contyp.startsWith(acctyp)) System.out.printf("getOne content type = %s\n", contyp);
        String respbody = Util.getAllLines(new InputStreamReader(response.getEntity().getContent()));
        String xrespbody = normalizeXML(respbody);
        if(!(xrespbody.equals("<t1><f1>123</f1><f2>getOne</f2></t1>") ||
             xrespbody.equals("{\"f1\":123,\"f2\":\"getOne\"}"))) System.out.printf("getOne body = %s\n", respbody);
    }
    private static void testGetAll(String urlprefix, String acctyp) throws IOException {
        HttpGet request = new HttpGet(urlprefix + "/t1");
        request.setHeader("Accept", acctyp);
        HttpClient client = HttpClientBuilder.create().build();
        HttpResponse response = client.execute(request);
        if(response.getStatusLine().getStatusCode() != 200) System.out.printf("getAll status = %d\n", response.getStatusLine().getStatusCode());
        String contyp = response.getFirstHeader("Content-Type").getValue().split(";")[0];
        if(!contyp.startsWith(acctyp)) System.out.printf("getAll content type = %s\n", contyp);
        String respbody = Util.getAllLines(new InputStreamReader(response.getEntity().getContent()));
        String xrespbody = normalizeXML(respbody);
        if(!(xrespbody.equals("<t1array><t1><f1>1</f1><f2>getAll #1</f2></t1><t1><f1>2</f1><f2>getAll #2</f2></t1><t1><f1>3</f1><f2>getAll #3</f2></t1></t1array>") ||
             xrespbody.equals("[{\"f1\":1,\"f2\":\"getAll #1\"},{\"f1\":2,\"f2\":\"getAll #2\"},{\"f1\":3,\"f2\":\"getAll #3\"}]"))) System.out.printf("getAll body = %s\n", respbody);
    }
    private static void testGetSome(String urlprefix, int start, int finish, String acctyp) throws IOException {
        HttpGet request = new HttpGet(urlprefix + "/t1?start=" + start + "&finish=" + finish);
        request.setHeader("Accept", acctyp);
        HttpClient client = HttpClientBuilder.create().build();
        HttpResponse response = client.execute(request);
        if(response.getStatusLine().getStatusCode() != 200) System.out.printf("getAll status = %d\n", response.getStatusLine().getStatusCode());
        String contyp = response.getFirstHeader("Content-Type").getValue().split(";")[0];
        if(!contyp.startsWith(acctyp)) System.out.printf("getSome content type = %s\n", contyp);
        String respbody = Util.getAllLines(new InputStreamReader(response.getEntity().getContent()));
        String xrespbody = normalizeXML(respbody);
        if(!(xrespbody.equals("<t1array><t1><f1>10</f1><f2>getSome #10</f2></t1><t1><f1>11</f1><f2>getSome #11</f2></t1><t1><f1>12</f1><f2>getSome #12</f2></t1></t1array>") ||
             xrespbody.equals("[{\"f1\":10,\"f2\":\"getSome #10\"},{\"f1\":11,\"f2\":\"getSome #11\"},{\"f1\":12,\"f2\":\"getSome #12\"}]"))) System.out.printf("getSome body = %s\n", respbody);
    }
    private static void testPost(String urlprefix, String contyp, T1 o, String acctyp) throws IOException {
        String reqbody = Util.Object2String(o, contyp);
        HttpPost request = new HttpPost(urlprefix + "/t1");
        request.setHeader("Content-Type", contyp);
        request.setEntity(new StringEntity(reqbody));
        request.setHeader("Accept", acctyp);
        HttpClient client = HttpClientBuilder.create().build();
        HttpResponse response = client.execute(request);
        if(response.getStatusLine().getStatusCode() != 200) System.out.printf("getPost status = %d\n", response.getStatusLine().getStatusCode());
        String contyp2 = response.getFirstHeader("Content-Type").getValue().split(";")[0];
        if(!contyp2.startsWith(acctyp)) System.out.printf("getPost content type = %s\n", contyp);
        String respbody = Util.getAllLines(new InputStreamReader(response.getEntity().getContent()));
        String xrespbody = normalizeXML(respbody);
        if(!(xrespbody.equals("<t1><f1>123</f1><f2>ABC</f2></t1>") ||
             xrespbody.equals("{\"f1\":123,\"f2\":\"ABC\"}"))) System.out.printf("getPost body = %s\n", respbody);
    }
    private static void testPut(String urlprefix, String contyp, T1 o) throws IOException {
        String reqbody = Util.Object2String(o, contyp);
        HttpPut request = new HttpPut(urlprefix + "/t1/" + o.getF1());
        request.setHeader("Content-Type", contyp);
        request.setEntity(new StringEntity(reqbody));
        HttpClient client = HttpClientBuilder.create().build();
        HttpResponse response = client.execute(request);
        if(response.getStatusLine().getStatusCode() != 204) System.out.printf("getPut status = %d\n", response.getStatusLine().getStatusCode());
    }
    private static void testDelete(String urlprefix, int f1) throws IOException {
        HttpDelete request = new HttpDelete(urlprefix + "/t1/" + f1);   
        HttpClient client = HttpClientBuilder.create().build();
        HttpResponse response = client.execute(request);
        if(response.getStatusLine().getStatusCode() != 204) System.out.printf("getDelete status = %d\n", response.getStatusLine().getStatusCode());
    }
    private static void test(String urlprefix, String typ) throws IOException {
        System.out.println("**** " + urlprefix + " (" + typ + ") ****");
        testGetOne(urlprefix, 123, typ);
        testGetAll(urlprefix, typ);
        testGetSome(urlprefix, 10, 12, typ);
        testPost(urlprefix, typ, new T1(123, "ABC"), typ);
        testPut(urlprefix, typ, new T1(123, "ABC"));
        testDelete(urlprefix, 123);
    }
    private static void test(String urlprefix) throws IOException {
        test(urlprefix, "application/xml");
        test(urlprefix, "application/json");
    }
    public static void main(String[] args) throws IOException {
        Logger.getRootLogger().setLevel(Level.OFF);
        test("http://localhost:8080/wsrest/testapi1");
        test("http://localhost:8080/wsrest/testapi2");
        test("http://localhost:8080/wsrest/testapi3");
        test("http://localhost/testapi.ashx");
        test("http://localhost/testapi.svc");
        test("http://localhost/testapi");
        test("http://localhost:81/testapi.php");
    }
}
