Skip to content

Commit

Permalink
Deleted a method used only for tests, revamp tests a little to ensure…
Browse files Browse the repository at this point in the history
… testing properly in the same class as the unit test.
  • Loading branch information
chikitulfo committed Dec 16, 2022
1 parent 3f77824 commit ca3225f
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,6 @@ private void rotate() {
removeOldFiles();
}

protected static String computePattern(ZonedDateTime initInstant, Path basePattern) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(ZoneId.systemDefault());
String formattedInstant = formatter.format(initInstant);
String computedFileName = String.format("%s-%s", FilenameUtils.getName(basePattern.toString()) , formattedInstant);
return computedFileName;
}

protected String computePattern() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(ZoneId.systemDefault());
String formattedInstant = formatter.format(initInstant);
Expand Down
35 changes: 0 additions & 35 deletions src/test/java/hudson/plugins/audit_trail/AuditTrailFilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,39 +81,4 @@ public void createItemLogsTheNewItemName() throws Exception {
String log = Util.loadFile(new File(tmpDir.getRoot(), "create-item.log.0"), StandardCharsets.UTF_8);
Assert.assertTrue("logged actions: " + log, Pattern.compile(".*createItem \\(" + jobName + "\\).*by \\QNA from 127.0.0.1\\E.*", Pattern.DOTALL).matcher(log).matches());
}

@Test
public void createItemLogsTheNewItemNameWithRotateDaily() throws Exception {
File logFile = new File(tmpDir.getRoot(), "create-item.log");
JenkinsRule.WebClient wc = j.createWebClient();
new SimpleAuditTrailPluginConfiguratorHelper(logFile).sendConfigurationToRotateDaily(j, wc);

String jobName = "Job With Space";
HtmlPage configure = wc.goTo("view/all/newJob");
HtmlForm form = configure.getFormByName("createItem");
form.getInputByName("name").setValueAttribute(jobName);
form.getInputByName("name").blur();
// not clear to me why the input is not visible in the test (yet it exists in the page)
// for some reason the two next calls are needed
form.getInputByValue("hudson.model.FreeStyleProject").click(false, false, false, true, false, true, false);
form.getInputByValue("hudson.model.FreeStyleProject").setChecked(true);
wc.waitForBackgroundJavaScript(50);
j.submit(form);

ZonedDateTime initInstant = ZonedDateTime.now();
String logRotateComputedName = LogFileAuditLogger.computePattern(initInstant, Paths.get(logFile.getPath()));

// Check that a file with the corresponded expected format was created
Path logFileRotating = tmpDir.getRoot().toPath().resolve(logRotateComputedName);
Assert.assertTrue(logFileRotating.toFile().exists());

// Check that the action was logged in the file
String log = Util.loadFile(new File(tmpDir.getRoot(), logRotateComputedName), StandardCharsets.UTF_8);
Assert.assertTrue("logged actions: " + log, Pattern.compile(".*createItem \\(" + jobName + "\\).*by \\QNA from 127.0.0.1\\E.*", Pattern.DOTALL).matcher(log).matches());

// Check that there is only one daily log file in the directory
String directoryPath = logFile.getParent();
Collection<File> directoryFiles = FileUtils.listFiles(new File(directoryPath), new RegexFileFilter(".*" + logFile.getName() + LogFileAuditLogger.DAILY_ROTATING_FILE_REGEX_PATTERN), DirectoryFileFilter.DIRECTORY);
Assert.assertEquals(directoryFiles.size(), 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
import java.time.ZonedDateTime;
import java.util.Collection;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.*;

/**
* Created by Pierre Beitz
* on 2019-05-05.
Expand Down Expand Up @@ -97,9 +93,11 @@ public void logFileIsReusedIfRestartedWithDailyRotation() throws IOException {
*/
@Test
public void logFileProperlyRotatingInNextDayWithDailyRotation() throws IOException {
ZonedDateTime zonedDateTimeA = ZonedDateTime.now().plusDays(1);
ZonedDateTime zonedDateTime1 = ZonedDateTime.now();
ZonedDateTime zonedDateTime2 = zonedDateTime1.plusDays(1);
MockedStatic<ZonedDateTime> mockedLocalDateTime = Mockito.mockStatic(ZonedDateTime.class, Mockito.withSettings().defaultAnswer(Answers.CALLS_REAL_METHODS));

mockedLocalDateTime.when(ZonedDateTime::now).thenReturn(zonedDateTime1);
// Check that the log file is created with the corresponded format (Today date)
Path logFile = folder.getRoot().toPath().resolve("file");
LogFileAuditLogger logFileAuditLogger = new LogFileAuditLogger(logFile.toString(), 0, 2, null, true);
Expand All @@ -117,7 +115,7 @@ public void logFileProperlyRotatingInNextDayWithDailyRotation() throws IOExcepti
Assert.assertTrue(log.contains("configuringAFileLoggerRotatingDaily - line1"));

// Increase +1 day
mockedLocalDateTime.when(ZonedDateTime::now).thenReturn(zonedDateTimeA);
mockedLocalDateTime.when(ZonedDateTime::now).thenReturn(zonedDateTime2);

// Log something else
logFileAuditLogger.log("configuringAFileLoggerRotatingDaily - line2");
Expand Down

0 comments on commit ca3225f

Please sign in to comment.