/*
 * Copyright 2021 Danilo Reinert
 *
 * 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.reinert.requestor.gwt;

import java.util.logging.Logger;

import com.google.gwt.junit.client.GWTTestCase;

import io.reinert.requestor.core.PollingRequest;
import io.reinert.requestor.core.PollingStrategy;
import io.reinert.requestor.core.RequestFilter;
import io.reinert.requestor.core.Response;
import io.reinert.requestor.core.Session;
import io.reinert.requestor.core.callback.ResponseCallback;

/**
 * Integration tests of {@link RequestFilter}.
 */
public class RequestGwtTest extends GWTTestCase {

    private static final int TIMEOUT = 5000;
    private static final Logger logger = Logger.getLogger(RequestGwtTest.class.getName());

    private Session session;

    @Override
    public String getModuleName() {
        return "io.reinert.requestor.gwt.RequestorGwtTest";
    }

    @Override
    protected void gwtSetUp() throws Exception {
        super.gwtSetUp();

        session = new GwtSession();
        session.setMediaType("application/json");
    }

    public void testStopShortPolling() {
        final PollingRequest<Void> request =
                session.req("https://httpbin.org/get")
                        .poll(PollingStrategy.SHORT, 500)
                        .get();

        request.onStatus(200,
                new ResponseCallback() {
                    public void execute(Response response) {
                        // The request can be sent one more time after stopPolling is called in SHORT strategy
                        // depending on the time to receive the responses
                        assertTrue(request.getPollingCounter() <= 3);

                        if (request.getPollingCounter() == 2) {
                            request.stopPolling();
                            finishTest();
                        }
                    }
                });

        delayTestFinish(TIMEOUT);
    }

    public void testStopLongPolling() {
        final PollingRequest<Void> request =
                session.req("https://httpbin.org/get")
                        .poll(PollingStrategy.LONG, 500)
                        .get();

        request.onStatus(200,
                new ResponseCallback() {
                    public void execute(Response response) {
                        assertTrue(request.getPollingCounter() <= 2);

                        if (request.getPollingCounter() == 2) {
                            request.stopPolling();
                            finishTest();
                        }
                    }
                });

        delayTestFinish(TIMEOUT);
    }

    public void testShortPollingLimit() {
        final PollingRequest<Void> request =
                session.req("https://httpbin.org/get")
                        .poll(PollingStrategy.SHORT, 500, 3)
                        .get();

        request.onStatus(200,
                new ResponseCallback() {
                    public void execute(Response response) {
                        assertTrue(request.getPollingCounter() <= 3);

                        if (request.getPollingCounter() == 3) {
                            finishTest();
                        }
                    }
                });

        delayTestFinish(TIMEOUT);
    }

    public void testLongPollingLimit() {
        final PollingRequest<Void> request =
                session.req("https://httpbin.org/get")
                        .poll(PollingStrategy.LONG, 0, 3)
                        .get();

        request.onStatus(200,
                new ResponseCallback() {
                    public void execute(Response response) {
                        assertTrue(request.getPollingCounter() <= 3);

                        if (request.getPollingCounter() == 3) {
                            finishTest();
                        }
                    }
                });

        delayTestFinish(TIMEOUT);
    }
}

@@ -1000,16 +1000,83 @@
 allback;
+%0Aimport io.reinert.requestor.core.callback.ResponseRequestCallback;
 %0A%0A/**%0A *
@@ -3275,61 +3275,8 @@
 ) %7B%0A
-        final PollingRequest%3CVoid%3E request =%0A        
     
@@ -3330,32 +3330,24 @@
             
- 
-       
 .poll(Pollin
@@ -3383,32 +3383,39 @@
             
+.get()%0A
         
 .get();%0A%0A   
@@ -3402,33 +3402,24 @@
         
-.get();%0A%0A
         
 request.
@@ -3402,39 +3402,32 @@
                 
-request
 .onStatus(200,%0A 
@@ -3424,32 +3424,16 @@
 tus(200,
-%0A               
  new Res
@@ -3433,32 +3433,39 @@
 new Response
+Request
 Callback
 () %7B%0A       
@@ -3444,32 +3444,38 @@
 eRequestCallback
+%3CVoid%3E
 () %7B%0A           
@@ -3512,32 +3512,62 @@
 esponse response
+, PollingRequest%3CVoid%3E request
 ) %7B%0A            
@@ -3886,61 +3886,8 @@
 ) %7B%0A
-        final PollingRequest%3CVoid%3E request =%0A        
     
@@ -3941,32 +3941,24 @@
             
- 
-       
 .poll(Pollin
@@ -3991,32 +3991,39 @@
             
+.get()%0A
         
 .get();%0A%0A   
@@ -4010,33 +4010,24 @@
         
-.get();%0A%0A
         
 request.
@@ -4010,39 +4010,32 @@
                 
-request
 .onStatus(200,%0A 
@@ -4032,32 +4032,16 @@
 tus(200,
-%0A               
  new Res
@@ -4041,32 +4041,39 @@
 new Response
+Request
 Callback
 () %7B%0A       
@@ -4052,32 +4052,38 @@
 eRequestCallback
+%3CVoid%3E
 () %7B%0A           
@@ -4120,32 +4120,62 @@
 esponse response
+, PollingRequest%3CVoid%3E request
 ) %7B%0A            

Use request callbacks on RequestGwtTest