Example usage for java.util.stream IntStream rangeClosed

List of usage examples for java.util.stream IntStream rangeClosed

Introduction

In this page you can find the example usage for java.util.stream IntStream rangeClosed.

Prototype

public static IntStream rangeClosed(int startInclusive, int endInclusive) 

Source Link

Document

Returns a sequential ordered IntStream from startInclusive (inclusive) to endInclusive (inclusive) by an incremental step of 1 .

Usage

From source file:com.ikanow.aleph2.logging.service.TestLoggingService.java

/**
 * Tests and empty management schema falls back to defaults and filters items by the
 * defaults used in the config file./*from  w ww  .j  ava2 s.  c  o  m*/
 * @throws InterruptedException
 * @throws ExecutionException
 */
@Test
public void test_logEmptyManagement() throws InterruptedException, ExecutionException {
    //if no logging schema is supplied, falls back to defaults in config file (if any)
    //config file is set to:
    //SYSTEM: DEBUG
    //USER: ERROR
    //therefore we should see 
    //5 messages of each type to make it through via the system calls (15)
    //5 messages of DEBUG making it through via user calls (5)
    //5 messages of each type to make it through via the external calls (15)
    final String subsystem_name = "logging_test3";
    final int num_messages_to_log_each_type = 5;
    final List<Level> levels = Arrays.asList(Level.DEBUG, Level.INFO, Level.ERROR);
    final DataBucketBean test_bucket = getEmptyTestBucket("test3");
    final IBucketLogger user_logger = logging_service.getLogger(test_bucket);
    final IBucketLogger system_logger = logging_service.getSystemLogger(test_bucket);
    final IBucketLogger external_logger = logging_service.getExternalLogger(subsystem_name);
    //log a few messages
    IntStream.rangeClosed(1, num_messages_to_log_each_type).boxed().forEach(i -> {
        levels.stream().forEach(level -> {
            user_logger.log(level, ErrorUtils.lazyBuildMessage(true, () -> subsystem_name,
                    () -> "test_message " + i, () -> null, () -> "no error", () -> Collections.emptyMap()));
            system_logger.log(level, ErrorUtils.lazyBuildMessage(true, () -> subsystem_name,
                    () -> "test_message " + i, () -> null, () -> "no error", () -> Collections.emptyMap()));
            external_logger.log(level, ErrorUtils.lazyBuildMessage(true, () -> subsystem_name,
                    () -> "test_message " + i, () -> null, () -> "no error", () -> Collections.emptyMap()));
        });
    });

    user_logger.flush();
    system_logger.flush();
    external_logger.flush();

    //check its in ES, wait 10s max for the index to refresh
    //USER + SYSTEM
    final DataBucketBean logging_test_bucket = BucketUtils.convertDataBucketBeanToLogging(test_bucket);
    final IDataWriteService<BasicMessageBean> logging_crud = search_index_service.getDataService().get()
            .getWritableDataService(BasicMessageBean.class, logging_test_bucket, Optional.empty(),
                    Optional.empty())
            .get();
    waitForResults(logging_crud, 10, 20);
    assertEquals(20, logging_crud.countObjects().get().longValue()); //should only have logged ERROR messages

    //EXTERNAL
    final DataBucketBean logging_external_test_bucket = BucketUtils
            .convertDataBucketBeanToLogging(BeanTemplateUtils.clone(test_bucket)
                    .with(DataBucketBean::full_name, "/external/" + subsystem_name + "/").done());
    final IDataWriteService<BasicMessageBean> logging_crud_external = search_index_service.getDataService()
            .get().getWritableDataService(BasicMessageBean.class, logging_external_test_bucket,
                    Optional.empty(), Optional.empty())
            .get();
    waitForResults(logging_crud_external, 10, 15);
    assertEquals(15, logging_crud_external.countObjects().get().longValue());

    //cleanup
    logging_crud.deleteDatastore().get();
}

From source file:com.ikanow.aleph2.shared.crud.elasticsearch.services.TestElasticsearchCrudService.java

