Repository: ambari
Updated Branches:
  refs/heads/branch-2.0.maint 80207df77 -> 84a7c5d27


AMBARI-10405 Some service components are missing from API response and causes 
problems (dsen)


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

Branch: refs/heads/branch-2.0.maint
Commit: 84a7c5d27fde9da0fc99cf8d13c65e4ec32e1041
Parents: 80207df
Author: Dmytro Sen <d...@apache.org>
Authored: Tue Apr 14 12:32:33 2015 +0300
Committer: Yusaku Sako <yus...@hortonworks.com>
Committed: Fri May 1 17:29:07 2015 -0700

----------------------------------------------------------------------
 .../ambari/server/controller/AmbariServer.java  |  28 +--
 .../server/controller/ControllerModule.java     |   4 +-
 .../controller/utilities/DatabaseChecker.java   | 182 +++++++++++++++++++
 .../server/state/ServiceComponentImpl.java      |  16 +-
 .../server/controller/AmbariServerTest.java     |  47 -----
 .../utilities/DatabaseCheckerTest.java          |  91 ++++++++++
 6 files changed, 291 insertions(+), 77 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/84a7c5d2/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariServer.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariServer.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariServer.java
index 4e684d3..24dc23d 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariServer.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariServer.java
@@ -57,6 +57,7 @@ import 
org.apache.ambari.server.controller.internal.StackDefinedPropertyProvider
 import 
org.apache.ambari.server.controller.internal.StackDependencyResourceProvider;
 import 
org.apache.ambari.server.controller.internal.UserPrivilegeResourceProvider;
 import 
org.apache.ambari.server.controller.internal.ViewPermissionResourceProvider;
+import org.apache.ambari.server.controller.utilities.DatabaseChecker;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.PersistenceType;
 import org.apache.ambari.server.orm.dao.BlueprintDAO;
@@ -87,7 +88,6 @@ import 
org.apache.ambari.server.security.unsecured.rest.ConnectionInfo;
 import org.apache.ambari.server.state.Clusters;
 import org.apache.ambari.server.state.ConfigHelper;
 import org.apache.ambari.server.utils.StageUtils;
-import org.apache.ambari.server.utils.VersionUtils;
 import org.apache.ambari.server.view.ViewRegistry;
 import org.apache.velocity.app.Velocity;
 import org.eclipse.jetty.server.Connector;
