BlueprintResourceProvider extends AbstractControllerResourceProvider { @Override public RequestStatus createResources(Request request) throws SystemException, UnsupportedPropertyException, ResourceAlreadyExistsException, NoSuchParentResourceException { for (Map<String, Object> properties : request.getProperties()) { try { createResources(getCreateCommand(properties, request.getRequestInfoProperties())); }catch(IllegalArgumentException e) { LOG.error("Exception while creating blueprint", e); throw e; } } notifyCreate(Resource.Type.Blueprint, request); return getRequestStatus(null); } BlueprintResourceProvider(Set<String> propertyIds,
                            Map<Resource.Type, String> keyPropertyIds,
                            AmbariManagementController controller); static void init(BlueprintFactory factory, BlueprintDAO dao, SecurityConfigurationFactory
    securityFactory, Gson gson, AmbariMetaInfo metaInfo); @Override RequestStatus createResources(Request request); @Override //todo: continue to use dao/entity directly or use blueprint factory? Set<Resource> getResources(Request request, Predicate predicate); @Override RequestStatus updateResources(Request request, Predicate predicate); @Override RequestStatus deleteResources(Request request, Predicate predicate); static List<Map<String, Object>> populateSettingList(
          Collection<? extends BlueprintSettingEntity> settings); static final String BLUEPRINT_NAME_PROPERTY_ID; static final String STACK_NAME_PROPERTY_ID; static final String STACK_VERSION_PROPERTY_ID; static final String BLUEPRINT_SECURITY_PROPERTY_ID; static final String BLUEPRINTS_PROPERTY_ID; static final String HOST_GROUP_PROPERTY_ID; static final String HOST_GROUP_NAME_PROPERTY_ID; static final String HOST_GROUP_CARDINALITY_PROPERTY_ID; static final String COMPONENT_PROPERTY_ID; static final String COMPONENT_NAME_PROPERTY_ID; static final String COMPONENT_PROVISION_ACTION_PROPERTY_ID; static final String CONFIGURATION_PROPERTY_ID; static final String SETTING_PROPERTY_ID; static final String PROPERTIES_PROPERTY_ID; static final String PROPERTIES_ATTRIBUTES_PROPERTY_ID; static final String SCHEMA_IS_NOT_SUPPORTED_MESSAGE; static final String REQUEST_BODY_EMPTY_ERROR_MESSAGE; static final String CONFIGURATION_LIST_CHECK_ERROR_MESSAGE; static final String CONFIGURATION_MAP_CHECK_ERROR_MESSAGE; static final String CONFIGURATION_MAP_SIZE_CHECK_ERROR_MESSAGE; }
