List of usage examples for java.time Instant toEpochMilli
public long toEpochMilli()
From source file:org.noorganization.instalist.server.api.ProductResourceTest.java
@Test public void testPutProduct() throws Exception { String url = "/groups/%d/products/%s"; Instant preUpdate = mProduct.getUpdated(); ProductInfo updatedProduct = new ProductInfo().withDeleted(false).withName("changedproduct"); Response notAuthorizedResponse = target(String.format(url, mGroup.getId(), mProduct.getUUID().toString())) .request().put(Entity.json(updatedProduct)); assertEquals(401, notAuthorizedResponse.getStatus()); Response wrongAuthResponse = target(String.format(url, mGroup.getId(), mProduct.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token wrongauth").put(Entity.json(updatedProduct)); assertEquals(401, wrongAuthResponse.getStatus()); Response wrongGroupResponse = target(String.format(url, mNAGroup.getId(), mNAProduct.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedProduct)); assertEquals(401, wrongGroupResponse.getStatus()); Response wrongListResponse = target(String.format(url, mGroup.getId(), mNAProduct.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedProduct)); assertEquals(404, wrongListResponse.getStatus()); Response goneResponse = target(String.format(url, mGroup.getId(), mDeletedProduct.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedProduct)); assertEquals(410, goneResponse.getStatus()); mManager.refresh(mProduct);//from www . j a va 2 s. c o m assertEquals("product1", mProduct.getName()); updatedProduct.setLastChanged(new Date(preUpdate.toEpochMilli() - 10000)); Response conflictResponse = target(String.format(url, mGroup.getId(), mProduct.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedProduct)); assertEquals(409, conflictResponse.getStatus()); mManager.refresh(mProduct); assertEquals("product1", mProduct.getName()); updatedProduct.setLastChanged(Date.from(Instant.now())); Response okResponse = target(String.format(url, mGroup.getId(), mProduct.getUUID().toString())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedProduct)); assertEquals(200, okResponse.getStatus()); mManager.refresh(mProduct); assertEquals("changedproduct", mProduct.getName()); assertEquals(1f, mProduct.getDefaultAmount(), 0.001f); assertEquals(0.5f, mProduct.getStepAmount(), 0.001f); assertNull(mProduct.getUnit()); assertTrue(preUpdate + " is not before " + mProduct.getUpdated(), preUpdate.isBefore(mProduct.getUpdated())); }
From source file:org.noorganization.instalist.server.api.IngredientResourceTest.java
@Test public void testPutIngredient() throws Exception { String url = "/groups/%d/ingredients/%s"; Instant preUpdate = mIngredient.getUpdated(); IngredientInfo updatedIngred = new IngredientInfo().withAmount(3f); Response notAuthorizedResponse = target( String.format(url, mGroup.getId(), mIngredient.getUUID().toString())).request() .put(Entity.json(updatedIngred)); assertEquals(401, notAuthorizedResponse.getStatus()); Response wrongAuthResponse = target(String.format(url, mGroup.getId(), mIngredient.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token wrongauth").put(Entity.json(updatedIngred)); assertEquals(401, wrongAuthResponse.getStatus()); Response wrongGroupResponse = target( String.format(url, mNAGroup.getId(), mNAIngredient.getUUID().toString())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedIngred)); assertEquals(401, wrongGroupResponse.getStatus()); Response wrongListResponse = target(String.format(url, mGroup.getId(), mNAIngredient.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedIngred)); assertEquals(404, wrongListResponse.getStatus()); Response goneResponse = target(String.format(url, mGroup.getId(), mDeletedIngredient.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedIngred)); assertEquals(410, goneResponse.getStatus()); mManager.refresh(mIngredient);//from w ww . j av a 2 s . c om assertEquals(1f, mIngredient.getAmount(), 0.001f); updatedIngred.setLastChanged(new Date(preUpdate.toEpochMilli() - 10000)); Response conflictResponse = target(String.format(url, mGroup.getId(), mIngredient.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedIngred)); assertEquals(409, conflictResponse.getStatus()); mManager.refresh(mIngredient); assertEquals(1f, mIngredient.getAmount(), 0.001f); updatedIngred.setLastChanged(Date.from(Instant.now())); Response okResponse = target(String.format(url, mGroup.getId(), mIngredient.getUUID().toString())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedIngred)); assertEquals(200, okResponse.getStatus()); mManager.refresh(mIngredient); assertEquals(3f, mIngredient.getAmount(), 0.001f); assertTrue(preUpdate + " is not before " + mIngredient.getUpdated(), preUpdate.isBefore(mIngredient.getUpdated())); }
From source file:org.noorganization.instalist.server.api.TaggedProductResourceTest.java
@Test public void testPutTaggedProduct() throws Exception { String url = "/groups/%d/taggedproducts/%s"; Instant preUpdate = mTaggedProduct.getUpdated(); TaggedProductInfo updatedTP = new TaggedProductInfo().withTagUUID(mNATag.getUUID()); Response notAuthorizedResponse = target( String.format(url, mGroup.getId(), mTaggedProduct.getUUID().toString())).request() .put(Entity.json(updatedTP)); assertEquals(401, notAuthorizedResponse.getStatus()); Response wrongAuthResponse = target(String.format(url, mGroup.getId(), mTaggedProduct.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token wrongauth").put(Entity.json(updatedTP)); assertEquals(401, wrongAuthResponse.getStatus()); Response wrongGroupResponse = target( String.format(url, mNAGroup.getId(), mNATaggedProduct.getUUID().toString())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedTP)); assertEquals(401, wrongGroupResponse.getStatus()); Response wrongListResponse = target( String.format(url, mGroup.getId(), mNATaggedProduct.getUUID().toString())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedTP)); assertEquals(404, wrongListResponse.getStatus()); Response goneResponse = target(String.format(url, mGroup.getId(), mDeletedIngredient.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedTP)); assertEquals(410, goneResponse.getStatus()); Response wrongRefResponse = target(String.format(url, mGroup.getId(), mTaggedProduct.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedTP)); assertEquals(400, wrongRefResponse.getStatus()); mManager.refresh(mTaggedProduct);//w w w .j a va 2 s. c o m assertEquals(mTag.getUUID(), mTaggedProduct.getTag().getUUID()); updatedTP.setTagUUID(mTag.getUUID()); updatedTP.setLastChanged(new Date(preUpdate.toEpochMilli() - 10000)); Response conflictResponse = target(String.format(url, mGroup.getId(), mTaggedProduct.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedTP)); assertEquals(409, conflictResponse.getStatus()); mManager.refresh(mTaggedProduct); assertEquals(mTag.getUUID(), mTaggedProduct.getTag().getUUID()); updatedTP.setLastChanged(Date.from(Instant.now())); Response okResponse = target(String.format(url, mGroup.getId(), mTaggedProduct.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedTP)); assertEquals(200, okResponse.getStatus()); mManager.refresh(mTaggedProduct); assertTrue(preUpdate + " is not before " + mTaggedProduct.getUpdated(), preUpdate.isBefore(mTaggedProduct.getUpdated())); }
From source file:com.netflix.genie.common.dto.JobTest.java
/** * Test to make sure can build a valid Job with optional parameters. *///from w w w . ja va 2 s . c o m @Test public void canBuildJobWithOptionals() { final Job.Builder builder = new Job.Builder(NAME, USER, VERSION); builder.withCommandArgs(COMMAND_ARGS); final String archiveLocation = UUID.randomUUID().toString(); builder.withArchiveLocation(archiveLocation); final String clusterName = UUID.randomUUID().toString(); builder.withClusterName(clusterName); final String commandName = UUID.randomUUID().toString(); builder.withCommandName(commandName); final Instant finished = Instant.now(); builder.withFinished(finished); final Instant started = Instant.now(); builder.withStarted(started); builder.withStatus(JobStatus.SUCCEEDED); final String statusMsg = UUID.randomUUID().toString(); builder.withStatusMsg(statusMsg); final Instant created = Instant.now(); builder.withCreated(created); final String description = UUID.randomUUID().toString(); builder.withDescription(description); final String id = UUID.randomUUID().toString(); builder.withId(id); final Set<String> tags = Sets.newHashSet(UUID.randomUUID().toString(), UUID.randomUUID().toString(), UUID.randomUUID().toString()); builder.withTags(tags); final Instant updated = Instant.now(); builder.withUpdated(updated); final String grouping = UUID.randomUUID().toString(); builder.withGrouping(grouping); final String groupingInstance = UUID.randomUUID().toString(); builder.withGroupingInstance(groupingInstance); final Job job = builder.build(); Assert.assertThat(job.getName(), Matchers.is(NAME)); Assert.assertThat(job.getUser(), Matchers.is(USER)); Assert.assertThat(job.getVersion(), Matchers.is(VERSION)); Assert.assertThat(job.getCommandArgs().orElseThrow(IllegalArgumentException::new), Matchers.is(StringUtils.join(COMMAND_ARGS, StringUtils.SPACE))); Assert.assertThat(job.getArchiveLocation().orElseThrow(IllegalArgumentException::new), Matchers.is(archiveLocation)); Assert.assertThat(job.getClusterName().orElseThrow(IllegalArgumentException::new), Matchers.is(clusterName)); Assert.assertThat(job.getCommandName().orElseThrow(IllegalArgumentException::new), Matchers.is(commandName)); Assert.assertThat(job.getFinished().orElseThrow(IllegalArgumentException::new), Matchers.is(finished)); Assert.assertThat(job.getStarted().orElseThrow(IllegalArgumentException::new), Matchers.is(started)); Assert.assertThat(job.getStatus(), Matchers.is(JobStatus.SUCCEEDED)); Assert.assertThat(job.getStatusMsg().orElseThrow(IllegalArgumentException::new), Matchers.is(statusMsg)); Assert.assertThat(job.getCreated().orElseThrow(IllegalArgumentException::new), Matchers.is(created)); Assert.assertThat(job.getDescription().orElseThrow(IllegalArgumentException::new), Matchers.is(description)); Assert.assertThat(job.getId().orElseThrow(IllegalArgumentException::new), Matchers.is(id)); Assert.assertThat(job.getTags(), Matchers.is(tags)); Assert.assertThat(job.getUpdated().orElseThrow(IllegalArgumentException::new), Matchers.is(updated)); Assert.assertThat(job.getRuntime(), Matchers.is(Duration.ofMillis(finished.toEpochMilli() - started.toEpochMilli()))); Assert.assertThat(job.getGrouping().orElseThrow(IllegalArgumentException::new), Matchers.is(grouping)); Assert.assertThat(job.getGroupingInstance().orElseThrow(IllegalArgumentException::new), Matchers.is(groupingInstance)); }
From source file:com.netflix.genie.common.dto.JobTest.java
/** * Test to make sure can build a valid Job with optional parameters. *//*from w ww . j ava2 s. c o m*/ @Test @SuppressWarnings("deprecation") public void canBuildJobWithOptionalsDeprecated() { final Job.Builder builder = new Job.Builder(NAME, USER, VERSION); builder.withCommandArgs(StringUtils.join(COMMAND_ARGS, StringUtils.SPACE)); final String archiveLocation = UUID.randomUUID().toString(); builder.withArchiveLocation(archiveLocation); final String clusterName = UUID.randomUUID().toString(); builder.withClusterName(clusterName); final String commandName = UUID.randomUUID().toString(); builder.withCommandName(commandName); final Instant finished = Instant.now(); builder.withFinished(finished); final Instant started = Instant.now(); builder.withStarted(started); builder.withStatus(JobStatus.SUCCEEDED); final String statusMsg = UUID.randomUUID().toString(); builder.withStatusMsg(statusMsg); final Instant created = Instant.now(); builder.withCreated(created); final String description = UUID.randomUUID().toString(); builder.withDescription(description); final String id = UUID.randomUUID().toString(); builder.withId(id); final Set<String> tags = Sets.newHashSet(UUID.randomUUID().toString(), UUID.randomUUID().toString(), UUID.randomUUID().toString()); builder.withTags(tags); final Instant updated = Instant.now(); builder.withUpdated(updated); final String grouping = UUID.randomUUID().toString(); builder.withGrouping(grouping); final String groupingInstance = UUID.randomUUID().toString(); builder.withGroupingInstance(groupingInstance); final Job job = builder.build(); Assert.assertThat(job.getName(), Matchers.is(NAME)); Assert.assertThat(job.getUser(), Matchers.is(USER)); Assert.assertThat(job.getVersion(), Matchers.is(VERSION)); Assert.assertThat(job.getCommandArgs().orElseThrow(IllegalArgumentException::new), Matchers.is(StringUtils.join(COMMAND_ARGS, StringUtils.SPACE))); Assert.assertThat(job.getArchiveLocation().orElseThrow(IllegalArgumentException::new), Matchers.is(archiveLocation)); Assert.assertThat(job.getClusterName().orElseThrow(IllegalArgumentException::new), Matchers.is(clusterName)); Assert.assertThat(job.getCommandName().orElseThrow(IllegalArgumentException::new), Matchers.is(commandName)); Assert.assertThat(job.getFinished().orElseThrow(IllegalArgumentException::new), Matchers.is(finished)); Assert.assertThat(job.getStarted().orElseThrow(IllegalArgumentException::new), Matchers.is(started)); Assert.assertThat(job.getStatus(), Matchers.is(JobStatus.SUCCEEDED)); Assert.assertThat(job.getStatusMsg().orElseThrow(IllegalArgumentException::new), Matchers.is(statusMsg)); Assert.assertThat(job.getCreated().orElseThrow(IllegalArgumentException::new), Matchers.is(created)); Assert.assertThat(job.getDescription().orElseThrow(IllegalArgumentException::new), Matchers.is(description)); Assert.assertThat(job.getId().orElseThrow(IllegalArgumentException::new), Matchers.is(id)); Assert.assertThat(job.getTags(), Matchers.is(tags)); Assert.assertThat(job.getUpdated().orElseThrow(IllegalArgumentException::new), Matchers.is(updated)); Assert.assertThat(job.getRuntime(), Matchers.is(Duration.ofMillis(finished.toEpochMilli() - started.toEpochMilli()))); Assert.assertThat(job.getGrouping().orElseThrow(IllegalArgumentException::new), Matchers.is(grouping)); Assert.assertThat(job.getGroupingInstance().orElseThrow(IllegalArgumentException::new), Matchers.is(groupingInstance)); }
From source file:org.apache.usergrid.corepersistence.CpEntityManager.java
@Override public Map createCollectionSettings(String collectionName, String owner, Map<String, Object> newSettings) { //TODO: change timeservice as below then use timeservice. Instant timeInstance = Instant.now(); Long epoch = timeInstance.toEpochMilli(); Map<String, Object> updatedSettings = new HashMap<>(); updatedSettings.put("lastUpdated", epoch); // this needs the method that can extract the user from the token no matter the token. // Possible values are app credentials, org credentials, or the user email(Admin tokens). updatedSettings.put("lastUpdateBy", owner); CollectionSettings collectionSettings = collectionSettingsFactory .getInstance(new CollectionSettingsScopeImpl(getAppIdObject(), collectionName)); Optional<Map<String, Object>> existingSettings = collectionSettings.getCollectionSettings(collectionName); // If there is an existing schema then take the lastReindexed time and keep it around. // Otherwise initialize to 0. if (existingSettings.isPresent()) { Map<String, Object> jsonMapData = existingSettings.get(); updatedSettings.put("lastReindexed", jsonMapData.get("lastReindexed")); } else {/* w w w . j a v a 2 s . c o m*/ updatedSettings.put("lastReindexed", 0); } // if fields specified, then put in settings if (newSettings.get("fields") != null) { updatedSettings.put("fields", newSettings.get("fields")); } // if region specified Object region = newSettings.get(AUTHORITATIVE_REGION_SETTING); if (region != null) { // passing an empty string causes region to be removed from settings if (region.toString().trim().isEmpty()) { updatedSettings.remove(AUTHORITATIVE_REGION_SETTING); } else { // make sure region is in the configured region list List regionList = Arrays.asList(entityManagerFig.getRegionList().toLowerCase().split(",")); if (!regionList.contains(region)) { throw new NullArgumentException("Region " + region + " not in region list"); } updatedSettings.put(AUTHORITATIVE_REGION_SETTING, region); } } collectionSettings.putCollectionSettings(collectionName, JsonUtils.mapToJsonString(updatedSettings)); return updatedSettings; }
From source file:org.haiku.haikudepotserver.security.AuthenticationServiceImpl.java
/** * <p>This will return a JWT (java web token) that is signed by a secret that allows for the client to get * that token and for it to be used as a form of authentication for some period of time.</p> *//*from ww w . j a v a 2s . c o m*/ @Override public String generateToken(User user) { Preconditions.checkArgument(null != user, "the user must be provided"); Instant instant = Instant.now(); JWTClaimsSet claimsSet = new JWTClaimsSet.Builder() .subject(user.getNickname() + SUFFIX_JSONWEBTOKEN_SUBJECT) .issueTime(new java.util.Date(instant.toEpochMilli())) .expirationTime(new java.util.Date( instant.plus(jsonWebTokenExpirySeconds, ChronoUnit.SECONDS).toEpochMilli())) .issuer(jsonWebTokenIssuer).build(); SignedJWT signedJWT = new SignedJWT(new JWSHeader(JWSAlgorithm.HS256), claimsSet); try { signedJWT.sign(jsonWebTokenSigner); } catch (JOSEException je) { throw new IllegalStateException("unable to sign a jwt", je); } return signedJWT.serialize(); }
From source file:org.jboss.pnc.environment.openshift.OpenshiftStartedEnvironment.java
public OpenshiftStartedEnvironment(ExecutorService executor, OpenshiftBuildAgentConfig openshiftBuildAgentConfig, OpenshiftEnvironmentDriverModuleConfig environmentConfiguration, PullingMonitor pullingMonitor, RepositorySession repositorySession, String systemImageId, DebugData debugData, String accessToken, boolean tempBuild, Instant temporaryBuildExpireDate, MetricsConfiguration metricsConfiguration, Map<String, String> parameters) { creationPodRetry = DEFAULT_CREATION_POD_RETRY; if (environmentConfiguration.getCreationPodRetry() != null) { try {// w w w . ja v a2s. co m creationPodRetry = Integer.parseInt(environmentConfiguration.getCreationPodRetry()); } catch (NumberFormatException e) { logger.error( "Couldn't parse the value of creation pod retry from the configuration. Using default"); } } logger.info("Creating new build environment using image id: " + environmentConfiguration.getImageId()); this.executor = executor; this.openshiftBuildAgentConfig = openshiftBuildAgentConfig; this.environmentConfiguration = environmentConfiguration; this.pullingMonitor = pullingMonitor; this.repositorySession = repositorySession; this.imageId = systemImageId == null ? environmentConfiguration.getImageId() : systemImageId; this.debugData = debugData; if (metricsConfiguration != null) { this.gaugeMetric = Optional.of(metricsConfiguration.getGaugeMetric()); } createRoute = environmentConfiguration.getExposeBuildAgentOnPublicUrl(); client = new ClientBuilder(environmentConfiguration.getRestEndpointUrl()) .usingToken(environmentConfiguration.getRestAuthToken()).build(); client.getServerReadyStatus(); // make sure client is connected runtimeProperties = new HashMap<>(); final String buildAgentHost = environmentConfiguration.getBuildAgentHost(); String expiresDateStamp = Long.toString(temporaryBuildExpireDate.toEpochMilli()); runtimeProperties.put("build-agent-host", buildAgentHost); runtimeProperties.put("containerPort", environmentConfiguration.getContainerPort()); runtimeProperties.put("buildContentId", repositorySession.getBuildRepositoryId()); runtimeProperties.put("accessToken", accessToken); runtimeProperties.put("tempBuild", Boolean.toString(tempBuild)); runtimeProperties.put("expiresDate", "ts" + expiresDateStamp); runtimeProperties.put("resourcesMemory", builderPodMemory(environmentConfiguration, parameters)); createEnvironment(); }
From source file:org.nodatime.tzvalidate.Java8Dump.java
@Override public ZoneTransitions getTransitions(String id, int fromYear, int toYear) { ZoneId zone = ZoneId.of(id); ZoneRules rules = zone.getRules(); DateTimeFormatter nameFormat = DateTimeFormatter.ofPattern("zzz", Locale.US); ZoneTransitions transitions = new ZoneTransitions(id); Instant start = ZonedDateTime.of(fromYear, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).toInstant(); transitions.addTransition(null, rules.getOffset(start).getTotalSeconds() * 1000, rules.isDaylightSavings(start), nameFormat.format(start.atZone(zone))); Instant end = ZonedDateTime.of(toYear, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).toInstant(); ZoneOffsetTransition transition = rules.nextTransition(start.minusNanos(1)); while (transition != null && transition.getInstant().isBefore(end)) { Instant instant = transition.getInstant(); transitions.addTransition(new Date(instant.toEpochMilli()), rules.getOffset(instant).getTotalSeconds() * 1000, rules.isDaylightSavings(instant), nameFormat.format(instant.atZone(zone))); transition = rules.nextTransition(instant); }//from ww w. j a v a 2 s . c o m return transitions; }