Example usage for java.time Instant isBefore

List of usage examples for java.time Instant isBefore

Introduction

In this page you can find the example usage for java.time Instant isBefore.

Prototype

public boolean isBefore(Instant otherInstant) 

Source Link

Document

Checks if this instant is before the specified instant.

Usage

From source file:org.noorganization.instalist.server.api.RecipeResourceTest.java

@Test
public void testPutRecipe() throws Exception {
    String url = "/groups/%d/recipes/%s";
    Instant preUpdate = mRecipe.getUpdated();
    RecipeInfo updatedList = new RecipeInfo().withDeleted(false).withName("changedrecipe");

    Response notAuthorizedResponse = target(String.format(url, mGroup.getId(), mRecipe.getUUID().toString()))
            .request().put(Entity.json(updatedList));
    assertEquals(401, notAuthorizedResponse.getStatus());

    Response wrongAuthResponse = target(String.format(url, mGroup.getId(), mRecipe.getUUID().toString()))
            .request().header(HttpHeaders.AUTHORIZATION, "X-Token wrongauth").put(Entity.json(updatedList));
    assertEquals(401, wrongAuthResponse.getStatus());

    Response wrongGroupResponse = target(String.format(url, mNAGroup.getId(), mNARecipe.getUUID().toString()))
            .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedList));
    assertEquals(401, wrongGroupResponse.getStatus());

    Response wrongListResponse = target(String.format(url, mGroup.getId(), mNARecipe.getUUID().toString()))
            .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedList));
    assertEquals(404, wrongListResponse.getStatus());

    Response goneResponse = target(String.format(url, mGroup.getId(), mDeletedRecipe.getUUID().toString()))
            .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedList));
    assertEquals(410, goneResponse.getStatus());
    mManager.refresh(mRecipe);/*from  w ww . j a  v a 2 s  .com*/
    assertEquals("recipe1", mRecipe.getName());

    updatedList.setLastChanged(new Date(preUpdate.toEpochMilli() - 10000));
    Response conflictResponse = target(String.format(url, mGroup.getId(), mRecipe.getUUID().toString()))
            .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedList));
    assertEquals(409, conflictResponse.getStatus());
    mManager.refresh(mRecipe);
    assertEquals("recipe1", mRecipe.getName());

    updatedList.setLastChanged(Date.from(Instant.now()));
    Response okResponse = target(String.format(url, mGroup.getId(), mRecipe.getUUID().toString())).request()
            .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedList));
    assertEquals(200, okResponse.getStatus());
    mManager.refresh(mRecipe);
    assertEquals("changedrecipe", mRecipe.getName());
    assertTrue(preUpdate + " is not before " + mRecipe.getUpdated(), preUpdate.isBefore(mRecipe.getUpdated()));
}

From source file:org.noorganization.instalist.server.api.TaggedProductResourceTest.java

@Test
public void testDeleteTaggedProducts() throws Exception {
    String url = "/groups/%d/taggedproducts/%s";
    Instant preDelete = mTaggedProduct.getUpdated();

    Response notAuthorizedResponse = target(
            String.format(url, mGroup.getId(), mTaggedProduct.getUUID().toString())).request().delete();
    assertEquals(401, notAuthorizedResponse.getStatus());

    Response wrongAuthResponse = target(String.format(url, mGroup.getId(), mTaggedProduct.getUUID().toString()))
            .request().header(HttpHeaders.AUTHORIZATION, "X-Token wrongauth").delete();
    assertEquals(401, wrongAuthResponse.getStatus());

    Response wrongGroupResponse = target(
            String.format(url, mNAGroup.getId(), mNATaggedProduct.getUUID().toString())).request()
                    .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).delete();
    assertEquals(401, wrongGroupResponse.getStatus());

    Response wrongListResponse = target(
            String.format(url, mGroup.getId(), mNATaggedProduct.getUUID().toString())).request()
                    .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).delete();
    assertEquals(404, wrongListResponse.getStatus());

    Response okResponse = target(String.format(url, mGroup.getId(), mTaggedProduct.getUUID().toString()))
            .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).delete();
    assertEquals(200, 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", mTaggedProduct.getUUID());
    List<TaggedProduct> savedTaggedProducts = savedTaggedProductQuery.getResultList();
    assertEquals(0, savedTaggedProducts.size());
    TypedQuery<DeletedObject> savedDeletedTPQuery = mManager.createQuery("select do "
            + "from DeletedObject do where do.group = :group and do.UUID = :uuid and " + "do.type = :type",
            DeletedObject.class);
    savedDeletedTPQuery.setParameter("group", mGroup);
    savedDeletedTPQuery.setParameter("uuid", mTaggedProduct.getUUID());
    savedDeletedTPQuery.setParameter("type", DeletedObject.Type.TAGGEDPRODUCT);
    List<DeletedObject> savedDeletedTaggedProducts = savedDeletedTPQuery.getResultList();
    assertEquals(1, savedDeletedTaggedProducts.size());
    assertTrue(preDelete.isBefore(savedDeletedTaggedProducts.get(0).getUpdated()));
}

