package org.apache.mesos.elasticsearch.systemtest;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.jayway.awaitility.Awaitility;
import com.mashape.unirest.http.exceptions.UnirestException;
import org.apache.log4j.Logger;
import org.apache.mesos.mini.MesosCluster;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import java.util.concurrent.TimeUnit;

/**
 * Tests configuration of framework roles
 */
public class FrameworkRoleSystemTest {

    public static final Logger LOGGER = Logger.getLogger(FrameworkRoleSystemTest.class);

    @Rule
    public final MesosCluster CLUSTER = MesosCluster.builder()
            .numberOfSlaves(3)
            .privateRegistryPort(15000) // Currently you have to choose an available port by yourself
            .slaveResources(new String[]{"ports(*):[9200-9200,9300-9300]", "ports(*):[9201-9201,9301-9301]", "ports(*):[9202-9202,9302-9302]"})
            .build();

    @Before
    public void before() throws Exception {
        CLUSTER.injectImage("mesos/elasticsearch-executor");
    }

    @After
    public void after() {
        CLUSTER.stop();
    }

    @Test
    public void miniMesosReportsTheRequestedFrameworkRole() throws UnirestException, JsonParseException, JsonMappingException {
        final String ARBITRARY_ROLE_STRING = "*";

        LOGGER.info("Starting Elasticsearch scheduler");
        ElasticsearchSchedulerContainer scheduler = new ElasticsearchSchedulerContainer(
                CLUSTER.getConfig().dockerClient,
                CLUSTER.getMesosContainer().getIpAddress(),
                ARBITRARY_ROLE_STRING
        );
        CLUSTER.addAndStartContainer(scheduler);
        LOGGER.info("Started Elasticsearch scheduler on " + scheduler.getIpAddress() + ":31100");

        Awaitility.await().atMost(30, TimeUnit.SECONDS).until(() -> CLUSTER.getStateInfo().getFramework("elasticsearch") != null);
        Assert.assertEquals(ARBITRARY_ROLE_STRING, CLUSTER.getStateInfo().getFramework("elasticsearch").getRole());
        scheduler.remove();
    }
}

@@ -453,24 +453,50 @@
 unit.Test;%0A%0A
+import java.util.TreeMap;%0A
 import java.
@@ -610,16 +610,600 @@
 emTest %7B
+%0A    protected static final int NODE_COUNT = 3;%0A%0A    protected static final MesosClusterConfig CONFIG = MesosClusterConfig.builder()%0A            .numberOfSlaves(NODE_COUNT)%0A            .privateRegistryPort(15000) // Currently you have to choose an available port by yourself%0A            .slaveResources(new String%5B%5D%7B%22ports(*):%5B9200-9200,9300-9300%5D%22, %22ports(*):%5B9201-9201,9301-9301%5D%22, %22ports(*):%5B9202-9202,9302-9302%5D%22%7D)%0A            .extraEnvironmentVariables(new TreeMap%3CString, String%3E()%7B%7B%0A                this.put(%22MESOS_ROLES%22, %22*,foobar%22);%0A                %7D%7D)%0A            .build();
 %0A%0A    pu
@@ -1897,20 +1897,193 @@
 orts
-TheRequested
+FrameworkRoleStar() throws UnirestException, JsonParseException, JsonMappingException %7B%0A        testMiniMesosReportsFrameworkRole(%22*%22);%0A    %7D%0A%0A    @Test%0A    public void miniMesosReports
 Fram
@@ -2091,16 +2091,21 @@
 workRole
+Other
 () throw
@@ -2179,50 +2179,191 @@
     
-final String ARBITRARY_ROLE_STRING = %22*%22;%0A
+testMiniMesosReportsFrameworkRole(%22foobar%22);%0A    %7D%0A%0A    private void testMiniMesosReportsFrameworkRole(String role) throws UnirestException, JsonParseException, JsonMappingException %7B
 %0A   
@@ -2412,17 +2412,46 @@
 cheduler
-%22
+ with framework role: %22 + role
 );%0A     
@@ -2660,37 +2660,20 @@
         
-ARBITRARY_ROLE_STRING
+role
 %0A       
@@ -2987,29 +2987,12 @@
 als(
-ARBITRARY_ROLE_STRING
+role
 , CL

Test scheduler starts with a different framework role