Repository: ambari Updated Branches: refs/heads/trunk 988248ca0 -> 8ac658307
AMBARI-11393. Posting a Blueprint with an Invalid Stack version number throws incorrect error. (rnettleton) Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/8ac65830 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/8ac65830 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/8ac65830 Branch: refs/heads/trunk Commit: 8ac6583078e3c181455423910de353cb8d01525a Parents: 988248c Author: Bob Nettleton <[email protected]> Authored: Tue May 26 16:50:21 2015 -0400 Committer: Bob Nettleton <[email protected]> Committed: Tue May 26 16:50:48 2015 -0400 ---------------------------------------------------------------------- .../server/topology/BlueprintFactory.java | 40 ++++++++++++++++++-- .../server/topology/BlueprintFactoryTest.java | 20 ++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/8ac65830/ambari-server/src/main/java/org/apache/ambari/server/topology/BlueprintFactory.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/topology/BlueprintFactory.java b/ambari-server/src/main/java/org/apache/ambari/server/topology/BlueprintFactory.java index f02db81..210504d 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/topology/BlueprintFactory.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/topology/BlueprintFactory.java @@ -21,7 +21,9 @@ package org.apache.ambari.server.topology; import com.google.inject.Inject; import org.apache.ambari.server.AmbariException; +import org.apache.ambari.server.ObjectNotFoundException; import org.apache.ambari.server.StackAccessException; +import org.apache.ambari.server.controller.AmbariManagementController; import org.apache.ambari.server.controller.AmbariServer; import org.apache.ambari.server.controller.internal.Stack; import org.apache.ambari.server.controller.utilities.PropertyHelper; @@ -66,6 +68,16 @@ public class BlueprintFactory { private static BlueprintDAO blueprintDAO; private ConfigurationFactory configFactory = new ConfigurationFactory(); + private final StackFactory stackFactory; + + public BlueprintFactory() { + this(new DefaultStackFactory()); + } + + protected BlueprintFactory(StackFactory stackFactory) { + this.stackFactory = stackFactory; + } + public Blueprint getBlueprint(String blueprintName) throws NoSuchStackException { BlueprintEntity entity = blueprintDAO.findByName(blueprintName); //todo: just return null? @@ -95,14 +107,13 @@ public class BlueprintFactory { return new BlueprintImpl(name, hostGroups, stack, configuration); } - //todo: StackFactory protected Stack createStack(Map<String, Object> properties) throws NoSuchStackException { String stackName = String.valueOf(properties.get(STACK_NAME_PROPERTY_ID)); String stackVersion = String.valueOf(properties.get(STACK_VERSION_PROPERTY_ID)); try { //todo: don't pass in controller - return new Stack(stackName, stackVersion, AmbariServer.getController()); - } catch (StackAccessException e) { + return stackFactory.createStack(stackName, stackVersion, AmbariServer.getController()); + } catch (ObjectNotFoundException e) { throw new NoSuchStackException(stackName, stackVersion); } catch (AmbariException e) { //todo: @@ -196,4 +207,27 @@ public class BlueprintFactory { public static void init(BlueprintDAO dao) { blueprintDAO = dao; } + + /** + * Internal interface used to abstract out the process of creating the Stack object. + * + * This is used to simplify unit testing, since a new Factory can be provided to + * simulate various Stack or error conditions. + */ + interface StackFactory { + public Stack createStack(String stackName, String stackVersion, AmbariManagementController managementController) throws AmbariException; + } + + /** + * Default implementation of StackFactory. + * + * Calls the Stack constructor to create the Stack instance. + * + */ + private static class DefaultStackFactory implements StackFactory { + @Override + public Stack createStack(String stackName, String stackVersion, AmbariManagementController managementController) throws AmbariException { + return new Stack(stackName, stackVersion, managementController); + } + } } http://git-wip-us.apache.org/repos/asf/ambari/blob/8ac65830/ambari-server/src/test/java/org/apache/ambari/server/topology/BlueprintFactoryTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/topology/BlueprintFactoryTest.java b/ambari-server/src/test/java/org/apache/ambari/server/topology/BlueprintFactoryTest.java index cd465cf..31b8f5c 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/topology/BlueprintFactoryTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/topology/BlueprintFactoryTest.java @@ -18,6 +18,7 @@ package org.apache.ambari.server.topology; +import org.apache.ambari.server.ObjectNotFoundException; import org.apache.ambari.server.controller.internal.BlueprintResourceProvider; import org.apache.ambari.server.controller.internal.BlueprintResourceProviderTest; import org.apache.ambari.server.controller.internal.Stack; @@ -25,6 +26,7 @@ import org.apache.ambari.server.orm.dao.BlueprintDAO; import org.apache.ambari.server.orm.entities.BlueprintConfigEntity; import org.apache.ambari.server.orm.entities.BlueprintEntity; import org.apache.ambari.server.stack.NoSuchStackException; +import org.easymock.EasyMockSupport; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -161,6 +163,24 @@ public class BlueprintFactoryTest { verify(dao, entity, configEntity); } + @Test(expected=NoSuchStackException.class) + public void testCreateInvalidStack() throws Exception { + EasyMockSupport mockSupport = new EasyMockSupport(); + BlueprintFactory.StackFactory mockStackFactory = + mockSupport.createMock(BlueprintFactory.StackFactory.class); + + // setup mock to throw exception, to simulate invalid stack request + expect(mockStackFactory.createStack("null", "null", null)).andThrow(new ObjectNotFoundException("Invalid Stack")); + + mockSupport.replayAll(); + + BlueprintFactory factoryUnderTest = + new BlueprintFactory(mockStackFactory); + factoryUnderTest.createStack(new HashMap<String, Object>()); + + mockSupport.verifyAll(); + } + @Test(expected=IllegalArgumentException.class) public void testCreate_NoBlueprintName() throws Exception { Map<String, Object> props = BlueprintResourceProviderTest.getBlueprintTestProperties().iterator().next();
