Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ExtractReportStepTest.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  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.
  10. *
  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.
  15. *
  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.
  19. */
  20. package org.sonar.ce.task.projectanalysis.step;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import org.apache.commons.io.FileUtils;
  25. import org.junit.Rule;
  26. import org.junit.Test;
  27. import org.junit.rules.ExpectedException;
  28. import org.sonar.api.impl.utils.JUnitTempFolder;
  29. import org.sonar.api.utils.MessageException;
  30. import org.sonar.api.utils.System2;
  31. import org.sonar.api.utils.ZipUtils;
  32. import org.sonar.api.utils.log.LogTester;
  33. import org.sonar.api.utils.log.LoggerLevel;
  34. import org.sonar.ce.task.CeTask;
  35. import org.sonar.ce.task.projectanalysis.batch.BatchReportDirectoryHolderImpl;
  36. import org.sonar.ce.task.projectanalysis.batch.MutableBatchReportDirectoryHolder;
  37. import org.sonar.ce.task.step.TestComputationStepContext;
  38. import org.sonar.db.DbTester;
  39. import org.sonar.db.ce.CeTaskTypes;
  40. import static org.assertj.core.api.Assertions.assertThat;
  41. public class ExtractReportStepTest {
  42. private static final String TASK_UUID = "1";
  43. @Rule
  44. public JUnitTempFolder tempFolder = new JUnitTempFolder();
  45. @Rule
  46. public LogTester logTester = new LogTester();
  47. @Rule
  48. public ExpectedException expectedException = ExpectedException.none();
  49. @Rule
  50. public DbTester dbTester = DbTester.create(System2.INSTANCE);
  51. private MutableBatchReportDirectoryHolder reportDirectoryHolder = new BatchReportDirectoryHolderImpl();
  52. private CeTask ceTask = new CeTask.Builder()
  53. .setOrganizationUuid("org1")
  54. .setType(CeTaskTypes.REPORT)
  55. .setUuid(TASK_UUID)
  56. .build();
  57. private ExtractReportStep underTest = new ExtractReportStep(dbTester.getDbClient(), ceTask, tempFolder, reportDirectoryHolder);
  58. @Test
  59. public void fail_if_report_zip_does_not_exist() {
  60. expectedException.expect(MessageException.class);
  61. expectedException.expectMessage("Analysis report 1 is missing in database");
  62. underTest.execute(new TestComputationStepContext());
  63. }
  64. @Test
  65. public void unzip_report() throws Exception {
  66. logTester.setLevel(LoggerLevel.DEBUG);
  67. File reportFile = generateReport();
  68. try (InputStream input = FileUtils.openInputStream(reportFile)) {
  69. dbTester.getDbClient().ceTaskInputDao().insert(dbTester.getSession(), TASK_UUID, input);
  70. }
  71. dbTester.getSession().commit();
  72. dbTester.getSession().close();
  73. underTest.execute(new TestComputationStepContext());
  74. // directory contains the uncompressed report (which contains only metadata.pb in this test)
  75. File unzippedDir = reportDirectoryHolder.getDirectory();
  76. assertThat(unzippedDir).isDirectory().exists();
  77. assertThat(unzippedDir.listFiles()).hasSize(1);
  78. assertThat(new File(unzippedDir, "metadata.pb")).hasContent("{metadata}");
  79. assertThat(logTester.logs(LoggerLevel.DEBUG)).anyMatch(log -> log.matches("Analysis report is \\d+ bytes uncompressed"));
  80. }
  81. private File generateReport() throws IOException {
  82. File zipDir = tempFolder.newDir();
  83. File metadataFile = new File(zipDir, "metadata.pb");
  84. FileUtils.write(metadataFile, "{metadata}");
  85. File zip = tempFolder.newFile();
  86. ZipUtils.zipDir(zipDir, zip);
  87. return zip;
  88. }
  89. }