ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adorosz...@apache.org
Subject [ambari] branch trunk updated: AMBARI-22771. Fix broken unit test
Date Mon, 15 Jan 2018 14:10:29 GMT
This is an automated email from the ASF dual-hosted git repository.

adoroszlai pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ambari.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 0e32150  AMBARI-22771. Fix broken unit test
0e32150 is described below

commit 0e321500a17d454e978e1e59c74453e02a22d3a3
Author: Doroszlai, Attila <adoroszlai@apache.org>
AuthorDate: Mon Jan 15 15:00:11 2018 +0100

    AMBARI-22771. Fix broken unit test
---
 .../controller/RootServiceResponseFactoryTest.java | 145 ++++++++++-----------
 1 file changed, 69 insertions(+), 76 deletions(-)

diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/RootServiceResponseFactoryTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/controller/RootServiceResponseFactoryTest.java
index e194115..2e16766 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/RootServiceResponseFactoryTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/RootServiceResponseFactoryTest.java
@@ -20,6 +20,8 @@ package org.apache.ambari.server.controller;
 
 import static junit.framework.Assert.assertEquals;
 import static junit.framework.Assert.assertTrue;
+import static org.apache.ambari.server.controller.RootComponent.AMBARI_SERVER;
+import static org.apache.ambari.server.controller.RootService.AMBARI;
 
 import java.sql.SQLException;
 import java.util.Collections;
@@ -29,6 +31,7 @@ import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.ObjectNotFoundException;
 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.junit.After;
