/* 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 org.camunda.bpm.client.backoff;

import org.assertj.core.util.Lists;
import org.camunda.bpm.client.task.impl.ExternalTaskImpl;
import org.junit.Before;
import org.junit.Test;

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

/**
 * @author Nikola Koevski
 */
public class ExponentialBackoffStrategyTest {

  protected ExponentialBackoffStrategy backoffStrategy;

  @Before
  public void setup() {
    backoffStrategy = new ExponentialBackoffStrategy();
  }

  @Test
  public void shouldAdvanceBackoffStrategy() {
    // given
    long initialWaitingTime = backoffStrategy.calculateBackoffTime();
    assertThat(initialWaitingTime).isEqualTo(0L);

    // when
    // no external tasks available
    backoffStrategy.reconfigure(Lists.emptyList());
    long waitingTime1 = backoffStrategy.calculateBackoffTime();
    backoffStrategy.reconfigure(Lists.emptyList());
    long waitingTime2 = backoffStrategy.calculateBackoffTime();

    // then
    assertThat(waitingTime1).isGreaterThan(0L);
    assertThat(waitingTime2).isGreaterThan(waitingTime1);
  }

  @Test
  public void shouldResetBackoffStrategy() {
    // given
    backoffStrategy.reconfigure(Lists.emptyList());
    long waitingTime = backoffStrategy.calculateBackoffTime();
    assertThat(waitingTime).isGreaterThan(0L);

    // when
    backoffStrategy.reconfigure(Lists.newArrayList(new ExternalTaskImpl()));

    // then
    long waitingTime2 = backoffStrategy.calculateBackoffTime();
    assertThat(waitingTime2).isEqualTo(0L);
  }
}

@@ -1237,16 +1237,43 @@
 n%0A    //
+ in consecutive iterations,
  no exte
@@ -1283,16 +1283,20 @@
 l tasks 
+are 
 availabl
@@ -1569,36 +1569,34 @@
 ingTime1).is
-GreaterThan(
+EqualTo(50
 0L);%0A    ass
@@ -1623,32 +1623,21 @@
 ).is
-GreaterThan(waitingTime1
+EqualTo(1000L
 );%0A 
@@ -1770,32 +1770,33 @@
 long waitingTime
+1
  = backoffStrate
@@ -1852,24 +1852,23 @@
 Time
+1
 ).is
-GreaterThan(
+EqualTo(50
 0L);
@@ -1877,24 +1877,64 @@
     // when%0A
+    // a not-empty response is received%0A
     backoffS
@@ -2122,11 +2122,437 @@
 0L);%0A  %7D
+%0A%0A  @Test%0A  public void shouldCapWaitingTime() %7B%0A    // given%0A    long waitingTime = backoffStrategy.calculateBackoffTime();%0A    assertThat(waitingTime).isEqualTo(0L);%0A%0A    // when%0A    // reach maximum waiting time%0A    for (int i=0; i%3C8; i++) %7B%0A      backoffStrategy.reconfigure(Lists.emptyList());%0A    %7D%0A%0A    // then%0A    waitingTime = backoffStrategy.calculateBackoffTime();%0A    assertThat(waitingTime).isEqualTo(60000L);%0A  %7D
 %0A%7D%0A

refactor backoff strategy tests