@Test
public void test_CreateMultipleObjects() throws InterruptedException, ExecutionException {

    final ElasticsearchCrudService<TestBean> service = getTestService("testCreateMultipleObjects",
            TestBean.class);

    // 1) Insertion without ids

    assertEquals(0, service.countObjects().get().intValue());

    final List<TestBean> l = IntStream.rangeClosed(1, 10).boxed().map(
            i -> BeanTemplateUtils.build(TestBean.class).with("test_string", "test_string" + i).done().get())
            .collect(Collectors.toList());

    final Future<Tuple2<Supplier<List<Object>>, Supplier<Long>>> result = service.storeObjects(l);
    result.get();//from ww  w  .  j  ava  2s.  c  o  m

    assertEquals(10, service.countObjects().get().intValue());
    assertEquals((Long) (long) 10, result.get()._2().get());

    final List<Object> ids = result.get()._1().get();
    IntStream.rangeClosed(1, 10).boxed().map(i -> Tuples._2T(i, ids.get(i - 1)))
            .forEach(Lambdas.wrap_consumer_u(io -> {
                final Optional<TestBean> tb = service.getObjectById(io._2()).get();
                assertTrue("TestBean should be present: " + io, tb.isPresent());
                assertEquals("test_string" + io._1(), tb.get().test_string);
            }));

    // 2) Insertion with ids

    service.deleteDatastore().get();

    final List<TestBean> l2 = IntStream.rangeClosed(51, 100).boxed().map(i -> BeanTemplateUtils
            .build(TestBean.class).with("_id", "id" + i).with("test_string", "test_string" + i).done().get())
            .collect(Collectors.toList());

    final Future<Tuple2<Supplier<List<Object>>, Supplier<Long>>> result_2 = service.storeObjects(l2);
    result_2.get();

    assertEquals(50, service.countObjects().get().intValue());
    assertEquals(50, result_2.get()._2().get().intValue());

    // 4) Insertion with dups - fail on insert dups

    final List<TestBean> l4 = IntStream.rangeClosed(21, 120).boxed().map(i -> BeanTemplateUtils
            .build(TestBean.class).with("_id", "id" + i).with("test_string", "test_string2" + i).done().get())
            .collect(Collectors.toList());

    final Future<Tuple2<Supplier<List<Object>>, Supplier<Long>>> result_4 = service.storeObjects(l4); // (defaults to adding true)
    result_4.get();

    try {
        assertEquals(50, result_4.get()._2().get().intValue());
        assertEquals(100, service.countObjects().get().intValue());
    } catch (Exception e) {
    }

    // 5) Insertion with dups - overwrite 

    final List<TestBean> l5 = IntStream.rangeClosed(21, 120).boxed().map(i -> BeanTemplateUtils
            .build(TestBean.class).with("_id", "id" + i).with("test_string", "test_string5" + i).done().get())
            .collect(Collectors.toList());

    final Future<Tuple2<Supplier<List<Object>>, Supplier<Long>>> result_5 = service.storeObjects(l5, true); // (defaults to adding true)
    result_5.get();

    try {
        assertEquals(100, result_5.get()._2().get().intValue());
        assertEquals(100, service.countObjects().get().intValue());

        assertEquals(100,
                service.countObjectsBySpec(
                        CrudUtils.allOf(TestBean.class).rangeAbove("test_string", "test_string5", true)).get()
                        .intValue());
    } catch (Exception e) {
    }

}

From source file:com.ikanow.aleph2.logging.service.TestLoggingService.java

@Test
public void test_NoLoggingService() throws InterruptedException, ExecutionException {
    final String subsystem_name = "logging_test4";
    final int num_messages_to_log = 50;
    final DataBucketBean test_bucket = getTestBucket("test4", Optional.of(Level.ALL.toString()),
            Optional.empty());/*from w ww. ja va  2  s . co m*/
    final IBucketLogger user_logger = logging_service_no.getLogger(test_bucket);
    final IBucketLogger system_logger = logging_service_no.getSystemLogger(test_bucket);
    final IBucketLogger external_logger = logging_service_no.getExternalLogger(subsystem_name);
    //log a few messages
    IntStream.rangeClosed(1, num_messages_to_log).boxed().forEach(i -> {
        user_logger.log(Level.ERROR, ErrorUtils.lazyBuildMessage(true, () -> subsystem_name,
                () -> "test_message " + i, () -> null, () -> "no error", () -> Collections.emptyMap()));
        system_logger.log(Level.ERROR, ErrorUtils.lazyBuildMessage(true, () -> subsystem_name,
                () -> "test_message " + i, () -> null, () -> "no error", () -> Collections.emptyMap()));
        external_logger.log(Level.ERROR, ErrorUtils.lazyBuildMessage(true, () -> subsystem_name,
                () -> "test_message " + i, () -> null, () -> "no error", () -> Collections.emptyMap()));
    });

    user_logger.flush();
    system_logger.flush();
    external_logger.flush();

    //check its in ES, wait 10s max for the index to refresh
    final DataBucketBean logging_test_bucket = BucketUtils.convertDataBucketBeanToLogging(test_bucket);
    final IDataWriteService<BasicMessageBean> logging_crud = search_index_service.getDataService().get()
            .getWritableDataService(BasicMessageBean.class, logging_test_bucket, Optional.empty(),
                    Optional.empty())
            .get();
    assertEquals(0, logging_crud.countObjects().get().longValue());

    final DataBucketBean logging_external_test_bucket = BucketUtils
            .convertDataBucketBeanToLogging(BeanTemplateUtils.clone(test_bucket)
                    .with(DataBucketBean::full_name, "/external/" + subsystem_name + "/").done());
    final IDataWriteService<BasicMessageBean> logging_crud_external = search_index_service.getDataService()
            .get().getWritableDataService(BasicMessageBean.class, logging_external_test_bucket,
                    Optional.empty(), Optional.empty())
            .get();
    assertEquals(0, logging_crud_external.countObjects().get().longValue());

    //cleanup
    logging_crud.deleteDatastore().get();
}

From source file:com.ikanow.aleph2.shared.crud.mongodb.services.TestMongoDbCrudService.java

