/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 com.lofidewanto.demo.client.common;

import org.fusesource.restygwt.client.MethodCallback;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

@RunWith(MockitoJUnitRunner.class)
public class AbstractMethodCallbackTest {

	@Spy
	private AbstractMethodCallback abstractMethodCallback;

	@Mock(answer = Answers.RETURNS_DEEP_STUBS)
	private Exception exception;

	@Before
	public void setUp() throws Exception {
	}

	@Test
	public void checkTimeoutErrorWithJsonErrorText() throws Exception {
		final boolean isTimeout = abstractMethodCallback.checkTimeoutError(
				new Exception(
						AbstractMethodCallback.ERROR_TEXT_RESPONSE_WAS_NOT_A_VALID_JSON));

		assertEquals(false, isTimeout);
	}

	@Test
	public void checkTimeoutErrorWithTimeoutError() throws Exception {
		doNothing().when(abstractMethodCallback).redirectToLogin();

		final boolean isTimeout = abstractMethodCallback.checkTimeoutError(
				new Exception(AbstractMethodCallback.ERROR_TEXT_LOGIN_FORM));

		assertEquals(true, isTimeout);
	}

	@Test
	public void execute() {
		doNothing().when(abstractMethodCallback).showLoadingMessage();

		abstractMethodCallback.execute();
	}

	@Test
	public void executeCallService() {
		Exception exception = new Exception(
				AbstractMethodCallback.ERROR_TEXT_RESPONSE_WAS_NOT_A_VALID_JSON);

		doAnswer(invocationOnMock -> {
			((MethodCallback) invocationOnMock.getArguments()[1])
					.onFailure(null, exception);
			return null;
		}).when(abstractMethodCallback)
				.executeCallService(any(MethodCallback.class));
	}

	@Test
	public void executeOnFailure() {
		doReturn(false).when(abstractMethodCallback)
				.checkTimeoutError(exception);
		doReturn(
				AbstractMethodCallback.ERROR_TEXT_RESPONSE_WAS_NOT_A_VALID_JSON)
				.when(exception).getMessage();
		doNothing().when(abstractMethodCallback).onFailure(any(), any());
		doNothing().when(abstractMethodCallback)
				.hideLoadingMessage(anyBoolean());

		abstractMethodCallback.executeOnFailure(null, exception);

		verify(abstractMethodCallback, times(1)).onFailure(any(), any());
	}
}
@@ -845,16 +845,96 @@
 ommon;%0A%0A
+import java.util.logging.Logger;%0A%0Aimport org.fusesource.restygwt.client.Method;%0A
 import o
@@ -1004,16 +1004,41 @@
 Before;%0A
+import org.junit.Ignore;%0A
 import o
@@ -1342,16 +1342,62 @@
 oolean;%0A
+import static org.mockito.Matchers.anyObject;%0A
 import s
@@ -1683,16 +1683,121 @@
 Test %7B%0A%0A
+%09private static final Logger logger = Logger%0A%09%09%09.getLogger(AbstractMethodCallbackTest.class.getName());%0A%0A
 %09@Spy%0A%09p
@@ -2706,197 +2706,223 @@
 ();%0A
-%09%7D%0A%0A%09@Test%0A%09public void executeCallService() %7B%0A%09%09Exception exception = new Exception(%0A%09%09%09%09AbstractMethodCallback.ERROR_TEXT_RESPONSE_WAS_NOT_A_VALID_JSON);%0A%0A%09%09doAnswer(invocationOnMock -%3E %7B
+%0A%09%09verify(abstractMethodCallback, times(1)).executeCallService(any());%0A%09%7D%0A%0A%09@Ignore%0A%09@Test%0A%09public void executeCallService() %7B%0A%09%09doAnswer(invocationOnMock -%3E %7B%0A%09%09%09logger.info(invocationOnMock.getMock().toString());%0A
 %0A%09%09%09
@@ -2975,46 +2975,62 @@
 s()%5B
-1
+0
 %5D)
+.
 %0A%09%09%09%09%09
-.
 on
-Failure(null, exception
+Success(any(Method.class), anyObject()
 );%0A
+%0A
 %09%09%09r
@@ -3126,16 +3126,181 @@
 class));
+%0A%0A%09%09abstractMethodCallback.executeCallService(any(MethodCallback.class));%0A%0A%09%09verify(abstractMethodCallback, times(1)).%0A%09%09%09%09onSuccess(any(Method.class), anyObject());
 %0A%09%7D%0A%0A%09@T

update Mockito test