/*

    Copyright 2015-2016 Artem Stasiuk

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.github.terma.jenkins.githubprcoveragestatus;

import hudson.model.Build;
import org.junit.Test;
import org.mockito.Mockito;

import java.io.IOException;

public class MasterCoverageActionTest {

    private Build build = Mockito.mock(Build.class);

    @Test
    public void skipStepIfResultOfBuildIsNotSuccess() throws IOException, InterruptedException {
        getMasterCoverageAction().perform(build, null, null, null);
    }

    private MasterCoverageAction getMasterCoverageAction() {
        final MasterCoverageAction masterCoverageAction = new MasterCoverageAction();
        masterCoverageAction.setDisableSimpleCov(false);
        masterCoverageAction.setUseSonarForMasterCoverage(false);
        return masterCoverageAction;
    }
}

@@ -640,369 +640,2693 @@
 son.
-model.Build;%0Aimport org.junit.Test;%0Aimport org.mockito.Mockito;%0A%0Aimport java.io.IOException;%0A%0Apublic class MasterCoverageActionTest %7B%0A%0A    private Build build = Mockito.mock(Build.class);%0A%0A    @Test%0A    public void skipStepIfResultOfBuildIsNotSuccess() throws IOException, InterruptedException %7B%0A        getMasterCoverageAction().perform(build, null, null, null
+EnvVars;%0Aimport hudson.model.Build;%0Aimport hudson.model.Result;%0Aimport hudson.model.TaskListener;%0Aimport lombok.SneakyThrows;%0Aimport org.junit.Before;%0Aimport org.junit.Rule;%0Aimport org.junit.Test;%0Aimport org.jvnet.hudson.test.JenkinsRule;%0Aimport org.mockito.Mockito;%0A%0Aimport java.io.ByteArrayOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.PrintStream;%0Aimport java.util.Map;%0A%0Aimport static org.hamcrest.CoreMatchers.equalTo;%0Aimport static org.junit.Assert.assertThat;%0Aimport static org.mockito.Matchers.any;%0Aimport static org.mockito.Mockito.when;%0A%0Apublic class MasterCoverageActionTest %7B%0A%0A    @Rule%0A    public JenkinsRule jenkinsRule = new JenkinsRule();%0A%0A    private Build build;%0A    private TaskListener listener;%0A    EnvVars envVars;%0A    private CoverageRepository coverageRepository;%0A    final String GIT_URL = %22http://bitbucket/project/repository.git%22;%0A    final float CURRENT_COVERAGE = 0.47f;%0A    Configuration configuration;%0A%0A    @Before%0A    @SneakyThrows%0A    public void before() %7B%0A        build = Mockito.mock(Build.class);%0A        listener = Mockito.mock(TaskListener.class);%0A        coverageRepository = Mockito.mock(CoverageRepository.class);%0A        ServiceRegistry.setCoverageRepository(coverageRepository);%0A%0A        envVars = new EnvVars();%0A        envVars.put(%22GIT_URL%22, GIT_URL);%0A        when(listener.getLogger()).thenReturn(new PrintStream(new ByteArrayOutputStream()));%0A        when(build.getEnvironment(listener)).thenReturn(envVars);%0A        when(coverageRepository.get(any())).thenReturn(CURRENT_COVERAGE);%0A%0A        configuration = new Configuration();%0A    %7D%0A%0A    @Test%0A    public void skipStepIfResultOfBuildIsNotSuccess() throws IOException, InterruptedException %7B%0A        final Map%3CString, Float%3E coverageByRepo = configuration.getDescriptor().getCoverageByRepo();%0A        coverageByRepo.remove(GIT_URL);%0A%0A        when(build.getResult()).thenReturn(Result.FAILURE);%0A        getMasterCoverageAction().perform(build, null, null, null);%0A%0A        assertThat(%22Still has zero coverage entry%22, coverageByRepo.size(), equalTo(0));%0A    %7D%0A%0A    @Test%0A    public void testStoreCoverage() throws IOException, InterruptedException %7B%0A        final Map%3CString, Float%3E coverageByRepo = configuration.getDescriptor().getCoverageByRepo();%0A        coverageByRepo.remove(GIT_URL);%0A        assertThat(%22No coverage entry%22, coverageByRepo.isEmpty(), equalTo(true));%0A%0A        when(build.getResult()).thenReturn(Result.SUCCESS);%0A        getMasterCoverageAction().perform(build, null, null, listener);%0A%0A        assertThat(%22Has coverage entry%22, coverageByRepo.size(), equalTo(1));%0A        assertThat(%22Has coverage entry%22, coverageByRepo.get(GIT_URL), equalTo(CURRENT_COVERAGE)
 );%0A 
@@ -3539,74 +3539,8 @@
 e);%0A
-        masterCoverageAction.setUseSonarForMasterCoverage(false);%0A
     
@@ -3578,10 +3578,11 @@
 ;%0A    %7D%0A
+%0A
 %7D%0A

Verify storage of coverage works