@Test
public void multiObjectRetrieve() throws InterruptedException, ExecutionException {

    final MongoDbCrudService<TestBean, String> service = getTestService("multiObjectRetrieve", TestBean.class,
            String.class);

    final List<TestBean> l = IntStream.rangeClosed(0, 9).boxed()
            .map(i -> BeanTemplateUtils.build(TestBean.class).with("_id", "id" + i)
                    .with("test_string", "test_string" + i).with("test_long", (Long) (long) i).done().get())
            .collect(Collectors.toList());

    service.storeObjects(l);/*  w ww  .ja va2s.c o  m*/

    assertEquals(10, service._state.orig_coll.count());

    service.optimizeQuery(Arrays.asList("test_string")).get(); // (The get() waits for completion)

    // For asserting vs strings where possible:
    final JacksonDBCollection<TestBean, String> mapper = service._state.coll;

    // 1) Simple retrieve, no fields specified - sort

    final QueryComponent<TestBean> query = CrudUtils.allOf(TestBean.class).rangeAbove("_id", "id4", true)
            .withPresent("test_long").orderBy(Tuples._2T("test_string", -1));

    try (Cursor<TestBean> cursor = service.getObjectsBySpec(query).get()) {

        assertEquals(5, cursor.count());

        final List<TestBean> objs = StreamSupport.stream(Optionals.ofNullable(cursor).spliterator(), false)
                .collect(Collectors.toList());

        assertEquals(5, objs.size());

        final DBObject first_obj = mapper.convertToDbObject(objs.get(0));

        assertEquals("{ \"_id\" : \"id9\" , \"test_string\" : \"test_string9\" , \"test_long\" : 9}",
                first_obj.toString());
    } catch (Exception e) {
        //(fail on close, normally carry on - but here error out)
        fail("getObjectsBySpec errored on close");
    }

    // 2) Simple retrieve, field specified (exclusive) - sort and limit

    final QueryComponent<TestBean> query_2 = CrudUtils.allOf(TestBean.class).rangeAbove("_id", "id4", false)
            .withPresent("test_long").orderBy(Tuples._2T("test_long", 1)).limit(4);

    try (Cursor<TestBean> cursor = service.getObjectsBySpec(query_2, Arrays.asList("test_string"), false)
            .get()) {

        assertEquals(6, cursor.count()); // (count ignores limit)

        final List<TestBean> objs = StreamSupport.stream(Optionals.ofNullable(cursor).spliterator(), false)
                .collect(Collectors.toList());

        assertEquals(4, objs.size());

        final DBObject first_obj = mapper.convertToDbObject(objs.get(0));

        assertEquals("{ \"_id\" : \"id4\" , \"test_long\" : 4}", first_obj.toString());
    } catch (Exception e) {
        //(fail on close, normally carry on - but here error out)
        fail("getObjectsBySpec errored on close");
    }

    // 3) Simple retrieve, no docs returned

    final QueryComponent<TestBean> query_3 = CrudUtils.allOf(TestBean.class).rangeAbove("_id", "id9", true)
            .withPresent("test_long").limit(4);

    try (Cursor<TestBean> cursor = service.getObjectsBySpec(query_3, Arrays.asList("test_string"), false)
            .get()) {
        final List<TestBean> objs = StreamSupport.stream(Optionals.ofNullable(cursor).spliterator(), false)
                .collect(Collectors.toList());

        assertEquals(0, objs.size());
    } catch (Exception e) {
        //(fail on close, normally carry on - but here error out)
        fail("getObjectsBySpec errored on close");
    }
}

From source file:com.ikanow.aleph2.shared.crud.elasticsearch.services.TestElasticsearchCrudService.java

