List of usage examples for java.time Instant isBefore
public boolean isBefore(Instant otherInstant)
From source file:org.noorganization.instalist.server.api.CategoriesResourceTest.java
@Test public void testPutCategory() throws Exception { final String url = "/groups/%d/categories/%s"; Response wrongTokenResponse = target(String.format(url, mGroup.getId(), mCategory.getId())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token wrongToken") .put(Entity.json(new CategoryInfo().withName("dev111"))); assertEquals(401, wrongTokenResponse.getStatus()); Response wrongGroupResponse = target( String.format(url, mNotAccessibleGroup.getId(), mCategory.getUUID().toString())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken) .put(Entity.json(new CategoryInfo().withName("dev111"))); assertEquals(401, wrongGroupResponse.getStatus()); Response wrongCatResponse = target( String.format(url, mGroup.getId(), mNotAccessibleCategory.getUUID().toString())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken) .put(Entity.json(new CategoryInfo().withName("dev111"))); assertEquals(404, wrongCatResponse.getStatus()); Response invalidCatResponse = target(String.format(url, mGroup.getId(), mCategory.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(new CategoryInfo() .withUUID(mNotAccessibleCategory.getUUID().toString()).withName("dev111"))); assertEquals(400, invalidCatResponse.getStatus()); mManager.refresh(mNotAccessibleCategory); mManager.refresh(mCategory);/*from w w w . j a va 2s .c om*/ assertEquals("cat1", mCategory.getName()); assertEquals("cat2", mNotAccessibleCategory.getName()); Instant beforeChange = Instant.now(); Thread.sleep(200); Response validCatResponse = target(String.format(url, mGroup.getId(), mCategory.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken) .put(Entity.json(new CategoryInfo().withName("dev111"))); assertEquals(200, validCatResponse.getStatus()); mManager.refresh(mCategory); assertEquals("dev111", mCategory.getName()); assertTrue(beforeChange.isBefore(mCategory.getUpdated())); Response conflictCatResponse = target(String.format(url, mGroup.getId(), mCategory.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken) .put(Entity.json(new CategoryInfo().withName("cat1").withLastChanged(Date.from(beforeChange)))); assertEquals(409, conflictCatResponse.getStatus()); mManager.refresh(mCategory); assertEquals("dev111", mCategory.getName()); assertTrue(beforeChange.isBefore(mCategory.getUpdated())); }
From source file:org.ng200.openolympus.controller.api.ContestTimerController.java
@RequestMapping("/api/contestTimeRemaining/{contest}") public @ResponseBody Map<String, String> getTimeRemaining(@PathVariable("contest") final Contest contest, final Principal principal) { Assertions.resourceExists(contest);/* w w w . j a v a2 s. c o m*/ Instant end = contest.getStartTime().toInstant(); final boolean isInProcess = contest.getStartTime().before(java.util.Date.from(Instant.now())); if (isInProcess) { if (this.contestSecurityService.isSuperuser(principal)) { end = this.contestService.getContestEndIncludingAllTimeExtensions(contest); } else if (principal != null) { final User user = this.userRepository.findByUsername(principal.getName()); if (user != null) { end = end.plusMillis(contest.getDuration()) .plusMillis(this.contestService.getTimeExtensions(contest, user)); } } } if (principal != null) { this.contestService.getTimeExtensions(contest, this.userRepository.findByUsername(principal.getName())); } final Instant cur = Instant.now(); if (end.isBefore(cur)) { return new HashMap<String, String>() { /** * */ private static final long serialVersionUID = 5419629757758058933L; { this.put("timer", "00:00:00"); this.put("status", "ended"); } }; } final Duration duration = Duration.between(cur, end); final DecimalFormat format = new DecimalFormat("00"); final long hours = duration.toHours(); final long minutes = duration.minusHours(hours).toMinutes(); final long seconds = duration.minusHours(hours).minusMinutes(minutes).getSeconds(); return new HashMap<String, String>() { /** * */ private static final long serialVersionUID = -4698243010184691932L; { this.put("timer", format.format(hours) + ":" + format.format(minutes) + ":" + format.format(seconds)); this.put("status", isInProcess ? "inProgress" : "notStartedYet"); } }; }
From source file:org.noorganization.instalist.server.api.TagResourceTest.java
@Test public void testPostTag() throws Exception { String url = "/groups/%d/tags"; TagInfo newTag = new TagInfo().withUUID(mTag.getUUID()).withName("tag3"); Instant preInsert = Instant.now(); Response notAuthorizedResponse = target(String.format(url, mGroup.getId())).request() .post(Entity.json(newTag)); assertEquals(401, notAuthorizedResponse.getStatus()); Response wrongAuthResponse = target(String.format(url, mGroup.getId())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token wrongauth").post(Entity.json(newTag)); assertEquals(401, wrongAuthResponse.getStatus()); Response wrongGroupResponse = target(String.format(url, mNAGroup.getId())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).post(Entity.json(newTag)); assertEquals(401, wrongGroupResponse.getStatus()); Response goneResponse = target(String.format(url, mGroup.getId())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).post(Entity.json(newTag)); assertEquals(409, goneResponse.getStatus()); mManager.refresh(mTag);//from w w w . j av a2s.c o m assertEquals("tag1", mTag.getName()); newTag.setUUID(UUID.randomUUID()); Response okResponse = target(String.format(url, mGroup.getId())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).post(Entity.json(newTag)); assertEquals(201, okResponse.getStatus()); TypedQuery<Tag> savedTagQuery = mManager .createQuery("select t from Tag t where " + "t.group = :group and t.UUID = :uuid", Tag.class); savedTagQuery.setParameter("group", mGroup); savedTagQuery.setParameter("uuid", UUID.fromString(newTag.getUUID())); List<Tag> savedTags = savedTagQuery.getResultList(); assertEquals(1, savedTags.size()); assertEquals("tag3", savedTags.get(0).getName()); assertTrue(preInsert.isBefore(savedTags.get(0).getUpdated())); }
From source file:org.noorganization.instalist.server.api.UnitResourceTest.java
@Test public void testPostUnit() throws Exception { String url = "/groups/%d/units"; UnitInfo newUnit = new UnitInfo().withUUID(mUnit.getUUID()).withName("unit4"); Instant preInsert = Instant.now(); Response notAuthorizedResponse = target(String.format(url, mGroup.getId())).request() .post(Entity.json(newUnit)); assertEquals(401, notAuthorizedResponse.getStatus()); Response wrongAuthResponse = target(String.format(url, mGroup.getId())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token wrongauth").post(Entity.json(newUnit)); assertEquals(401, wrongAuthResponse.getStatus()); Response wrongGroupResponse = target(String.format(url, mNAGroup.getId())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).post(Entity.json(newUnit)); assertEquals(401, wrongGroupResponse.getStatus()); Response goneResponse = target(String.format(url, mGroup.getId())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).post(Entity.json(newUnit)); assertEquals(409, goneResponse.getStatus()); mManager.refresh(mUnit);/* w w w . j a v a2s . co m*/ assertEquals("unit1", mUnit.getName()); newUnit.setUUID(UUID.randomUUID()); Response okResponse = target(String.format(url, mGroup.getId())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).post(Entity.json(newUnit)); assertEquals(201, okResponse.getStatus()); TypedQuery<Unit> savedUnitQuery = mManager .createQuery("select u from " + "Unit u where u.group = :group and u.UUID = :uuid", Unit.class); savedUnitQuery.setParameter("group", mGroup); savedUnitQuery.setParameter("uuid", UUID.fromString(newUnit.getUUID())); List<Unit> savedUnits = savedUnitQuery.getResultList(); assertEquals(1, savedUnits.size()); assertEquals("unit4", savedUnits.get(0).getName()); assertTrue(preInsert.isBefore(savedUnits.get(0).getUpdated())); }
From source file:org.noorganization.instalist.server.api.TaggedProductResourceTest.java
@Test public void testPostTaggedProduct() throws Exception { String url = "/groups/%d/taggedproducts"; TaggedProductInfo newTP = new TaggedProductInfo().withUUID(mTaggedProduct.getUUID()) .withProductUUID(mProduct.getUUID()).withTagUUID(mNATag.getUUID()); Instant preInsert = Instant.now(); Response notAuthorizedResponse = target(String.format(url, mGroup.getId())).request() .post(Entity.json(newTP)); assertEquals(401, notAuthorizedResponse.getStatus()); Response wrongAuthResponse = target(String.format(url, mGroup.getId())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token wrongauth").post(Entity.json(newTP)); assertEquals(401, wrongAuthResponse.getStatus()); Response wrongGroupResponse = target(String.format(url, mNAGroup.getId())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).post(Entity.json(newTP)); assertEquals(401, wrongGroupResponse.getStatus()); newTP.setUUID(UUID.randomUUID()); Response wrongRefResponse = target(String.format(url, mGroup.getId())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).post(Entity.json(newTP)); assertEquals(400, wrongRefResponse.getStatus()); newTP.setTagUUID(mTag.getUUID());//from w w w . ja v a2 s . c om Response okResponse = target(String.format(url, mGroup.getId())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).post(Entity.json(newTP)); assertEquals(201, okResponse.getStatus()); TypedQuery<TaggedProduct> savedTaggedProductQuery = mManager.createQuery( "select tp from " + "TaggedProduct tp where tp.group = :group and tp.UUID = :uuid", TaggedProduct.class); savedTaggedProductQuery.setParameter("group", mGroup); savedTaggedProductQuery.setParameter("uuid", UUID.fromString(newTP.getUUID())); List<TaggedProduct> savedIngredients = savedTaggedProductQuery.getResultList(); assertEquals(1, savedIngredients.size()); assertTrue(preInsert.isBefore(savedIngredients.get(0).getUpdated())); }
From source file:org.noorganization.instalist.server.api.RecipeResourceTest.java
@Test public void testPostRecipe() throws Exception { String url = "/groups/%d/recipes"; RecipeInfo newRecipe = new RecipeInfo().withUUID(mRecipe.getUUID()).withName("recipe3"); Instant preInsert = Instant.now(); Response notAuthorizedResponse = target(String.format(url, mGroup.getId())).request() .post(Entity.json(newRecipe)); assertEquals(401, notAuthorizedResponse.getStatus()); Response wrongAuthResponse = target(String.format(url, mGroup.getId())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token wrongauth").post(Entity.json(newRecipe)); assertEquals(401, wrongAuthResponse.getStatus()); Response wrongGroupResponse = target(String.format(url, mNAGroup.getId())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).post(Entity.json(newRecipe)); assertEquals(401, wrongGroupResponse.getStatus()); Response goneResponse = target(String.format(url, mGroup.getId())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).post(Entity.json(newRecipe)); assertEquals(409, goneResponse.getStatus()); mManager.refresh(mRecipe);/* w ww . j a va2 s .co m*/ assertEquals("recipe1", mRecipe.getName()); newRecipe.setUUID(UUID.randomUUID()); Response okResponse = target(String.format(url, mGroup.getId())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).post(Entity.json(newRecipe)); assertEquals(201, okResponse.getStatus()); TypedQuery<Recipe> savedRecipeQuery = mManager .createQuery("select r from " + "Recipe r where r.group = :group and r.UUID = :uuid", Recipe.class); savedRecipeQuery.setParameter("group", mGroup); savedRecipeQuery.setParameter("uuid", UUID.fromString(newRecipe.getUUID())); List<Recipe> savedRecipes = savedRecipeQuery.getResultList(); assertEquals(1, savedRecipes.size()); assertEquals("recipe3", savedRecipes.get(0).getName()); assertTrue(preInsert.isBefore(savedRecipes.get(0).getUpdated())); }
From source file:org.noorganization.instalist.server.api.ProductResourceTest.java
@Test public void testPostProduct() throws Exception { String url = "/groups/%d/products"; ProductInfo newProduct = new ProductInfo().withUUID(mProduct.getUUID()).withName("product4") .withDefaultAmount(1.0f).withStepAmount(0.3f); Instant preInsert = Instant.now(); Response notAuthorizedResponse = target(String.format(url, mGroup.getId())).request() .post(Entity.json(newProduct)); assertEquals(401, notAuthorizedResponse.getStatus()); Response wrongAuthResponse = target(String.format(url, mGroup.getId())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token wrongauth").post(Entity.json(newProduct)); assertEquals(401, wrongAuthResponse.getStatus()); Response wrongGroupResponse = target(String.format(url, mNAGroup.getId())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).post(Entity.json(newProduct)); assertEquals(401, wrongGroupResponse.getStatus()); Response goneResponse = target(String.format(url, mGroup.getId())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).post(Entity.json(newProduct)); assertEquals(409, goneResponse.getStatus()); mManager.refresh(mProduct);/*from w w w . ja va 2 s. com*/ assertEquals("product1", mProduct.getName()); newProduct.setUUID(UUID.randomUUID()); Response okResponse = target(String.format(url, mGroup.getId())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).post(Entity.json(newProduct)); assertEquals(201, okResponse.getStatus()); TypedQuery<Product> savedProductsQuery = mManager.createQuery( "select p from " + "Product p where p.group = :group and p.UUID = :uuid", Product.class); savedProductsQuery.setParameter("group", mGroup); savedProductsQuery.setParameter("uuid", UUID.fromString(newProduct.getUUID())); List<Product> savedProducts = savedProductsQuery.getResultList(); assertEquals(1, savedProducts.size()); assertEquals("product4", savedProducts.get(0).getName()); assertTrue(preInsert.isBefore(savedProducts.get(0).getUpdated())); }
From source file:org.noorganization.instalist.server.api.EntryResourceTest.java
@Test public void testPostEntry() throws Exception { String url = "/groups/%d/listentries"; EntryInfo newProduct = new EntryInfo().withUUID(mEntry.getUUID()).withProductUUID(mProduct.getUUID()) .withListUUID(mList.getUUID()).withAmount(3f); Instant preInsert = Instant.now(); Response notAuthorizedResponse = target(String.format(url, mGroup.getId())).request() .post(Entity.json(newProduct)); assertEquals(401, notAuthorizedResponse.getStatus()); Response wrongAuthResponse = target(String.format(url, mGroup.getId())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token wrongauth").post(Entity.json(newProduct)); assertEquals(401, wrongAuthResponse.getStatus()); Response wrongGroupResponse = target(String.format(url, mNAGroup.getId())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).post(Entity.json(newProduct)); assertEquals(401, wrongGroupResponse.getStatus()); Response goneResponse = target(String.format(url, mGroup.getId())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).post(Entity.json(newProduct)); assertEquals(409, goneResponse.getStatus()); mManager.refresh(mEntry);/*from w w w. j av a 2s . c o m*/ assertEquals(1f, mEntry.getAmount(), 0.001f); newProduct.setUUID(UUID.randomUUID()); Response okResponse = target(String.format(url, mGroup.getId())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).post(Entity.json(newProduct)); assertEquals(201, okResponse.getStatus()); TypedQuery<ListEntry> savedEntriesQuery = mManager.createQuery( "select le from " + "ListEntry le where le.group = :group and le.UUID = :uuid", ListEntry.class); savedEntriesQuery.setParameter("group", mGroup); savedEntriesQuery.setParameter("uuid", UUID.fromString(newProduct.getUUID())); List<ListEntry> savedEntries = savedEntriesQuery.getResultList(); assertEquals(1, savedEntries.size()); assertEquals(3f, savedEntries.get(0).getAmount(), 0.001f); assertTrue(preInsert.isBefore(savedEntries.get(0).getUpdated())); }
From source file:org.noorganization.instalist.server.api.IngredientResourceTest.java
@Test public void testPostIngredient() throws Exception { String url = "/groups/%d/ingredients"; IngredientInfo newIngred = new IngredientInfo().withUUID(mIngredient.getUUID()) .withProductUUID(mProduct.getUUID()).withRecipeUUID(mRecipe.getUUID()).withAmount(3f); Instant preInsert = Instant.now(); Response notAuthorizedResponse = target(String.format(url, mGroup.getId())).request() .post(Entity.json(newIngred)); assertEquals(401, notAuthorizedResponse.getStatus()); Response wrongAuthResponse = target(String.format(url, mGroup.getId())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token wrongauth").post(Entity.json(newIngred)); assertEquals(401, wrongAuthResponse.getStatus()); Response wrongGroupResponse = target(String.format(url, mNAGroup.getId())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).post(Entity.json(newIngred)); assertEquals(401, wrongGroupResponse.getStatus()); Response goneResponse = target(String.format(url, mGroup.getId())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).post(Entity.json(newIngred)); assertEquals(409, goneResponse.getStatus()); mManager.refresh(mIngredient);// w ww. ja v a 2 s . com assertEquals(1f, mIngredient.getAmount(), 0.001f); newIngred.setUUID(UUID.randomUUID()); Response okResponse = target(String.format(url, mGroup.getId())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).post(Entity.json(newIngred)); assertEquals(201, okResponse.getStatus()); TypedQuery<Ingredient> savedIngredientQuery = mManager.createQuery( "select i from " + "Ingredient i where i.group = :group and i.UUID = :uuid", Ingredient.class); savedIngredientQuery.setParameter("group", mGroup); savedIngredientQuery.setParameter("uuid", UUID.fromString(newIngred.getUUID())); List<Ingredient> savedIngredients = savedIngredientQuery.getResultList(); assertEquals(1, savedIngredients.size()); assertEquals(3f, savedIngredients.get(0).getAmount(), 0.001f); assertTrue(preInsert.isBefore(savedIngredients.get(0).getUpdated())); }