コード例 #1
0
  private ManagedApplication factorizeConfiguration() {

    Rule rule1 = Mockito.mock(Rule.class);
    Mockito.when(rule1.getCommandsToInvoke()).thenReturn(Arrays.asList("cmd1"));

    Rule rule2 = Mockito.mock(Rule.class);
    Mockito.when(rule2.getCommandsToInvoke()).thenReturn(Arrays.asList("cmd2", "cmd3"));

    Application app = new Application("app", new TestApplicationTemplate());
    AutonomicApplicationContext ctx = Mockito.spy(new AutonomicApplicationContext(app));
    Mockito.when(ctx.findRulesToExecute()).thenReturn(Arrays.asList(rule1, rule2));
    this.autonomicMngr.appNameToContext.put("app", ctx);

    return new ManagedApplication(app);
  }
コード例 #2
0
  @Test
  public void testHandleEvent_withContext_withRule() throws Exception {

    Mockito.when(this.preferencesMngr.get(Mockito.anyString(), Mockito.anyString()))
        .thenReturn("5");

    Rule rule1 = Mockito.mock(Rule.class);
    Mockito.when(rule1.getCommandsToInvoke()).thenReturn(Arrays.asList("cmd1"));

    Rule rule2 = Mockito.mock(Rule.class);
    Mockito.when(rule2.getCommandsToInvoke()).thenReturn(Arrays.asList("cmd2", "cmd3"));

    Application app = new Application("app", new TestApplicationTemplate());
    AutonomicApplicationContext ctx = Mockito.spy(new AutonomicApplicationContext(app));
    Mockito.when(ctx.findRulesToExecute()).thenReturn(Arrays.asList(rule1, rule2));
    this.autonomicMngr.appNameToContext.put("app", ctx);

    ManagedApplication ma = new ManagedApplication(app);
    this.autonomicMngr.handleEvent(ma, new MsgNotifAutonomic("app", "/root", "event", null));

    Mockito.verify(this.preferencesMngr, Mockito.times(2))
        .get(Mockito.anyString(), Mockito.anyString());

    Map<?, ?> eventNameToLastRecordTime =
        TestUtils.getInternalField(ctx, "eventNameToLastRecordTime", Map.class);
    Assert.assertEquals(1, eventNameToLastRecordTime.size());
    Assert.assertNotNull(eventNameToLastRecordTime.get("event"));

    Mockito.verify(rule1, Mockito.times(1)).getCommandsToInvoke();
    Mockito.verify(rule2, Mockito.times(1)).getCommandsToInvoke();

    ArgumentCaptor<CommandExecutionContext> execCtx =
        ArgumentCaptor.forClass(CommandExecutionContext.class);
    Mockito.verify(this.commandsMngr, Mockito.times(3))
        .execute(
            Mockito.any(Application.class),
            Mockito.anyString(),
            Mockito.any(CommandExecutionContext.class));

    for (CommandExecutionContext c : execCtx.getAllValues()) {
      Assert.assertEquals(5, c.getMaxVm());
      Assert.assertFalse(c.isStrictMaxVm());
    }
  }