/** Test that a node starts and sets SERVICE_UP correctly. */
  @Test(groups = "Integration")
  public void canStartupAndShutdown() {
    solr = app.createAndManageChild(EntitySpec.create(SolrServer.class));
    app.start(ImmutableList.of(testLocation));

    EntityAsserts.assertAttributeEqualsEventually(solr, Startable.SERVICE_UP, true);
    Entities.dumpInfo(app);

    solr.stop();

    EntityAsserts.assertAttributeEqualsEventually(solr, Startable.SERVICE_UP, false);
  }
  /** Test that a core can be created and used with SolrJ client. */
  @Test(groups = "Integration")
  public void testConnection() throws Exception {
    solr =
        app.createAndManageChild(
            EntitySpec.create(SolrServer.class)
                .configure(
                    SolrServer.SOLR_CORE_CONFIG,
                    ImmutableMap.of("example", "classpath://solr/example.tgz")));
    app.start(ImmutableList.of(testLocation));

    EntityAsserts.assertAttributeEqualsEventually(solr, Startable.SERVICE_UP, true);

    SolrJSupport client = new SolrJSupport(solr, "example");

    Iterable<SolrDocument> results = client.getDocuments();
    assertTrue(Iterables.isEmpty(results));

    client.addDocument(MutableMap.<String, Object>of("id", "1", "description", "first"));
    client.addDocument(MutableMap.<String, Object>of("id", "2", "description", "second"));
    client.addDocument(MutableMap.<String, Object>of("id", "3", "description", "third"));
    client.commit();

    results = client.getDocuments();
    assertEquals(Iterables.size(results), 3);
  }
  // TODO BROOKLYN-272, Disabled, because fails non-deterministically in jenkins: BROOKLYN-256
  //    java.lang.AssertionError: Expected: eventually IsEqualTo([starting, running, stopping,
  // stopped]); got most recently: [starting, running, starting, running, stopping, stopped] (waited
  // 1s 11ms 498us 762ns, checked 100)
  //        at org.apache.brooklyn.test.Asserts.fail(Asserts.java:721)
  //        at org.apache.brooklyn.test.Asserts.eventually(Asserts.java:791)
  //        at org.apache.brooklyn.test.Asserts.eventually(Asserts.java:751)
  //        at
  // org.apache.brooklyn.entity.stock.BasicStartableTest.testTransitionsThroughLifecycles(BasicStartableTest.java:170)
  @Test(groups = {"Broken"})
  public void testTransitionsThroughLifecycles() throws Exception {
    BasicStartable startable = app.addChild(EntitySpec.create(BasicStartable.class));
    EntityAsserts.assertAttributeEqualsEventually(
        app, Attributes.SERVICE_STATE_ACTUAL, Lifecycle.STOPPED);

    final RecordingSensorEventListener<Lifecycle> listener =
        new RecordingSensorEventListener<Lifecycle>(true);
    mgmt.getSubscriptionContext(startable)
        .subscribe(startable, Attributes.SERVICE_STATE_ACTUAL, listener);

    app.start(ImmutableList.of(loc1));
    app.config().set(StartableApplication.DESTROY_ON_STOP, false);
    app.stop();

    Iterable<Lifecycle> expected =
        Lists.newArrayList(
            Lifecycle.STARTING, Lifecycle.RUNNING, Lifecycle.STOPPING, Lifecycle.STOPPED);
    Asserts.eventually(
        new Supplier<Iterable<Lifecycle>>() {
          @Override
          public Iterable<Lifecycle> get() {
            return MutableList.copyOf(listener.getEventValuesSortedByTimestamp());
          }
        },
        Predicates.equalTo(expected));
  }
 @Test
 public void testStartsWhenNoStartableChildren() {
   BasicStartable startable =
       app.createAndManageChild(
           EntitySpec.create(BasicStartable.class).child(EntitySpec.create(BasicEntity.class)));
   app.start(ImmutableList.of(app.newSimulatedLocation()));
   EntityAsserts.assertAttributeEquals(
       startable, Attributes.SERVICE_STATE_ACTUAL, Lifecycle.RUNNING);
 }
  @Test
  public void testSendPerfCountersToSensors() {
    AttributeSensor<String> stringSensor = Sensors.newStringSensor("foo.bar");
    AttributeSensor<Integer> integerSensor = Sensors.newIntegerSensor("bar.baz");
    AttributeSensor<Double> doubleSensor = Sensors.newDoubleSensor("baz.quux");

    Collection<WindowsPerformanceCounterPollConfig<?>> polls =
        ImmutableSet.<WindowsPerformanceCounterPollConfig<?>>of(
            new WindowsPerformanceCounterPollConfig(stringSensor)
                .performanceCounterName("\\processor information(_total)\\% processor time"),
            new WindowsPerformanceCounterPollConfig(integerSensor)
                .performanceCounterName("\\integer.sensor"),
            new WindowsPerformanceCounterPollConfig(doubleSensor)
                .performanceCounterName("\\double\\sensor\\with\\multiple\\sub\\paths"));

    WindowsPerformanceCounterFeed.SendPerfCountersToSensors sendPerfCountersToSensors =
        new WindowsPerformanceCounterFeed.SendPerfCountersToSensors(entity, polls);

    assertNull(entity.getAttribute(stringSensor));

    StringBuilder responseBuilder = new StringBuilder();
    // NOTE: This builds the response in a different order to which they are passed to the
    // SendPerfCountersToSensors constructor
    // this tests that the values are applied correctly even if the (possibly non-deterministic)
    // order in which
    // they are returned by the Get-Counter scriptlet is different
    addMockResponse(
        responseBuilder,
        "\\\\machine.name\\double\\sensor\\with\\multiple\\sub\\paths",
        "3.1415926");
    addMockResponse(
        responseBuilder,
        "\\\\win-lge7uj2blau\\processor information(_total)\\% processor time",
        "99.9");
    addMockResponse(responseBuilder, "\\\\machine.name\\integer.sensor", "15");

    sendPerfCountersToSensors.onSuccess(new WinRmToolResponse(responseBuilder.toString(), "", 0));

    EntityAsserts.assertAttributeEquals(entity, stringSensor, "99.9");
    EntityAsserts.assertAttributeEquals(entity, integerSensor, 15);
    EntityAsserts.assertAttributeEquals(entity, doubleSensor, 3.1415926);
  }