Prechádzať zdrojové kódy

AMBARI-5182. Retrieve registered host from API throws exception (dlysnichenko)

Lisnichenko Dmitro 11 rokov pred
rodič
commit
a3410253ca

+ 6 - 2
ambari-server/src/main/java/org/apache/ambari/server/controller/nagios/NagiosPropertyProvider.java

@@ -164,8 +164,12 @@ public class NagiosPropertyProvider extends BaseProvider implements PropertyProv
       
       if (null == matchValue)
         continue;
-      
-      String clusterName = res.getPropertyValue(clusterNameProperty).toString();
+
+      Object clusterPropertyValue = res.getPropertyValue(clusterNameProperty);
+      if (null == clusterPropertyValue)
+        continue;
+
+      String clusterName = clusterPropertyValue.toString();
       if (null == clusterName)
         continue;
       

+ 23 - 0
ambari-server/src/test/java/org/apache/ambari/server/controller/nagios/NagiosPropertyProviderTest.java

@@ -120,6 +120,29 @@ public class NagiosPropertyProviderTest {
     
     Assert.assertFalse("Expected no alerts", values.containsKey("alerts"));
   }
+
+
+  @Test
+  public void testClusterDoesNotExistNPE() throws Exception {
+    TestStreamProvider streamProvider = new TestStreamProvider("nagios_alerts.txt");
+
+    NagiosPropertyProvider npp = new NagiosPropertyProvider(Resource.Type.Service,
+        streamProvider,
+        "ServiceInfo/cluster_name",
+        "ServiceInfo/service_name");
+
+    Resource resource = new ResourceImpl(Resource.Type.Service);
+    resource.setProperty("ServiceInfo/cluster_name", null);
+    resource.setProperty("ServiceInfo/service_name", "HBASE");
+
+    // request with an empty set should get all supported properties
+    Request request = PropertyHelper.getReadRequest(Collections.<String>emptySet(),
+            new HashMap<String, TemporalInfo>());
+
+    Set<Resource> set = npp.populateResources(Collections.singleton(resource), request, null);
+    Assert.assertEquals(1, set.size());
+
+  }
   
   @Test
   public void testNoNagiosServerCompoonent() throws Exception {