From source file:org.noorganization.instalist.server.api.ListResourceTest.java

@Test
public void testPutList() throws Exception {
    String url = "/groups/%d/lists/%s";
    Instant preUpdate = mListWC.getUpdated();
    ListInfo updatedList = new ListInfo().withDeleted(false).withName("changedlist");

    Response notAuthorizedResponse = target(String.format(url, mGroup.getId(), mListWC.getUUID().toString()))
            .request().put(Entity.json(updatedList));
    assertEquals(401, notAuthorizedResponse.getStatus());

    Response wrongAuthResponse = target(String.format(url, mGroup.getId(), mListWC.getUUID().toString()))
            .request().header(HttpHeaders.AUTHORIZATION, "X-Token wrongauth").put(Entity.json(updatedList));
    assertEquals(401, wrongAuthResponse.getStatus());

    Response wrongGroupResponse = target(String.format(url, mNAGroup.getId(), mNAList.getUUID().toString()))
            .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedList));
    assertEquals(401, wrongGroupResponse.getStatus());

    Response wrongListResponse = target(String.format(url, mGroup.getId(), mNAList.getUUID().toString()))
            .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedList));
    assertEquals(404, wrongListResponse.getStatus());

    Response goneResponse = target(String.format(url, mGroup.getId(), mDeletedList.getUUID().toString()))
            .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedList));
    assertEquals(410, goneResponse.getStatus());
    mManager.refresh(mListWC);/*from w  w  w.  j  av a2s  .c o  m*/
    assertEquals("list1", mListWC.getName());

    updatedList.setLastChanged(new Date(preUpdate.toEpochMilli() - 10000));
    Response conflictResponse = target(String.format(url, mGroup.getId(), mListWC.getUUID().toString()))
            .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedList));
    assertEquals(409, conflictResponse.getStatus());
    mManager.refresh(mListWC);
    assertEquals("list1", mListWC.getName());

    Thread.sleep(1000);
    updatedList.setLastChanged(new Date(System.currentTimeMillis()));
    Response okResponse = target(String.format(url, mGroup.getId(), mListWC.getUUID().toString())).request()
            .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedList));
    assertEquals(200, okResponse.getStatus());
    mManager.refresh(mListWC);
    assertEquals("changedlist", mListWC.getName());
    assertEquals(mCat, mListWC.getCategory());
    assertTrue(preUpdate.toEpochMilli() + " is not before " + mListWC.getUpdated().toEpochMilli(),
            preUpdate.isBefore(mListWC.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);// www . ja va 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.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);/* w  ww . j  a va2 s  . co  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.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);/*from  w ww .j a v  a  2s. com*/
    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:org.sakaiproject.assignment.impl.AssignmentServiceImpl.java

@Override
public String getSubmissionStatus(String submissionId) {
    String status = "";
    AssignmentSubmission submission;// w ww.  j a v a2s . c  om
    try {
        submission = getSubmission(submissionId);
    } catch (PermissionException e) {
        log.warn("Could not get submission with id {}, {}", submissionId, e.getMessage());
        return status;
    }
    Assignment assignment = submission.getAssignment();
    String assignmentReference = AssignmentReferenceReckoner.reckoner().assignment(assignment).reckon()
            .getReference();
    boolean allowGrade = assignment != null && allowGradeSubmission(assignmentReference);

    Instant submitTime = submission.getDateSubmitted();
    Instant returnTime = submission.getDateReturned();
    Instant lastModTime = submission.getDateModified();

    if (submission.getSubmitted() || (!submission.getSubmitted() && allowGrade)) {
        if (submitTime != null) {
            if (submission.getReturned()) {
                if (returnTime != null && returnTime.isBefore(submitTime)) {
                    if (!submission.getGraded()) {
                        status = resourceLoader.getString("gen.resub") + " "
                                + getUsersLocalDateTimeString(submitTime);
                        if (submitTime.isAfter(assignment.getDueDate())) {
                            status = status + resourceLoader.getString("gen.late2");
                        }
                    } else
                        status = resourceLoader.getString("gen.returned");
                } else
                    status = resourceLoader.getString("gen.returned");
            } else if (submission.getGraded() && allowGrade) {
                status = StringUtils.isNotBlank(submission.getGrade()) ? resourceLoader.getString("grad3")
                        : resourceLoader.getString("gen.commented");
            } else {
                if (allowGrade) {
                    // ungraded submission
                    status = resourceLoader.getString("ungra");
                } else {
                    status = resourceLoader.getString("gen.subm4") + " "
                            + getUsersLocalDateTimeString(submitTime);
                }
            }
        } else {
            if (submission.getReturned()) {
                // instructor can return grading to non-submitted user
                status = resourceLoader.getString("gen.returned");
            } else if (submission.getGraded() && allowGrade) {
                // instructor can grade non-submitted ones
                status = StringUtils.isNotBlank(submission.getGrade()) ? resourceLoader.getString("grad3")
                        : resourceLoader.getString("gen.commented");
            } else {
                if (allowGrade) {
                    // show "no submission" to graders
                    status = resourceLoader.getString("listsub.nosub");
                } else {
                    if (assignment.getHonorPledge() && submission.getHonorPledge()) {
                        status = resourceLoader.getString("gen.hpsta");
                    } else {
                        // show "not started" to students
                        status = resourceLoader.getString("gen.notsta");
                    }
                }
            }
        }
    } else {
        if (submission.getGraded()) {
            if (submission.getReturned()) {
                // modified time is after returned time + 10 seconds
                if (lastModTime != null && returnTime != null && lastModTime.isAfter(returnTime.plusSeconds(10))
                        && !allowGrade) {
                    // working on a returned submission now
                    status = resourceLoader.getString("gen.dra2") + " " + resourceLoader.getString("gen.inpro");
                } else {
                    // not submitted submmission has been graded and returned
                    status = resourceLoader.getString("gen.returned");
                }
            } else if (allowGrade) {
                // grade saved but not release yet, show this to graders
                status = StringUtils.isNotBlank(submission.getGrade()) ? resourceLoader.getString("grad3")
                        : resourceLoader.getString("gen.commented");
            } else {
                // submission saved, not submitted.
                status = resourceLoader.getString("gen.dra2") + " " + resourceLoader.getString("gen.inpro");
            }
        } else {
            if (allowGrade)
                status = resourceLoader.getString("ungra");
            else {
                // TODO add a submission state of draft so we can eliminate the date check here
                if (assignment.getHonorPledge() && submission.getHonorPledge()
                        && submission.getDateCreated().equals(submission.getDateModified())) {
                    status = resourceLoader.getString("gen.hpsta");
                } else {
                    // submission saved, not submitted,
                    status = resourceLoader.getString("gen.dra2") + " " + resourceLoader.getString("gen.inpro");
                }
            }
        }
    }

    return status;
}

From source file:org.sakaiproject.assignment.impl.AssignmentServiceImpl.java

@Override
public boolean isPeerAssessmentOpen(Assignment assignment) {
    if (assignment.getAllowPeerAssessment()) {
        Instant now = Instant.now();
        return now.isBefore(assignment.getPeerAssessmentPeriodDate()) && now.isAfter(assignment.getCloseDate());
    }/*  ww w .  ja v  a  2 s  .co m*/
    return false;
}