Kaynağa Gözat

AMBARI-1561. API should return nagios_alerts as a JSON, not a stringified JSON. (swagle)

git-svn-id: https://svn.apache.org/repos/asf/incubator/ambari/trunk@1453481 13f79535-47bb-0310-9956-ffa450edef68
Siddharth Wagle 12 yıl önce
ebeveyn
işleme
4f8357fdf5

+ 3 - 0
CHANGES.txt

@@ -416,6 +416,9 @@ Trunk (unreleased changes):
 
  BUG FIXES
 
+ AMBARI-1561. API should return nagios_alerts as a JSON, not a 
+ stringified JSON. (swagle)
+
  AMBARI-1492. Add init.d scripts for Ambari server + agent. (swagle)
 
  AMBARI-1548. Implement Stacks API using the consistent API framework in 

+ 2 - 4
ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HttpProxyPropertyProvider.java

@@ -24,6 +24,7 @@ import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 
+import org.apache.ambari.server.api.services.parsers.JsonPropertyParser;
 import org.apache.ambari.server.controller.spi.Predicate;
 import org.apache.ambari.server.controller.spi.PropertyProvider;
 import org.apache.ambari.server.controller.spi.Request;
@@ -109,10 +110,7 @@ public class HttpProxyPropertyProvider extends BaseProvider implements PropertyP
     InputStream in = null;
     try {
       in = streamProvider.readFrom(url);
-      
-      String str = IOUtils.toString(in, "UTF-8");
-      
-      r.setProperty(propertyIdToSet, str);
+      r.setProperty(propertyIdToSet, new JsonPropertyParser().parse(IOUtils.toString(in, "UTF-8")));
     }
     catch (IOException ioe) {
       LOG.error("Error reading HTTP response from " + url);

+ 22 - 5
ambari-server/src/test/java/org/apache/ambari/server/controller/internal/HttpPropertyProviderTest.java

@@ -23,6 +23,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.util.Collections;
 import java.util.HashSet;
+import java.util.Map;
 import java.util.Set;
 
 import org.apache.ambari.server.controller.spi.Request;
@@ -73,8 +74,25 @@ public class HttpPropertyProviderTest {
    Assert.assertNotNull("Expected non-null for 'nagios_alerts'",
      resource.getPropertyValue(PROPERTY_ID_NAGIOS_ALERTS));        
   }
-  
-  
+
+  @Test
+  public void testReadWithRequestedJson() throws Exception {
+
+  Set<String> propertyIds = new HashSet<String>();
+  propertyIds.add(PropertyHelper.getPropertyId("HostRoles", "nagios_alerts"));
+  propertyIds.add(PROPERTY_ID_COMPONENT_NAME);
+  Resource resource = doPopulate("NAGIOS_SERVER", propertyIds);
+  Object propertyValue = resource.getPropertyValue(PROPERTY_ID_NAGIOS_ALERTS);
+
+  Assert.assertNotNull("Expected non-null for 'nagios_alerts'", propertyValue);
+  Assert.assertTrue("Expected Set for parsed JSON", propertyValue instanceof Set);
+
+  Object propertyEntry = ((Set) propertyValue).iterator().next();
+
+  Assert.assertTrue(propertyEntry instanceof Map);
+  Assert.assertEquals("Alert Body", ((Map) propertyEntry).get("nagios_alert"));
+  }
+
   @Test
   public void testReadGangliaServer() throws Exception {
     
@@ -112,9 +130,8 @@ public class HttpPropertyProviderTest {
 
     @Override
     public InputStream readFrom(String spec) throws IOException {
-      return new ByteArrayInputStream("PROPERTY_TEST".getBytes());
+        String responseStr = "[{\"nagios_alert\": \"Alert Body\"}]";
+        return new ByteArrayInputStream(responseStr.getBytes("UTF-8"));
     }
   }
-  
-  
 }