@@ -41,13 +44,17 @@ import com.google.inject.Injector;
 
 public class RootServiceResponseFactoryTest {
 
+  private Injector injector;
+
   @Inject
   private RootServiceResponseFactory responseFactory;
-  private Injector injector;
 
   @Inject
   private AmbariMetaInfo ambariMetaInfo;
 
+  @Inject
+  private Configuration config;
+
   @Before
   public void setUp() throws Exception {
     injector = Guice.createInjector(new InMemoryDefaultTestModule());
@@ -61,116 +68,102 @@ public class RootServiceResponseFactoryTest {
   }
 
   @Test
-  public void testGetRootServices() throws Exception {
+  public void getReturnsAllServicesForNullServiceName() throws Exception {
     // Request a null service name
     RootServiceRequest request = new RootServiceRequest(null);
     Set<RootServiceResponse> rootServices = responseFactory.getRootServices(request);
-    assertEquals(RootService.values().length,
-        rootServices.size());
+    assertEquals(RootService.values().length, rootServices.size());
+  }
 
+  @Test
+  public void getReturnsAllServicesForNullRequest() throws Exception {
     // null request
-    request = null;
-    rootServices = responseFactory.getRootServices(request);
-    assertEquals(RootService.values().length,
-        rootServices.size());
+    Set<RootServiceResponse> rootServices = responseFactory.getRootServices(null);
+    assertEquals(RootService.values().length, rootServices.size());
+  }
 
+  @Test(expected = ObjectNotFoundException.class)
+  public void getThrowsForNonExistentService() throws Exception {
     // Request nonexistent service
-    try {
-      request = new RootServiceRequest("XXX");
-      rootServices = responseFactory.getRootServices(request);
-    } catch (Exception e) {
-      assertTrue(e instanceof ObjectNotFoundException);
-    }
+    RootServiceRequest request = new RootServiceRequest("XXX");
+    responseFactory.getRootServices(request);
+  }
 
+  @Test
+  public void getReturnsSingleServiceForValidServiceName() throws Exception {
     // Request existent service
-    request = new RootServiceRequest(
-        RootService.AMBARI.name());
-
-    rootServices = responseFactory.getRootServices(request);
-    assertEquals(1, rootServices.size());
-    assertTrue(rootServices.contains(new RootServiceResponse(
-        RootService.AMBARI.name())));
+    RootServiceRequest request = new RootServiceRequest(AMBARI.name());
+    Set<RootServiceResponse> rootServices = responseFactory.getRootServices(request);
+    assertEquals(Collections.singleton(new RootServiceResponse(AMBARI.name())), rootServices);
   }
 
-  @Test
-  public void testGetRootServiceComponents() throws Exception {
+  @Test(expected = ObjectNotFoundException.class)
+  public void getThrowsForNullServiceNameNullComponentName() throws Exception {
     // Request null service name, null component name
-    RootServiceComponentRequest request = new RootServiceComponentRequest(null,
-        null);
-
-    Set<RootServiceComponentResponse> rootServiceComponents;
-    try {
-      rootServiceComponents = responseFactory.getRootServiceComponents(request);
-    } catch (Exception e) {
-      assertTrue(e instanceof ObjectNotFoundException);
-    }
+    RootServiceComponentRequest request = new RootServiceComponentRequest(null, null);
 
-    RootComponent ambariServerComponent = RootComponent.AMBARI_SERVER;
+    responseFactory.getRootServiceComponents(request);
+  }
 
+  @Test(expected = ObjectNotFoundException.class)
+  public void getThrowsForNullServiceNameValidComponentName() throws Exception {
     // Request null service name, not-null component name
-    request = new RootServiceComponentRequest(null, ambariServerComponent.name());
+    RootServiceComponentRequest request = new RootServiceComponentRequest(null, AMBARI_SERVER.name());
 
-    try {
-      rootServiceComponents = responseFactory.getRootServiceComponents(request);
-    } catch (Exception e) {
-      assertTrue(e instanceof ObjectNotFoundException);
-    }
+    responseFactory.getRootServiceComponents(request);
+  }
 
+  @Test
+  public void getReturnsAllComponentsForValidServiceNameNullComponentName() throws Exception
{
     // Request existent service name, null component name
-    String serviceName = RootService.AMBARI.name();
-    request = new RootServiceComponentRequest(serviceName, null);
-
-    rootServiceComponents = responseFactory.getRootServiceComponents(request);
-    assertEquals(
-        RootService.AMBARI.getComponents().length,
-        rootServiceComponents.size());
+    RootServiceComponentRequest request = new RootServiceComponentRequest(AMBARI.name(),
null);
 
-    String ambariVersion = ambariMetaInfo.getServerVersion();
+    Set<RootServiceComponentResponse> rootServiceComponents = responseFactory.getRootServiceComponents(request);
+    assertEquals(AMBARI.getComponents().length, rootServiceComponents.size());
 
-    for (int i = 0; i < RootService.AMBARI.getComponents().length; i++) {
-      RootComponent component = RootService.AMBARI.getComponents()[i];
+    for (int i = 0; i < AMBARI.getComponents().length; i++) {
+      RootComponent component = AMBARI.getComponents()[i];
 
-      if (component.name().equals(ambariServerComponent.name())) {
+      if (component.name().equals(AMBARI_SERVER.name())) {
         for (RootServiceComponentResponse response : rootServiceComponents) {
-          if (response.getComponentName().equals(ambariServerComponent.name())) {
-            assertEquals(ambariVersion, response.getComponentVersion());
-            assertEquals(1, response.getProperties().size(), 1);
-            assertTrue(response.getProperties().containsKey("jdk_location"));
+          if (response.getComponentName().equals(AMBARI_SERVER.name())) {
+            verifyResponseForAmbariServer(response);
           }
         }
       } else {
         assertTrue(rootServiceComponents.contains(new RootServiceComponentResponse(
-            serviceName, component.name(), RootServiceResponseFactory.NOT_APPLICABLE,
+            AMBARI.name(), component.name(), RootServiceResponseFactory.NOT_APPLICABLE,
             Collections.emptyMap())));
       }
     }
+  }
 
+  @Test
+  public void getReturnsSingleComponentForValidServiceAndComponentName() throws Exception
{
     // Request existent service name, existent component name
-    request = new RootServiceComponentRequest(
-        RootService.AMBARI.name(),
-        RootService.AMBARI.getComponents()[0].name());
+    RootServiceComponentRequest request = new RootServiceComponentRequest(AMBARI.name(),
AMBARI_SERVER.name());
+
+    Set<RootServiceComponentResponse> rootServiceComponents = responseFactory.getRootServiceComponents(request);
 
-    rootServiceComponents = responseFactory.getRootServiceComponents(request);
     assertEquals(1, rootServiceComponents.size());
     for (RootServiceComponentResponse response : rootServiceComponents) {
-      if (response.getComponentName().equals(
-          RootService.AMBARI.getComponents()[0].name())) {
-        assertEquals(ambariVersion, response.getComponentVersion());
-        assertEquals(2, response.getProperties().size());
-        assertTrue(response.getProperties().containsKey("jdk_location"));
-        assertTrue(response.getProperties().containsKey("java.version"));
-      }
+      verifyResponseForAmbariServer(response);
     }
+  }
 
-    // Request existent service name, and component, not belongs to requested
-    // service
-    request = new RootServiceComponentRequest(
-        RootService.AMBARI.name(), "XXX");
-    
-    try {
-      rootServiceComponents = responseFactory.getRootServiceComponents(request);
-    } catch (Exception e) {
-      assertTrue(e instanceof ObjectNotFoundException);
-    }
+  @Test(expected = ObjectNotFoundException.class)
+  public void getThrowsForNonexistentComponent() throws Exception {
+    // Request existent service name, and component, not belongs to requested service
+    RootServiceComponentRequest request = new RootServiceComponentRequest(AMBARI.name(),
"XXX");
+    responseFactory.getRootServiceComponents(request);
+  }
+
+  private void verifyResponseForAmbariServer(RootServiceComponentResponse response) {
+    assertEquals(ambariMetaInfo.getServerVersion(), response.getComponentVersion());
+    // all properties from config + "jdk_location" + "java.version"
+    int expectedPropertyCount = config.getAmbariProperties().size() + 2;
+    assertEquals(response.getProperties().toString(), expectedPropertyCount, response.getProperties().size());
+    assertTrue(response.getProperties().containsKey("jdk_location"));
+    assertTrue(response.getProperties().containsKey("java.version"));
   }
 }

-- 
To stop receiving notification emails like this one, please contact
['"commits@ambari.apache.org" <commits@ambari.apache.org>'].

Mime
View raw message