Example usage for java.time LocalDateTime plus

List of usage examples for java.time LocalDateTime plus

Introduction

In this page you can find the example usage for java.time LocalDateTime plus.

Prototype

@Override
public LocalDateTime plus(long amountToAdd, TemporalUnit unit) 

Source Link

Document

Returns a copy of this date-time with the specified amount added.

Usage

From source file:org.apache.hadoop.yarn.server.resourcemanager.security.TestJWTSecurityHandler.java

@Test
public void testJWTRenewal() throws Exception {
    config.set(YarnConfiguration.HOPS_RM_SECURITY_ACTOR_KEY,
            "org.apache.hadoop.yarn.server.resourcemanager.security.TestingRMAppSecurityActions");
    config.set(YarnConfiguration.RM_JWT_VALIDITY_PERIOD, "5s");
    config.set(YarnConfiguration.RM_JWT_EXPIRATION_LEEWAY, "2s");

    RMAppSecurityActions actor = Mockito.spy(new TestingRMAppSecurityActions());
    RMAppSecurityActionsFactory.getInstance().register(actor);

    RMAppSecurityManager securityManager = new RMAppSecurityManager(rmContext);
    JWTSecurityHandler jwtHandler = Mockito.spy(new MockJWTSecurityHandler(rmContext, securityManager));

    securityManager.registerRMAppSecurityHandlerWithType(jwtHandler, JWTSecurityHandler.class);
    securityManager.init(config);/*from  w w w .ja  v a2  s  .  c o  m*/
    securityManager.start();

    ApplicationId appId = ApplicationId.newInstance(System.currentTimeMillis(), 1);
    String user = "Dorothy";
    JWTSecurityHandler.JWTMaterialParameter jwtParam = new JWTSecurityHandler.JWTMaterialParameter(appId, user);
    JWTSecurityHandler.JWTSecurityManagerMaterial jwt = jwtHandler.generateMaterial(jwtParam);

    jwtParam = new JWTSecurityHandler.JWTMaterialParameter(appId, user);
    jwtParam.setExpirationDate(jwt.getExpirationDate());
    jwtParam.setToken(jwt.getToken());
    securityManager.registerWithMaterialRenewers(jwtParam);

    Mockito.verify(jwtHandler).registerRenewer(Mockito.eq(jwtParam));

    // Renewal should roughly happen after 7s
    int sleeped = 15;
    while (!((MockJWTSecurityHandler) jwtHandler).getRenewer().hasRun() && sleeped > 0) {
        TimeUnit.SECONDS.sleep(1);
        sleeped--;
    }

    Assert.assertNotEquals("Waited too long for JWT renewal to happen", 0, sleeped);
    JWTSecurityHandler.JWTMaterialParameter jwtRenewParam = new JWTSecurityHandler.JWTMaterialParameter(appId,
            user);
    jwtRenewParam.setToken(jwt.getToken());

    // Recompute new expiration date for renewed token
    LocalDateTime now = jwtHandler.getNow();
    LocalDateTime expirationDate = now.plus(jwtHandler.getValidityPeriod().getFirst(),
            jwtHandler.getValidityPeriod().getSecond());
    jwtRenewParam.setExpirationDate(expirationDate);
    Mockito.verify(actor).renewJWT(Mockito.eq(jwtRenewParam));

    securityManager.stop();
}

From source file:org.apache.hadoop.yarn.server.resourcemanager.security.TestX509SecurityHandler.java

@Test
public void testCertificateRenewal() throws Exception {
    conf.set(YarnConfiguration.HOPS_RM_SECURITY_ACTOR_KEY,
            "org.apache.hadoop.yarn.server.resourcemanager.security.TestingRMAppSecurityActions");
    RMAppSecurityManager rmAppSecurityManager = new RMAppSecurityManager(rmContext);
    MockX509SecurityHandler x509SecurityHandler = new MockX509SecurityHandler(rmContext, rmAppSecurityManager,
            false);//from   www.  j  a v  a 2s .  c  o  m
    rmAppSecurityManager.registerRMAppSecurityHandler(x509SecurityHandler);
    rmAppSecurityManager.init(conf);
    rmAppSecurityManager.start();

    LocalDateTime now = DateUtils.getNow();
    LocalDateTime expiration = now.plus(10, ChronoUnit.SECONDS);
    ApplicationId appId = ApplicationId.newInstance(DateUtils.localDateTime2UnixEpoch(now), 1);
    x509SecurityHandler.setOldCertificateExpiration(DateUtils.localDateTime2UnixEpoch(expiration));

    X509SecurityHandler.X509MaterialParameter x509Param = new X509SecurityHandler.X509MaterialParameter(appId,
            "Dolores", 1);
    x509Param.setExpiration(DateUtils.localDateTime2UnixEpoch(expiration));
    x509SecurityHandler.registerRenewer(x509Param);
    Map<ApplicationId, ScheduledFuture> tasks = x509SecurityHandler.getRenewalTasks();
    ScheduledFuture renewalTask = tasks.get(appId);
    assertFalse(renewalTask.isCancelled());
    assertFalse(renewalTask.isDone());

    // Wait until the scheduled task is executed
    TimeUnit.SECONDS.sleep(10);
    assertTrue(renewalTask.isDone());
    assertFalse(x509SecurityHandler.getRenewalException());
    assertTrue(tasks.isEmpty());
    rmAppSecurityManager.stop();
}

From source file:org.apache.hadoop.yarn.server.resourcemanager.security.TestX509SecurityHandler.java