@Test
public void test_CreateMultipleObjects_JSON() throws InterruptedException, ExecutionException {

    final ElasticsearchCrudService<JsonNode> service = getTestService("testCreateMultipleObjects_json",
            TestBean.class).getRawService();

    // 1) Insertion without ids
    {//from  ww  w .ja  va  2  s .  c o m
        assertEquals(0, service.countObjects().get().intValue());

        final List<JsonNode> l = IntStream
                .rangeClosed(1, 10).boxed().map(i -> BeanTemplateUtils.build(TestBean.class)
                        .with("test_string", "test_string" + i).done().get())
                .map(o -> BeanTemplateUtils.toJson(o)).collect(Collectors.toList());

        final Future<Tuple2<Supplier<List<Object>>, Supplier<Long>>> result = service.storeObjects(l);
        result.get();

        assertEquals(10, service.countObjects().get().intValue());
        assertEquals((Long) (long) 10, result.get()._2().get());

        final List<Object> ids = result.get()._1().get();
        assertEquals(10, ids.size());
        IntStream.rangeClosed(1, 10).boxed().map(i -> Tuples._2T(i, ids.get(i - 1)))
                .forEach(Lambdas.wrap_consumer_u(io -> {
                    final Optional<JsonNode> tb = service.getObjectById(io._2()).get();
                    assertTrue("TestBean should be present: " + io, tb.isPresent());
                    assertEquals("test_string" + io._1(), tb.get().get("test_string").asText());
                }));
    }
    // 2) Insertion with ids
    {
        service.deleteDatastore().get();

        final List<JsonNode> l2 = IntStream.rangeClosed(51, 100).boxed()
                .map(i -> BeanTemplateUtils.build(TestBean.class).with("_id", "id" + i)
                        .with("test_string", "test_string" + i).done().get())
                .map(o -> BeanTemplateUtils.toJson(o)).collect(Collectors.toList());

        final Future<Tuple2<Supplier<List<Object>>, Supplier<Long>>> result_2 = service.storeObjects(l2);
        result_2.get();

        assertEquals(50, service.countObjects().get().intValue());
        assertEquals(50, result_2.get()._2().get().intValue());
    }

    // 4) Insertion with dups - fail on insert dups
    {
        final List<JsonNode> l4 = IntStream.rangeClosed(21, 120).boxed()
                .map(i -> BeanTemplateUtils.build(TestBean.class).with("_id", "id" + i)
                        .with("test_string", "test_string2" + i).done().get())
                .map(o -> BeanTemplateUtils.toJson(o)).collect(Collectors.toList());

        final Future<Tuple2<Supplier<List<Object>>, Supplier<Long>>> result_4 = service.storeObjects(l4); // (defaults to adding true)
        result_4.get();

        try {
            assertEquals(50, result_4.get()._2().get().intValue());
            assertEquals(100, service.countObjects().get().intValue());
        } catch (Exception e) {
        }
    }
    // 5) Insertion with dups - overwrite 
    {
        final List<JsonNode> l5 = IntStream.rangeClosed(21, 120).boxed()
                .map(i -> BeanTemplateUtils.build(TestBean.class).with("_id", "id" + i)
                        .with("test_string", "test_string5" + i).done().get())
                .map(o -> BeanTemplateUtils.toJson(o)).collect(Collectors.toList());

        final Future<Tuple2<Supplier<List<Object>>, Supplier<Long>>> result_5 = service.storeObjects(l5, true); // (defaults to adding true)
        result_5.get();

        try {
            assertEquals(100, result_5.get()._2().get().intValue());
            assertEquals(100, service.countObjects().get().intValue());

            assertEquals(100, service
                    .countObjectsBySpec(CrudUtils.allOf().rangeAbove("test_string", "test_string5", true)).get()
                    .intValue());
        } catch (Exception e) {
        }
    }
    // 6) Do some testing of overridding _index and _type
    {
        final ElasticsearchCrudService<JsonNode> service_overrides = getTestService(
                "testcreatemultipleobjects_json_overrides__0123456789ab", TestBean.class).getRawService();
        final ElasticsearchCrudService<JsonNode> service_overrides_1 = getTestService(
                "testcreatemultipleobjects_json_overrides__0123456789ab_1", TestBean.class).getRawService();

        final JsonNode j1 = BeanTemplateUtils.configureMapper(Optional.empty()).createObjectNode()
                .put("test_string", "j1").put("_id", "alex")
                .put("_index", "testcreatemultipleobjects_json_overrides__0123456789ax") // (will fail because uui doens't match)
                .put("_type", "test");
        final JsonNode j2 = BeanTemplateUtils.configureMapper(Optional.empty()).createObjectNode()
                .put("test_string", "j2")
                //(no _id, _type)
                .put("_index", "testcreatemultipleobjects_json_overrides__0123456789ab_1");

        final JsonNode j3 = BeanTemplateUtils.configureMapper(Optional.empty()).createObjectNode()
                .put("test_string", "j3")
                //(no _id, _type)
                .put("_index", "testcreatemultipleobjects_json_overrides") // (no __, will be ignored)
        ;

        final Future<Tuple2<Supplier<List<Object>>, Supplier<Long>>> result = service_overrides
                .storeObjects(Arrays.asList(j1, j2, j3));
        assertEquals(3L, result.get()._2().get().longValue());

        assertEquals(2L, service_overrides.countObjects().get().intValue());
        assertEquals(1L, service_overrides_1.countObjectsBySpec(CrudUtils.allOf()).get().intValue());
    }
    // (same but no __ so will be ignored)
    {
        final ElasticsearchCrudService<JsonNode> service_overrides = getTestService(
                "testcreatemultipleobjects_json_overrides", TestBean.class).getRawService();
        final ElasticsearchCrudService<JsonNode> service_overrides_1 = getTestService(
                "testcreatemultipleobjects_json_overrides_1", TestBean.class).getRawService();

        final JsonNode j1 = BeanTemplateUtils.configureMapper(Optional.empty()).createObjectNode()
                .put("test_string", "j1").put("_id", "alex")
                .put("_index", "testcreatemultipleobjects_json_overrides").put("_type", "test");
        final JsonNode j2 = BeanTemplateUtils.configureMapper(Optional.empty()).createObjectNode()
                .put("test_string", "j2")
                //(no _id, _type)
                .put("_index", "testcreatemultipleobjects_json_overrides_1") //(no __, ignored)
        ;

        final JsonNode j3 = BeanTemplateUtils.configureMapper(Optional.empty()).createObjectNode()
                .put("test_string", "j3")
                //(no _id, _type)
                .put("_index", "testcreatemultipleobjects_json_overrides") // (no __, will be ignored)
        ;

        final Future<Tuple2<Supplier<List<Object>>, Supplier<Long>>> result = service_overrides
                .storeObjects(Arrays.asList(j1, j2, j3));
        assertEquals(3L, result.get()._2().get().longValue());

        assertEquals(3L, service_overrides.countObjects().get().intValue());
        assertEquals(0L, service_overrides_1.countObjectsBySpec(CrudUtils.allOf()).get().intValue());
    }

    //TODO: same for object deletion
}

From source file:com.ikanow.aleph2.logging.service.TestLoggingService.java

