/*
 * Copyright (C) 2015 AppTik Project
 *
 * 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 io.apptik.comm.jus;

import com.squareup.okhttp.mockwebserver.MockResponse;
import com.squareup.okhttp.mockwebserver.MockWebServer;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import java.util.concurrent.ExecutionException;

import io.apptik.comm.jus.http.MediaType;

import static org.assertj.core.api.Assertions.assertThat;

public class TransformersTests {

    @Rule
    public final MockWebServer server = new MockWebServer();

    public RequestQueue queue;

    @Before
    public void setup() {
        queue = Jus.newRequestQueue();
    }

    @After
    public void after() {
        queue.stopWhenDone();
    }

    @Test
    public void requestConverterTest() {
        //server.enqueue(new MockResponse().setBody("Hi"));
        Transformer.RequestTransformer inactiveTransformer =
                new Transformer.RequestTransformer(new RequestQueue.RequestFilter() {
                    @Override
                    public boolean apply(Request<?> request) {
                        return false;
                    }
                }) {
                    @Override
                    public NetworkRequest transform(NetworkRequest networkRequest) {
                        return NetworkRequest.create(MediaType.parse("text/plain"), "Holla");
                    }
                };
        Transformer.RequestTransformer activeTransformer =
                new Transformer.RequestTransformer(new RequestQueue.RequestFilter() {
                    @Override
                    public boolean apply(Request<?> request) {
                        return true;
                    }
                }) {
                    @Override
                    public NetworkRequest transform(NetworkRequest networkRequest) {
                        return NetworkRequest.create(MediaType.parse("text/plain"), "Holla");
                    }
                };
        Request request = new Request("POST", server.url("/").toString(), String.class)
                .setNetworkRequest
                        (NetworkRequest.create(MediaType.parse("text/plain"), "Hi"));

        queue.add(request).cancel();
        assertThat(request.getNetworkRequest().getBodyAsString()).isEqualTo("Hi");
        request = request.clone();
        queue.addRequestTransformer(inactiveTransformer);
        queue.add(request).cancel();
        assertThat(request.getNetworkRequest().getBodyAsString()).isEqualTo("Hi");
        request = request.clone();
        queue.addRequestTransformer(activeTransformer);
        queue.add(request).cancel();
        assertThat(request.getNetworkRequest().getBodyAsString()).isEqualTo("Holla");


    }

    @Test
    public void responseConverterTest() throws ExecutionException, InterruptedException {
        Transformer.ResponseTransformer inactiveTransformer =
                new Transformer.ResponseTransformer(new RequestQueue.RequestFilter() {
                    @Override
                    public boolean apply(Request<?> request) {
                        return false;
                    }
                }) {
                    @Override
                    public NetworkResponse transform(NetworkResponse networkResponse) {
                        return NetworkResponse.create(MediaType.parse("text/plain"), "Holla");
                    }
                };
        Transformer.ResponseTransformer activeTransformer =
                new Transformer.ResponseTransformer(new RequestQueue.RequestFilter() {
                    @Override
                    public boolean apply(Request<?> request) {
                        return true;
                    }
                }) {
                    @Override
                    public NetworkResponse transform(NetworkResponse networkResponse) {
                        return NetworkResponse.create(MediaType.parse("text/plain"), "Holla");
                    }
                };

        Request<String> request = new Request<String>("GET", server.url("/").toString(), String
                .class);

        server.enqueue(new MockResponse().setBody("Hi"));
        queue.add(request);
        assertThat(request.getFuture().get()).isEqualTo("Hi");
        request = request.clone();
        queue.addResponseTransformer(inactiveTransformer);
        server.enqueue(new MockResponse().setBody("Hi"));
        queue.add(request);
        assertThat(request.getFuture().get()).isEqualTo("Hi");
        request = request.clone();
        queue.addResponseTransformer(activeTransformer);
        server.enqueue(new MockResponse().setBody("Hi"));
        queue.add(request);
        assertThat(request.getFuture().get()).isEqualTo("Holla");

    }

}

@@ -1314,23 +1314,25 @@
  request
-Convert
+Transform
 erTest()
@@ -3337,15 +3337,17 @@
 onse
-Convert
+Transform
 erTe

fix namings