@@ -213,7 +213,8 @@ public class AmbariServer {
     server.setSessionIdManager(sessionIdManager);
     Server serverForAgent = new Server();
 
-    checkDBVersion();
+    DatabaseChecker.checkDBVersion();
+    DatabaseChecker.checkDBConsistency();
 
     try {
       ClassPathXmlApplicationContext parentSpringAppContext =
@@ -572,29 +573,6 @@ public class AmbariServer {
     }
   }
 
-  protected void checkDBVersion() throws AmbariException {
-    LOG.info("Checking DB store version");
-    MetainfoEntity schemaVersionEntity = 
metainfoDAO.findByKey(Configuration.SERVER_VERSION_KEY);
-    String schemaVersion = null;
-    String serverVersion = null;
-
-    if (schemaVersionEntity != null) {
-      schemaVersion = schemaVersionEntity.getMetainfoValue();
-      serverVersion = ambariMetaInfo.getServerVersion();
-    }
-
-    if (schemaVersionEntity==null || 
VersionUtils.compareVersions(schemaVersion, serverVersion, 3) != 0) {
-      String error = "Current database store version is not compatible with " +
-          "current server version"
-          + ", serverVersion=" + serverVersion
-          + ", schemaVersion=" + schemaVersion;
-      LOG.warn(error);
-      throw new AmbariException(error);
-    }
-
-    LOG.info("DB store version is compatible");
-  }
-
   public void stop() throws Exception {
     try {
       server.stop();

http://git-wip-us.apache.org/repos/asf/ambari/blob/84a7c5d2/ambari-server/src/main/java/org/apache/ambari/server/controller/ControllerModule.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/controller/ControllerModule.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/controller/ControllerModule.java
index 9c8bcd7..8afa0bb 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/controller/ControllerModule.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/controller/ControllerModule.java
@@ -65,6 +65,7 @@ import 
org.apache.ambari.server.controller.internal.MemberResourceProvider;
 import 
org.apache.ambari.server.controller.internal.RepositoryVersionResourceProvider;
 import org.apache.ambari.server.controller.internal.ServiceResourceProvider;
 import org.apache.ambari.server.controller.spi.ResourceProvider;
+import org.apache.ambari.server.controller.utilities.DatabaseChecker;
 import org.apache.ambari.server.orm.DBAccessor;
 import org.apache.ambari.server.orm.DBAccessorImpl;
 import org.apache.ambari.server.orm.PersistenceType;
@@ -314,6 +315,7 @@ public class ControllerModule extends AbstractModule {
     bind(ViewInstanceHandlerList.class).to(AmbariHandlerList.class);
 
     requestStaticInjection(ExecutionCommandWrapper.class);
+    requestStaticInjection(DatabaseChecker.class);
 
     bindByAnnotation(null);
   }
@@ -488,4 +490,4 @@ public class ControllerModule extends AbstractModule {
 
     return beanDefinitions;
   }
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/84a7c5d2/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/DatabaseChecker.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/DatabaseChecker.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/DatabaseChecker.java
new file mode 100644
index 0000000..3bc4fa0
--- /dev/null
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/DatabaseChecker.java
@@ -0,0 +1,182 @@
+/**
+ * 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.ambari.server.controller.utilities;
+
+import com.google.gson.Gson;
+import com.google.inject.Inject;
+import com.google.inject.Injector;
+import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.api.services.AmbariMetaInfo;
+import org.apache.ambari.server.configuration.Configuration;
+import org.apache.ambari.server.orm.dao.ClusterDAO;
+import org.apache.ambari.server.orm.dao.MetainfoDAO;
+import org.apache.ambari.server.orm.entities.ClusterEntity;
+import org.apache.ambari.server.orm.entities.ClusterServiceEntity;
+import org.apache.ambari.server.orm.entities.HostComponentDesiredStateEntity;
+import org.apache.ambari.server.orm.entities.HostComponentStateEntity;
+import org.apache.ambari.server.orm.entities.MetainfoEntity;
+import 
org.apache.ambari.server.orm.entities.ServiceComponentDesiredStateEntity;
+import org.apache.ambari.server.orm.entities.ServiceDesiredStateEntity;
+import org.apache.ambari.server.state.ComponentInfo;
+import org.apache.ambari.server.state.StackId;
+import org.apache.ambari.server.utils.VersionUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Collection;
+import java.util.List;
+
+public class DatabaseChecker {
+
+  static Logger LOG = LoggerFactory.getLogger(DatabaseChecker.class);
+
+  @Inject
+  static Injector injector;
+  static AmbariMetaInfo ambariMetaInfo;
+  static MetainfoDAO metainfoDAO;
+
+  public static void checkDBConsistency() throws AmbariException {
+    LOG.info("Checking DB consistency");
+
+    boolean checkPassed = true;
+    if (ambariMetaInfo == null) {
+      ambariMetaInfo = injector.getInstance(AmbariMetaInfo.class);
+    }
+
+    ClusterDAO clusterDAO = injector.getInstance(ClusterDAO.class);
+    List<ClusterEntity> clusters = clusterDAO.findAll();
+    for (ClusterEntity clusterEntity: clusters) {
+      String desiredStackVersion = clusterEntity.getDesiredStackVersion();
+      StackId stackId = new Gson().fromJson(desiredStackVersion, 
StackId.class);
+
+      Collection<ClusterServiceEntity> serviceEntities =
+        clusterEntity.getClusterServiceEntities();
+      for (ClusterServiceEntity clusterServiceEntity : serviceEntities) {
+
+        ServiceDesiredStateEntity serviceDesiredStateEntity =
+          clusterServiceEntity.getServiceDesiredStateEntity();
+        if (serviceDesiredStateEntity == null) {
+          checkPassed = false;
+          LOG.error(String.format("ServiceDesiredStateEntity is null for " +
+              "ServiceComponentDesiredStateEntity, clusterName=%s, 
serviceName=%s ",
+            clusterEntity.getClusterName(), 
clusterServiceEntity.getServiceName()));
+        }
+        Collection<ServiceComponentDesiredStateEntity> scDesiredStateEntities =
+          clusterServiceEntity.getServiceComponentDesiredStateEntities();
+        if (scDesiredStateEntities == null ||
+          scDesiredStateEntities.isEmpty()) {
+          checkPassed = false;
+          LOG.error(String.format("serviceComponentDesiredStateEntities is 
null or empty for " +
+              "ServiceComponentDesiredStateEntity, clusterName=%s, 
serviceName=%s ",
+            clusterEntity.getClusterName(), 
clusterServiceEntity.getServiceName()));
+        } else {
+          for (ServiceComponentDesiredStateEntity scDesiredStateEnity : 
scDesiredStateEntities) {
+
+            Collection<HostComponentDesiredStateEntity> 
schDesiredStateEntities =
+              scDesiredStateEnity.getHostComponentDesiredStateEntities();
+            Collection<HostComponentStateEntity> schStateEntities =
+              scDesiredStateEnity.getHostComponentStateEntities();
+
+            ComponentInfo componentInfo = ambariMetaInfo.getComponent(
+              stackId.getStackName(), stackId.getStackVersion(),
+              scDesiredStateEnity.getServiceName(), 
scDesiredStateEnity.getComponentName());
+
+            boolean zeroCardinality = componentInfo.getCardinality() == null
+              || componentInfo.getCardinality().startsWith("0")
+              || 
scDesiredStateEnity.getComponentName().equals("SECONDARY_NAMENODE"); // 
cardinality 0 for NameNode HA
+
+            boolean componentCheckFailed = false;
+
+            if (schDesiredStateEntities == null) {
+              componentCheckFailed = true;
+              LOG.error(String.format("hostComponentDesiredStateEntities is 
null for " +
+                  "ServiceComponentDesiredStateEntity, clusterName=%s, 
serviceName=%s, componentName=%s ",
+                clusterEntity.getClusterName(), 
scDesiredStateEnity.getServiceName(), scDesiredStateEnity.getComponentName()));
+            } else if (!zeroCardinality && schDesiredStateEntities.isEmpty()) {
+              componentCheckFailed = true;
+              LOG.error(String.format("hostComponentDesiredStateEntities is 
empty for " +
+                  "ServiceComponentDesiredStateEntity, clusterName=%s, 
serviceName=%s, componentName=%s ",
+                clusterEntity.getClusterName(), 
scDesiredStateEnity.getServiceName(), scDesiredStateEnity.getComponentName()));
+            }
+
+            if (schStateEntities == null) {
+              componentCheckFailed = true;
+              LOG.error(String.format("hostComponentStateEntities is null for 
" +
+                  "ServiceComponentDesiredStateEntity, clusterName=%s, 
serviceName=%s, componentName=%s ",
+                clusterEntity.getClusterName(), 
scDesiredStateEnity.getServiceName(), scDesiredStateEnity.getComponentName()));
+            } else if (!zeroCardinality && schStateEntities.isEmpty()) {
+              componentCheckFailed = true;
+              LOG.error(String.format("hostComponentStateEntities is empty for 
" +
+                  "ServiceComponentDesiredStateEntity, clusterName=%s, 
serviceName=%s, componentName=%s ",
+                clusterEntity.getClusterName(), 
scDesiredStateEnity.getServiceName(), scDesiredStateEnity.getComponentName()));
+            }
+
+            if (!componentCheckFailed &&
+              schDesiredStateEntities.size() != schStateEntities.size()) {
+              checkPassed = false;
+              LOG.error(String.format("HostComponentStateEntities and 
HostComponentDesiredStateEntities " +
+                  "tables must contain equal number of rows mapped to 
ServiceComponentDesiredStateEntity, " +
+                  "(clusterName=%s, serviceName=%s, componentName=%s) ", 
clusterEntity.getClusterName(),
+                scDesiredStateEnity.getServiceName(), 
scDesiredStateEnity.getComponentName()));
+            }
+            checkPassed = checkPassed && !componentCheckFailed;
+          }
+        }
+      }
+    }
+    if (checkPassed) {
+      LOG.info("DB consistency check passed.");
+    } else {
+      LOG.error("DB consistency check failed.");
+    }
+  }
+
+  public static void checkDBVersion() throws AmbariException {
+
+    LOG.info("Checking DB store version");
+    if (ambariMetaInfo == null) {
+      ambariMetaInfo = injector.getInstance(AmbariMetaInfo.class);
+    }
+    if (metainfoDAO == null) {
+      metainfoDAO = injector.getInstance(MetainfoDAO.class);
+    }
+
+    MetainfoEntity schemaVersionEntity = 
metainfoDAO.findByKey(Configuration.SERVER_VERSION_KEY);
+    String schemaVersion = null;
+    String serverVersion = null;
+
+    if (schemaVersionEntity != null) {
+      schemaVersion = schemaVersionEntity.getMetainfoValue();
+      serverVersion = ambariMetaInfo.getServerVersion();
+    }
+
+    if (schemaVersionEntity==null || 
VersionUtils.compareVersions(schemaVersion, serverVersion, 3) != 0) {
+      String error = "Current database store version is not compatible with " +
+        "current server version"
+        + ", serverVersion=" + serverVersion
+        + ", schemaVersion=" + schemaVersion;
+      LOG.warn(error);
+      throw new AmbariException(error);
+    }
+
+    LOG.info("DB store version is compatible");
+  }
+
+
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/84a7c5d2/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceComponentImpl.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceComponentImpl.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceComponentImpl.java
index 1bd2814..547011f 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceComponentImpl.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceComponentImpl.java
@@ -24,6 +24,7 @@ import java.util.Map.Entry;
 import java.util.concurrent.locks.ReadWriteLock;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 
+import com.google.inject.ProvisionException;
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.ObjectNotFoundException;
 import org.apache.ambari.server.ServiceComponentHostNotFoundException;
@@ -123,10 +124,17 @@ public class ServiceComponentImpl implements 
ServiceComponent {
       pk.setHostName(hostComponentStateEntity.getHostName());
 
       HostComponentDesiredStateEntity hostComponentDesiredStateEntity = 
hostComponentDesiredStateDAO.findByPK(pk);
-
-      hostComponents.put(hostComponentStateEntity.getHostName(),
+      try {
+        hostComponents.put(hostComponentStateEntity.getHostName(),
           serviceComponentHostFactory.createExisting(this,
-              hostComponentStateEntity, hostComponentDesiredStateEntity));
+            hostComponentStateEntity, hostComponentDesiredStateEntity));
+      } catch(ProvisionException ex) {
+        StackId stackId = service.getCluster().getCurrentStackVersion();
+        LOG.error(String.format("Can not get host component info: 
stackName=%s, stackVersion=%s, serviceName=%s, componentName=%s, hostname=%s",
+          stackId.getStackName(), stackId.getStackVersion(),
+          
service.getName(),serviceComponentDesiredStateEntity.getComponentName(), 
hostComponentStateEntity.getHostName()));
+        ex.printStackTrace();
+      }
     }
 
     StackId stackId = service.getDesiredStackVersion();
@@ -665,4 +673,4 @@ public class ServiceComponentImpl implements 
ServiceComponent {
     return hostComponents.size();
   }
 
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/84a7c5d2/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariServerTest.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariServerTest.java
 
b/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariServerTest.java
index 3a0d3fd..484f398 100644
--- 
a/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariServerTest.java
+++ 
b/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariServerTest.java
@@ -18,22 +18,15 @@
 
 package org.apache.ambari.server.controller;
 
-import static org.easymock.EasyMock.createMock;
-import static org.easymock.EasyMock.expect;
 import static org.easymock.EasyMock.replay;
-import static org.junit.Assert.fail;
 
 import java.net.Authenticator;
 import java.net.InetAddress;
 import java.net.PasswordAuthentication;
 
 import org.apache.ambari.server.AmbariException;
-import org.apache.ambari.server.api.services.AmbariMetaInfo;
-import org.apache.ambari.server.configuration.Configuration;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
-import org.apache.ambari.server.orm.dao.MetainfoDAO;
-import org.apache.ambari.server.orm.entities.MetainfoEntity;
 import org.apache.velocity.app.Velocity;
 import org.easymock.EasyMock;
 import org.eclipse.jetty.servlet.ServletContextHandler;
@@ -45,7 +38,6 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.google.inject.Guice;
-import com.google.inject.Inject;
 import com.google.inject.Injector;
 
 public class AmbariServerTest {
@@ -53,8 +45,6 @@ public class AmbariServerTest {
   private static final Logger log = 
LoggerFactory.getLogger(AmbariServerTest.class);
   private Injector injector;
 
-  @Inject
-  private AmbariMetaInfo ambariMetaInfo;
 
   @Before
   public void setup() throws Exception {
@@ -68,43 +58,6 @@ public class AmbariServerTest {
   }
 
   @Test
-  public void testCheckDBVersion_Valid() throws Exception {
-    MetainfoDAO metainfoDAO =  createMock(MetainfoDAO.class);
-    MetainfoEntity metainfoEntity = new MetainfoEntity();
-    String serverVersion = ambariMetaInfo.getServerVersion();
-    metainfoEntity.setMetainfoName(Configuration.SERVER_VERSION_KEY);
-    metainfoEntity.setMetainfoValue(serverVersion);
-    expect(metainfoDAO.findByKey(Configuration.SERVER_VERSION_KEY)).
-            andReturn(metainfoEntity);
-    replay(metainfoDAO);
-    AmbariServer ambariServer = new AmbariServer();
-    ambariServer.metainfoDAO = metainfoDAO;
-    ambariServer.ambariMetaInfo = ambariMetaInfo;
-    ambariServer.checkDBVersion();
-  }
-
-  @Test
-  public void testCheckDBVersion_Invalid() throws Exception {
-    MetainfoDAO metainfoDAO =  createMock(MetainfoDAO.class);
-    MetainfoEntity metainfoEntity = new MetainfoEntity();
-    metainfoEntity.setMetainfoName(Configuration.SERVER_VERSION_KEY);
-    metainfoEntity.setMetainfoValue("0.0.0"); // Incompatible version
-    expect(metainfoDAO.findByKey(Configuration.SERVER_VERSION_KEY)).
-            andReturn(metainfoEntity);
-    replay(metainfoDAO);
-    AmbariServer ambariServer = new AmbariServer();
-    ambariServer.metainfoDAO = metainfoDAO;
-    ambariServer.ambariMetaInfo = ambariMetaInfo;
-
-    try {
-      ambariServer.checkDBVersion();
-      fail();
-    } catch(AmbariException e) {
-      // Expected
-    }
-  }
-
-  @Test
   public void testVelocityLogger() throws Exception {
     new AmbariServer();
     Assert.assertEquals(AmbariServer.VELOCITY_LOG_CATEGORY, 
Velocity.getProperty("runtime.log.logsystem.log4j.logger"));

http://git-wip-us.apache.org/repos/asf/ambari/blob/84a7c5d2/ambari-server/src/test/java/org/apache/ambari/server/controller/utilities/DatabaseCheckerTest.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/test/java/org/apache/ambari/server/controller/utilities/DatabaseCheckerTest.java
 
b/ambari-server/src/test/java/org/apache/ambari/server/controller/utilities/DatabaseCheckerTest.java
new file mode 100644
index 0000000..03d3661
--- /dev/null
+++ 
b/ambari-server/src/test/java/org/apache/ambari/server/controller/utilities/DatabaseCheckerTest.java
@@ -0,0 +1,91 @@
+/**
+ * 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.ambari.server.controller.utilities;
+
+import com.google.inject.Guice;
+import com.google.inject.Inject;
+import com.google.inject.Injector;
+import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.api.services.AmbariMetaInfo;
+import org.apache.ambari.server.configuration.Configuration;
+import org.apache.ambari.server.orm.GuiceJpaInitializer;
+import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
+import org.apache.ambari.server.orm.dao.MetainfoDAO;
+import org.apache.ambari.server.orm.entities.MetainfoEntity;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.junit.Assert.fail;
+
+public class DatabaseCheckerTest {
+  private Injector injector;
+
+  @Inject
+  private AmbariMetaInfo ambariMetaInfo;
+
+  @Before
+  public void setup() throws Exception {
+    injector = Guice.createInjector(new InMemoryDefaultTestModule());
+    injector.getInstance(GuiceJpaInitializer.class);
+    injector.injectMembers(this);
+  }
+
+  @After
+  public void teardown() throws AmbariException {
+  }
+
+  @Test
+  public void testCheckDBVersion_Valid() throws Exception {
+    MetainfoDAO metainfoDAO =  createMock(MetainfoDAO.class);
+    MetainfoEntity metainfoEntity = new MetainfoEntity();
+    String serverVersion = ambariMetaInfo.getServerVersion();
+    metainfoEntity.setMetainfoName(Configuration.SERVER_VERSION_KEY);
+    metainfoEntity.setMetainfoValue(serverVersion);
+    expect(metainfoDAO.findByKey(Configuration.SERVER_VERSION_KEY)).
+      andReturn(metainfoEntity);
+    replay(metainfoDAO);
+    DatabaseChecker.metainfoDAO = metainfoDAO;
+    DatabaseChecker.ambariMetaInfo = ambariMetaInfo;
+    DatabaseChecker.checkDBVersion();
+  }
+
+  @Test
+  public void testCheckDBVersion_Invalid() throws Exception {
+    MetainfoDAO metainfoDAO =  createMock(MetainfoDAO.class);
+    MetainfoEntity metainfoEntity = new MetainfoEntity();
+    metainfoEntity.setMetainfoName(Configuration.SERVER_VERSION_KEY);
+    metainfoEntity.setMetainfoValue("0.0.0"); // Incompatible version
+    expect(metainfoDAO.findByKey(Configuration.SERVER_VERSION_KEY)).
+      andReturn(metainfoEntity);
+    replay(metainfoDAO);
+    DatabaseChecker.metainfoDAO = metainfoDAO;
+    DatabaseChecker.ambariMetaInfo = ambariMetaInfo;
+
+    try {
+      DatabaseChecker.checkDBVersion();
+      fail();
+    } catch(AmbariException e) {
+      // Expected
+    }
+  }
+}

Reply via email to