@Test
public void test_Log4JLoggingService() throws InterruptedException, ExecutionException {
    //add in our appender so we can count messages
    final TestAppender appender = new TestAppender("test", null, PatternLayout.createDefaultLayout());
    appender.start();/*from  w w w.j av  a  2s . c  o m*/
    final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    final AbstractConfiguration config = (AbstractConfiguration) ctx.getConfiguration();
    config.addAppender(appender);
    AppenderRef ref = AppenderRef.createAppenderRef("test", null, null);
    AppenderRef[] refs = new AppenderRef[] { ref };
    LoggerConfig loggerConfig = LoggerConfig.createLogger("false", Level.ALL, "test", "true", refs, null,
            config, null);
    loggerConfig.addAppender(appender, null, null);
    config.addLogger("test", loggerConfig);
    ctx.updateLoggers();
    config.getRootLogger().addAppender(appender, Level.ALL, null);

    //run a normal test
    final String subsystem_name = "logging_test5";
    final int num_messages_to_log = 50;
    final DataBucketBean test_bucket = getTestBucket("test5", Optional.of(Level.ALL.toString()),
            Optional.empty());
    final IBucketLogger user_logger = logging_service_log4j.getLogger(test_bucket);
    final IBucketLogger system_logger = logging_service_log4j.getSystemLogger(test_bucket);
    final IBucketLogger external_logger = logging_service_log4j.getExternalLogger(subsystem_name);
    //log a few messages
    IntStream.rangeClosed(1, num_messages_to_log).boxed().forEach(i -> {
        user_logger.log(Level.ERROR, ErrorUtils.lazyBuildMessage(true, () -> subsystem_name,
                () -> "test_message " + i, () -> null, () -> "no error", () -> Collections.emptyMap()));
        system_logger.log(Level.ERROR, ErrorUtils.lazyBuildMessage(true, () -> subsystem_name,
                () -> "test_message " + i, () -> null, () -> "no error", () -> Collections.emptyMap()));
        external_logger.log(Level.ERROR, ErrorUtils.lazyBuildMessage(true, () -> subsystem_name,
                () -> "test_message " + i, () -> null, () -> "no error", () -> Collections.emptyMap()));
    });

    user_logger.flush();
    system_logger.flush();
    external_logger.flush();

    //check in our appender for how many messages we received      
    assertEquals(num_messages_to_log * 3, appender.message_count);

    //cleanup
    config.removeAppender("test");
    ctx.updateLoggers();
}

From source file:com.ikanow.aleph2.shared.crud.mongodb.services.TestMongoDbCrudService.java

@Test
public void testCounting() throws InterruptedException, ExecutionException {

    final MongoDbCrudService<TestBean, String> service = getTestService("testCounting", TestBean.class,
            String.class);

    final List<TestBean> l = IntStream.rangeClosed(0, 9).boxed()
            .map(i -> BeanTemplateUtils.build(TestBean.class).with("_id", "id" + i)
                    .with("test_string", "test_string" + i).with("test_long", (Long) (long) i).done().get())
            .collect(Collectors.toList());

    service.storeObjects(l);//from  w w w . ja v a  2  s .  c om

    assertEquals(10, service._state.orig_coll.count());

    service.optimizeQuery(Arrays.asList("test_string")).get(); // (The get() waits for completion)

    // 1) all docs

    assertEquals(10L, (long) service.countObjects().get());

    // 2) count subset of docs

    final QueryComponent<TestBean> query_2 = CrudUtils.allOf(TestBean.class).rangeAbove("_id", "id4", false)
            .withPresent("test_long").orderBy(Tuples._2T("test_long", 1));

    assertEquals(6L, (long) service.countObjectsBySpec(query_2).get());

    // 3) subset of docs (limit)

    final QueryComponent<TestBean> query_3 = CrudUtils.allOf(TestBean.class).rangeAbove("_id", "id4", false)
            .withPresent("test_long").orderBy(Tuples._2T("test_long", 1)).limit(4);

    assertEquals(4L, (long) service.countObjectsBySpec(query_3).get());

    // 4) no docs

    final QueryComponent<TestBean> query_4 = CrudUtils.allOf(TestBean.class).rangeAbove("_id", "id99", false)
            .withPresent("test_long").orderBy(Tuples._2T("test_long", 1)).limit(4);

    assertEquals(0L, (long) service.countObjectsBySpec(query_4).get());
}

From source file:org.ow2.proactive.connector.iaas.cloud.provider.azure.AzureProvider.java

private List<ScriptResult> executeScriptOnVM(VirtualMachine vm, InstanceScript instanceScript) {

    // Concatenate all provided scripts in one (Multiple VMExtensions per handler not supported)
    StringBuilder concatenatedScripts = new StringBuilder();
    Arrays.stream(instanceScript.getScripts()).forEach(script -> {
        concatenatedScripts.append(script).append(SCRIPT_SEPARATOR);
    });//from  w  w w. j  av a2 s  .co m

    Optional<VirtualMachineExtension> vmExtension = vm.extensions().values().stream()
            .filter(extension -> extension.publisherName().equals(SCRIPT_EXTENSION_PUBLISHER)
                    && extension.typeName().equals(SCRIPT_EXTENSION_TYPE))
            .findAny();
    if (vmExtension.isPresent()) {
        vm.update().updateExtension(vmExtension.get().name())
                .withPublicSetting(SCRIPT_EXTENSION_CMD_KEY, concatenatedScripts.toString()).parent().apply();
    } else {
        vm.update().defineNewExtension(createUniqueScriptName(vm.name()))
                .withPublisher(SCRIPT_EXTENSION_PUBLISHER).withType(SCRIPT_EXTENSION_TYPE)
                .withVersion(SCRIPT_EXTENSION_VERSION).withMinorVersionAutoUpgrade()
                .withPublicSetting(SCRIPT_EXTENSION_CMD_KEY, concatenatedScripts.toString()).attach().apply();
    }

    // Unable to retrieve scripts output, returns empty results instead
    return IntStream.rangeClosed(1, instanceScript.getScripts().length)
            .mapToObj(scriptNumber -> new ScriptResult(vm.vmId(), "", "")).collect(Collectors.toList());
}

From source file:com.ikanow.aleph2.logging.service.TestLoggingService.java