@Test public void testCreateResources() throws Exception { AmbariManagementController managementController = createMock(AmbariManagementController.class); Request request = createMock(Request.class); Setting setting = createStrictMock(Setting.class); Set<Map<String, Object>> setProperties = getBlueprintTestProperties(); Map<String, String> requestInfoProperties = getTestRequestInfoProperties(); Map<String, Set<HashMap<String, String>>> settingProperties = getSettingProperties(); expect(blueprintFactory.createBlueprint(setProperties.iterator().next(), null)).andReturn(blueprint).once(); expect(securityFactory.createSecurityConfigurationFromRequest(null, true)).andReturn(null).anyTimes(); blueprint.validateRequiredProperties(); blueprint.validateTopology(); expect(blueprint.getSetting()).andReturn(setting).anyTimes(); expect(setting.getProperties()).andReturn(settingProperties).anyTimes(); expect(blueprint.toEntity()).andReturn(entity); expect(blueprint.getName()).andReturn(BLUEPRINT_NAME).atLeastOnce(); expect(request.getProperties()).andReturn(setProperties); expect(request.getRequestInfoProperties()).andReturn(requestInfoProperties); expect(dao.findByName(BLUEPRINT_NAME)).andReturn(null); dao.create(entity); replay(dao, entity, metaInfo, blueprintFactory, securityFactory, blueprint, setting, request, managementController); ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider( Resource.Type.Blueprint, PropertyHelper.getPropertyIds(Resource.Type.Blueprint), PropertyHelper.getKeyPropertyIds(Resource.Type.Blueprint), managementController); AbstractResourceProviderTest.TestObserver observer = new AbstractResourceProviderTest.TestObserver(); ((ObservableResourceProvider)provider).addObserver(observer); provider.createResources(request); ResourceProviderEvent lastEvent = observer.getLastEvent(); assertNotNull(lastEvent); assertEquals(Resource.Type.Blueprint, lastEvent.getResourceType()); assertEquals(ResourceProviderEvent.Type.Create, lastEvent.getType()); assertEquals(request, lastEvent.getRequest()); assertNull(lastEvent.getPredicate()); verify(dao, entity, blueprintFactory, securityFactory, metaInfo, request, managementController); }
@Test() public void testCreateResources_ReqestBodyIsEmpty() throws Exception { AmbariManagementController managementController = createMock(AmbariManagementController.class); Request request = createMock(Request.class); Set<Map<String, Object>> setProperties = getBlueprintTestProperties(); Map<String, String> requestInfoProperties = new HashMap<String, String>(); requestInfoProperties.put(Request.REQUEST_INFO_BODY_PROPERTY, null); expect(request.getProperties()).andReturn(setProperties); expect(request.getRequestInfoProperties()).andReturn(requestInfoProperties); replay(request, managementController); ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider( Resource.Type.Blueprint, PropertyHelper.getPropertyIds(Resource.Type.Blueprint), PropertyHelper.getKeyPropertyIds(Resource.Type.Blueprint), managementController); try { provider.createResources(request); fail("Exception expected"); } catch (IllegalArgumentException e) { assertEquals(BlueprintResourceProvider.REQUEST_BODY_EMPTY_ERROR_MESSAGE, e.getMessage()); } verify(request, managementController); }
@Test public void testCreateResources_NoValidation() throws Exception { AmbariManagementController managementController = createMock(AmbariManagementController.class); Request request = createMock(Request.class); Setting setting = createStrictMock(Setting.class); Map<String, Set<HashMap<String, String>>> settingProperties = getSettingProperties(); Set<Map<String, Object>> setProperties = getBlueprintTestProperties(); Map<String, String> requestInfoProperties = getTestRequestInfoProperties(); requestInfoProperties.put("validate_topology", "false"); expect(blueprintFactory.createBlueprint(setProperties.iterator().next(), null)).andReturn(blueprint).once(); blueprint.validateRequiredProperties(); expect(blueprint.getSetting()).andReturn(setting).anyTimes(); expect(setting.getProperties()).andReturn(settingProperties).anyTimes(); expect(blueprint.toEntity()).andReturn(entity); expect(blueprint.getName()).andReturn(BLUEPRINT_NAME).atLeastOnce(); expect(request.getProperties()).andReturn(setProperties); expect(request.getRequestInfoProperties()).andReturn(requestInfoProperties); expect(dao.findByName(BLUEPRINT_NAME)).andReturn(null); dao.create(entity); replay(dao, entity, metaInfo, blueprintFactory, blueprint, setting, request, managementController); ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider( Resource.Type.Blueprint, PropertyHelper.getPropertyIds(Resource.Type.Blueprint), PropertyHelper.getKeyPropertyIds(Resource.Type.Blueprint), managementController); AbstractResourceProviderTest.TestObserver observer = new AbstractResourceProviderTest.TestObserver(); ((ObservableResourceProvider)provider).addObserver(observer); provider.createResources(request); ResourceProviderEvent lastEvent = observer.getLastEvent(); assertNotNull(lastEvent); assertEquals(Resource.Type.Blueprint, lastEvent.getResourceType()); assertEquals(ResourceProviderEvent.Type.Create, lastEvent.getType()); assertEquals(request, lastEvent.getRequest()); assertNull(lastEvent.getPredicate()); verify(dao, entity, blueprintFactory, metaInfo, request, managementController); }
@Test public void testCreateResources_TopologyValidationFails() throws Exception { Request request = createMock(Request.class); Set<Map<String, Object>> setProperties = getBlueprintTestProperties(); Map<String, String> requestInfoProperties = getTestRequestInfoProperties(); expect(blueprintFactory.createBlueprint(setProperties.iterator().next(), null)).andReturn(blueprint).once(); blueprint.validateRequiredProperties(); expect(blueprint.getName()).andReturn(BLUEPRINT_NAME).atLeastOnce(); blueprint.validateTopology(); expectLastCall().andThrow(new InvalidTopologyException("test")); expect(request.getProperties()).andReturn(setProperties); expect(request.getRequestInfoProperties()).andReturn(requestInfoProperties); expect(dao.findByName(BLUEPRINT_NAME)).andReturn(null); replay(dao, entity, metaInfo, blueprintFactory, blueprint, request); ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider( Resource.Type.Blueprint, PropertyHelper.getPropertyIds(Resource.Type.Blueprint), PropertyHelper.getKeyPropertyIds(Resource.Type.Blueprint), createMock(AmbariManagementController.class)); AbstractResourceProviderTest.TestObserver observer = new AbstractResourceProviderTest.TestObserver(); ((ObservableResourceProvider)provider).addObserver(observer); try { provider.createResources(request); fail("Expected exception due to topology validation error"); } catch (IllegalArgumentException e) { } verify(dao, entity, blueprintFactory, metaInfo, request); }
@Test public void testCreateResources_withConfiguration() throws Exception { Set<Map<String, Object>> setProperties = getBlueprintTestProperties(); setConfigurationProperties(setProperties); AmbariManagementController managementController = createMock(AmbariManagementController.class); Map<String, String> requestInfoProperties = getTestRequestInfoProperties(); Request request = createMock(Request.class); Setting setting = createStrictMock(Setting.class); Map<String, Set<HashMap<String, String>>> settingProperties = getSettingProperties(); expect(blueprintFactory.createBlueprint(setProperties.iterator().next(), null)).andReturn(blueprint).once(); blueprint.validateRequiredProperties(); blueprint.validateTopology(); expect(blueprint.getSetting()).andReturn(setting).anyTimes(); expect(setting.getProperties()).andReturn(settingProperties).anyTimes(); expect(blueprint.toEntity()).andReturn(entity); expect(blueprint.getName()).andReturn(BLUEPRINT_NAME).atLeastOnce(); expect(request.getProperties()).andReturn(setProperties); expect(request.getRequestInfoProperties()).andReturn(requestInfoProperties); expect(dao.findByName(BLUEPRINT_NAME)).andReturn(null); dao.create(entity); replay(dao, entity, metaInfo, blueprintFactory, blueprint, setting, request, managementController); ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider( Resource.Type.Blueprint, PropertyHelper.getPropertyIds(Resource.Type.Blueprint), PropertyHelper.getKeyPropertyIds(Resource.Type.Blueprint), managementController); AbstractResourceProviderTest.TestObserver observer = new AbstractResourceProviderTest.TestObserver(); ((ObservableResourceProvider)provider).addObserver(observer); provider.createResources(request); ResourceProviderEvent lastEvent = observer.getLastEvent(); assertNotNull(lastEvent); assertEquals(Resource.Type.Blueprint, lastEvent.getResourceType()); assertEquals(ResourceProviderEvent.Type.Create, lastEvent.getType()); assertEquals(request, lastEvent.getRequest()); assertNull(lastEvent.getPredicate()); verify(dao, entity, blueprintFactory, metaInfo, request, managementController); }
@Test public void testCreateResource_BlueprintFactoryThrowsException() throws Exception { Request request = createMock(Request.class); Set<Map<String, Object>> setProperties = getBlueprintTestProperties(); setProperties.iterator().next().remove(BlueprintResourceProvider.HOST_GROUP_PROPERTY_ID); Map<String, String> requestInfoProperties = getTestRequestInfoProperties(); expect(blueprintFactory.createBlueprint(setProperties.iterator().next(), null)).andThrow( new IllegalArgumentException("Blueprint name must be provided")); expect(securityFactory.createSecurityConfigurationFromRequest(null,true)).andReturn(null).anyTimes(); expect(request.getProperties()).andReturn(setProperties); expect(request.getRequestInfoProperties()).andReturn(requestInfoProperties); replay(dao, entity, metaInfo, blueprintFactory, securityFactory, blueprint, request); try { provider.createResources(request); fail("Exception expected"); } catch (IllegalArgumentException e) { } verify(dao, entity, blueprintFactory, metaInfo, request); }
@Test public void testCreateResources_withSecurityConfiguration() throws Exception { AmbariManagementController managementController = createMock(AmbariManagementController.class); Request request = createMock(Request.class); Setting setting = createStrictMock(Setting.class); Set<Map<String, Object>> setProperties = getBlueprintTestProperties(); Map<String, String> requestInfoProperties = getTestRequestInfoProperties(); Map<String, Set<HashMap<String, String>>> settingProperties = getSettingProperties(); SecurityConfiguration securityConfiguration = new SecurityConfiguration(SecurityType.KERBEROS, "testRef", null); expect(securityFactory.createSecurityConfigurationFromRequest(EasyMock.<Map<String, Object>>anyObject(), anyBoolean())).andReturn (securityConfiguration).once(); expect(blueprintFactory.createBlueprint(setProperties.iterator().next(), securityConfiguration)).andReturn(blueprint).once(); blueprint.validateRequiredProperties(); blueprint.validateTopology(); expect(blueprint.getSetting()).andReturn(setting).anyTimes(); expect(setting.getProperties()).andReturn(settingProperties).anyTimes(); expect(blueprint.toEntity()).andReturn(entity); expect(blueprint.getName()).andReturn(BLUEPRINT_NAME).atLeastOnce(); expect(request.getProperties()).andReturn(setProperties); expect(request.getRequestInfoProperties()).andReturn(requestInfoProperties); expect(dao.findByName(BLUEPRINT_NAME)).andReturn(null); dao.create(entity); replay(dao, entity, metaInfo, blueprintFactory, securityFactory, blueprint, setting, request, managementController); ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider( Resource.Type.Blueprint, PropertyHelper.getPropertyIds(Resource.Type.Blueprint), PropertyHelper.getKeyPropertyIds(Resource.Type.Blueprint), managementController); AbstractResourceProviderTest.TestObserver observer = new AbstractResourceProviderTest.TestObserver(); ((ObservableResourceProvider)provider).addObserver(observer); provider.createResources(request); ResourceProviderEvent lastEvent = observer.getLastEvent(); assertNotNull(lastEvent); assertEquals(Resource.Type.Blueprint, lastEvent.getResourceType()); assertEquals(ResourceProviderEvent.Type.Create, lastEvent.getType()); assertEquals(request, lastEvent.getRequest()); assertNull(lastEvent.getPredicate()); verify(dao, entity, blueprintFactory, metaInfo, request, managementController); }
@Test public void testCreateResources_withEmptyConfiguration() throws Exception { Set<Map<String, Object>> setProperties = getBlueprintTestProperties(); setConfigurationProperties(setProperties); AmbariManagementController managementController = createMock(AmbariManagementController.class); Map<String, String> requestInfoProperties = new HashMap<String, String>(); Map<String, Set<HashMap<String, String>>> settingProperties = getSettingProperties(); requestInfoProperties.put(Request.REQUEST_INFO_BODY_PROPERTY, "{\"configurations\":[]}"); Request request = createMock(Request.class); Setting setting = createStrictMock(Setting.class); expect(blueprintFactory.createBlueprint(setProperties.iterator().next(), null)).andReturn(blueprint).once(); blueprint.validateRequiredProperties(); blueprint.validateTopology(); expect(blueprint.getSetting()).andReturn(setting).anyTimes(); expect(setting.getProperties()).andReturn(settingProperties).anyTimes(); expect(blueprint.toEntity()).andReturn(entity); expect(blueprint.getName()).andReturn(BLUEPRINT_NAME).atLeastOnce(); expect(request.getProperties()).andReturn(setProperties); expect(request.getRequestInfoProperties()).andReturn(requestInfoProperties); expect(dao.findByName(BLUEPRINT_NAME)).andReturn(null); dao.create(entity); replay(dao, entity, metaInfo, blueprintFactory, blueprint, setting, request, managementController); ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider( Resource.Type.Blueprint, PropertyHelper.getPropertyIds(Resource.Type.Blueprint), PropertyHelper.getKeyPropertyIds(Resource.Type.Blueprint), managementController); AbstractResourceProviderTest.TestObserver observer = new AbstractResourceProviderTest.TestObserver(); ((ObservableResourceProvider)provider).addObserver(observer); provider.createResources(request); ResourceProviderEvent lastEvent = observer.getLastEvent(); assertNotNull(lastEvent); assertEquals(Resource.Type.Blueprint, lastEvent.getResourceType()); assertEquals(ResourceProviderEvent.Type.Create, lastEvent.getType()); assertEquals(request, lastEvent.getRequest()); assertNull(lastEvent.getPredicate()); verify(dao, entity, blueprintFactory, metaInfo, request, managementController); }
@Test public void testCreateResources_withSingleConfigurationType() throws Exception { Set<Map<String, Object>> setProperties = getBlueprintTestProperties(); setConfigurationProperties(setProperties); AmbariManagementController managementController = createMock(AmbariManagementController.class); Map<String, String> requestInfoProperties = new HashMap<String, String>(); Map<String, Set<HashMap<String, String>>> settingProperties = getSettingProperties(); requestInfoProperties.put(Request.REQUEST_INFO_BODY_PROPERTY, "{\"configurations\":[{\"configuration-type\":{\"properties\":{\"property\":\"value\"}}}]}"); Request request = createMock(Request.class); Setting setting = createStrictMock(Setting.class); expect(blueprintFactory.createBlueprint(setProperties.iterator().next(), null)).andReturn(blueprint).once(); blueprint.validateRequiredProperties(); blueprint.validateTopology(); expect(blueprint.getSetting()).andReturn(setting).anyTimes(); expect(setting.getProperties()).andReturn(settingProperties).anyTimes(); expect(blueprint.toEntity()).andReturn(entity); expect(blueprint.getName()).andReturn(BLUEPRINT_NAME).atLeastOnce(); expect(request.getProperties()).andReturn(setProperties); expect(request.getRequestInfoProperties()).andReturn(requestInfoProperties); expect(dao.findByName(BLUEPRINT_NAME)).andReturn(null); dao.create(entity); replay(dao, entity, metaInfo, blueprintFactory, blueprint, setting, request, managementController); ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider( Resource.Type.Blueprint, PropertyHelper.getPropertyIds(Resource.Type.Blueprint), PropertyHelper.getKeyPropertyIds(Resource.Type.Blueprint), managementController); AbstractResourceProviderTest.TestObserver observer = new AbstractResourceProviderTest.TestObserver(); ((ObservableResourceProvider)provider).addObserver(observer); provider.createResources(request); ResourceProviderEvent lastEvent = observer.getLastEvent(); assertNotNull(lastEvent); assertEquals(Resource.Type.Blueprint, lastEvent.getResourceType()); assertEquals(ResourceProviderEvent.Type.Create, lastEvent.getType()); assertEquals(request, lastEvent.getRequest()); assertNull(lastEvent.getPredicate()); verify(dao, entity, blueprintFactory, metaInfo, request, managementController); }
@Test public void testCreateResources_wrongConfigurationsStructure_withWrongConfigMapSize() throws ResourceAlreadyExistsException, SystemException, UnsupportedPropertyException, NoSuchParentResourceException { Request request = createMock(Request.class); Set<Map<String, Object>> setProperties = getBlueprintTestProperties(); Map<String, String> requestInfoProperties = new HashMap<String, String>(); String configurationData = "{\"configurations\":[{\"config-type1\":{\"properties\" :{\"property\":\"property-value\"}}," + "\"config-type2\" : {\"properties_attributes\" : {\"property\" : \"property-value\"}, \"properties\" : {\"property\" : \"property-value\"}}}" + "]}"; requestInfoProperties.put(Request.REQUEST_INFO_BODY_PROPERTY, configurationData); expect(request.getProperties()).andReturn(setProperties); expect(request.getRequestInfoProperties()).andReturn(requestInfoProperties); replay(dao, metaInfo, request); try { provider.createResources(request); fail("Exception expected"); } catch (IllegalArgumentException e) { assertEquals(BlueprintResourceProvider.CONFIGURATION_MAP_SIZE_CHECK_ERROR_MESSAGE, e.getMessage()); } verify(dao, metaInfo, request); }
@Test public void testCreateResources_wrongConfigurationStructure_withoutConfigMaps() throws ResourceAlreadyExistsException, SystemException, UnsupportedPropertyException, NoSuchParentResourceException { Request request = createMock(Request.class); Set<Map<String, Object>> setProperties = getBlueprintTestProperties(); Map<String, String> requestInfoProperties = new HashMap<String, String>(); String configurationData = "{\"configurations\":[\"config-type1\", \"config-type2\"]}"; requestInfoProperties.put(Request.REQUEST_INFO_BODY_PROPERTY, configurationData); expect(request.getProperties()).andReturn(setProperties); expect(request.getRequestInfoProperties()).andReturn(requestInfoProperties); replay(dao, metaInfo, request); try { provider.createResources(request); fail("Exception expected"); } catch (IllegalArgumentException e) { assertEquals(BlueprintResourceProvider.CONFIGURATION_MAP_CHECK_ERROR_MESSAGE, e.getMessage()); } verify(dao, metaInfo, request); }
@Test public void testCreateResources_wrongConfigurationStructure_withoutConfigsList() throws ResourceAlreadyExistsException, SystemException, UnsupportedPropertyException, NoSuchParentResourceException { Request request = createMock(Request.class); Set<Map<String, Object>> setProperties = getBlueprintTestProperties(); Map<String, String> requestInfoProperties = new HashMap<String, String>(); String configurationData = "{\"configurations\":{\"config-type1\": \"properties\", \"config-type2\": \"properties\"}}"; requestInfoProperties.put(Request.REQUEST_INFO_BODY_PROPERTY, configurationData); expect(request.getProperties()).andReturn(setProperties); expect(request.getRequestInfoProperties()).andReturn(requestInfoProperties); replay(dao, metaInfo, request); try { provider.createResources(request); fail("Exception expected"); } catch (IllegalArgumentException e) { assertEquals(BlueprintResourceProvider.CONFIGURATION_LIST_CHECK_ERROR_MESSAGE, e.getMessage()); } verify(dao, metaInfo, request); }