org.assertj.core.util.DateUtil Java Examples

The following examples show how to use org.assertj.core.util.DateUtil. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CamundaEventingIT.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldEventHistoryTaskFollowUpDateChanges() {
  // given
  startEventingInstance();
  Task task = taskService.createTaskQuery().active().singleResult();
  eventCaptor.clear();

  Date now = DateUtil.now();

  // when
  task.setFollowUpDate(now);
  taskService.saveTask(task);

  // then
  HistoryEvent taskChangeEvent = eventCaptor.historyEvents.pop();
  assertThat(taskChangeEvent.getEventType()).isEqualTo("update");
  if (taskChangeEvent instanceof HistoricTaskInstanceEventEntity) {
    assertThat(((HistoricTaskInstanceEventEntity) taskChangeEvent).getFollowUpDate()).isEqualTo(now);
  } else {
    fail("Expected task instance change event");
  }
}
 
Example #2
Source File: CamundaEventingIT.java    From camunda-bpm-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldEventHistoryTaskFollowUpDateChanges() {
  // given
  startEventingInstance();
  Task task = taskService.createTaskQuery().active().singleResult();
  eventCaptor.clear();

  Date now = DateUtil.now();

  // when
  task.setFollowUpDate(now);
  taskService.saveTask(task);

  // then
  HistoryEvent taskChangeEvent = eventCaptor.historyEvents.pop();
  assertThat(taskChangeEvent.getEventType()).isEqualTo("update");
  if (taskChangeEvent instanceof HistoricTaskInstanceEventEntity) {
    assertThat(((HistoricTaskInstanceEventEntity) taskChangeEvent).getFollowUpDate()).isEqualTo(now);
  } else {
    fail("Expected task instance change event");
  }
}
 
Example #3
Source File: TokenHelperTest.java    From springboot-jwt-starter with MIT License 5 votes vote down vote up
@Test
public void getUsernameFromToken() throws Exception {
    when(timeProviderMock.now()).thenReturn(DateUtil.now());

    final String token = createToken(device);

    assertThat(tokenHelper.getUsernameFromToken(token)).isEqualTo(TEST_USERNAME);
}
 
Example #4
Source File: AuthenticationControllerTest.java    From springboot-jwt-starter with MIT License 5 votes vote down vote up
@Test
public void shouldRefreshExpiredMobileToken() throws Exception {
    Date beforeSomeTime = new Date(DateUtil.now().getTime() - 15 * 1000);
    when(timeProviderMock.now())
            .thenReturn(beforeSomeTime);
    device.setNormal(true);
    String token = createToken(device);
    this.mvc.perform(post("/auth/refresh").header("Authorization", "Bearer " + token))
            .andExpect(content().json("{access_token:null,expires_in:null}"));
}
 
Example #5
Source File: AuthenticationControllerTest.java    From springboot-jwt-starter with MIT License 5 votes vote down vote up
@Test
public void shouldNotRefreshExpiredWebToken() throws Exception {
    Date beforeSomeTime = new Date(DateUtil.now().getTime() - 15 * 1000);
    when(timeProviderMock.now())
            .thenReturn(beforeSomeTime);
    device.setNormal(true);
    String token = createToken(device);
    this.mvc.perform(post("/auth/refresh")
            .header("Authorization", "Bearer " + token))
            .andExpect(content().json("{access_token:null,expires_in:null}"));
}
 
Example #6
Source File: AuthenticationControllerTest.java    From springboot-jwt-starter with MIT License 5 votes vote down vote up
@Test
public void shouldGetEmptyTokenStateWhenGivenValidOldToken() throws Exception {
    when(timeProviderMock.now())
            .thenReturn(DateUtil.yesterday());
    this.mvc.perform(post("/auth/refresh")
            .header("Authorization", "Bearer 123"))
            .andExpect(content().json("{access_token:null,expires_in:null}"));
}
 
Example #7
Source File: AuthenticationControllerTest.java    From springboot-jwt-starter with MIT License 5 votes vote down vote up
@Before
public void setup() {

    mvc = MockMvcBuilders
            .webAppContextSetup(context)
            .apply(springSecurity())
            .build();

    User user = new User();
    user.setUsername("username");
    Authority authority = new Authority();
    authority.setId(0L);
    authority.setName( UserRoleName.ROLE_USER );
    List<Authority> authorities = Arrays.asList(authority);
    user.setAuthorities(authorities);
    user.setLastPasswordResetDate(new Timestamp(DateUtil.yesterday().getTime()));
    when(this.userDetailsService.loadUserByUsername(eq("testUser"))).thenReturn(user);
    MockitoAnnotations.initMocks(this);

    ReflectionTestUtils.setField(tokenHelper, "EXPIRES_IN", 100); // 100 sec
    ReflectionTestUtils.setField(tokenHelper, "MOBILE_EXPIRES_IN", 200); // 200 sec
    ReflectionTestUtils.setField(tokenHelper, "SECRET", "queenvictoria");

    device.setMobile(false);
    device.setNormal(false);
    device.setTablet(false);
}
 
