/*
 * Copyright 2017 Banco Bilbao Vizcaya Argentaria, S.A..
 *
 * 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 com.bbva.arq.devops.ae.mirrorgate.util;

import static com.bbva.arq.devops.ae.mirrorgate.core.dto.FailureTendency.up;
import static com.bbva.arq.devops.ae.mirrorgate.core.utils.BuildStatsUtils.failureTendency;
import static org.junit.Assert.assertEquals;

import com.bbva.arq.devops.ae.mirrorgate.core.dto.BuildStats;
import com.bbva.arq.devops.ae.mirrorgate.core.dto.FailureTendency;
import com.bbva.arq.devops.ae.mirrorgate.core.utils.BuildStatsUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class BuildStatsUtilsTests {

    @Test
    public void itShouldReturnAnEmptyResultWhenCalledWithNoResults() {
        BuildStats stats = BuildStatsUtils.combineBuildStats();

        assertEquals(stats.getCount(), 0);
        assertEquals(stats.getDuration(), .0,0);
        assertEquals(stats.getFailureRate(), 0);

    }


    @Test
    public void itShouldReturnAnEmptyResultWhenCalledWithEmptyResults() {
        BuildStats stats = BuildStatsUtils.combineBuildStats(new BuildStats());

        assertEquals(stats.getCount(), 0);
        assertEquals(stats.getDuration(), .0,0);
        assertEquals(stats.getFailureRate(), 0);

    }

    @Test
    public void itShouldAddCounts() {
        BuildStats stats = BuildStatsUtils.combineBuildStats(
                new BuildStats().setCount(1),
                new BuildStats().setCount(2),
                new BuildStats().setCount(3)
        );

        assertEquals(stats.getCount(), 6);

    }

    @Test
    public void itShouldAverageFailureRates() {
        BuildStats stats = BuildStatsUtils.combineBuildStats(
                new BuildStats().setCount(1),
                new BuildStats().setCount(2),
                new BuildStats().setCount(3).setFailureRate(100)
        );

        assertEquals(stats.getFailureRate(), 50);

    }

    @Test
    public void itShouldAverageDurations() {
        BuildStats stats = BuildStatsUtils.combineBuildStats(
                new BuildStats().setCount(1).setDuration(100),
                new BuildStats().setCount(2).setDuration(200),
                new BuildStats().setCount(4).setDuration(400)
        );

        assertEquals(stats.getDuration(), 300,0);

    }

    @Test
    public void failureTendencyIsUp() {
        BuildStats statsSevenDaysBefore = new BuildStats().setFailureRate(100);
        BuildStats statsFifteenDaysBefore = new BuildStats().setFailureRate(300);

        FailureTendency failureTendencyTest = failureTendency(statsSevenDaysBefore.getFailureRate(),statsFifteenDaysBefore.getFailureRate());
        assertEquals(failureTendencyTest, up);
    }
}

@@ -665,16 +665,175 @@
 .util;%0A%0A
+import static com.bbva.arq.devops.ae.mirrorgate.core.dto.FailureTendency.down;%0Aimport static com.bbva.arq.devops.ae.mirrorgate.core.dto.FailureTendency.equal;%0A
 import s
@@ -3055,17 +3055,41 @@
 ic void 
-f
+itShouldReturnTheCorrectF
 ailureTe
@@ -3098,12 +3098,8 @@
 ency
-IsUp
 () %7B
@@ -3274,46 +3274,143 @@
     
-FailureTendency failureTendencyTest = 
+assertEquals(failureTendency(statsSevenDaysBefore.getFailureRate(),statsFifteenDaysBefore.getFailureRate()), up);%0A        assertEquals(
 fail
@@ -3418,35 +3418,37 @@
 reTendency(stats
-Sev
+Fifte
 enDaysBefore.get
@@ -3458,37 +3458,35 @@
 lureRate(),stats
-Fifte
+Sev
 enDaysBefore.get
@@ -3499,16 +3499,23 @@
 eRate())
+, down)
 ;%0A      
@@ -3548,16 +3548,92 @@
 ency
-Test, up
+(statsSevenDaysBefore.getFailureRate(),statsSevenDaysBefore.getFailureRate()), equal
 );%0A 

improve failure tendency test