slider-26 - convenience method for publishing configuration

git-svn-id: https://svn.apache.org/repos/asf/incubator/slider/trunk@1593419 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/slider-core/src/main/java/org/apache/slider/providers/agent/AgentProviderService.java b/slider-core/src/main/java/org/apache/slider/providers/agent/AgentProviderService.java
index b183840..10e1a1c 100644
--- a/slider-core/src/main/java/org/apache/slider/providers/agent/AgentProviderService.java
+++ b/slider-core/src/main/java/org/apache/slider/providers/agent/AgentProviderService.java
@@ -38,6 +38,7 @@
 import org.apache.slider.core.exceptions.SliderException;
 import org.apache.slider.core.launch.CommandLineBuilder;
 import org.apache.slider.core.launch.ContainerLauncher;
+import org.apache.slider.core.registry.docstore.PublishedConfiguration;
 import org.apache.slider.core.registry.info.RegisteredEndpoint;
 import org.apache.slider.core.registry.info.ServiceInstanceData;
 import org.apache.slider.providers.AbstractProviderService;
@@ -225,6 +226,15 @@
     return metainfo;
   }
 
+  protected void publishComponentConfiguration(String name, String description,
+                                             Iterable<Map.Entry<String, String>> entries) {
+    PublishedConfiguration pubconf = new PublishedConfiguration();
+    pubconf.description = description;
+    pubconf.putValues(entries);
+    log.info("publishing {}", pubconf);
+    getStateAccessor().getPublishedConfigurations().put(name, pubconf);
+  }
+
   protected void setRoleHostMapping(String role, String host) {
     List<String> hosts = roleHostMapping.get(role);
     if (hosts == null) {
diff --git a/slider-core/src/test/groovy/org/apache/slider/server/appmaster/web/rest/publisher/TestPublisherRestResources.groovy b/slider-core/src/test/groovy/org/apache/slider/server/appmaster/web/rest/publisher/TestPublisherRestResources.groovy
new file mode 100644
index 0000000..b887d5b
--- /dev/null
+++ b/slider-core/src/test/groovy/org/apache/slider/server/appmaster/web/rest/publisher/TestPublisherRestResources.groovy
@@ -0,0 +1,123 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.slider.server.appmaster.web.rest.publisher
+
+import com.sun.jersey.api.client.Client
+import com.sun.jersey.api.client.ClientResponse
+import com.sun.jersey.api.client.WebResource
+import groovy.transform.CompileStatic
+import groovy.util.logging.Slf4j
+import org.apache.slider.api.StatusKeys
+import org.apache.slider.client.SliderClient
+import org.apache.slider.core.main.ServiceLauncher
+import org.apache.slider.core.registry.docstore.PublishedConfiguration
+import org.apache.slider.providers.agent.AgentTestBase
+import org.apache.slider.server.appmaster.web.rest.RestPaths
+import org.junit.Test
+
+import javax.ws.rs.core.MediaType
+
+import static org.apache.slider.common.params.Arguments.ARG_OPTION
+import static org.apache.slider.common.params.Arguments.ARG_PROVIDER
+import static org.apache.slider.providers.agent.AgentKeys.*
+import static org.apache.slider.providers.agent.AgentTestUtils.createTestClient
+
+@CompileStatic
+@Slf4j
+class TestPublisherRestResources extends AgentTestBase {
+
+  public static final String PUBLISHER_URI = RestPaths.SLIDER_PATH_PUBLISHER;
+  public static final String WADL = "vnd.sun.wadl+xml"
+
+  @Test
+  public void testRestURIs() throws Throwable {
+    def clustername = "test_publisherws"
+    createMiniCluster(
+        clustername,
+        configuration,
+        1,
+        1,
+        1,
+        true,
+        false)
+    Map<String, Integer> roles = [:]
+    File slider_core = new File(new File(".").absoluteFile, "src/test/python");
+    String app_def = "appdef_1.tar"
+    File app_def_path = new File(slider_core, app_def)
+    String agt_ver = "version"
+    File agt_ver_path = new File(slider_core, agt_ver)
+    String agt_conf = "agent.ini"
+    File agt_conf_path = new File(slider_core, agt_conf)
+    assert app_def_path.exists()
+    assert agt_ver_path.exists()
+    assert agt_conf_path.exists()
+    ServiceLauncher<SliderClient> launcher = buildAgentCluster(clustername,
+        roles,
+        [
+            ARG_PROVIDER, "org.apache.slider.server.appmaster.web.rest.publisher.TestSliderProviderFactory",
+            ARG_OPTION, PACKAGE_PATH, slider_core.absolutePath,
+            ARG_OPTION, APP_DEF, "file://" + app_def_path.absolutePath,
+            ARG_OPTION, AGENT_CONF, "file://" + agt_conf_path.absolutePath,
+            ARG_OPTION, AGENT_VERSION, "file://" + agt_ver_path.absolutePath
+        ],
+        true, true,
+        true)
+    SliderClient sliderClient = launcher.service
+    def report = waitForClusterLive(sliderClient)
+    def trackingUrl = report.trackingUrl
+    log.info("tracking URL is $trackingUrl")
+    def publisher_url = appendToURL(trackingUrl, PUBLISHER_URI)
+
+    
+    def status = dumpClusterStatus(sliderClient, "agent AM")
+    def liveURL = status.getInfo(StatusKeys.INFO_AM_WEB_URL) 
+    if (liveURL) {
+      publisher_url = appendToURL(liveURL, PUBLISHER_URI)
+      
+    }
+    
+    log.info("Publisher  is $publisher_url")
+    log.info("stacks is ${liveURL}stacks")
+    log.info("conf   is ${liveURL}conf")
+
+
+    //WS get
+    Client client = createTestClient();
+
+    // test the available GET URIs
+    WebResource webResource = client.resource(publisher_url + "/dummy-site");
+
+    PublishedConfiguration config = webResource.type(MediaType.APPLICATION_JSON)
+                          .get(PublishedConfiguration.class);
+    assert config != null
+    Map<String,String> entries = config.entries
+    log.info("entries are {}", entries)
+    assert entries.get("prop1").equals("val1")
+    assert entries.get("prop2").equals("val2")
+
+    // some negative tests...
+    webResource = client.resource(appendToURL(publisher_url,
+        "/foobar-site"));
+
+    ClientResponse response = webResource.type(MediaType.APPLICATION_JSON)
+                         .get(ClientResponse.class);
+    assert response.getStatus() == 404
+ }
+
+}
diff --git a/slider-core/src/test/java/org/apache/slider/providers/agent/AgentProviderServiceTest.java b/slider-core/src/test/java/org/apache/slider/providers/agent/AgentProviderServiceTest.java
index d6cdb9c..862fea6 100644
--- a/slider-core/src/test/java/org/apache/slider/providers/agent/AgentProviderServiceTest.java
+++ b/slider-core/src/test/java/org/apache/slider/providers/agent/AgentProviderServiceTest.java
@@ -35,6 +35,7 @@
 import org.apache.slider.core.conf.ConfTreeOperations;
 import org.apache.slider.core.conf.MapOperations;
 import org.apache.slider.core.exceptions.SliderException;
+import org.apache.slider.core.launch.ContainerLauncher;
 import org.apache.slider.server.appmaster.model.mock.MockContainerId;
 import org.apache.slider.server.appmaster.model.mock.MockFileSystem;
 import org.apache.slider.server.appmaster.model.mock.MockNodeId;
@@ -127,7 +128,7 @@
     replay(access, ctx, container, sliderFileSystem);
 
     try {
-      mockAps.buildContainerLaunchContext(null,
+      mockAps.buildContainerLaunchContext(createNiceMock(ContainerLauncher.class),
           instanceDefinition,
                                           container,
                                           role,
diff --git a/slider-core/src/test/java/org/apache/slider/server/appmaster/web/rest/publisher/TestAgentProviderService.java b/slider-core/src/test/java/org/apache/slider/server/appmaster/web/rest/publisher/TestAgentProviderService.java
new file mode 100644
index 0000000..6aa7732
--- /dev/null
+++ b/slider-core/src/test/java/org/apache/slider/server/appmaster/web/rest/publisher/TestAgentProviderService.java
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.slider.server.appmaster.web.rest.publisher;
+
+import org.apache.slider.providers.agent.AgentProviderService;
+import org.apache.slider.server.appmaster.state.StateAccessForProviders;
+import org.apache.slider.server.services.registry.RegistryViewForProviders;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ *
+ */
+public class TestAgentProviderService extends AgentProviderService{
+  protected static final Logger log =
+      LoggerFactory.getLogger(TestAgentProviderService.class);
+
+  public TestAgentProviderService() {
+    super();
+    log.info("TestAgentProviderService created");
+  }
+
+  @Override
+  public void bind(StateAccessForProviders stateAccessor,
+                   RegistryViewForProviders registry) {
+    super.bind(stateAccessor, registry);
+    Map<String,String> dummyProps = new HashMap<>();
+    dummyProps.put("prop1", "val1");
+    dummyProps.put("prop2", "val2");
+    log.info("publishing dummy-site.xml with values {}", dummyProps);
+    publishComponentConfiguration("dummy-site", "dummy configuration",
+                                  dummyProps.entrySet());
+
+  }
+
+}
diff --git a/slider-core/src/test/java/org/apache/slider/server/appmaster/web/rest/publisher/TestSliderProviderFactory.java b/slider-core/src/test/java/org/apache/slider/server/appmaster/web/rest/publisher/TestSliderProviderFactory.java
new file mode 100644
index 0000000..f49e15a
--- /dev/null
+++ b/slider-core/src/test/java/org/apache/slider/server/appmaster/web/rest/publisher/TestSliderProviderFactory.java
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.slider.server.appmaster.web.rest.publisher;
+
+import org.apache.slider.providers.ProviderService;
+import org.apache.slider.providers.agent.AgentProviderFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ *
+ */
+public class TestSliderProviderFactory extends AgentProviderFactory{
+  protected static final Logger log =
+      LoggerFactory.getLogger(TestSliderProviderFactory.class);
+
+  public TestSliderProviderFactory() {
+    log.info("Created TestSliderProviderFactory");
+  }
+
+  @Override
+  public ProviderService createServerProvider() {
+    log.info("Creating TestAgentProviderService");
+    return new TestAgentProviderService();
+  }
+}