Example #8
Source File: TokenHelperTest.java    From springboot-jwt-starter with MIT License 5 votes vote down vote up
@Test
public void canRefreshToken() throws Exception {
    when(timeProviderMock.now())
            .thenReturn(DateUtil.now())
            .thenReturn(DateUtil.tomorrow());
    String firstToken = createToken(device);
    String refreshedToken = tokenHelper.refreshToken(firstToken, device);
    Date firstTokenDate = tokenHelper.getIssuedAtDateFromToken(firstToken);
    Date refreshedTokenDate = tokenHelper.getIssuedAtDateFromToken(refreshedToken);
    assertThat(firstTokenDate).isBefore(refreshedTokenDate);
}
 
Example #9
Source File: TokenHelperTest.java    From springboot-jwt-starter with MIT License 5 votes vote down vote up
@Test
public void changedPasswordCannotBeRefreshed() throws Exception {
    when(timeProviderMock.now())
            .thenReturn(DateUtil.now());

    User user = mock(User.class);
    when(user.getLastPasswordResetDate()).thenReturn(new Timestamp(DateUtil.tomorrow().getTime()));
    String token = createToken(device);
    assertThat(tokenHelper.validateToken(token, user)).isFalse();
}
 
Example #10
Source File: TokenHelperTest.java    From springboot-jwt-starter with MIT License 5 votes vote down vote up
@Test
public void getAudienceFromMobileToken() throws Exception {
    when(timeProviderMock.now()).thenReturn(DateUtil.now());
    device.setMobile(true);
    final String token = createToken(this.device);
    assertThat(tokenHelper.getAudienceFromToken(token)).isEqualTo(tokenHelper.AUDIENCE_MOBILE);
}
 
Example #11
Source File: TokenHelperTest.java    From springboot-jwt-starter with MIT License 5 votes vote down vote up
@Test
public void getAudienceFromToken() throws Exception {
    when(timeProviderMock.now()).thenReturn(DateUtil.now());
    device.setNormal(true);
    final String token = createToken(this.device);

    assertThat(tokenHelper.getAudienceFromToken(token)).isEqualTo(tokenHelper.AUDIENCE_WEB);
}
 
Example #12
Source File: TokenHelperTest.java    From springboot-jwt-starter with MIT License 5 votes vote down vote up
@Test
public void expiredTokenCannotBeRefreshed() {
    when(timeProviderMock.now())
            .thenReturn(DateUtil.yesterday());

    String token = createToken(device);
    tokenHelper.refreshToken(token, device);
}
 
Example #13
Source File: TokenHelperTest.java    From springboot-jwt-starter with MIT License 5 votes vote down vote up
@Test
public void getCreatedDateFromToken() {
    final Date now = DateUtil.now();
    when(timeProviderMock.now()).thenReturn(now);

    final String token = createToken(device);

    assertThat(tokenHelper.getIssuedAtDateFromToken(token)).isInSameMinuteWindowAs(now);
}
 
Example #14
Source File: JwtTokenUtilTest.java    From spring-react-boilerplate with MIT License 5 votes vote down vote up
@Test
public void testGenerateTokenGeneratesDifferentTokensForDifferentCreationDates() throws Exception {
    when(clockMock.now())
        .thenReturn(DateUtil.yesterday())
        .thenReturn(DateUtil.now());

    final String token = createToken();
    final String laterToken = createToken();

    assertThat(token).isNotEqualTo(laterToken);
}
 
Example #15
Source File: TokenHelperTest.java    From springboot-jwt-starter with MIT License 5 votes vote down vote up
@Test
public void mobileTokenShouldExpire() {
    Date beforeSomeTime = new Date(DateUtil.now().getTime() - 20 * 1000);

    when(timeProviderMock.now())
            .thenReturn(beforeSomeTime);

    UserDetails userDetails = mock(User.class);
    when(userDetails.getUsername()).thenReturn(TEST_USERNAME);

    device.setMobile(true);
    final String mobileToken = createToken(device);
    assertThat(tokenHelper.validateToken(mobileToken, userDetails)).isFalse();
}
 
Example #16
Source File: TokenHelperTest.java    From springboot-jwt-starter with MIT License 5 votes vote down vote up
@Test
public void mobileTokenShouldLiveLonger() {
    Date beforeSomeTime = new Date(DateUtil.now().getTime() - 15 * 1000);

    UserDetails userDetails = mock(User.class);
    when(userDetails.getUsername()).thenReturn(TEST_USERNAME);

    when(timeProviderMock.now())
            .thenReturn(beforeSomeTime);
    device.setMobile(true);
    final String mobileToken = createToken(device);
    assertThat(tokenHelper.validateToken(mobileToken, userDetails)).isTrue();
}
 