@SuppressWarnings("unchecked")
@Test/*w ww  .j a va  2 s  .  c o m*/
public void test_logFunctions() throws InterruptedException, ExecutionException {
    final String subsystem_name = "logging_test6";
    final int num_messages_to_log_each_type = 5;
    final List<Level> levels = Arrays.asList(Level.DEBUG, Level.INFO, Level.ERROR);
    final DataBucketBean test_bucket = getTestBucket("test6", Optional.of(Level.ERROR.toString()),
            Optional.empty());
    final IBucketLogger user_logger = logging_service.getLogger(test_bucket);
    final IBucketLogger system_logger = logging_service.getSystemLogger(test_bucket);
    final IBucketLogger external_logger = logging_service.getExternalLogger(subsystem_name);

    //log a few messages
    IntStream.rangeClosed(1, num_messages_to_log_each_type).boxed().forEach(i -> {
        levels.stream().forEach(level -> {
            //append
            user_logger.log(level,
                    ErrorUtils.lazyBuildMessage(true, () -> subsystem_name, () -> "test_message " + i,
                            () -> null, () -> "no error", () -> Collections.emptyMap()),
                    "key1", Collections.emptyList(), Optional.empty(), LoggingMergeFunctions.appendMessage());
            system_logger.log(level,
                    ErrorUtils.lazyBuildMessage(true, () -> subsystem_name, () -> "test_message " + i,
                            () -> null, () -> "no error", () -> Collections.emptyMap()),
                    "key1", Collections.emptyList(), Optional.empty(), LoggingMergeFunctions.appendMessage());
            external_logger.log(level,
                    ErrorUtils.lazyBuildMessage(true, () -> subsystem_name, () -> "test_message " + i,
                            () -> null, () -> "no error", () -> Collections.emptyMap()),
                    "key1", Collections.emptyList(), Optional.empty(), LoggingMergeFunctions.appendMessage());

            //count
            user_logger.log(level,
                    ErrorUtils.lazyBuildMessage(true, () -> subsystem_name, () -> "test_message " + i,
                            () -> null, () -> "no error", () -> Collections.emptyMap()),
                    "key1", Collections.emptyList(), Optional.empty(), LoggingMergeFunctions.countMessages());
            system_logger.log(level,
                    ErrorUtils.lazyBuildMessage(true, () -> subsystem_name, () -> "test_message " + i,
                            () -> null, () -> "no error", () -> Collections.emptyMap()),
                    "key1", Collections.emptyList(), Optional.empty(), LoggingMergeFunctions.countMessages());
            external_logger.log(level,
                    ErrorUtils.lazyBuildMessage(true, () -> subsystem_name, () -> "test_message " + i,
                            () -> null, () -> "no error", () -> Collections.emptyMap()),
                    "key1", Collections.emptyList(), Optional.empty(), LoggingMergeFunctions.countMessages());

            //sum
            user_logger.log(level,
                    ErrorUtils.lazyBuildMessage(true, () -> subsystem_name, () -> "test_message " + i,
                            () -> null, () -> "no error", () -> ImmutableMap.of(VALUE_FIELD, (double) i)),
                    "key1", Collections.emptyList(), Optional.empty(),
                    LoggingMergeFunctions.sumField(VALUE_FIELD));
            system_logger.log(level,
                    ErrorUtils.lazyBuildMessage(true, () -> subsystem_name, () -> "test_message " + i,
                            () -> null, () -> "no error", () -> ImmutableMap.of(VALUE_FIELD, (double) i)),
                    "key1", Collections.emptyList(), Optional.empty(),
                    LoggingMergeFunctions.sumField(VALUE_FIELD));
            external_logger.log(level,
                    ErrorUtils.lazyBuildMessage(true, () -> subsystem_name, () -> "test_message " + i,
                            () -> null, () -> "no error", () -> ImmutableMap.of(VALUE_FIELD, (double) i)),
                    "key1", Collections.emptyList(), Optional.empty(),
                    LoggingMergeFunctions.sumField(VALUE_FIELD));

            //min
            user_logger.log(level,
                    ErrorUtils.lazyBuildMessage(true, () -> subsystem_name, () -> "test_message " + i,
                            () -> null, () -> "no error", () -> ImmutableMap.of(VALUE_FIELD, (double) i)),
                    "key1", Collections.emptyList(), Optional.empty(),
                    LoggingMergeFunctions.minField(VALUE_FIELD));
            system_logger.log(level,
                    ErrorUtils.lazyBuildMessage(true, () -> subsystem_name, () -> "test_message " + i,
                            () -> null, () -> "no error", () -> ImmutableMap.of(VALUE_FIELD, (double) i)),
                    "key1", Collections.emptyList(), Optional.empty(),
                    LoggingMergeFunctions.minField(VALUE_FIELD));
            external_logger.log(level,
                    ErrorUtils.lazyBuildMessage(true, () -> subsystem_name, () -> "test_message " + i,
                            () -> null, () -> "no error", () -> ImmutableMap.of(VALUE_FIELD, (double) i)),
                    "key1", Collections.emptyList(), Optional.empty(),
                    LoggingMergeFunctions.minField(VALUE_FIELD));
            //max
            user_logger.log(level,
                    ErrorUtils.lazyBuildMessage(true, () -> subsystem_name, () -> "test_message " + i,
                            () -> null, () -> "no error", () -> ImmutableMap.of(VALUE_FIELD, (double) i)),
                    "key1", Collections.emptyList(), Optional.empty(),
                    LoggingMergeFunctions.maxField(VALUE_FIELD));
            system_logger.log(level,
                    ErrorUtils.lazyBuildMessage(true, () -> subsystem_name, () -> "test_message " + i,
                            () -> null, () -> "no error", () -> ImmutableMap.of(VALUE_FIELD, (double) i)),
                    "key1", Collections.emptyList(), Optional.empty(),
                    LoggingMergeFunctions.maxField(VALUE_FIELD));
            external_logger.log(level,
                    ErrorUtils.lazyBuildMessage(true, () -> subsystem_name, () -> "test_message " + i,
                            () -> null, () -> "no error", () -> ImmutableMap.of(VALUE_FIELD, (double) i)),
                    "key1", Collections.emptyList(), Optional.empty(),
                    LoggingMergeFunctions.maxField(VALUE_FIELD));

            //minmax
            user_logger.log(level,
                    ErrorUtils.lazyBuildMessage(true, () -> subsystem_name, () -> "test_message " + i,
                            () -> null, () -> "no error", () -> ImmutableMap.of(VALUE_FIELD, (double) i)),
                    "key1", Collections.emptyList(), Optional.empty(),
                    LoggingMergeFunctions.minMaxField(VALUE_FIELD));
            system_logger.log(level,
                    ErrorUtils.lazyBuildMessage(true, () -> subsystem_name, () -> "test_message " + i,
                            () -> null, () -> "no error", () -> ImmutableMap.of(VALUE_FIELD, (double) i)),
                    "key1", Collections.emptyList(), Optional.empty(),
                    LoggingMergeFunctions.minMaxField(VALUE_FIELD));
            external_logger.log(level,
                    ErrorUtils.lazyBuildMessage(true, () -> subsystem_name, () -> "test_message " + i,
                            () -> null, () -> "no error", () -> ImmutableMap.of(VALUE_FIELD, (double) i)),
                    "key1", Collections.emptyList(), Optional.empty(),
                    LoggingMergeFunctions.minMaxField(VALUE_FIELD));

            //chaining test
            user_logger.log(level,
                    ErrorUtils.lazyBuildMessage(true, () -> subsystem_name, () -> "test_message " + i,
                            () -> null, () -> "no error", () -> ImmutableMap.of(VALUE_FIELD, (double) i)),
                    "key1", Collections.emptyList(), Optional.empty(),
                    LoggingMergeFunctions.sumField(VALUE_FIELD, "out1", false),
                    LoggingMergeFunctions.sumField(VALUE_FIELD, "out2", false));
            system_logger.log(level,
                    ErrorUtils.lazyBuildMessage(true, () -> subsystem_name, () -> "test_message " + i,
                            () -> null, () -> "no error", () -> ImmutableMap.of(VALUE_FIELD, (double) i)),
                    "key1", Collections.emptyList(), Optional.empty(),
                    LoggingMergeFunctions.sumField(VALUE_FIELD, "out1", false),
                    LoggingMergeFunctions.sumField(VALUE_FIELD, "out2", false));
            external_logger.log(level,
                    ErrorUtils.lazyBuildMessage(true, () -> subsystem_name, () -> "test_message " + i,
                            () -> null, () -> "no error", () -> ImmutableMap.of(VALUE_FIELD, (double) i)),
                    "key1", Collections.emptyList(), Optional.empty(),
                    LoggingMergeFunctions.sumField(VALUE_FIELD, "out1", false),
                    LoggingMergeFunctions.sumField(VALUE_FIELD, "out2", false));
        });
    });

    user_logger.flush();
    system_logger.flush();
    external_logger.flush();

    //check its in ES, wait 10s max for the index to refresh
    final DataBucketBean logging_test_bucket = BucketUtils.convertDataBucketBeanToLogging(test_bucket);
    final IDataWriteService<BasicMessageBean> logging_crud = search_index_service.getDataService().get()
            .getWritableDataService(BasicMessageBean.class, logging_test_bucket, Optional.empty(),
                    Optional.empty())
            .get();
    waitForResults(logging_crud, 10, 70);
    assertEquals(70, logging_crud.countObjects().get().longValue()); //should only have logged ERROR messages

    final DataBucketBean logging_external_test_bucket = BucketUtils
            .convertDataBucketBeanToLogging(BeanTemplateUtils.clone(test_bucket)
                    .with(DataBucketBean::full_name, "/external/" + subsystem_name + "/").done());
    final IDataWriteService<BasicMessageBean> logging_crud_external = search_index_service.getDataService()
            .get().getWritableDataService(BasicMessageBean.class, logging_external_test_bucket,
                    Optional.empty(), Optional.empty())
            .get();
    waitForResults(logging_crud_external, 10, 105);
    assertEquals(105, logging_crud_external.countObjects().get().longValue());

    //cleanup
    logging_crud.deleteDatastore().get();
}

