Repository: ambari Updated Branches: refs/heads/trunk 892ea706b -> 2585179a8
AMBARI-6777. Views: view.xml instance changes are not picked up on redeploy. Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/2585179a Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/2585179a Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/2585179a Branch: refs/heads/trunk Commit: 2585179a80c3cdefa7fa69be09689308150ffddb Parents: 892ea70 Author: Siddharth Wagle <swa...@hortonworks.com> Authored: Thu Aug 7 10:34:45 2014 -0700 Committer: Siddharth Wagle <swa...@hortonworks.com> Committed: Thu Aug 7 10:34:45 2014 -0700 ---------------------------------------------------------------------- .../server/orm/entities/ViewInstanceEntity.java | 25 ++++ .../server/upgrade/UpgradeCatalog170.java | 2 + .../apache/ambari/server/view/ViewRegistry.java | 126 ++++++++++++++++--- .../main/resources/Ambari-DDL-MySQL-CREATE.sql | 2 +- .../main/resources/Ambari-DDL-Oracle-CREATE.sql | 2 +- .../resources/Ambari-DDL-Postgres-CREATE.sql | 2 +- .../Ambari-DDL-Postgres-EMBEDDED-CREATE.sql | 2 +- .../ambari/server/view/ViewRegistryTest.java | 84 ++++++++----- 8 files changed, 191 insertions(+), 54 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/2585179a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ViewInstanceEntity.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ViewInstanceEntity.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ViewInstanceEntity.java index fa303c7..78afdff 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ViewInstanceEntity.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ViewInstanceEntity.java @@ -125,6 +125,13 @@ public class ViewInstanceEntity implements ViewInstanceDefinition { private String icon64; /** + * The XML driven instance flag. + */ + @Column(name="xml_driven") + @Basic + private char xmlDriven = 'N'; + + /** * The instance properties. */ @OneToMany(cascade = CascadeType.ALL, mappedBy = "viewInstance") @@ -398,6 +405,24 @@ public class ViewInstanceEntity implements ViewInstanceDefinition { } /** + * Get the xml driven flag. + * + * @return the xml driven flag + */ + public boolean isXmlDriven() { + return xmlDriven == 'y' || xmlDriven == 'Y'; + } + + /** + * Set the xml driven flag. + * + * @param xmlDriven the xml driven flag + */ + public void setXmlDriven(boolean xmlDriven) { + this.xmlDriven = (xmlDriven) ? 'Y' : 'N'; + } + + /** * Get the instance properties. * * @return the instance properties http://git-wip-us.apache.org/repos/asf/ambari/blob/2585179a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog170.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog170.java b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog170.java index 9fbed00..dba6ef7 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog170.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog170.java @@ -186,6 +186,8 @@ public class UpgradeCatalog170 extends AbstractUpgradeCatalog { Integer.class, 1, 1, false)); dbAccessor.addColumn("viewinstance", new DBColumnInfo("resource_id", Long.class, 1, 1, false)); + dbAccessor.addColumn("viewinstance", new DBColumnInfo("xml_driven", + Character.class, 1, null, true)); dbAccessor.addColumn("clusters", new DBColumnInfo("resource_id", Long.class, 1, 1, false)); http://git-wip-us.apache.org/repos/asf/ambari/blob/2585179a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java index 58e68c2..b3eece2 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java @@ -76,6 +76,7 @@ import javax.xml.bind.Unmarshaller; import java.beans.IntrospectionException; import java.io.File; +import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; @@ -410,13 +411,17 @@ public class ViewRegistry { // extract the archive and get the class loader ClassLoader cl = extractViewArchive(archiveFile, helper.getFile(archivePath)); + viewConfig = helper.getViewConfigFromExtractedArchive(archivePath); + ViewEntity viewDefinition = createViewDefinition(viewConfig, configuration, cl, archivePath); Set<ViewInstanceEntity> instanceDefinitions = new HashSet<ViewInstanceEntity>(); for (InstanceConfig instanceConfig : viewConfig.getInstances()) { try { - instanceDefinitions.add(createViewInstanceDefinition(viewConfig, viewDefinition, instanceConfig)); + ViewInstanceEntity instanceEntity = createViewInstanceDefinition(viewConfig, viewDefinition, instanceConfig); + instanceEntity.setXmlDriven(true); + instanceDefinitions.add(instanceEntity); } catch (Exception e) { LOG.error("Caught exception adding view instance for view " + viewDefinition.getViewName(), e); @@ -529,6 +534,9 @@ public class ViewRegistry { ViewInstanceEntity entity = getInstanceDefinition(viewName, version, instanceName); if (entity != null) { + if (entity.isXmlDriven()) { + throw new IllegalStateException("View instances defined via xml can't be updated through api requests"); + } if (LOG.isDebugEnabled()) { LOG.debug("Updating view instance " + viewName + "/" + version + "/" + instanceName); @@ -564,8 +572,9 @@ public class ViewRegistry { * Uninstall a view instance for the view with the given view name. * * @param instanceEntity the view instance entity + * @throws IllegalStateException if the given instance is not in a valid state */ - public void uninstallViewInstance(ViewInstanceEntity instanceEntity) { + public void uninstallViewInstance(ViewInstanceEntity instanceEntity) throws IllegalStateException { ViewEntity viewEntity = getDefinition(instanceEntity.getViewName()); if (viewEntity != null) { @@ -574,7 +583,9 @@ public class ViewRegistry { String version = viewEntity.getVersion(); if (getInstanceDefinition(viewName, version, instanceName) != null) { - + if (instanceEntity.isXmlDriven()) { + throw new IllegalStateException("View instances defined via xml can't be deleted through api requests"); + } if (LOG.isDebugEnabled()) { LOG.debug("Deleting view instance " + viewName + "/" + version + "/" +instanceName); @@ -969,13 +980,29 @@ public class ViewRegistry { } } - // sync the given view with the db + /** + * Sync given view with data in DB. Ensures that view data in DB is updated, + * all instances changes from xml config are reflected to DB + * + * @param view view config from xml + * @param instanceDefinitions view instances from xml + * @throws Exception + */ private void syncView(ViewEntity view, Set<ViewInstanceEntity> instanceDefinitions) throws Exception { String viewName = view.getName(); + // get or create an admin resource type to represent this view + ResourceTypeEntity resourceTypeEntity = resourceTypeDAO.findByName(viewName); + if (resourceTypeEntity == null) { + resourceTypeEntity = new ResourceTypeEntity(); + resourceTypeEntity.setName(view.getName()); + resourceTypeDAO.create(resourceTypeEntity); + } + view.setResourceType(resourceTypeEntity); + ViewEntity persistedView = viewDAO.findByName(viewName); // if the view is not yet persisted ... @@ -983,13 +1010,31 @@ public class ViewRegistry { if (LOG.isDebugEnabled()) { LOG.debug("Creating View " + viewName + "."); } + + for( ViewInstanceEntity instance : view.getInstances()) { + + // create an admin resource to represent this view instance + ResourceEntity resourceEntity = new ResourceEntity(); + resourceEntity.setResourceType(resourceTypeEntity); + resourceDAO.create(resourceEntity); + + instance.setResource(resourceEntity); + } // ... merge it - persistedView = viewDAO.merge(view); + viewDAO.merge(view); + + persistedView = viewDAO.findByName(viewName); + if (persistedView == null) { + String message = "View " + viewName + " can not be found."; + + LOG.error(message); + throw new IllegalStateException(message); + } } - Map<String, ViewInstanceEntity> instanceEntityMap = new HashMap<String, ViewInstanceEntity>(); + Map<String, ViewInstanceEntity> xmlInstanceEntityMap = new HashMap<String, ViewInstanceEntity>(); for( ViewInstanceEntity instance : view.getInstances()) { - instanceEntityMap.put(instance.getName(), instance); + xmlInstanceEntityMap.put(instance.getName(), instance); } view.setResourceType(persistedView.getResourceType()); @@ -1002,7 +1047,12 @@ public class ViewRegistry { ViewInstanceEntity instance = view.getInstanceDefinition(instanceName); - instanceEntityMap.remove(instanceName); + if (persistedInstance.isXmlDriven() && !xmlInstanceEntityMap.containsKey(instanceName)) { + instanceDAO.remove(persistedInstance); + xmlInstanceEntityMap.remove(instanceName); + continue; + } + xmlInstanceEntityMap.remove(instanceName); // if the persisted instance is not in the registry ... if (instance == null) { @@ -1013,23 +1063,39 @@ public class ViewRegistry { } instance.setViewInstanceId(persistedInstance.getViewInstanceId()); - // apply the persisted overrides to the in-memory instance - instance.setLabel(persistedInstance.getLabel()); - instance.setDescription(persistedInstance.getDescription()); - instance.setVisible(persistedInstance.isVisible()); - instance.setData(persistedInstance.getData()); - instance.setProperties(persistedInstance.getProperties()); - instance.setEntities(persistedInstance.getEntities()); + if (instance.isXmlDriven()) { + // override db data with data from {@InstanceConfig} + persistedInstance.setLabel(instance.getLabel()); + persistedInstance.setDescription(instance.getDescription()); + persistedInstance.setVisible(instance.isVisible()); + persistedInstance.setIcon(instance.getIcon()); + persistedInstance.setIcon64(instance.getIcon64()); + persistedInstance.setProperties(instance.getProperties()); + + instanceDAO.merge(persistedInstance); + } else { + // apply the persisted overrides to the in-memory instance + view.removeInstanceDefinition(instanceName); + view.addInstanceDefinition(persistedInstance); + } instance.setResource(persistedInstance.getResource()); } - // these instances appear in the archive but have been deleted - // from the db... remove them from the registry - for (ViewInstanceEntity instance : instanceEntityMap.values()) { - view.removeInstanceDefinition(instance.getName()); - instanceDefinitions.remove(instance); + // these instances appear in the archive but not present in the db... add + // them to db and registry + for (ViewInstanceEntity instance : xmlInstanceEntityMap.values()) { + // create an admin resource to represent this view instance + ResourceEntity resourceEntity = new ResourceEntity(); + resourceEntity.setResourceType(resourceTypeEntity); + resourceDAO.create(resourceEntity); + instance.setResource(resourceEntity); + + instanceDAO.merge(instance); + bindViewInstance(view, instance); + instanceDefinitions.add(instance); } + } // ensure that the extracted view archive directory exists @@ -1256,6 +1322,26 @@ public class ViewRegistry { } /** + * Get the view configuration from the extracted archive file. + * + * @param archivePath path to extracted archive + * + * @return the associated view configuration + * + * @throws JAXBException if xml is malformed + * @throws FileNotFoundException if xml was not found + */ + public ViewConfig getViewConfigFromExtractedArchive(String archivePath) + throws JAXBException, FileNotFoundException { + + InputStream configStream = new FileInputStream(new File(archivePath + File.separator + VIEW_XML)); + JAXBContext jaxbContext = JAXBContext.newInstance(ViewConfig.class); + Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); + + return (ViewConfig) jaxbUnmarshaller.unmarshal(configStream); + } + + /** * Get a new file instance for the given path. * * @param path the path http://git-wip-us.apache.org/repos/asf/ambari/blob/2585179a/ambari-server/src/main/resources/Ambari-DDL-MySQL-CREATE.sql ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/resources/Ambari-DDL-MySQL-CREATE.sql b/ambari-server/src/main/resources/Ambari-DDL-MySQL-CREATE.sql index b1d2eba..6e582e2 100644 --- a/ambari-server/src/main/resources/Ambari-DDL-MySQL-CREATE.sql +++ b/ambari-server/src/main/resources/Ambari-DDL-MySQL-CREATE.sql @@ -69,7 +69,7 @@ CREATE TABLE blueprint_configuration (blueprint_name VARCHAR(255) NOT NULL, type CREATE TABLE hostgroup_configuration (blueprint_name VARCHAR(255) NOT NULL, hostgroup_name VARCHAR(255) NOT NULL, type_name VARCHAR(255) NOT NULL, config_data TEXT NOT NULL, config_attributes TEXT, PRIMARY KEY(blueprint_name, hostgroup_name, type_name)); CREATE TABLE viewmain (view_name VARCHAR(255) NOT NULL, label VARCHAR(255), version VARCHAR(255), resource_type_id INTEGER NOT NULL, icon VARCHAR(255), icon64 VARCHAR(255), archive VARCHAR(255), mask VARCHAR(255), PRIMARY KEY(view_name)); CREATE TABLE viewinstancedata (view_instance_id BIGINT, view_name VARCHAR(255) NOT NULL, view_instance_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, user_name VARCHAR(255) NOT NULL, value VARCHAR(2000) NOT NULL, PRIMARY KEY(VIEW_INSTANCE_ID, NAME, USER_NAME)); -CREATE TABLE viewinstance (view_instance_id BIGINT, resource_id BIGINT NOT NULL, view_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, label VARCHAR(255), description VARCHAR(255), visible CHAR(1), icon VARCHAR(255), icon64 VARCHAR(255), PRIMARY KEY(view_instance_id)); +CREATE TABLE viewinstance (view_instance_id BIGINT, resource_id BIGINT NOT NULL, view_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, label VARCHAR(255), description VARCHAR(255), visible CHAR(1), icon VARCHAR(255), icon64 VARCHAR(255), xml_driven CHAR(1), PRIMARY KEY(view_instance_id)); CREATE TABLE viewinstanceproperty (view_name VARCHAR(255) NOT NULL, view_instance_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, value VARCHAR(2000) NOT NULL, PRIMARY KEY(view_name, view_instance_name, name)); CREATE TABLE viewparameter (view_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, description VARCHAR(255), required CHAR(1), masked CHAR(1), PRIMARY KEY(view_name, name)); CREATE TABLE viewresource (view_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, plural_name VARCHAR(255), id_property VARCHAR(255), subResource_names VARCHAR(255), provider VARCHAR(255), service VARCHAR(255), resource VARCHAR(255), PRIMARY KEY(view_name, name)); http://git-wip-us.apache.org/repos/asf/ambari/blob/2585179a/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql b/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql index 6ca8fdb..b037e82 100644 --- a/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql +++ b/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql @@ -59,7 +59,7 @@ CREATE TABLE blueprint_configuration (blueprint_name VARCHAR2(255) NOT NULL, typ CREATE TABLE hostgroup_configuration (blueprint_name VARCHAR2(255) NOT NULL, hostgroup_name VARCHAR2(255) NOT NULL, type_name VARCHAR2(255) NOT NULL, config_data CLOB NOT NULL, config_attributes CLOB, PRIMARY KEY(blueprint_name, hostgroup_name, type_name)); CREATE TABLE viewmain (view_name VARCHAR(255) NOT NULL, label VARCHAR(255), version VARCHAR(255), resource_type_id NUMBER(10) NOT NULL, icon VARCHAR(255), icon64 VARCHAR(255), archive VARCHAR(255), mask VARCHAR(255), PRIMARY KEY(view_name)); CREATE TABLE viewinstancedata (view_instance_id NUMBER(19), view_name VARCHAR(255) NOT NULL, view_instance_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, user_name VARCHAR(255) NOT NULL, value VARCHAR(2000) NOT NULL, PRIMARY KEY(view_instance_id, name, user_name)); -CREATE TABLE viewinstance (view_instance_id NUMBER(19), resource_id NUMBER(19) NOT NULL, view_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, label VARCHAR(255), description VARCHAR(255), visible CHAR(1), icon VARCHAR(255), icon64 VARCHAR(255), PRIMARY KEY(view_instance_id)); +CREATE TABLE viewinstance (view_instance_id NUMBER(19), resource_id NUMBER(19) NOT NULL, view_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, label VARCHAR(255), description VARCHAR(255), visible CHAR(1), icon VARCHAR(255), icon64 VARCHAR(255), xml_driven CHAR(1), PRIMARY KEY(view_instance_id)); CREATE TABLE viewinstanceproperty (view_name VARCHAR(255) NOT NULL, view_instance_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, value VARCHAR(2000) NOT NULL, PRIMARY KEY(view_name, view_instance_name, name)); CREATE TABLE viewparameter (view_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, description VARCHAR(255), required CHAR(1), masked CHAR(1), PRIMARY KEY(view_name, name)); CREATE TABLE viewresource (view_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, plural_name VARCHAR(255), id_property VARCHAR(255), subResource_names VARCHAR(255), provider VARCHAR(255), service VARCHAR(255), "resource" VARCHAR(255), PRIMARY KEY(view_name, name)); http://git-wip-us.apache.org/repos/asf/ambari/blob/2585179a/ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql b/ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql index 6ab57ac..4ee213d 100644 --- a/ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql +++ b/ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql @@ -97,7 +97,7 @@ CREATE TABLE hostgroup_configuration (blueprint_name VARCHAR(255) NOT NULL, host CREATE TABLE viewmain (view_name VARCHAR(255) NOT NULL, label VARCHAR(255), version VARCHAR(255), resource_type_id INTEGER NOT NULL, icon VARCHAR(255), icon64 VARCHAR(255), archive VARCHAR(255), mask VARCHAR(255), PRIMARY KEY(view_name)); CREATE TABLE viewinstancedata (view_instance_id BIGINT, view_name VARCHAR(255) NOT NULL, view_instance_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, user_name VARCHAR(255) NOT NULL, value VARCHAR(2000) NOT NULL, PRIMARY KEY(view_instance_id, name, user_name)); -CREATE TABLE viewinstance (view_instance_id BIGINT, resource_id BIGINT NOT NULL, view_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, label VARCHAR(255), description VARCHAR(255), visible CHAR(1), icon VARCHAR(255), icon64 VARCHAR(255), PRIMARY KEY(view_instance_id)); +CREATE TABLE viewinstance (view_instance_id BIGINT, resource_id BIGINT NOT NULL, view_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, label VARCHAR(255), description VARCHAR(255), visible CHAR(1), icon VARCHAR(255), icon64 VARCHAR(255), xml_driven CHAR(1), PRIMARY KEY(view_instance_id)); CREATE TABLE viewinstanceproperty (view_name VARCHAR(255) NOT NULL, view_instance_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, value VARCHAR(2000) NOT NULL, PRIMARY KEY(view_name, view_instance_name, name)); CREATE TABLE viewparameter (view_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, description VARCHAR(255), required CHAR(1), masked CHAR(1), PRIMARY KEY(view_name, name)); CREATE TABLE viewresource (view_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, plural_name VARCHAR(255), id_property VARCHAR(255), subResource_names VARCHAR(255), provider VARCHAR(255), service VARCHAR(255), resource VARCHAR(255), PRIMARY KEY(view_name, name)); http://git-wip-us.apache.org/repos/asf/ambari/blob/2585179a/ambari-server/src/main/resources/Ambari-DDL-Postgres-EMBEDDED-CREATE.sql ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/resources/Ambari-DDL-Postgres-EMBEDDED-CREATE.sql b/ambari-server/src/main/resources/Ambari-DDL-Postgres-EMBEDDED-CREATE.sql index ef70439..f4ac3f3 100644 --- a/ambari-server/src/main/resources/Ambari-DDL-Postgres-EMBEDDED-CREATE.sql +++ b/ambari-server/src/main/resources/Ambari-DDL-Postgres-EMBEDDED-CREATE.sql @@ -149,7 +149,7 @@ GRANT ALL PRIVILEGES ON TABLE ambari.hostgroup_configuration TO :username; CREATE TABLE ambari.viewmain (view_name VARCHAR(255) NOT NULL, label VARCHAR(255), version VARCHAR(255), resource_type_id INTEGER NOT NULL, icon VARCHAR(255), icon64 VARCHAR(255), archive VARCHAR(255), mask VARCHAR(255), PRIMARY KEY(view_name)); CREATE TABLE ambari.viewinstancedata (view_instance_id BIGINT, view_name VARCHAR(255) NOT NULL, view_instance_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, user_name VARCHAR(255) NOT NULL, value VARCHAR(2000) NOT NULL, PRIMARY KEY(view_instance_id, name, user_name)); -CREATE TABLE ambari.viewinstance (view_instance_id BIGINT, resource_id BIGINT NOT NULL, view_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, label VARCHAR(255), description VARCHAR(255), visible CHAR(1), icon VARCHAR(255), icon64 VARCHAR(255), PRIMARY KEY(view_instance_id)); +CREATE TABLE ambari.viewinstance (view_instance_id BIGINT, resource_id BIGINT NOT NULL, view_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, label VARCHAR(255), description VARCHAR(255), visible CHAR(1), icon VARCHAR(255), icon64 VARCHAR(255), xml_driven CHAR(1), PRIMARY KEY(view_instance_id)); CREATE TABLE ambari.viewinstanceproperty (view_name VARCHAR(255) NOT NULL, view_instance_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, value VARCHAR(2000) NOT NULL, PRIMARY KEY(view_name, view_instance_name, name)); CREATE TABLE ambari.viewparameter (view_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, description VARCHAR(255), required CHAR(1), masked CHAR(1), PRIMARY KEY(view_name, name)); CREATE TABLE ambari.viewresource (view_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, plural_name VARCHAR(255), id_property VARCHAR(255), subResource_names VARCHAR(255), provider VARCHAR(255), service VARCHAR(255), resource VARCHAR(255), PRIMARY KEY(view_name, name)); http://git-wip-us.apache.org/repos/asf/ambari/blob/2585179a/ambari-server/src/test/java/org/apache/ambari/server/view/ViewRegistryTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/view/ViewRegistryTest.java b/ambari-server/src/test/java/org/apache/ambari/server/view/ViewRegistryTest.java index fbe1c90..f383799 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/view/ViewRegistryTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/view/ViewRegistryTest.java @@ -18,6 +18,32 @@ package org.apache.ambari.server.view; +import static org.easymock.EasyMock.capture; +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.createNiceMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URI; +import java.util.Collection; +import java.util.Collections; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; +import java.util.Set; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; + +import javax.xml.bind.JAXBException; + import org.apache.ambari.server.api.resources.SubResourceDefinition; import org.apache.ambari.server.configuration.Configuration; import org.apache.ambari.server.controller.spi.Resource; @@ -48,36 +74,12 @@ import org.apache.ambari.server.view.events.EventImplTest; import org.apache.ambari.view.events.Event; import org.apache.ambari.view.events.Listener; import org.easymock.Capture; +import org.easymock.EasyMock; import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import javax.xml.bind.JAXBException; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URI; -import java.util.Collection; -import java.util.Collections; -import java.util.Enumeration; -import java.util.HashMap; -import java.util.Map; -import java.util.Properties; -import java.util.Set; -import java.util.jar.JarEntry; -import java.util.jar.JarFile; - -import static org.easymock.EasyMock.capture; -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.createNiceMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; -import static org.easymock.EasyMock.verify; - /** * ViewRegistry tests. */ @@ -214,6 +216,7 @@ public class ViewRegistryTest { expect(viewDir.listFiles()).andReturn(new File[]{viewArchive}); + expect(viewArchive.getAbsolutePath()).andReturn("/var/lib/ambari-server/resources/views/work/MY_VIEW{1.0.0}"); expect(viewArchive.isDirectory()).andReturn(false); expect(archiveDir.exists()).andReturn(false); @@ -252,12 +255,15 @@ public class ViewRegistryTest { expect(libDir.listFiles()).andReturn(new File[]{fileEntry}); expect(fileEntry.toURI()).andReturn(new URI("file:./")); - Capture<ViewEntity> captureViewEntity = new Capture<ViewEntity>(); + expect(vDAO.findByName("MY_VIEW{1.0.0}")).andReturn(viewDefinition); + expect(vDAO.findAll()).andReturn(Collections.<ViewEntity>emptyList()); - expect(vDAO.findByName("MY_VIEW{1.0.0}")).andReturn(null); - expect(vDAO.merge(capture(captureViewEntity))).andReturn(viewDefinition); + expect(rtDAO.findByName("MY_VIEW{1.0.0}")).andReturn(null); + rtDAO.create(EasyMock.anyObject(ResourceTypeEntity.class)); + EasyMock.expectLastCall().anyTimes(); - expect(vDAO.findAll()).andReturn(Collections.<ViewEntity>emptyList()); + rDAO.create(EasyMock.anyObject(ResourceEntity.class)); + EasyMock.expectLastCall().anyTimes(); // replay mocks replay(configuration, viewDir, extractedArchiveDir, viewArchive, archiveDir, entryFile, classesDir, @@ -269,7 +275,6 @@ public class ViewRegistryTest { Set<ViewInstanceEntity> instanceEntities = registry.readViewArchives(configuration); Assert.assertEquals(2, instanceEntities.size()); - Assert.assertEquals("MY_VIEW", captureViewEntity.getValue().getCommonName()); // verify mocks verify(configuration, viewDir, extractedArchiveDir, viewArchive, archiveDir, entryFile, classesDir, @@ -348,6 +353,7 @@ public class ViewRegistryTest { expect(viewDir.listFiles()).andReturn(new File[]{viewArchive}); expect(viewArchive.isDirectory()).andReturn(false); + expect(viewArchive.getAbsolutePath()).andReturn("/var/lib/ambari-server/resources/views/work/MY_VIEW{1.0.0}").anyTimes(); expect(archiveDir.exists()).andReturn(false); expect(archiveDir.getAbsolutePath()).andReturn( @@ -392,6 +398,13 @@ public class ViewRegistryTest { expect(vDAO.findAll()).andReturn(Collections.<ViewEntity>emptyList()); + expect(rtDAO.findByName("MY_VIEW{1.0.0}")).andReturn(null); + rtDAO.create(EasyMock.anyObject(ResourceTypeEntity.class)); + EasyMock.expectLastCall().anyTimes(); + + rDAO.create(EasyMock.anyObject(ResourceEntity.class)); + EasyMock.expectLastCall().anyTimes(); + Capture<ResourceEntity> resourceEntityCapture = new Capture<ResourceEntity>(); // replay mocks @@ -786,6 +799,17 @@ public class ViewRegistryTest { } @Override + public ViewConfig getViewConfigFromExtractedArchive(String archivePath) + throws JAXBException, FileNotFoundException { + for (File viewConfigKey: viewConfigs.keySet()) { + if (viewConfigKey.getAbsolutePath().equals(archivePath)) { + return viewConfigs.get(viewConfigKey); + } + } + return null; + } + + @Override public File getFile(String path) { return files.get(path); }