@Test(timeout = 12000)
public void testFailedCertificateRenewal() throws Exception {
    conf.set(YarnConfiguration.HOPS_RM_SECURITY_ACTOR_KEY,
            "org.apache.hadoop.yarn.server.resourcemanager.security.TestingRMAppSecurityActions");
    RMAppSecurityManager securityManager = new RMAppSecurityManager(rmContext);
    MockX509SecurityHandler.MockFailingX509SecurityHandler x509Handler = new MockX509SecurityHandler.MockFailingX509SecurityHandler(
            rmContext, securityManager, Integer.MAX_VALUE);
    securityManager.registerRMAppSecurityHandlerWithType(x509Handler, X509SecurityHandler.class);
    securityManager.init(conf);/* w ww. j av a2s .  com*/
    securityManager.start();

    LocalDateTime now = DateUtils.getNow();
    LocalDateTime expiration = now.plus(10, ChronoUnit.SECONDS);
    ApplicationId appId = ApplicationId.newInstance(DateUtils.localDateTime2UnixEpoch(now), 1);
    X509SecurityHandler.X509MaterialParameter x509Param = new X509SecurityHandler.X509MaterialParameter(appId,
            "Dolores", 1);
    x509Param.setExpiration(DateUtils.localDateTime2UnixEpoch(expiration));
    x509Handler.registerRenewer(x509Param);

    Map<ApplicationId, ScheduledFuture> tasks = x509Handler.getRenewalTasks();
    // There should be a scheduled task
    ScheduledFuture task = tasks.get(appId);
    assertFalse(task.isCancelled());
    assertFalse(task.isDone());
    assertFalse(x509Handler.hasRenewalFailed());
    assertEquals(0, x509Handler.getNumberOfRenewalFailures());

    TimeUnit.SECONDS.sleep(10);
    assertTrue(tasks.isEmpty());
    assertEquals(4, x509Handler.getNumberOfRenewalFailures());
    assertTrue(x509Handler.hasRenewalFailed());
    securityManager.stop();
}

From source file:org.apache.hadoop.yarn.server.resourcemanager.security.TestX509SecurityHandler.java

@Test(timeout = 12000)
public void testRetryCertificateRenewal() throws Exception {
    conf.set(YarnConfiguration.HOPS_RM_SECURITY_ACTOR_KEY,
            "org.apache.hadoop.yarn.server.resourcemanager.security.TestingRMAppSecurityActions");
    RMAppSecurityManager securityManager = new RMAppSecurityManager(rmContext);
    MockX509SecurityHandler.MockFailingX509SecurityHandler x509Handler = new MockX509SecurityHandler.MockFailingX509SecurityHandler(
            rmContext, securityManager, 2);
    securityManager.registerRMAppSecurityHandlerWithType(x509Handler, X509SecurityHandler.class);
    securityManager.init(conf);/*from  ww  w  .  j a  v a 2  s. c  o  m*/
    securityManager.start();

    LocalDateTime now = DateUtils.getNow();
    LocalDateTime expiration = now.plus(10, ChronoUnit.SECONDS);
    ApplicationId appId = ApplicationId.newInstance(DateUtils.localDateTime2UnixEpoch(now), 1);
    X509SecurityHandler.X509MaterialParameter x509Param = new X509SecurityHandler.X509MaterialParameter(appId,
            "Dolores", 1);
    x509Param.setExpiration(DateUtils.localDateTime2UnixEpoch(expiration));
    x509Handler.registerRenewer(x509Param);
    TimeUnit.SECONDS.sleep(10);
    assertEquals(2, x509Handler.getNumberOfRenewalFailures());
    assertFalse(x509Handler.hasRenewalFailed());
    assertTrue(x509Handler.getRenewalTasks().isEmpty());
    securityManager.stop();
}

From source file:org.talend.dataprep.transformation.actions.date.ModifyDate.java

/**
 * @see ColumnAction#applyOnColumn(DataSetRow, ActionContext)
 */// w w  w .  j  a va2  s .  c  o  m
@Override
public void applyOnColumn(DataSetRow row, ActionContext context) {
    final String columnId = context.getColumnId();

    final String value = row.get(columnId);
    if (StringUtils.isBlank(value)) {
        return;
    }

    Map<String, String> parameters = context.getParameters();
    String mode = parameters.get(MODE_PARAMETER);

    long amount;
    switch (mode) {
    case CONSTANT_MODE:
        amount = context.get(AMOUNT_CONTEXT_KEY);
        break;
    case OTHER_COLUMN_MODE:
        String otherColId = parameters.get(SELECTED_COLUMN_PARAMETER);
        if (!NumericHelper.isBigDecimal(row.get(otherColId))) {
            // In this case, do not change the original value
            return;
        }
        amount = computeAmount(row.get(otherColId));
        break;
    default:
        throw new TalendRuntimeException(ActionErrorCodes.BAD_ACTION_PARAMETER, //
                ExceptionContext.build().put("paramName", OtherColumnParameters.CONSTANT_MODE));
    }

    try {
        final DatePattern outputPattern = new DatePattern(context.get(PATTERN_CONTEXT_KEY));

        LocalDateTime date = Providers.get().parse(value, context.getRowMetadata().getById(columnId));

        date = date.plus(amount, context.get(UNIT_CONTEXT_KEY));

        row.set(columnId, outputPattern.getFormatter().format(date));

    } catch (DateTimeException e) {
        // cannot parse the date, let's leave it as is
        LOGGER.debug("Unable to parse date {}.", value, e);
    }
}