Example #17
Source File: TokenHelperTest.java    From springboot-jwt-starter with MIT License 5 votes vote down vote up
@Test
public void testGenerateTokenGeneratesDifferentTokensForDifferentCreationDates() throws Exception {
    when(timeProviderMock.now())
            .thenReturn(DateUtil.yesterday())
            .thenReturn(DateUtil.now());

    final String token = createToken(device);
    final String laterToken = createToken(device);

    assertThat(token).isNotEqualTo(laterToken);
}
 
Example #18
Source File: JwtTokenUtilTest.java    From spring-react-boilerplate with MIT License 5 votes vote down vote up
@Test
public void canValidateToken() throws Exception {
    when(clockMock.now())
        .thenReturn(DateUtil.now());
    UserDetails userDetails = mock(JwtUser.class);
    when(userDetails.getUsername()).thenReturn(TEST_USERNAME);
    when(userDetails.getAuthorities()).thenReturn((Collection) grantedAuthorities);

    String token = createToken();
    assertThat(jwtTokenUtil.validateToken(token, userDetails)).isTrue();
}
 
Example #19
Source File: JwtTokenUtilTest.java    From spring-react-boilerplate with MIT License 5 votes vote down vote up
@Test
public void canRefreshToken() throws Exception {
    when(clockMock.now())
        .thenReturn(DateUtil.now())
        .thenReturn(DateUtil.tomorrow());
    String firstToken = createToken();
    String refreshedToken = jwtTokenUtil.refreshToken(firstToken);
    Date firstTokenDate = jwtTokenUtil.getIssuedAtDateFromToken(firstToken);
    Date refreshedTokenDate = jwtTokenUtil.getIssuedAtDateFromToken(refreshedToken);
    assertThat(firstTokenDate).isBefore(refreshedTokenDate);
}
 
Example #20
Source File: JwtTokenUtilTest.java    From spring-react-boilerplate with MIT License 5 votes vote down vote up
@Test
public void notExpiredCanBeRefreshed() {
    when(clockMock.now())
        .thenReturn(DateUtil.now());
    String token = createToken();
    assertThat(jwtTokenUtil.canTokenBeRefreshed(token, DateUtil.yesterday())).isTrue();
}
 
Example #21
Source File: JwtTokenUtilTest.java    From spring-react-boilerplate with MIT License 5 votes vote down vote up
@Test
public void changedPasswordCannotBeRefreshed() throws Exception {
    when(clockMock.now())
        .thenReturn(DateUtil.now());
    String token = createToken();
    assertThat(jwtTokenUtil.canTokenBeRefreshed(token, DateUtil.tomorrow())).isFalse();
}
 
Example #22
Source File: JwtTokenUtilTest.java    From spring-react-boilerplate with MIT License 5 votes vote down vote up
@Test(expected = ExpiredJwtException.class)
public void expiredTokenCannotBeRefreshed() throws Exception {
    when(clockMock.now())
        .thenReturn(DateUtil.yesterday());
    String token = createToken();
    jwtTokenUtil.canTokenBeRefreshed(token, DateUtil.tomorrow());
}
 
Example #23
Source File: JwtTokenUtilTest.java    From spring-react-boilerplate with MIT License 5 votes vote down vote up
@Test
public void getExpirationDateFromToken() throws Exception {
    final Date now = DateUtil.now();
    when(clockMock.now()).thenReturn(now);
    final String token = createToken();

    final Date expirationDateFromToken = jwtTokenUtil.getExpirationDateFromToken(token);
    assertThat(DateUtil.timeDifference(expirationDateFromToken, now)).isCloseTo(3600000L, within(1000L));
}
 
Example #24
Source File: JwtTokenUtilTest.java    From spring-react-boilerplate with MIT License 5 votes vote down vote up
@Test
public void getCreatedDateFromToken() throws Exception {
    final Date now = DateUtil.now();
    when(clockMock.now()).thenReturn(now);

    final String token = createToken();

    assertThat(jwtTokenUtil.getIssuedAtDateFromToken(token)).isInSameMinuteWindowAs(now);
}
 
Example #25
Source File: JwtTokenUtilTest.java    From spring-react-boilerplate with MIT License 5 votes vote down vote up
@Test
public void getUsernameFromToken() throws Exception {
    when(clockMock.now()).thenReturn(DateUtil.now());

    final String token = createToken();

    assertThat(jwtTokenUtil.getUsernameFromToken(token)).isEqualTo(TEST_USERNAME);
}
 
Example #26
Source File: JWTAuthenticationTest.java    From SMSC with Apache License 2.0 4 votes vote down vote up
private Map<String, Object> createClaims(String creationDate) {
    Map<String, Object> claims = new HashMap<>();
    claims.put(CLAIM_KEY_USERNAME, "testUser");
    claims.put(CLAIM_KEY_CREATED, DateUtil.parseDatetime(creationDate));
    return claims;
}