|
@@ -29,10 +29,12 @@ import static org.easymock.EasyMock.resetToStrict;
|
|
|
import static org.easymock.EasyMock.verify;
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
|
|
+import java.io.File;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.Collections;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.HashSet;
|
|
|
+import java.util.Iterator;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Set;
|
|
@@ -44,18 +46,24 @@ import org.apache.ambari.server.controller.spi.Request;
|
|
|
import org.apache.ambari.server.controller.spi.Resource;
|
|
|
import org.apache.ambari.server.controller.utilities.PredicateBuilder;
|
|
|
import org.apache.ambari.server.controller.utilities.PropertyHelper;
|
|
|
+import org.apache.ambari.server.metadata.ActionMetadata;
|
|
|
import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
|
|
|
import org.apache.ambari.server.orm.dao.AlertDefinitionDAO;
|
|
|
import org.apache.ambari.server.orm.entities.AlertDefinitionEntity;
|
|
|
import org.apache.ambari.server.state.Cluster;
|
|
|
import org.apache.ambari.server.state.Clusters;
|
|
|
+import org.apache.ambari.server.state.alert.AlertDefinition;
|
|
|
+import org.apache.ambari.server.state.alert.AlertDefinitionFactory;
|
|
|
import org.apache.ambari.server.state.alert.AlertDefinitionHash;
|
|
|
+import org.apache.ambari.server.state.alert.Source;
|
|
|
+import org.apache.ambari.server.state.alert.SourceType;
|
|
|
import org.easymock.Capture;
|
|
|
import org.easymock.EasyMock;
|
|
|
import org.junit.Assert;
|
|
|
import org.junit.Before;
|
|
|
import org.junit.Test;
|
|
|
|
|
|
+import com.google.gson.Gson;
|
|
|
import com.google.inject.Binder;
|
|
|
import com.google.inject.Guice;
|
|
|
import com.google.inject.Injector;
|
|
@@ -69,6 +77,7 @@ public class AlertDefinitionResourceProviderTest {
|
|
|
|
|
|
private AlertDefinitionDAO dao = null;
|
|
|
private AlertDefinitionHash definitionHash = null;
|
|
|
+ private AlertDefinitionFactory m_factory = new AlertDefinitionFactory();
|
|
|
private Injector m_injector;
|
|
|
|
|
|
private static String DEFINITION_UUID = UUID.randomUUID().toString();
|
|
@@ -82,6 +91,7 @@ public class AlertDefinitionResourceProviderTest {
|
|
|
new InMemoryDefaultTestModule()).with(new MockModule()));
|
|
|
|
|
|
AlertDefinitionResourceProvider.init(m_injector);
|
|
|
+ m_injector.injectMembers(m_factory);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -149,6 +159,7 @@ public class AlertDefinitionResourceProviderTest {
|
|
|
AlertDefinitionResourceProvider.ALERT_DEF_ID,
|
|
|
AlertDefinitionResourceProvider.ALERT_DEF_NAME,
|
|
|
AlertDefinitionResourceProvider.ALERT_DEF_LABEL,
|
|
|
+ AlertDefinitionResourceProvider.ALERT_DEF_SOURCE,
|
|
|
AlertDefinitionResourceProvider.ALERT_DEF_SOURCE_TYPE);
|
|
|
|
|
|
AmbariManagementController amc = createMock(AmbariManagementController.class);
|
|
@@ -174,9 +185,20 @@ public class AlertDefinitionResourceProviderTest {
|
|
|
Resource r = results.iterator().next();
|
|
|
|
|
|
Assert.assertEquals("my_def", r.getPropertyValue(AlertDefinitionResourceProvider.ALERT_DEF_NAME));
|
|
|
- Assert.assertEquals("metric", r.getPropertyValue(AlertDefinitionResourceProvider.ALERT_DEF_SOURCE_TYPE));
|
|
|
+
|
|
|
+ Assert.assertEquals(
|
|
|
+ SourceType.METRIC.name(),
|
|
|
+ r.getPropertyValue(AlertDefinitionResourceProvider.ALERT_DEF_SOURCE_TYPE));
|
|
|
+
|
|
|
+ Source source = getMockSource();
|
|
|
+ String okJson = source.getReporting().getOk().getText();
|
|
|
+ Object reporting = r.getPropertyValue(AlertDefinitionResourceProvider.ALERT_DEF_SOURCE_REPORTING);
|
|
|
+
|
|
|
+ Assert.assertTrue(reporting.toString().contains(okJson));
|
|
|
+
|
|
|
Assert.assertEquals("Mock Label",
|
|
|
r.getPropertyValue(AlertDefinitionResourceProvider.ALERT_DEF_LABEL));
|
|
|
+
|
|
|
Assert.assertNotNull(r.getPropertyValue("AlertDefinition/source/type"));
|
|
|
}
|
|
|
|
|
@@ -203,14 +225,25 @@ public class AlertDefinitionResourceProviderTest {
|
|
|
|
|
|
replay(amc, clusters, cluster, dao, definitionHash);
|
|
|
|
|
|
+ Gson gson = m_factory.getGson();
|
|
|
+ Source source = getMockSource();
|
|
|
+ String sourceJson = gson.toJson(source);
|
|
|
AlertDefinitionResourceProvider provider = createProvider(amc);
|
|
|
|
|
|
Map<String, Object> requestProps = new HashMap<String, Object>();
|
|
|
requestProps.put(AlertDefinitionResourceProvider.ALERT_DEF_CLUSTER_NAME, "c1");
|
|
|
requestProps.put(AlertDefinitionResourceProvider.ALERT_DEF_INTERVAL, "1");
|
|
|
requestProps.put(AlertDefinitionResourceProvider.ALERT_DEF_NAME, "my_def");
|
|
|
- requestProps.put(AlertDefinitionResourceProvider.ALERT_DEF_SERVICE_NAME, "HDFS");
|
|
|
- requestProps.put(AlertDefinitionResourceProvider.ALERT_DEF_SOURCE_TYPE, "METRIC");
|
|
|
+
|
|
|
+ requestProps.put(AlertDefinitionResourceProvider.ALERT_DEF_SERVICE_NAME,
|
|
|
+ "HDFS");
|
|
|
+
|
|
|
+ requestProps.put(AlertDefinitionResourceProvider.ALERT_DEF_SOURCE,
|
|
|
+ sourceJson);
|
|
|
+
|
|
|
+ requestProps.put(AlertDefinitionResourceProvider.ALERT_DEF_SOURCE_TYPE,
|
|
|
+ SourceType.METRIC.name());
|
|
|
+
|
|
|
requestProps.put(AlertDefinitionResourceProvider.ALERT_DEF_LABEL,
|
|
|
"Mock Label (Create)");
|
|
|
|
|
@@ -230,10 +263,23 @@ public class AlertDefinitionResourceProviderTest {
|
|
|
Assert.assertEquals(Integer.valueOf(1), entity.getScheduleInterval());
|
|
|
Assert.assertNull(entity.getScope());
|
|
|
Assert.assertEquals("HDFS", entity.getServiceName());
|
|
|
- Assert.assertNotNull(entity.getSource());
|
|
|
Assert.assertEquals("METRIC", entity.getSourceType());
|
|
|
Assert.assertEquals("Mock Label (Create)", entity.getLabel());
|
|
|
|
|
|
+ // verify Source
|
|
|
+ Assert.assertNotNull(entity.getSource());
|
|
|
+ Source actualSource = gson.fromJson(entity.getSource(), Source.class);
|
|
|
+ Assert.assertNotNull(actualSource);
|
|
|
+
|
|
|
+ assertEquals(source.getReporting().getOk().getText(),
|
|
|
+ source.getReporting().getOk().getText());
|
|
|
+
|
|
|
+ assertEquals(source.getReporting().getWarning().getText(),
|
|
|
+ source.getReporting().getWarning().getText());
|
|
|
+
|
|
|
+ assertEquals(source.getReporting().getCritical().getText(),
|
|
|
+ source.getReporting().getCritical().getText());
|
|
|
+
|
|
|
verify(amc, clusters, cluster, dao);
|
|
|
|
|
|
}
|
|
@@ -386,7 +432,10 @@ public class AlertDefinitionResourceProviderTest {
|
|
|
/**
|
|
|
* @return
|
|
|
*/
|
|
|
- private List<AlertDefinitionEntity> getMockEntities() {
|
|
|
+ private List<AlertDefinitionEntity> getMockEntities() throws Exception {
|
|
|
+ Source source = getMockSource();
|
|
|
+ String sourceJson = new Gson().toJson(source);
|
|
|
+
|
|
|
AlertDefinitionEntity entity = new AlertDefinitionEntity();
|
|
|
entity.setClusterId(Long.valueOf(1L));
|
|
|
entity.setComponentName(null);
|
|
@@ -397,12 +446,35 @@ public class AlertDefinitionResourceProviderTest {
|
|
|
entity.setHash(DEFINITION_UUID);
|
|
|
entity.setScheduleInterval(Integer.valueOf(2));
|
|
|
entity.setServiceName(null);
|
|
|
- entity.setSourceType("metric");
|
|
|
- entity.setSource("{'jmx': 'beanName/attributeName', 'host': '{{aa:123445}}'}");
|
|
|
-
|
|
|
+ entity.setSourceType(SourceType.METRIC.name());
|
|
|
+ entity.setSource(sourceJson);
|
|
|
return Arrays.asList(entity);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private Source getMockSource() throws Exception {
|
|
|
+ File alertsFile = new File(
|
|
|
+ "src/test/resources/stacks/HDP/2.0.5/services/HDFS/alerts.json");
|
|
|
+
|
|
|
+ Assert.assertTrue(alertsFile.exists());
|
|
|
+
|
|
|
+ Set<AlertDefinition> set = m_factory.getAlertDefinitions(alertsFile, "HDFS");
|
|
|
+ AlertDefinition nameNodeCpu = null;
|
|
|
+ Iterator<AlertDefinition> definitions = set.iterator();
|
|
|
+ while (definitions.hasNext()) {
|
|
|
+ AlertDefinition definition = definitions.next();
|
|
|
+
|
|
|
+ if (definition.getName().equals("namenode_cpu")) {
|
|
|
+ nameNodeCpu = definition;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Assert.assertNotNull(nameNodeCpu.getSource());
|
|
|
+ return nameNodeCpu.getSource();
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
*
|
|
|
*/
|
|
@@ -418,6 +490,7 @@ public class AlertDefinitionResourceProviderTest {
|
|
|
EasyMock.createNiceMock(Clusters.class));
|
|
|
binder.bind(Cluster.class).toInstance(
|
|
|
EasyMock.createNiceMock(Cluster.class));
|
|
|
+ binder.bind(ActionMetadata.class);
|
|
|
}
|
|
|
}
|
|
|
}
|