3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.computation.task.projectanalysis.step;
23 import java.io.IOException;
24 import org.apache.commons.io.FileUtils;
25 import org.hamcrest.Description;
26 import org.hamcrest.TypeSafeMatcher;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.rules.ExpectedException;
30 import org.sonar.api.utils.ZipUtils;
31 import org.sonar.api.utils.internal.JUnitTempFolder;
32 import org.sonar.api.utils.log.LogTester;
33 import org.sonar.api.utils.log.LoggerLevel;
34 import org.sonar.db.ce.CeTaskTypes;
35 import org.sonar.server.computation.task.projectanalysis.batch.MutableBatchReportDirectoryHolder;
36 import org.sonar.ce.queue.CeTask;
37 import org.sonar.ce.queue.report.ReportFiles;
39 import static org.mockito.Matchers.argThat;
40 import static org.mockito.Mockito.mock;
41 import static org.mockito.Mockito.verify;
42 import static org.mockito.Mockito.when;
44 public class ExtractReportStepTest {
46 public static final String TASK_UUID = "1";
48 public JUnitTempFolder tempFolder = new JUnitTempFolder();
51 public LogTester logTester = new LogTester().setLevel(LoggerLevel.INFO);
54 public ExpectedException expectedException = ExpectedException.none();
56 MutableBatchReportDirectoryHolder reportDirectoryHolder = mock(MutableBatchReportDirectoryHolder.class);
57 ReportFiles reportFiles = mock(ReportFiles.class);
58 CeTask ceTask = new CeTask.Builder().setType(CeTaskTypes.REPORT).setUuid(TASK_UUID).build();
60 ExtractReportStep underTest = new ExtractReportStep(reportFiles, ceTask, tempFolder, reportDirectoryHolder);
63 public void fail_if_report_zip_does_not_exist() throws Exception {
64 File zip = tempFolder.newFile();
65 FileUtils.forceDelete(zip);
66 expectedException.expect(IllegalStateException.class);
67 expectedException.expectMessage("Fail to unzip " + zip.getPath());
69 when(reportFiles.fileForUuid(TASK_UUID)).thenReturn(zip);
75 public void unzip_report() throws Exception {
76 File zipDir = tempFolder.newDir();
77 final File metadataFile = new File(zipDir, "metadata.pb");
78 FileUtils.write(metadataFile, "{report}");
79 File zip = tempFolder.newFile();
80 ZipUtils.zipDir(zipDir, zip);
81 when(reportFiles.fileForUuid(TASK_UUID)).thenReturn(zip);
85 verify(reportDirectoryHolder).setDirectory(argThat(new TypeSafeMatcher<File>() {
87 protected boolean matchesSafely(File dir) {
89 return dir.isDirectory() && dir.exists() &&
90 // directory contains the uncompressed report (which contains only metadata.pb in this test)
91 dir.listFiles().length == 1 &&
92 FileUtils.contentEquals(dir.listFiles()[0], metadataFile);
93 } catch (IOException e) {
94 throw new IllegalStateException(e);
99 public void describeTo(Description description) {