This is an automated email from the ASF dual-hosted git repository.

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


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

commit 15634ceee6dfd96f82699eb0c7a0230005a2e69f
Author: Doroszlai, Attila <[email protected]>
AuthorDate: Mon Jan 15 14:01:49 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 61a5ed6..cec3667 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.RootServiceResponseFactory.Components.AMBARI_SERVER;
+import static 
org.apache.ambari.server.controller.RootServiceResponseFactory.Services.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.controller.RootServiceResponseFactory.Components;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
@@ -42,13 +45,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());
@@ -62,116 +69,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(RootServiceResponseFactory.Services.values().length,
-        rootServices.size());
+    assertEquals(RootServiceResponseFactory.Services.values().length, 
rootServices.size());
+  }
 
+  @Test
+  public void getReturnsAllServicesForNullRequest() throws Exception {
     // null request
-    request = null;
-    rootServices = responseFactory.getRootServices(request);
-    assertEquals(RootServiceResponseFactory.Services.values().length,
-        rootServices.size());
+    Set<RootServiceResponse> rootServices = 
responseFactory.getRootServices(null);
+    assertEquals(RootServiceResponseFactory.Services.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(
-        RootServiceResponseFactory.Services.AMBARI.name());
-
-    rootServices = responseFactory.getRootServices(request);
-    assertEquals(1, rootServices.size());
-    assertTrue(rootServices.contains(new RootServiceResponse(
-        RootServiceResponseFactory.Services.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);
 
-    RootServiceResponseFactory.Components ambariServerComponent = 
RootServiceResponseFactory.Components.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 = RootServiceResponseFactory.Services.AMBARI.name();
-    request = new RootServiceComponentRequest(serviceName, null);
-
-    rootServiceComponents = responseFactory.getRootServiceComponents(request);
-    assertEquals(
-        RootServiceResponseFactory.Services.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 < 
RootServiceResponseFactory.Services.AMBARI.getComponents().length; i++) {
-      Components component = 
RootServiceResponseFactory.Services.AMBARI.getComponents()[i];
+    for (int i = 0; i < AMBARI.getComponents().length; i++) {
+      Components 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.<String, String> emptyMap())));
       }
     }
+  }
 
+  @Test
+  public void getReturnsSingleComponentForValidServiceAndComponentName() 
throws Exception {
     // Request existent service name, existent component name
-    request = new RootServiceComponentRequest(
-        RootServiceResponseFactory.Services.AMBARI.name(),
-        RootServiceResponseFactory.Services.AMBARI.getComponents()[0].name());
+    RootServiceComponentRequest request = new 
RootServiceComponentRequest(AMBARI.name(), Components.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(
-          
RootServiceResponseFactory.Services.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(
-        RootServiceResponseFactory.Services.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
['"[email protected]" <[email protected]>'].

Reply via email to