package com.github.mike10004.seleniumhelp;

import com.github.mike10004.chromecookieimplant.ChromeCookie;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.junit.Test;

import java.io.File;
import java.net.URL;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.junit.Assert.*;

public class ChromeCookieTransformTest {

    @Test
    public void transform() throws Exception {
        ChromeCookieTransform t = new ChromeCookieTransform();
        DeserializableCookie input = new DeserializableCookie();
        input.setDomain(".example.com");
        input.setHttpOnly(false);
        input.setSecure(true);
        input.setPath("/");
        ChromeCookie output = t.transform(input);
        assertEquals("chrome cookie URL should be https if input cookie is secure", "https", new URL(output.url).getProtocol());
    }

    @Test
    public void transform_deserializable() throws Exception {
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String dcJson = "{\"name\":\"JSESSIONID\",\"attribs\":{\"path\":\"/\",\"domain\":\".www.linkedin.com\",\"max-age\":\"7776000\",\"version\":\"1\"},\"value\":\"ajax:6662599637803940570\",\"cookieDomain\":\"www.linkedin.com\",\"cookieExpiryDate\":\"Apr 16, 2017 5:52:11 PM\",\"cookiePath\":\"/\",\"isSecure\":true,\"cookieVersion\":0,\"creationDate\":\"Jan 16, 2017 4:51:57 PM\",\"httpOnly\":false}";
        DeserializableCookie d = gson.fromJson(dcJson, DeserializableCookie.class);
        ChromeCookieTransform t = new ChromeCookieTransform();
        ChromeCookie c = t.transform(d);
        gson.toJson(c, System.out);
        System.out.println();

        DeserializableCookie[] cookies;
        try (java.io.Reader reader = new java.io.FileReader("/tmp/cookies.json")) {
            cookies = gson.fromJson(reader, DeserializableCookie[].class);
        }
        List<ChromeCookie> ccookies = Stream.of(cookies).map(t::transform).collect(Collectors.toList());
        File outfile = new File("/tmp/chrome-cookies.json");
        try (java.io.Writer writer = new java.io.FileWriter(outfile)) {
            gson.toJson(ccookies, writer);
        }
        System.out.format("%s written with ChromeCookie array%n", outfile);
    }

}
@@ -907,1390 +907,5 @@
   %7D%0A
-%0A    @Test%0A    public void transform_deserializable() throws Exception %7B%0A        Gson gson = new GsonBuilder().setPrettyPrinting().create();%0A        String dcJson = %22%7B%5C%22name%5C%22:%5C%22JSESSIONID%5C%22,%5C%22attribs%5C%22:%7B%5C%22path%5C%22:%5C%22/%5C%22,%5C%22domain%5C%22:%5C%22.www.linkedin.com%5C%22,%5C%22max-age%5C%22:%5C%227776000%5C%22,%5C%22version%5C%22:%5C%221%5C%22%7D,%5C%22value%5C%22:%5C%22ajax:6662599637803940570%5C%22,%5C%22cookieDomain%5C%22:%5C%22www.linkedin.com%5C%22,%5C%22cookieExpiryDate%5C%22:%5C%22Apr 16, 2017 5:52:11 PM%5C%22,%5C%22cookiePath%5C%22:%5C%22/%5C%22,%5C%22isSecure%5C%22:true,%5C%22cookieVersion%5C%22:0,%5C%22creationDate%5C%22:%5C%22Jan 16, 2017 4:51:57 PM%5C%22,%5C%22httpOnly%5C%22:false%7D%22;%0A        DeserializableCookie d = gson.fromJson(dcJson, DeserializableCookie.class);%0A        ChromeCookieTransform t = new ChromeCookieTransform();%0A        ChromeCookie c = t.transform(d);%0A        gson.toJson(c, System.out);%0A        System.out.println();%0A%0A        DeserializableCookie%5B%5D cookies;%0A        try (java.io.Reader reader = new java.io.FileReader(%22/tmp/cookies.json%22)) %7B%0A            cookies = gson.fromJson(reader, DeserializableCookie%5B%5D.class);%0A        %7D%0A        List%3CChromeCookie%3E ccookies = Stream.of(cookies).map(t::transform).collect(Collectors.toList());%0A        File outfile = new File(%22/tmp/chrome-cookies.json%22);%0A        try (java.io.Writer writer = new java.io.FileWriter(outfile)) %7B%0A            gson.toJson(ccookies, writer);%0A        %7D%0A        System.out.format(%22%25s written with ChromeCookie array%25n%22, outfile);%0A    %7D%0A%0A
 %7D

remove test that used local cookies.json file