Skip to content

Commit

Permalink
#31442 add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinogiardino committed Feb 24, 2025
1 parent 42f60fa commit 3db063d
Showing 1 changed file with 96 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
package com.dotcms.util;

import com.dotcms.api.web.HttpServletRequestThreadLocal;
import com.dotmarketing.util.DateUtil;
import org.junit.BeforeClass;
import org.junit.Test;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.text.ParseException;
import java.time.Duration;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Optional;

import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.*;

public class TimeMachineUtilTest {

@BeforeClass
public static void prepare() throws Exception {
//Setting web app environment
IntegrationTestInitService.getInstance().init();
mockStatic(DateUtil.class);
}

/**
Expand Down Expand Up @@ -95,4 +101,91 @@ public void whenTimeMachineIsNotRunningShouldReturnTrue(){

assertTrue(TimeMachineUtil.isRunning());
}
}

/**
* Method to Test: {@link TimeMachineUtil#parseTimeMachineDate(String)}
* When: When the input date is null
* Should: Return an empty {@link Optional}
*/
@Test
public void testParseTimeMachineDate_NullDate() {
Optional<Instant> result = TimeMachineUtil.parseTimeMachineDate(null);
assertFalse(result.isPresent());
}

/**
* Method to Test: {@link TimeMachineUtil#parseTimeMachineDate(String)}
* When: When the input date is invalid
* Should: Throw an {@link IllegalArgumentException}
*/
@Test(expected = IllegalArgumentException.class)
public void testParseTimeMachineDate_InvalidDate() {
TimeMachineUtil.parseTimeMachineDate("invalid-date");
}

/**
* Method to Test: {@link TimeMachineUtil#parseTimeMachineDate(String)}
* When: When the input date is valid and within the grace window
* Should: Return an empty {@link Optional}
*/
@Test
public void testParseTimeMachineDate_ValidDateWithinGraceWindow() throws ParseException {
Instant now = Instant.now().plus(Duration.ofMinutes(3));

// Format the Instant to a string that DateUtil.convertDate can parse
DateTimeFormatter formatter = DateTimeFormatter.ISO_INSTANT;
String dateWithinGraceWindow = formatter.format(now);

// Convert Instant to Date
Date dateObject = Date.from(now);
when(DateUtil.convertDate(dateWithinGraceWindow)).thenReturn(dateObject);

Optional<Instant> result = TimeMachineUtil.parseTimeMachineDate(dateWithinGraceWindow);
assertTrue(result.isEmpty());
}

/**
* Method to Test: {@link TimeMachineUtil#parseTimeMachineDate(String)}
* When: When the input date is valid and outside the grace window
* Should: Return a present {@link Optional} with the parsed date
*/
@Test
public void testParseTimeMachineDate_ValidDateOutsideGraceWindow() throws ParseException {
Instant now = Instant.now().plus(Duration.ofMinutes(10));

// Format the Instant to a string that DateUtil.convertDate can parse
DateTimeFormatter formatter = DateTimeFormatter.ISO_INSTANT;
String dateWithinGraceWindow = formatter.format(now);

Date dateObject = Date.from(now);
when(DateUtil.convertDate(dateWithinGraceWindow)).thenReturn(dateObject);

Optional<Instant> result = TimeMachineUtil.parseTimeMachineDate(dateWithinGraceWindow);
assertTrue(result.isPresent());
assertEquals(now.truncatedTo(ChronoUnit.SECONDS), result.get().truncatedTo(ChronoUnit.SECONDS));
}

/**
* Method to Test: {@link TimeMachineUtil#isOlderThanGraceWindow(Instant)}
* When: When the input date is within the grace window
* Should: Return false
*/
@Test
public void testIsOlderThanGraceWindow_shouldReturnTrue() {
Instant now = Instant.now();
Instant futureDate= now.plus(Duration.ofMinutes(5)); // 5 minutes in the future
assertFalse(TimeMachineUtil.isOlderThanGraceWindow(futureDate));
}

/**
* Method to Test: {@link TimeMachineUtil#isOlderThanGraceWindow(Instant)}
* When: When the input date is outside the grace window
* Should: Return true
*/
@Test
public void testIsOlderThanGraceWindow_shouldReturnFalse() {
Instant now = Instant.now();
Instant futureDate = now.plus(Duration.ofMinutes(6)); // 6 minutes in the future
assertTrue(TimeMachineUtil.isOlderThanGraceWindow(futureDate));
}
}

0 comments on commit 3db063d

Please sign in to comment.