From source file:com.ikanow.aleph2.shared.crud.elasticsearch.services.TestElasticsearchCrudService.java

@Test
public void test_CreateMultipleObjects_JSON_batch() throws InterruptedException, ExecutionException {

    final ElasticsearchCrudService<JsonNode> service = getTestService("testCreateMultipleObjects_json_batch",
            TestBean.class).getRawService();

    @SuppressWarnings("unchecked")
    final ElasticsearchCrudService<JsonNode>.ElasticsearchBatchSubsystem batch_service = service
            .getUnderlyingPlatformDriver(ElasticsearchBatchSubsystem.class, Optional.empty()).get();

    batch_service.setBatchProperties(Optional.empty(), Optional.empty(),
            Optional.of(Duration.of(2, ChronoUnit.SECONDS)), Optional.of(10));

    // 1) Insertion without ids

    assertEquals(0, service.countObjects().get().intValue());

    final List<JsonNode> l = IntStream.rangeClosed(1, 10).boxed().map(
            i -> BeanTemplateUtils.build(TestBean.class).with("test_string", "test_string" + i).done().get())
            .map(o -> BeanTemplateUtils.toJson(o)).collect(Collectors.toList());

    batch_service.storeObjects(l, false);

    //Sleep for 5s to let it flush
    try {// www  . j  av a  2s .  c  o  m
        Thread.sleep(5000L);
    } catch (Exception e) {
    }

    assertEquals(10, service.countObjects().get().intValue());

    // Check all the expected objects exist:

    IntStream.rangeClosed(1, 10).boxed().map(i -> "test_string" + i).forEach(Lambdas.wrap_consumer_u(field -> {
        Optional<JsonNode> obj = service.getObjectBySpec(CrudUtils.allOf().when("test_string", field)).get();
        assertTrue("NEeds to find: " + field, obj.isPresent());
    }));

    // 2) Insertion with ids

    service.deleteDatastore().get();

    batch_service.setBatchProperties(Optional.of(30), Optional.empty(), Optional.empty(), Optional.of(0));

    final List<JsonNode> l2 = IntStream.rangeClosed(51, 100).boxed()
            .map(i -> BeanTemplateUtils.build(TestBean.class).with("_id", "id" + i)
                    .with("test_string", "test_string" + i).done().get())
            .map(o -> BeanTemplateUtils.toJson(o)).collect(Collectors.toList());

    batch_service.storeObjects(l2, false);

    try {
        Thread.sleep(1500L);
    } catch (Exception e) {
    }

    assertEquals(30, service.countObjects().get().intValue());

    //Sleep for total 5s to let it flush
    try {
        Thread.sleep(3500L);
    } catch (Exception e) {
    }

    assertEquals(50, service.countObjects().get().intValue());

    IntStream.rangeClosed(51, 100).boxed().map(i -> "test_string" + i)
            .forEach(Lambdas.wrap_consumer_u(field -> {
                Optional<JsonNode> obj = service.getObjectBySpec(CrudUtils.allOf().when("test_string", field))
                        .get();
                assertTrue("NEeds to find: " + field, obj.isPresent());
            }));

    // 4) Insertion with dups - fail on insert dups

    batch_service.setBatchProperties(Optional.empty(), Optional.of(10000L), Optional.empty(), Optional.of(5));

    final List<JsonNode> l4 = IntStream.rangeClosed(21, 120).boxed()
            .map(i -> BeanTemplateUtils.build(TestBean.class).with("_id", "id" + i)
                    .with("test_string", "test_string2" + i).done().get())
            .map(o -> BeanTemplateUtils.toJson(o)).collect(Collectors.toList());

    batch_service.storeObjects(l4, false);

    //Sleep for total 5s to let it flush
    try {
        Thread.sleep(5000L);
    } catch (Exception e) {
    }

    assertEquals(100, service.countObjects().get().intValue());

    // 5) Insertion with dups - overwrite 

    batch_service.setBatchProperties(Optional.of(20), Optional.empty(),
            Optional.of(Duration.of(4, ChronoUnit.SECONDS)), Optional.empty());
    try {
        Thread.sleep(100L);
    } catch (Exception e) {
    }

    final List<JsonNode> l5_1 = IntStream.rangeClosed(21, 59).boxed()
            .map(i -> BeanTemplateUtils.build(TestBean.class).with("_id", "id" + i)
                    .with("test_string", "test_string5" + i).done().get())
            .map(o -> BeanTemplateUtils.toJson(o)).collect(Collectors.toList());

    final List<JsonNode> l5_2 = IntStream.rangeClosed(60, 120).boxed()
            .map(i -> BeanTemplateUtils.build(TestBean.class).with("_id", "id" + i)
                    .with("test_string", "test_string5" + i).done().get())
            .map(o -> BeanTemplateUtils.toJson(o)).collect(Collectors.toList());

    batch_service.storeObjects(l5_1, true);

    // (wait for it to refresh)
    try {
        Thread.sleep(1100L);
    } catch (Exception e) {
    }

    assertEquals(100, service.countObjects().get().intValue()); // first batch                  

    // Check only some objects are overwritten
    IntStream.rangeClosed(21, 40).boxed().map(i -> "test_string5" + i)
            .forEach(Lambdas.wrap_consumer_u(field -> {
                Optional<JsonNode> obj = service.getObjectBySpec(CrudUtils.allOf().when("test_string", field))
                        .get();
                assertTrue("NEeds to find: " + field, obj.isPresent());
            }));
    IntStream.rangeClosed(41, 50).boxed().map(i -> "test_string2" + i)
            .forEach(Lambdas.wrap_consumer_u(field -> {
                Optional<JsonNode> obj = service.getObjectBySpec(CrudUtils.allOf().when("test_string", field))
                        .get();
                assertTrue("NEeds to find: " + field, obj.isPresent());
            }));
    IntStream.rangeClosed(51, 100).boxed().map(i -> "test_string" + i)
            .forEach(Lambdas.wrap_consumer_u(field -> {
                Optional<JsonNode> obj = service.getObjectBySpec(CrudUtils.allOf().when("test_string", field))
                        .get();
                assertTrue("NEeds to find: " + field, obj.isPresent());
            }));

    batch_service.storeObjects(l5_2, true);

    //Sleep for total 1s to let it flush
    try {
        Thread.sleep(5000L);
    } catch (Exception e) {
    }

    assertEquals(100, service.countObjects().get().intValue());

    // Check all objects are overwritten

    IntStream.rangeClosed(21, 120).boxed().map(i -> "test_string5" + i)
            .forEach(Lambdas.wrap_consumer_u(field -> {
                Optional<JsonNode> obj = service.getObjectBySpec(CrudUtils.allOf().when("test_string", field))
                        .get();
                assertTrue("NEeds to find: " + field, obj.isPresent());
            }));

}