Repository: ambari
Updated Branches:
  refs/heads/trunk e91f2a656 -> ca7b901e7


AMBARI-15812 : Changes to widgets to incorporate rate function in AMS not done 
in Upgrade path (avijayan)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/ca7b901e
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/ca7b901e
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/ca7b901e

Branch: refs/heads/trunk
Commit: ca7b901e701fe76429bc1e0f8afbf8c30da50a21
Parents: e91f2a6
Author: Aravindan Vijayan <[email protected]>
Authored: Mon Apr 11 15:40:31 2016 -0700
Committer: Aravindan Vijayan <[email protected]>
Committed: Mon Apr 11 15:40:31 2016 -0700

----------------------------------------------------------------------
 .../server/upgrade/UpgradeCatalog222.java       | 149 ++++++++-----
 .../server/upgrade/UpgradeCatalog222Test.java   | 211 ++++++++++++++++++-
 2 files changed, 308 insertions(+), 52 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/ca7b901e/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog222.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog222.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog222.java
index c958632..f988a7a 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog222.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog222.java
@@ -22,6 +22,8 @@ import java.io.File;
 import java.io.FileReader;
 import java.lang.reflect.Type;
 import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -105,9 +107,6 @@ public class UpgradeCatalog222 extends 
AbstractUpgradeCatalog {
   public static final String AMS_SERVICE_NAME = "AMBARI_METRICS";
   public static final String AMS_COLLECTOR_COMPONENT_NAME = 
"METRICS_COLLECTOR";
 
-  private static final String[] HDFS_WIDGETS_TO_UPDATE = new String[] {
-    "NameNode RPC", "NN Connection Load" };
-
   protected static final String WIDGET_TABLE = "widget";
   protected static final String WIDGET_DESCRIPTION = "description";
   protected static final String WIDGET_NAME = "widget_name";
@@ -180,6 +179,8 @@ public class UpgradeCatalog222 extends 
AbstractUpgradeCatalog {
     updateHiveConfig();
     updateHostRoleCommands();
     updateHDFSWidgetDefinition();
+    updateYARNWidgetDefinition();
+    updateHBASEWidgetDefinition();
     updateCorruptedReplicaWidget();
     updateZookeeperConfigs();
     createNewSliderConfigVersion();
@@ -428,6 +429,57 @@ public class UpgradeCatalog222 extends 
AbstractUpgradeCatalog {
 
   protected void updateHDFSWidgetDefinition() throws AmbariException {
     LOG.info("Updating HDFS widget definition.");
+
+    Map<String, List<String>> widgetMap = new HashMap<>();
+    Map<String, String> sectionLayoutMap = new HashMap<>();
+
+    List<String> hdfsSummaryWidgets = new ArrayList<>(Arrays.asList("NameNode 
RPC", "NN Connection Load",
+      "NameNode GC count", "NameNode GC time"));
+    widgetMap.put("HDFS_SUMMARY", hdfsSummaryWidgets);
+    sectionLayoutMap.put("HDFS_SUMMARY", "default_hdfs_dashboard");
+
+    List<String> hdfsHeatmapWidgets = new ArrayList<>(Arrays.asList("HDFS 
Bytes Read", "HDFS Bytes Written",
+      "DataNode Process Disk I/O Utilization", "DataNode Process Network I/O 
Utilization"));
+    widgetMap.put("HDFS_HEATMAPS", hdfsHeatmapWidgets);
+    sectionLayoutMap.put("HDFS_HEATMAPS", "default_hdfs_heatmap");
+
+    updateWidgetDefinitionsForService("HDFS", widgetMap, sectionLayoutMap);
+  }
+
+  protected void updateYARNWidgetDefinition() throws AmbariException {
+    LOG.info("Updating YARN widget definition.");
+
+    Map<String, List<String>> widgetMap = new HashMap<>();
+    Map<String, String> sectionLayoutMap = new HashMap<>();
+
+    List<String> yarnSummaryWidgets = new ArrayList<>(Arrays.asList("Container 
Failures", "App Failures"));
+    widgetMap.put("YARN_SUMMARY", yarnSummaryWidgets);
+    sectionLayoutMap.put("YARN_SUMMARY", "default_yarn_dashboard");
+
+    List<String> yarnHeatmapWidgets = new ArrayList<>(Arrays.asList("Container 
Failures"));
+    widgetMap.put("YARN_HEATMAPS", yarnHeatmapWidgets);
+    sectionLayoutMap.put("YARN_HEATMAPS", "default_yarn_heatmap");
+
+    updateWidgetDefinitionsForService("YARN", widgetMap, sectionLayoutMap);
+
+  }
+
+  protected void updateHBASEWidgetDefinition() throws AmbariException {
+
+    LOG.info("Updating HBASE widget definition.");
+
+    Map<String, List<String>> widgetMap = new HashMap<>();
+    Map<String, String> sectionLayoutMap = new HashMap<>();
+
+    List<String> hbaseSummaryWidgets = new ArrayList<>(Arrays.asList("Reads 
and Writes", "Blocked Updates"));
+    widgetMap.put("HBASE_SUMMARY", hbaseSummaryWidgets);
+    sectionLayoutMap.put("HBASE_SUMMARY", "default_hbase_dashboard");
+
+    updateWidgetDefinitionsForService("HBASE", widgetMap, sectionLayoutMap);
+  }
+
+  private void updateWidgetDefinitionsForService(String serviceName, 
Map<String, List<String>> widgetMap,
+                                                 Map<String, String> 
sectionLayoutMap) throws AmbariException {
     AmbariManagementController ambariManagementController = 
injector.getInstance(AmbariManagementController.class);
     AmbariMetaInfo ambariMetaInfo = injector.getInstance(AmbariMetaInfo.class);
     Type widgetLayoutType = new TypeToken<Map<String, 
List<WidgetLayout>>>(){}.getType();
@@ -443,61 +495,64 @@ public class UpgradeCatalog222 extends 
AbstractUpgradeCatalog {
       StackId stackId = cluster.getDesiredStackVersion();
       Map<String, Object> widgetDescriptor = null;
       StackInfo stackInfo = ambariMetaInfo.getStack(stackId.getStackName(), 
stackId.getStackVersion());
-      ServiceInfo serviceInfo = stackInfo.getService("HDFS");
+      ServiceInfo serviceInfo = stackInfo.getService(serviceName);
       if (serviceInfo == null) {
-        LOG.info("Skipping updating HDFS widget definition, because HDFS 
service is not present in cluster " +
+        LOG.info("Skipping updating widget definition, because " + serviceName 
+  " service is not present in cluster " +
           "cluster_name= " + cluster.getClusterName());
         continue;
       }
 
-      for (String widgetName : HDFS_WIDGETS_TO_UPDATE) {
-        List<WidgetEntity> widgetEntities = widgetDAO.findByName(clusterID,
-          widgetName, "ambari", "HDFS_SUMMARY");
-
-        if (widgetEntities != null && widgetEntities.size() > 0) {
-          WidgetEntity entityToUpdate = null;
-          if (widgetEntities.size() > 1) {
-            LOG.info("Found more that 1 entity with name = "+ widgetName +
-              " for cluster = " + cluster.getClusterName() + ", skipping 
update.");
-          } else {
-            entityToUpdate = widgetEntities.iterator().next();
-          }
-          if (entityToUpdate != null) {
-            LOG.info("Updating widget: " + entityToUpdate.getWidgetName());
-            // Get the definition from widgets.json file
-            WidgetLayoutInfo targetWidgetLayoutInfo = null;
-            File widgetDescriptorFile = serviceInfo.getWidgetsDescriptorFile();
-            if (widgetDescriptorFile != null && widgetDescriptorFile.exists()) 
{
-              try {
-                widgetDescriptor = gson.fromJson(new 
FileReader(widgetDescriptorFile), widgetLayoutType);
-              } catch (Exception ex) {
-                String msg = "Error loading widgets from file: " + 
widgetDescriptorFile;
-                LOG.error(msg, ex);
-                widgetDescriptor = null;
-              }
+      for (String section : widgetMap.keySet()) {
+        List<String> widgets = widgetMap.get(section);
+        for (String widgetName : widgets) {
+          List<WidgetEntity> widgetEntities = widgetDAO.findByName(clusterID,
+            widgetName, "ambari", section);
+
+          if (widgetEntities != null && widgetEntities.size() > 0) {
+            WidgetEntity entityToUpdate = null;
+            if (widgetEntities.size() > 1) {
+              LOG.info("Found more that 1 entity with name = "+ widgetName +
+                " for cluster = " + cluster.getClusterName() + ", skipping 
update.");
+            } else {
+              entityToUpdate = widgetEntities.iterator().next();
             }
-            if (widgetDescriptor != null) {
-              LOG.debug("Loaded widget descriptor: " + widgetDescriptor);
-              for (Object artifact : widgetDescriptor.values()) {
-                List<WidgetLayout> widgetLayouts = (List<WidgetLayout>) 
artifact;
-                for (WidgetLayout widgetLayout : widgetLayouts) {
-                  if 
(widgetLayout.getLayoutName().equals("default_hdfs_dashboard")) {
-                    for (WidgetLayoutInfo layoutInfo : 
widgetLayout.getWidgetLayoutInfoList()) {
-                      if (layoutInfo.getWidgetName().equals(widgetName)) {
-                        targetWidgetLayoutInfo = layoutInfo;
+            if (entityToUpdate != null) {
+              LOG.info("Updating widget: " + entityToUpdate.getWidgetName());
+              // Get the definition from widgets.json file
+              WidgetLayoutInfo targetWidgetLayoutInfo = null;
+              File widgetDescriptorFile = 
serviceInfo.getWidgetsDescriptorFile();
+              if (widgetDescriptorFile != null && 
widgetDescriptorFile.exists()) {
+                try {
+                  widgetDescriptor = gson.fromJson(new 
FileReader(widgetDescriptorFile), widgetLayoutType);
+                } catch (Exception ex) {
+                  String msg = "Error loading widgets from file: " + 
widgetDescriptorFile;
+                  LOG.error(msg, ex);
+                  widgetDescriptor = null;
+                }
+              }
+              if (widgetDescriptor != null) {
+                LOG.debug("Loaded widget descriptor: " + widgetDescriptor);
+                for (Object artifact : widgetDescriptor.values()) {
+                  List<WidgetLayout> widgetLayouts = (List<WidgetLayout>) 
artifact;
+                  for (WidgetLayout widgetLayout : widgetLayouts) {
+                    if 
(widgetLayout.getLayoutName().equals(sectionLayoutMap.get(section))) {
+                      for (WidgetLayoutInfo layoutInfo : 
widgetLayout.getWidgetLayoutInfoList()) {
+                        if (layoutInfo.getWidgetName().equals(widgetName)) {
+                          targetWidgetLayoutInfo = layoutInfo;
+                        }
                       }
                     }
                   }
                 }
               }
-            }
-            if (targetWidgetLayoutInfo != null) {
-              
entityToUpdate.setMetrics(gson.toJson(targetWidgetLayoutInfo.getMetricsInfo()));
-              
entityToUpdate.setWidgetValues(gson.toJson(targetWidgetLayoutInfo.getValues()));
-              widgetDAO.merge(entityToUpdate);
-            } else {
-              LOG.warn("Unable to find widget layout info for " + widgetName +
-                " in the stack: " + stackId);
+              if (targetWidgetLayoutInfo != null) {
+                
entityToUpdate.setMetrics(gson.toJson(targetWidgetLayoutInfo.getMetricsInfo()));
+                
entityToUpdate.setWidgetValues(gson.toJson(targetWidgetLayoutInfo.getValues()));
+                widgetDAO.merge(entityToUpdate);
+              } else {
+                LOG.warn("Unable to find widget layout info for " + widgetName 
+
+                  " in the stack: " + stackId);
+              }
             }
           }
         }

http://git-wip-us.apache.org/repos/asf/ambari/blob/ca7b901e/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog222Test.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog222Test.java
 
b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog222Test.java
index 4b73022..f0158fd 100644
--- 
a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog222Test.java
+++ 
b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog222Test.java
@@ -131,6 +131,8 @@ public class UpgradeCatalog222Test {
     Method updateHiveConfigs = 
UpgradeCatalog222.class.getDeclaredMethod("updateHiveConfig");
     Method updateHostRoleCommands = 
UpgradeCatalog222.class.getDeclaredMethod("updateHostRoleCommands");
     Method updateHDFSWidget = 
UpgradeCatalog222.class.getDeclaredMethod("updateHDFSWidgetDefinition");
+    Method updateYARNWidget = 
UpgradeCatalog222.class.getDeclaredMethod("updateYARNWidgetDefinition");
+    Method updateHBASEWidget = 
UpgradeCatalog222.class.getDeclaredMethod("updateHBASEWidgetDefinition");
     Method updateCorruptedReplicaWidget = 
UpgradeCatalog222.class.getDeclaredMethod("updateCorruptedReplicaWidget");
     Method createNewSliderConfigVersion = 
UpgradeCatalog222.class.getDeclaredMethod("createNewSliderConfigVersion");
     Method updateZookeeperConfigs = 
UpgradeCatalog222.class.getDeclaredMethod("updateZookeeperConfigs");
@@ -144,6 +146,8 @@ public class UpgradeCatalog222Test {
       .addMockedMethod(updateHiveConfigs)
       .addMockedMethod(updateHostRoleCommands)
       .addMockedMethod(updateHDFSWidget)
+      .addMockedMethod(updateYARNWidget)
+      .addMockedMethod(updateHBASEWidget)
       .addMockedMethod(updateCorruptedReplicaWidget)
       .addMockedMethod(createNewSliderConfigVersion)
       .addMockedMethod(updateZookeeperConfigs)
@@ -164,6 +168,10 @@ public class UpgradeCatalog222Test {
     expectLastCall().once();
     upgradeCatalog222.updateHDFSWidgetDefinition();
     expectLastCall().once();
+    upgradeCatalog222.updateYARNWidgetDefinition();
+    expectLastCall().once();
+    upgradeCatalog222.updateHBASEWidgetDefinition();
+    expectLastCall().once();
     upgradeCatalog222.updateCorruptedReplicaWidget();
     expectLastCall().once();
     upgradeCatalog222.updateZookeeperConfigs();
@@ -541,11 +549,39 @@ public class UpgradeCatalog222Test {
     final WidgetDAO widgetDAO = createNiceMock(WidgetDAO.class);
     final AmbariMetaInfo metaInfo = createNiceMock(AmbariMetaInfo.class);
     WidgetEntity widgetEntity = createNiceMock(WidgetEntity.class);
+    WidgetEntity widgetEntity2 = createNiceMock(WidgetEntity.class);
     StackId stackId = new StackId("HDP", "2.0.0");
     StackInfo stackInfo = createNiceMock(StackInfo.class);
     ServiceInfo serviceInfo = createNiceMock(ServiceInfo.class);
 
-    String widgetStr = 
"{\"layouts\":[{\"layout_name\":\"default_hdfs_dashboard\",\"display_name\":\"Standard
 HDFS 
Dashboard\",\"section_name\":\"HDFS_SUMMARY\",\"widgetLayoutInfo\":[{\"widget_name\":\"NameNode
 RPC\",\"metrics\":[],\"values\":[]}]}]}";
+    String widgetStr = "{\n" +
+      "  \"layouts\": [\n" +
+      "    {\n" +
+      "      \"layout_name\": \"default_hdfs_dashboard\",\n" +
+      "      \"display_name\": \"Standard HDFS Dashboard\",\n" +
+      "      \"section_name\": \"HDFS_SUMMARY\",\n" +
+      "      \"widgetLayoutInfo\": [\n" +
+      "        {\n" +
+      "          \"widget_name\": \"NameNode RPC\",\n" +
+      "          \"metrics\": [],\n" +
+      "          \"values\": []\n" +
+      "        }\n" +
+      "      ]\n" +
+      "    },\n" +
+      "        {\n" +
+      "      \"layout_name\": \"default_hdfs_heatmap\",\n" +
+      "      \"display_name\": \"Standard HDFS HeatMaps\",\n" +
+      "      \"section_name\": \"HDFS_HEATMAPS\",\n" +
+      "      \"widgetLayoutInfo\": [\n" +
+      "        {\n" +
+      "          \"widget_name\": \"HDFS Bytes Read\",\n" +
+      "          \"metrics\": [],\n" +
+      "          \"values\": []\n" +
+      "        }\n" +
+      "      ]\n" +
+      "    }\n" +
+      "  ]\n" +
+      "}";
 
     File dataDirectory = temporaryFolder.newFolder();
     File file = new File(dataDirectory, "hdfs_widget.json");
@@ -571,18 +607,183 @@ public class UpgradeCatalog222Test {
     }}).anyTimes();
     expect(cluster.getClusterId()).andReturn(1L).anyTimes();
     expect(stackInfo.getService("HDFS")).andReturn(serviceInfo);
+    expect(cluster.getDesiredStackVersion()).andReturn(stackId).anyTimes();
+    expect(metaInfo.getStack("HDP", "2.0.0")).andReturn(stackInfo).anyTimes();
+    expect(serviceInfo.getWidgetsDescriptorFile()).andReturn(file).anyTimes();
+
     expect(widgetDAO.findByName(1L, "NameNode RPC", "ambari", "HDFS_SUMMARY"))
       .andReturn(Collections.singletonList(widgetEntity));
-    expect(cluster.getDesiredStackVersion()).andReturn(stackId);
-    expect(metaInfo.getStack("HDP", "2.0.0")).andReturn(stackInfo);
-    expect(serviceInfo.getWidgetsDescriptorFile()).andReturn(file);
     expect(widgetDAO.merge(widgetEntity)).andReturn(null);
     expect(widgetEntity.getWidgetName()).andReturn("Namenode RPC").anyTimes();
 
-    replay(clusters, cluster, controller, widgetDAO, metaInfo, widgetEntity, 
stackInfo, serviceInfo);
+    expect(widgetDAO.findByName(1L, "HDFS Bytes Read", "ambari", 
"HDFS_HEATMAPS"))
+      .andReturn(Collections.singletonList(widgetEntity2));
+    expect(widgetDAO.merge(widgetEntity2)).andReturn(null);
+    expect(widgetEntity2.getWidgetName()).andReturn("HDFS Bytes 
Read").anyTimes();
+
+    replay(clusters, cluster, controller, widgetDAO, metaInfo, widgetEntity, 
widgetEntity2, stackInfo, serviceInfo);
 
     
mockInjector.getInstance(UpgradeCatalog222.class).updateHDFSWidgetDefinition();
 
+    verify(clusters, cluster, controller, widgetDAO, widgetEntity, 
widgetEntity2, stackInfo, serviceInfo);
+  }
+
+  @Test
+  public void testYARNWidgetUpdate() throws Exception {
+    final Clusters clusters = createNiceMock(Clusters.class);
+    final Cluster cluster = createNiceMock(Cluster.class);
+    final AmbariManagementController controller = 
createNiceMock(AmbariManagementController.class);
+    final Gson gson = new Gson();
+    final WidgetDAO widgetDAO = createNiceMock(WidgetDAO.class);
+    final AmbariMetaInfo metaInfo = createNiceMock(AmbariMetaInfo.class);
+    WidgetEntity widgetEntity = createNiceMock(WidgetEntity.class);
+    WidgetEntity widgetEntity2 = createNiceMock(WidgetEntity.class);
+    StackId stackId = new StackId("HDP", "2.0.0");
+    StackInfo stackInfo = createNiceMock(StackInfo.class);
+    ServiceInfo serviceInfo = createNiceMock(ServiceInfo.class);
+
+    String widgetStr = "{\n" +
+      "  \"layouts\": [\n" +
+      "    {\n" +
+      "      \"layout_name\": \"default_yarn_dashboard\",\n" +
+      "      \"display_name\": \"Standard YARN Dashboard\",\n" +
+      "      \"section_name\": \"YARN_SUMMARY\",\n" +
+      "      \"widgetLayoutInfo\": [\n" +
+      "        {\n" +
+      "          \"widget_name\": \"Container Failures\",\n" +
+      "          \"metrics\": [],\n" +
+      "          \"values\": []\n" +
+      "        }\n" +
+      "      ]\n" +
+      "    },\n" +
+      "        {\n" +
+      "      \"layout_name\": \"default_yarn_heatmap\",\n" +
+      "      \"display_name\": \"Standard YARN HeatMaps\",\n" +
+      "      \"section_name\": \"YARN_HEATMAPS\",\n" +
+      "      \"widgetLayoutInfo\": [\n" +
+      "        {\n" +
+      "          \"widget_name\": \"Container Failures\",\n" +
+      "          \"metrics\": [],\n" +
+      "          \"values\": []\n" +
+      "        }\n" +
+      "      ]\n" +
+      "    }\n" +
+      "  ]\n" +
+      "}";
+
+    File dataDirectory = temporaryFolder.newFolder();
+    File file = new File(dataDirectory, "yarn_widget.json");
+    FileUtils.writeStringToFile(file, widgetStr);
+
+    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
+      @Override
+      protected void configure() {
+        
bind(EntityManager.class).toInstance(createNiceMock(EntityManager.class));
+        bind(AmbariManagementController.class).toInstance(controller);
+        bind(Clusters.class).toInstance(clusters);
+        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
+        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
+        bind(Gson.class).toInstance(gson);
+        bind(WidgetDAO.class).toInstance(widgetDAO);
+        
bind(StackManagerFactory.class).toInstance(createNiceMock(StackManagerFactory.class));
+        bind(AmbariMetaInfo.class).toInstance(metaInfo);
+      }
+    });
+    expect(controller.getClusters()).andReturn(clusters).anyTimes();
+    expect(clusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
+      put("normal", cluster);
+    }}).anyTimes();
+    expect(cluster.getClusterId()).andReturn(1L).anyTimes();
+    expect(stackInfo.getService("YARN")).andReturn(serviceInfo);
+    expect(cluster.getDesiredStackVersion()).andReturn(stackId).anyTimes();
+    expect(metaInfo.getStack("HDP", "2.0.0")).andReturn(stackInfo).anyTimes();
+    expect(serviceInfo.getWidgetsDescriptorFile()).andReturn(file).anyTimes();
+
+    expect(widgetDAO.findByName(1L, "Container Failures", "ambari", 
"YARN_SUMMARY"))
+      .andReturn(Collections.singletonList(widgetEntity));
+    expect(widgetDAO.merge(widgetEntity)).andReturn(null);
+    expect(widgetEntity.getWidgetName()).andReturn("Container 
Failures").anyTimes();
+
+    expect(widgetDAO.findByName(1L, "Container Failures", "ambari", 
"YARN_HEATMAPS"))
+      .andReturn(Collections.singletonList(widgetEntity2));
+    expect(widgetDAO.merge(widgetEntity2)).andReturn(null);
+    expect(widgetEntity2.getWidgetName()).andReturn("Container 
Failures").anyTimes();
+
+    replay(clusters, cluster, controller, widgetDAO, metaInfo, widgetEntity, 
widgetEntity2, stackInfo, serviceInfo);
+
+    
mockInjector.getInstance(UpgradeCatalog222.class).updateYARNWidgetDefinition();
+
+    verify(clusters, cluster, controller, widgetDAO, widgetEntity, 
widgetEntity2, stackInfo, serviceInfo);
+  }
+
+
+  @Test
+  public void testHBASEWidgetUpdate() throws Exception {
+    final Clusters clusters = createNiceMock(Clusters.class);
+    final Cluster cluster = createNiceMock(Cluster.class);
+    final AmbariManagementController controller = 
createNiceMock(AmbariManagementController.class);
+    final Gson gson = new Gson();
+    final WidgetDAO widgetDAO = createNiceMock(WidgetDAO.class);
+    final AmbariMetaInfo metaInfo = createNiceMock(AmbariMetaInfo.class);
+    WidgetEntity widgetEntity = createNiceMock(WidgetEntity.class);
+    StackId stackId = new StackId("HDP", "2.0.0");
+    StackInfo stackInfo = createNiceMock(StackInfo.class);
+    ServiceInfo serviceInfo = createNiceMock(ServiceInfo.class);
+
+    String widgetStr = "{\n" +
+      "  \"layouts\": [\n" +
+      "    {\n" +
+      "      \"layout_name\": \"default_hbase_dashboard\",\n" +
+      "      \"display_name\": \"Standard HBASE Dashboard\",\n" +
+      "      \"section_name\": \"HBASE_SUMMARY\",\n" +
+      "      \"widgetLayoutInfo\": [\n" +
+      "        {\n" +
+      "          \"widget_name\": \"Blocked Updates\",\n" +
+      "          \"metrics\": [],\n" +
+      "          \"values\": []\n" +
+      "        }\n" +
+      "      ]\n" +
+      "    } " +
+      "]\n" +
+      "}";
+
+    File dataDirectory = temporaryFolder.newFolder();
+    File file = new File(dataDirectory, "hbase_widget.json");
+    FileUtils.writeStringToFile(file, widgetStr);
+
+    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
+      @Override
+      protected void configure() {
+        
bind(EntityManager.class).toInstance(createNiceMock(EntityManager.class));
+        bind(AmbariManagementController.class).toInstance(controller);
+        bind(Clusters.class).toInstance(clusters);
+        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
+        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
+        bind(Gson.class).toInstance(gson);
+        bind(WidgetDAO.class).toInstance(widgetDAO);
+        
bind(StackManagerFactory.class).toInstance(createNiceMock(StackManagerFactory.class));
+        bind(AmbariMetaInfo.class).toInstance(metaInfo);
+      }
+    });
+    expect(controller.getClusters()).andReturn(clusters).anyTimes();
+    expect(clusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
+      put("normal", cluster);
+    }}).anyTimes();
+    expect(cluster.getClusterId()).andReturn(1L).anyTimes();
+    expect(stackInfo.getService("HBASE")).andReturn(serviceInfo);
+    expect(cluster.getDesiredStackVersion()).andReturn(stackId).anyTimes();
+    expect(metaInfo.getStack("HDP", "2.0.0")).andReturn(stackInfo).anyTimes();
+    expect(serviceInfo.getWidgetsDescriptorFile()).andReturn(file).anyTimes();
+
+    expect(widgetDAO.findByName(1L, "Blocked Updates", "ambari", 
"HBASE_SUMMARY"))
+      .andReturn(Collections.singletonList(widgetEntity));
+    expect(widgetDAO.merge(widgetEntity)).andReturn(null);
+    expect(widgetEntity.getWidgetName()).andReturn("Blocked 
Updates").anyTimes();
+
+    replay(clusters, cluster, controller, widgetDAO, metaInfo, widgetEntity, 
stackInfo, serviceInfo);
+
+    
mockInjector.getInstance(UpgradeCatalog222.class).updateHBASEWidgetDefinition();
+
     verify(clusters, cluster, controller, widgetDAO, widgetEntity, stackInfo, 
serviceInfo);
   }
 

Reply via email to