private static Date truncateToSecondsImpl(Date d) { Instant instant = d.toInstant(); instant = instant.truncatedTo(ChronoUnit.SECONDS); return Date.from(instant); }
/** * Truncated the analysis date to seconds before comparing it to {@link Issue#creationDate()} is required because * {@link DefaultIssue#setCreationDate(Date)} does it. */ private static long truncateToSeconds(long analysisDate) { Instant instant = new Date(analysisDate).toInstant(); instant = instant.truncatedTo(ChronoUnit.SECONDS); return Date.from(instant).getTime(); }
private static Date truncateMillis(@Nullable Date d) { if (d == null) { return null; } return Date.from(d.toInstant().truncatedTo(ChronoUnit.SECONDS)); } }
private boolean validateIfUnmodifiedSince(Instant lastModified) { if (lastModified.isBefore(Instant.EPOCH)) { return false; } long ifUnmodifiedSince = getRequestHeaders().getIfUnmodifiedSince(); if (ifUnmodifiedSince == -1) { return false; } // We will perform this validation... Instant sinceInstant = Instant.ofEpochMilli(ifUnmodifiedSince); this.notModified = sinceInstant.isBefore(lastModified.truncatedTo(ChronoUnit.SECONDS)); return true; }
private static String logDate(Instant instant) { return DateUtils.formatDate(instant.truncatedTo(ChronoUnit.SECONDS)); } }
private boolean validateIfUnmodifiedSince(Instant lastModified) { if (lastModified.isBefore(Instant.EPOCH)) { return false; } long ifUnmodifiedSince = getRequestHeaders().getIfUnmodifiedSince(); if (ifUnmodifiedSince == -1) { return false; } // We will perform this validation... Instant sinceInstant = Instant.ofEpochMilli(ifUnmodifiedSince); this.notModified = sinceInstant.isBefore(lastModified.truncatedTo(ChronoUnit.SECONDS)); return true; }
@Before public void setup() throws URISyntaxException { this.currentDate = Instant.now().truncatedTo(ChronoUnit.SECONDS); this.dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US); this.dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); }
@GET @Path("/info/{date}") public Response info(@PathParam("date") String date) { Instant instant = DateUtil.parseInstant(date); List<BackupMetadata> metadataList = backupStatusMgr.getLatestBackupMetadata( BackupVersion.SNAPSHOT_META_SERVICE, new DateRange( instant, instant.plus(1, ChronoUnit.DAYS).truncatedTo(ChronoUnit.DAYS))); return Response.ok(metadataList).build(); }
/** * Load closed issues for the specified Component, which have at least one line diff in changelog AND are * neither hotspots nor manual vulnerabilities. * <p> * Closed issues do not have a line number in DB (it is unset when the issue is closed), this method * returns {@link DefaultIssue} objects which line number is populated from the most recent diff logging * the removal of the line. Closed issues which do not have such diff are not loaded. * <p> * To not depend on purge configuration of closed issues, only issues which close date is less than 30 days ago at * 00H00 are returned. */ public List<DefaultIssue> loadClosedIssues(String componentUuid) { if (closedIssueMaxAge == 0) { return emptyList(); } Date date = new Date(system2.now()); long closeDateAfter = date.toInstant() .minus(closedIssueMaxAge, ChronoUnit.DAYS) .truncatedTo(ChronoUnit.DAYS) .toEpochMilli(); try (DbSession dbSession = dbClient.openSession(false)) { return loadClosedIssues(dbSession, componentUuid, closeDateAfter); } }
public List<BackupMetadata> getLatestBackupMetadata( BackupVersion backupVersion, DateUtil.DateRange dateRange) { Instant startDay = dateRange.getStartTime().truncatedTo(ChronoUnit.DAYS); Instant endDay = dateRange.getEndTime().truncatedTo(ChronoUnit.DAYS);
@Test public void handleReturnValueLastModified() throws Exception { Instant currentTime = Instant.now().truncatedTo(ChronoUnit.SECONDS); Instant oneMinAgo = currentTime.minusSeconds(60); long timestamp = currentTime.toEpochMilli(); MockServerWebExchange exchange = MockServerWebExchange.from(get("/path").ifModifiedSince(timestamp)); ResponseEntity<String> entity = ok().lastModified(oneMinAgo.toEpochMilli()).body("body"); MethodParameter returnType = on(TestController.class).resolveReturnType(entity(String.class)); HandlerResult result = handlerResult(entity, returnType); this.resultHandler.handleResult(exchange, result).block(Duration.ofSeconds(5)); assertConditionalResponse(exchange, HttpStatus.NOT_MODIFIED, null, null, oneMinAgo); }
@Test public void handleReturnValueChangedETagAndLastModified() throws Exception { String etag = "\"deadb33f8badf00d\""; String newEtag = "\"changed-etag-value\""; Instant currentTime = Instant.now().truncatedTo(ChronoUnit.SECONDS); Instant oneMinAgo = currentTime.minusSeconds(60); MockServerWebExchange exchange = MockServerWebExchange.from(get("/path") .ifNoneMatch(etag) .ifModifiedSince(currentTime.toEpochMilli()) ); ResponseEntity<String> entity = ok().eTag(newEtag).lastModified(oneMinAgo.toEpochMilli()).body("body"); MethodParameter returnType = on(TestController.class).resolveReturnType(entity(String.class)); HandlerResult result = handlerResult(entity, returnType); this.resultHandler.handleResult(exchange, result).block(Duration.ofSeconds(5)); assertConditionalResponse(exchange, HttpStatus.OK, "body", newEtag, oneMinAgo); }
@Test public void handleReturnValueETagAndLastModified() throws Exception { String eTag = "\"deadb33f8badf00d\""; Instant currentTime = Instant.now().truncatedTo(ChronoUnit.SECONDS); Instant oneMinAgo = currentTime.minusSeconds(60); MockServerWebExchange exchange = MockServerWebExchange.from(get("/path") .ifNoneMatch(eTag) .ifModifiedSince(currentTime.toEpochMilli()) ); ResponseEntity<String> entity = ok().eTag(eTag).lastModified(oneMinAgo.toEpochMilli()).body("body"); MethodParameter returnType = on(TestController.class).resolveReturnType(entity(String.class)); HandlerResult result = handlerResult(entity, returnType); this.resultHandler.handleResult(exchange, result).block(Duration.ofSeconds(5)); assertConditionalResponse(exchange, HttpStatus.NOT_MODIFIED, null, eTag, oneMinAgo); }
@Test public void sessionInformationForExistingSession() { Session session = createSession(SESSION_ID, USER_NAME, NOW); when(this.sessionRepository.findById(SESSION_ID)).thenReturn(session); SessionInformation sessionInfo = this.sessionRegistry .getSessionInformation(SESSION_ID); assertThat(sessionInfo.getSessionId()).isEqualTo(SESSION_ID); assertThat( sessionInfo.getLastRequest().toInstant().truncatedTo(ChronoUnit.MILLIS)) .isEqualTo(NOW.truncatedTo(ChronoUnit.MILLIS)); assertThat(sessionInfo.getPrincipal()).isEqualTo(USER_NAME); assertThat(sessionInfo.isExpired()).isFalse(); }
@Test public void sessionInformationForExpiredSession() { Session session = createSession(SESSION_ID, USER_NAME, NOW); session.setAttribute(SpringSessionBackedSessionInformation.EXPIRED_ATTR, Boolean.TRUE); when(this.sessionRepository.findById(SESSION_ID)).thenReturn(session); SessionInformation sessionInfo = this.sessionRegistry .getSessionInformation(SESSION_ID); assertThat(sessionInfo.getSessionId()).isEqualTo(SESSION_ID); assertThat( sessionInfo.getLastRequest().toInstant().truncatedTo(ChronoUnit.MILLIS)) .isEqualTo(NOW.truncatedTo(ChronoUnit.MILLIS)); assertThat(sessionInfo.getPrincipal()).isEqualTo(USER_NAME); assertThat(sessionInfo.isExpired()).isTrue(); }
@Test public void testRecordWithJsr310LogicalTypes() throws IOException { TestRecordWithJsr310LogicalTypes record = new TestRecordWithJsr310LogicalTypes( true, 34, 35L, 3.14F, 3019.34, null, java.time.LocalDate.now(), java.time.LocalTime.now().truncatedTo(ChronoUnit.MILLIS), java.time.Instant.now().truncatedTo(ChronoUnit.MILLIS), new BigDecimal(123.45f).setScale(2, RoundingMode.HALF_DOWN) ); File data = write(TestRecordWithJsr310LogicalTypes.getClassSchema(), record); List<TestRecordWithJsr310LogicalTypes> actual = read( TestRecordWithJsr310LogicalTypes.getClassSchema(), data); Assert.assertEquals("Should match written record", record, actual.get(0)); }
@Test public void findByPrincipalName() { Instant lastAccessed = Instant.now().minusMillis(10); Instant createdTime = lastAccessed.minusMillis(10); Duration maxInactive = Duration.ofHours(1); String sessionId = "some-id"; given(this.redisOperations.boundSetOps(anyString())) .willReturn(this.boundSetOperations); given(this.boundSetOperations.members()) .willReturn(Collections.singleton(sessionId)); given(this.redisOperations.boundHashOps(getKey(sessionId))) .willReturn(this.boundHashOperations); Map map = map(RedisOperationsSessionRepository.CREATION_TIME_ATTR, createdTime.toEpochMilli(), RedisOperationsSessionRepository.MAX_INACTIVE_ATTR, (int) maxInactive.getSeconds(), RedisOperationsSessionRepository.LAST_ACCESSED_ATTR, lastAccessed.toEpochMilli()); given(this.boundHashOperations.entries()).willReturn(map); Map<String, RedisSession> sessionIdToSessions = this.redisRepository .findByIndexNameAndIndexValue( FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, "principal"); assertThat(sessionIdToSessions).hasSize(1); RedisSession session = sessionIdToSessions.get(sessionId); assertThat(session).isNotNull(); assertThat(session.getId()).isEqualTo(sessionId); assertThat(session.getLastAccessedTime().truncatedTo(ChronoUnit.MILLIS)) .isEqualTo(lastAccessed.truncatedTo(ChronoUnit.MILLIS)); assertThat(session.getMaxInactiveInterval()).isEqualTo(maxInactive); assertThat(session.getCreationTime().truncatedTo(ChronoUnit.MILLIS)) .isEqualTo(createdTime.truncatedTo(ChronoUnit.MILLIS)); }
assertThat(session.<String>getAttribute(attribute2)) .isEqualTo(expected.getAttribute(attribute2)); assertThat(session.getCreationTime().truncatedTo(ChronoUnit.MILLIS)) .isEqualTo(expected.getCreationTime() .truncatedTo(ChronoUnit.MILLIS)); assertThat(session.getMaxInactiveInterval()) .isEqualTo(expected.getMaxInactiveInterval()); assertThat( session.getLastAccessedTime().truncatedTo(ChronoUnit.MILLIS)) .isEqualTo(expected.getLastAccessedTime() .truncatedTo(ChronoUnit.MILLIS)); }).verifyComplete();
assertThat(session.<String>getAttribute(attribute2)) .isEqualTo(expected.getAttribute(attribute2)); assertThat(session.getCreationTime().truncatedTo(ChronoUnit.MILLIS)) .isEqualTo(expected.getCreationTime().truncatedTo(ChronoUnit.MILLIS)); assertThat(session.getMaxInactiveInterval()) .isEqualTo(expected.getMaxInactiveInterval()); assertThat(session.getLastAccessedTime().truncatedTo(ChronoUnit.MILLIS)) .isEqualTo(expected.getLastAccessedTime().truncatedTo(ChronoUnit.MILLIS));
java.time.LocalDate.now(), java.time.LocalTime.now().truncatedTo(ChronoUnit.MILLIS), java.time.Instant.now().truncatedTo(ChronoUnit.MILLIS), new BigDecimal(123.45f).setScale(2, RoundingMode.HALF_DOWN) );