Repository: ambari Updated Branches: refs/heads/trunk 75c9df821 -> 4b3caee76
http://git-wip-us.apache.org/repos/asf/ambari/blob/4b3caee7/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/WidgetLayoutResourceProviderTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/WidgetLayoutResourceProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/WidgetLayoutResourceProviderTest.java new file mode 100644 index 0000000..6cb82d9 --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/WidgetLayoutResourceProviderTest.java @@ -0,0 +1,362 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.ambari.server.controller.internal; + +import com.google.inject.Binder; +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.Module; +import com.google.inject.util.Modules; +import org.apache.ambari.server.controller.AmbariManagementController; +import org.apache.ambari.server.controller.spi.Predicate; +import org.apache.ambari.server.controller.spi.Request; +import org.apache.ambari.server.controller.spi.Resource; +import org.apache.ambari.server.controller.utilities.PredicateBuilder; +import org.apache.ambari.server.controller.utilities.PropertyHelper; +import org.apache.ambari.server.metadata.ActionMetadata; +import org.apache.ambari.server.orm.InMemoryDefaultTestModule; +import org.apache.ambari.server.orm.dao.WidgetLayoutDAO; +import org.apache.ambari.server.orm.entities.WidgetLayoutEntity; +import org.apache.ambari.server.orm.entities.WidgetLayoutUserWidgetEntity; +import org.apache.ambari.server.state.Cluster; +import org.apache.ambari.server.state.Clusters; +import org.easymock.Capture; +import org.easymock.EasyMock; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedHashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import static org.easymock.EasyMock.anyObject; +import static org.easymock.EasyMock.capture; +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.createStrictMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.expectLastCall; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.resetToStrict; +import static org.easymock.EasyMock.verify; +import static org.junit.Assert.assertEquals; + +/** + * WidgetLayout tests + */ +public class WidgetLayoutResourceProviderTest { + + private WidgetLayoutDAO dao = null; + private Injector m_injector; + + @Before + public void before() { + dao = createStrictMock(WidgetLayoutDAO.class); + + m_injector = Guice.createInjector(Modules.override( + new InMemoryDefaultTestModule()).with(new MockModule())); + } + + /** + * @throws Exception + */ + @Test + public void testGetResourcesNoPredicate() throws Exception { + WidgetLayoutResourceProvider provider = createProvider(null); + + Request request = PropertyHelper.getReadRequest("WidgetLayouts/id"); + + Set<Resource> results = provider.getResources(request, null); + + assertEquals(0, results.size()); + } + + /** + * @throws Exception + */ + @Test + public void testGetSingleResource() throws Exception { + Request request = PropertyHelper.getReadRequest( + WidgetLayoutResourceProvider.WIDGETLAYOUT_CLUSTER_NAME_PROPERTY_ID, + WidgetLayoutResourceProvider.WIDGETLAYOUT_ID_PROPERTY_ID, + WidgetLayoutResourceProvider.WIDGETLAYOUT_DISPLAY_NAME_PROPERTY_ID, + WidgetLayoutResourceProvider.WIDGETLAYOUT_LAYOUT_NAME_PROPERTY_ID, + WidgetLayoutResourceProvider.WIDGETLAYOUT_SECTION_NAME_PROPERTY_ID, + WidgetLayoutResourceProvider.WIDGETLAYOUT_USERNAME_PROPERTY_ID, + WidgetLayoutResourceProvider.WIDGETLAYOUT_SCOPE_PROPERTY_ID, + WidgetLayoutResourceProvider.WIDGETLAYOUT_INFO_PROPERTY_ID); + + AmbariManagementController amc = createMock(AmbariManagementController.class); + Clusters clusters = createMock(Clusters.class); + Cluster cluster = createMock(Cluster.class); + expect(amc.getClusters()).andReturn(clusters).atLeastOnce(); + expect(clusters.getClusterById(1L)).andReturn(cluster).atLeastOnce(); + expect(cluster.getClusterName()).andReturn("c1").anyTimes(); + + Predicate predicate = new PredicateBuilder().property( + WidgetLayoutResourceProvider.WIDGETLAYOUT_CLUSTER_NAME_PROPERTY_ID).equals("c1") + .and().property(WidgetLayoutResourceProvider.WIDGETLAYOUT_ID_PROPERTY_ID).equals("1") + .and().property(WidgetLayoutResourceProvider.WIDGETLAYOUT_USERNAME_PROPERTY_ID).equals("username").toPredicate(); + + expect(dao.findById(1L)).andReturn(getMockEntities().get(0)); + + replay(amc, clusters, cluster, dao); + + WidgetLayoutResourceProvider provider = createProvider(amc); + Set<Resource> results = provider.getResources(request, predicate); + + assertEquals(1, results.size()); + + Resource r = results.iterator().next(); + Assert.assertEquals("section0", r.getPropertyValue(WidgetLayoutResourceProvider.WIDGETLAYOUT_SECTION_NAME_PROPERTY_ID)); + Assert.assertEquals("CLUSTER", r.getPropertyValue(WidgetLayoutResourceProvider.WIDGETLAYOUT_SCOPE_PROPERTY_ID)); + Assert.assertEquals("username", r.getPropertyValue(WidgetLayoutResourceProvider.WIDGETLAYOUT_USERNAME_PROPERTY_ID)); + Assert.assertEquals("displ_name", r.getPropertyValue(WidgetLayoutResourceProvider.WIDGETLAYOUT_DISPLAY_NAME_PROPERTY_ID)); + Assert.assertEquals("layout name0", r.getPropertyValue(WidgetLayoutResourceProvider.WIDGETLAYOUT_LAYOUT_NAME_PROPERTY_ID)); + + Assert.assertEquals("[]", r.getPropertyValue(WidgetLayoutResourceProvider.WIDGETLAYOUT_INFO_PROPERTY_ID).toString()); + } + + + /** + * @throws Exception + */ + @Test + public void testCreateResources() throws Exception { + AmbariManagementController amc = createMock(AmbariManagementController.class); + Clusters clusters = createMock(Clusters.class); + Cluster cluster = createMock(Cluster.class); + expect(amc.getClusters()).andReturn(clusters).atLeastOnce(); + expect(clusters.getCluster((String) anyObject())).andReturn(cluster).atLeastOnce(); + expect(cluster.getClusterId()).andReturn(Long.valueOf(1)).anyTimes(); + + Capture<WidgetLayoutEntity> entityCapture = new Capture<WidgetLayoutEntity>(); + dao.create(capture(entityCapture)); + expectLastCall(); + + replay(amc, clusters, cluster, dao); + + WidgetLayoutResourceProvider provider = createProvider(amc); + + Map<String, Object> requestProps = new HashMap<String, Object>(); + requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_CLUSTER_NAME_PROPERTY_ID, "c1"); + requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_LAYOUT_NAME_PROPERTY_ID, "layout_name"); + requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_DISPLAY_NAME_PROPERTY_ID, "display_name"); + requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_SECTION_NAME_PROPERTY_ID, "section_name"); + requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_USERNAME_PROPERTY_ID, "admin"); + requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_SCOPE_PROPERTY_ID, "CLUSTER"); + Set widgetsInfo = new LinkedHashSet(); + requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_INFO_PROPERTY_ID, widgetsInfo); + + Request request = PropertyHelper.getCreateRequest(Collections.singleton(requestProps), null); + provider.createResources(request); + + Assert.assertTrue(entityCapture.hasCaptured()); + WidgetLayoutEntity entity = entityCapture.getValue(); + Assert.assertNotNull(entity); + + Assert.assertEquals(Long.valueOf(1), entity.getClusterId()); + Assert.assertEquals("CLUSTER", entity.getScope()); + Assert.assertEquals("layout_name", entity.getLayoutName()); + Assert.assertEquals("display_name", entity.getDisplayName()); + Assert.assertEquals("section_name", entity.getSectionName()); + Assert.assertEquals("admin", entity.getUserName()); + Assert.assertNotNull(entity.getListWidgetLayoutUserWidgetEntity()); + + verify(amc, clusters, cluster, dao); + } + + /** + * @throws Exception + */ + @Test + public void testUpdateResources() throws Exception { + AmbariManagementController amc = createMock(AmbariManagementController.class); + Clusters clusters = createMock(Clusters.class); + Cluster cluster = createMock(Cluster.class); + expect(amc.getClusters()).andReturn(clusters).atLeastOnce(); + expect(clusters.getCluster((String) anyObject())).andReturn(cluster).atLeastOnce(); + expect(cluster.getClusterId()).andReturn(Long.valueOf(1)).atLeastOnce(); + + Capture<WidgetLayoutEntity> entityCapture = new Capture<WidgetLayoutEntity>(); + dao.create(capture(entityCapture)); + expectLastCall(); + + replay(amc, clusters, cluster, dao); + + Map<String, Object> requestProps = new HashMap<String, Object>(); + requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_CLUSTER_NAME_PROPERTY_ID, "c1"); + requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_LAYOUT_NAME_PROPERTY_ID, "layout_name"); + requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_DISPLAY_NAME_PROPERTY_ID, "display_name"); + requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_SECTION_NAME_PROPERTY_ID, "section_name"); + requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_USERNAME_PROPERTY_ID, "admin"); + requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_SCOPE_PROPERTY_ID, "CLUSTER"); + Set widgetsInfo = new LinkedHashSet(); + requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_INFO_PROPERTY_ID, widgetsInfo); + + Request request = PropertyHelper.getCreateRequest( + Collections.singleton(requestProps), null); + + WidgetLayoutResourceProvider provider = createProvider(amc); + + provider.createResources(request); + + Assert.assertTrue(entityCapture.hasCaptured()); + WidgetLayoutEntity entity = entityCapture.getValue(); + Assert.assertNotNull(entity); + + Predicate predicate = new PredicateBuilder().property( + WidgetLayoutResourceProvider.WIDGETLAYOUT_CLUSTER_NAME_PROPERTY_ID).equals("c1") + .and().property(WidgetLayoutResourceProvider.WIDGETLAYOUT_ID_PROPERTY_ID).equals("1") + .and().property(WidgetLayoutResourceProvider.WIDGETLAYOUT_USERNAME_PROPERTY_ID).equals("username").toPredicate(); + + // everything is mocked, there is no DB + entity.setId(Long.valueOf(1)); + + String oldLayoutName = entity.getLayoutName(); + String oldScope = entity.getScope(); + + resetToStrict(dao); + expect(dao.findById(1L)).andReturn(entity).anyTimes(); + expect(dao.merge((WidgetLayoutEntity) anyObject())).andReturn(entity).anyTimes(); + replay(dao); + + requestProps = new HashMap<String, Object>(); + requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_LAYOUT_NAME_PROPERTY_ID, "layout_name_new"); + requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_SCOPE_PROPERTY_ID, "USER"); + requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_ID_PROPERTY_ID, "1"); + requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_INFO_PROPERTY_ID, widgetsInfo); + + request = PropertyHelper.getUpdateRequest(requestProps, null); + + provider.updateResources(request, predicate); + + Assert.assertFalse(oldLayoutName.equals(entity.getLayoutName())); + Assert.assertFalse(oldScope.equals(entity.getScope())); + + verify(amc, clusters, cluster, dao); + } + + /** + * @throws Exception + */ + @Test + public void testDeleteResources() throws Exception { + AmbariManagementController amc = createMock(AmbariManagementController.class); + Clusters clusters = createMock(Clusters.class); + Cluster cluster = createMock(Cluster.class); + expect(amc.getClusters()).andReturn(clusters).atLeastOnce(); + expect(clusters.getCluster((String) anyObject())).andReturn(cluster).atLeastOnce(); + expect(cluster.getClusterId()).andReturn(Long.valueOf(1)).anyTimes(); + + Capture<WidgetLayoutEntity> entityCapture = new Capture<WidgetLayoutEntity>(); + dao.create(capture(entityCapture)); + expectLastCall(); + + replay(amc, clusters, cluster, dao); + + WidgetLayoutResourceProvider provider = createProvider(amc); + + Map<String, Object> requestProps = new HashMap<String, Object>(); + requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_CLUSTER_NAME_PROPERTY_ID, "c1"); + requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_LAYOUT_NAME_PROPERTY_ID, "layout_name"); + requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_DISPLAY_NAME_PROPERTY_ID, "display_name"); + requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_SECTION_NAME_PROPERTY_ID, "section_name"); + requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_USERNAME_PROPERTY_ID, "admin"); + requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_SCOPE_PROPERTY_ID, "CLUSTER"); + Set widgetsInfo = new LinkedHashSet(); + requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_INFO_PROPERTY_ID, widgetsInfo); + + Request request = PropertyHelper.getCreateRequest(Collections.singleton(requestProps), null); + + provider.createResources(request); + + Assert.assertTrue(entityCapture.hasCaptured()); + WidgetLayoutEntity entity = entityCapture.getValue(); + Assert.assertNotNull(entity); + + Predicate predicate = new PredicateBuilder().property( + WidgetLayoutResourceProvider.WIDGETLAYOUT_CLUSTER_NAME_PROPERTY_ID).equals("c1") + .and().property(WidgetLayoutResourceProvider.WIDGETLAYOUT_ID_PROPERTY_ID).equals("1") + .and().property(WidgetLayoutResourceProvider.WIDGETLAYOUT_USERNAME_PROPERTY_ID).equals("username").toPredicate(); + + // everything is mocked, there is no DB + entity.setId(Long.valueOf(1)); + + resetToStrict(dao); + expect(dao.findById(1L)).andReturn(entity).anyTimes(); + dao.remove(capture(entityCapture)); + expectLastCall(); + replay(dao); + + provider.deleteResources(predicate); + + WidgetLayoutEntity entity1 = entityCapture.getValue(); + Assert.assertEquals(Long.valueOf(1), entity1.getId()); + + verify(amc, clusters, cluster, dao); + } + + /** + * @param amc + * @return + */ + private WidgetLayoutResourceProvider createProvider(AmbariManagementController amc) { + return new WidgetLayoutResourceProvider(amc); + } + + /** + * @return + */ + private List<WidgetLayoutEntity> getMockEntities() throws Exception { + + WidgetLayoutEntity widgetLayoutEntity = new WidgetLayoutEntity(); + widgetLayoutEntity.setClusterId(Long.valueOf(1L)); + widgetLayoutEntity.setLayoutName("layout name0"); + widgetLayoutEntity.setSectionName("section0"); + widgetLayoutEntity.setUserName("username"); + widgetLayoutEntity.setScope("CLUSTER"); + widgetLayoutEntity.setDisplayName("displ_name"); + List<WidgetLayoutUserWidgetEntity> layoutUserWidgetEntityList = new LinkedList<WidgetLayoutUserWidgetEntity>(); + widgetLayoutEntity.setListWidgetLayoutUserWidgetEntity(layoutUserWidgetEntityList); + return Arrays.asList(widgetLayoutEntity); + } + + /** + * + */ + private class MockModule implements Module { + /** + * + */ + @Override + public void configure(Binder binder) { + binder.bind(WidgetLayoutDAO.class).toInstance(dao); + binder.bind(Clusters.class).toInstance( + EasyMock.createNiceMock(Clusters.class)); + binder.bind(Cluster.class).toInstance( + EasyMock.createNiceMock(Cluster.class)); + binder.bind(ActionMetadata.class); + } + } +} http://git-wip-us.apache.org/repos/asf/ambari/blob/4b3caee7/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/UserWidgetDAOTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/UserWidgetDAOTest.java b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/UserWidgetDAOTest.java deleted file mode 100644 index f86dde8..0000000 --- a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/UserWidgetDAOTest.java +++ /dev/null @@ -1,116 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ambari.server.orm.dao; - -import org.apache.ambari.server.orm.GuiceJpaInitializer; -import org.apache.ambari.server.orm.InMemoryDefaultTestModule; -import org.apache.ambari.server.orm.OrmTestHelper; -import org.apache.ambari.server.orm.entities.RepositoryVersionEntity; -import org.apache.ambari.server.orm.entities.UserWidgetEntity; -import org.apache.ambari.server.orm.entities.WidgetLayoutEntity; -import org.apache.ambari.server.orm.entities.WidgetLayoutUserWidgetEntity; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.google.inject.Guice; -import com.google.inject.Injector; -import com.google.inject.persist.PersistService; - -import java.util.LinkedList; -import java.util.List; - -/** - * UserWidgetDAO unit tests. - */ -public class UserWidgetDAOTest { - - private static Injector injector; - private UserWidgetDAO userWidgetDAO; - OrmTestHelper helper; - Long clusterId; - - - @Before - public void before() { - injector = Guice.createInjector(new InMemoryDefaultTestModule()); - userWidgetDAO = injector.getInstance(UserWidgetDAO.class); - injector.getInstance(GuiceJpaInitializer.class); - helper = injector.getInstance(OrmTestHelper.class); - clusterId = helper.createCluster(); - } - - private void createRecords() { - for (int i=0; i<3; i++) { - UserWidgetEntity userWidgetEntity = new UserWidgetEntity(); - userWidgetEntity.setDisplayName("display name" + i); - userWidgetEntity.setAuthor("author"); - userWidgetEntity.setClusterId(clusterId); - userWidgetEntity.setMetrics("metrics"); - userWidgetEntity.setDescription("description"); - userWidgetEntity.setProperties("{\"warning_threshold\": 0.5,\"error_threshold\": 0.7 }"); - userWidgetEntity.setScope("CLUSTER"); - userWidgetEntity.setUserWidgetName("widget" + i); - userWidgetEntity.setUserWidgetType("GAUGE"); - userWidgetEntity.setWidgetValues("${`jvmMemoryHeapUsed + jvmMemoryHeapMax`}"); - final WidgetLayoutEntity widgetLayoutEntity = new WidgetLayoutEntity(); - widgetLayoutEntity.setClusterId(clusterId); - widgetLayoutEntity.setLayoutName("layout name" + i); - widgetLayoutEntity.setSectionName("section" + i%2); - final WidgetLayoutUserWidgetEntity widgetLayoutUserWidget = new WidgetLayoutUserWidgetEntity(); - widgetLayoutUserWidget.setUserWidget(userWidgetEntity); - widgetLayoutUserWidget.setWidgetLayout(widgetLayoutEntity); - widgetLayoutUserWidget.setWidgetOrder(0); - - List<WidgetLayoutUserWidgetEntity> widgetLayoutUserWidgetEntityList = new LinkedList<WidgetLayoutUserWidgetEntity>(); - widgetLayoutUserWidgetEntityList.add(widgetLayoutUserWidget); - - userWidgetEntity.setListWidgetLayoutUserWidgetEntity(widgetLayoutUserWidgetEntityList); - userWidgetDAO.create(userWidgetEntity); - } - } - - @Test - public void testFindByCluster() { - createRecords(); - Assert.assertEquals(0, userWidgetDAO.findByCluster(99999).size()); - Assert.assertEquals(3, userWidgetDAO.findByCluster(clusterId).size()); - } - - @Test - public void testFindBySectionName() { - createRecords(); - Assert.assertEquals(0, userWidgetDAO.findBySectionName("non existing").size()); - Assert.assertEquals(2, userWidgetDAO.findBySectionName("section0").size()); - Assert.assertEquals(1, userWidgetDAO.findBySectionName("section1").size()); - } - - @Test - public void testFindAll() { - createRecords(); - Assert.assertEquals(3, userWidgetDAO.findAll().size()); - } - - @After - public void after() { - injector.getInstance(PersistService.class).stop(); - injector = null; - } -} http://git-wip-us.apache.org/repos/asf/ambari/blob/4b3caee7/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/WidgetDAOTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/WidgetDAOTest.java b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/WidgetDAOTest.java new file mode 100644 index 0000000..66e68e7 --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/WidgetDAOTest.java @@ -0,0 +1,121 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ambari.server.orm.dao; + +import org.apache.ambari.server.orm.GuiceJpaInitializer; +import org.apache.ambari.server.orm.InMemoryDefaultTestModule; +import org.apache.ambari.server.orm.OrmTestHelper; +import org.apache.ambari.server.orm.entities.WidgetEntity; +import org.apache.ambari.server.orm.entities.WidgetLayoutEntity; +import org.apache.ambari.server.orm.entities.WidgetLayoutUserWidgetEntity; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.persist.PersistService; + +import java.util.LinkedList; +import java.util.List; + +/** + * WidgetDAO unit tests. + */ +public class WidgetDAOTest { + + private static Injector injector; + private WidgetDAO widgetDAO; + private WidgetLayoutDAO widgetLayoutDAO; + OrmTestHelper helper; + Long clusterId; + + + @Before + public void before() { + injector = Guice.createInjector(new InMemoryDefaultTestModule()); + widgetDAO = injector.getInstance(WidgetDAO.class); + widgetLayoutDAO = injector.getInstance(WidgetLayoutDAO.class); + injector.getInstance(GuiceJpaInitializer.class); + helper = injector.getInstance(OrmTestHelper.class); + clusterId = helper.createCluster(); + } + + private void createRecords() { + for (int i=0; i<3; i++) { + WidgetEntity widgetEntity = new WidgetEntity(); + widgetEntity.setDisplayName("display name" + i); + widgetEntity.setAuthor("author"); + widgetEntity.setClusterId(clusterId); + widgetEntity.setMetrics("metrics"); + widgetEntity.setDescription("description"); + widgetEntity.setProperties("{\"warning_threshold\": 0.5,\"error_threshold\": 0.7 }"); + widgetEntity.setScope("CLUSTER"); + widgetEntity.setWidgetName("widget" + i); + widgetEntity.setWidgetType("GAUGE"); + widgetEntity.setWidgetValues("${`jvmMemoryHeapUsed + jvmMemoryHeapMax`}"); + final WidgetLayoutEntity widgetLayoutEntity = new WidgetLayoutEntity(); + widgetLayoutEntity.setClusterId(clusterId); + widgetLayoutEntity.setLayoutName("layout name" + i); + widgetLayoutEntity.setSectionName("section" + i%2); + widgetLayoutEntity.setDisplayName("display_name"); + widgetLayoutEntity.setUserName("user_name"); + widgetLayoutEntity.setScope("CLUSTER"); + final WidgetLayoutUserWidgetEntity widgetLayoutUserWidget = new WidgetLayoutUserWidgetEntity(); + widgetLayoutUserWidget.setWidget(widgetEntity); + widgetLayoutUserWidget.setWidgetLayout(widgetLayoutEntity); + widgetLayoutUserWidget.setWidgetOrder(0); + + widgetDAO.create(widgetEntity); + List<WidgetLayoutUserWidgetEntity> widgetLayoutUserWidgetEntityList = new LinkedList<WidgetLayoutUserWidgetEntity>(); + widgetLayoutUserWidgetEntityList.add(widgetLayoutUserWidget); + + widgetLayoutEntity.setListWidgetLayoutUserWidgetEntity(widgetLayoutUserWidgetEntityList); + widgetLayoutDAO.create(widgetLayoutEntity); + } + } + + @Test + public void testFindByCluster() { + createRecords(); + Assert.assertEquals(0, widgetDAO.findByCluster(99999).size()); + Assert.assertEquals(3, widgetDAO.findByCluster(clusterId).size()); + } + + @Test + public void testFindBySectionName() { + createRecords(); + Assert.assertEquals(0, widgetDAO.findBySectionName("non existing").size()); + Assert.assertEquals(2, widgetDAO.findBySectionName("section0").size()); + Assert.assertEquals(1, widgetDAO.findBySectionName("section1").size()); + } + + @Test + public void testFindAll() { + createRecords(); + Assert.assertEquals(3, widgetDAO.findAll().size()); + } + + @After + public void after() { + injector.getInstance(PersistService.class).stop(); + injector = null; + } +} http://git-wip-us.apache.org/repos/asf/ambari/blob/4b3caee7/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/WidgetLayoutDAOTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/WidgetLayoutDAOTest.java b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/WidgetLayoutDAOTest.java index 3d83399..b74bea4 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/WidgetLayoutDAOTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/WidgetLayoutDAOTest.java @@ -24,7 +24,7 @@ import com.google.inject.persist.PersistService; import org.apache.ambari.server.orm.GuiceJpaInitializer; import org.apache.ambari.server.orm.InMemoryDefaultTestModule; import org.apache.ambari.server.orm.OrmTestHelper; -import org.apache.ambari.server.orm.entities.UserWidgetEntity; +import org.apache.ambari.server.orm.entities.WidgetEntity; import org.apache.ambari.server.orm.entities.WidgetLayoutEntity; import org.apache.ambari.server.orm.entities.WidgetLayoutUserWidgetEntity; import org.junit.After; @@ -42,6 +42,7 @@ public class WidgetLayoutDAOTest { private static Injector injector; private WidgetLayoutDAO widgetLayoutDAO; + private WidgetDAO widgetDAO; OrmTestHelper helper; Long clusterId; @@ -50,6 +51,7 @@ public class WidgetLayoutDAOTest { public void before() { injector = Guice.createInjector(new InMemoryDefaultTestModule()); widgetLayoutDAO = injector.getInstance(WidgetLayoutDAO.class); + widgetDAO = injector.getInstance(WidgetDAO.class); injector.getInstance(GuiceJpaInitializer.class); helper = injector.getInstance(OrmTestHelper.class); clusterId = helper.createCluster(); @@ -61,29 +63,37 @@ public class WidgetLayoutDAOTest { widgetLayoutEntity.setClusterId(clusterId); widgetLayoutEntity.setLayoutName("layout name0"); widgetLayoutEntity.setSectionName("section0"); + widgetLayoutEntity.setUserName("username"); + widgetLayoutEntity.setScope("CLUSTER"); + widgetLayoutEntity.setDisplayName("displ_name"); WidgetLayoutEntity widgetLayoutEntity2 = new WidgetLayoutEntity(); widgetLayoutEntity2.setClusterId(clusterId); widgetLayoutEntity2.setLayoutName("layout name1"); widgetLayoutEntity2.setSectionName("section1"); + widgetLayoutEntity2.setUserName("username"); + widgetLayoutEntity2.setScope("CLUSTER"); + widgetLayoutEntity2.setDisplayName("displ_name2"); List<WidgetLayoutUserWidgetEntity> widgetLayoutUserWidgetEntityList = new LinkedList<WidgetLayoutUserWidgetEntity>(); for (int i=0; i<3; i++) { - UserWidgetEntity userWidgetEntity = new UserWidgetEntity(); - userWidgetEntity.setDisplayName("display name" + i); - userWidgetEntity.setAuthor("author"); - userWidgetEntity.setClusterId(clusterId); - userWidgetEntity.setMetrics("metrics"); - userWidgetEntity.setDescription("description"); - userWidgetEntity.setProperties("{\"warning_threshold\": 0.5,\"error_threshold\": 0.7 }"); - userWidgetEntity.setScope("CLUSTER"); - userWidgetEntity.setUserWidgetName("widget" + i); - userWidgetEntity.setUserWidgetType("GAUGE"); - userWidgetEntity.setWidgetValues("${`jvmMemoryHeapUsed + jvmMemoryHeapMax`}"); + WidgetEntity widgetEntity = new WidgetEntity(); + widgetEntity.setDisplayName("display name" + i); + widgetEntity.setAuthor("author"); + widgetEntity.setClusterId(clusterId); + widgetEntity.setMetrics("metrics"); + widgetEntity.setDescription("description"); + widgetEntity.setProperties("{\"warning_threshold\": 0.5,\"error_threshold\": 0.7 }"); + widgetEntity.setScope("CLUSTER"); + widgetEntity.setWidgetName("widget" + i); + widgetEntity.setWidgetType("GAUGE"); + widgetEntity.setWidgetValues("${`jvmMemoryHeapUsed + jvmMemoryHeapMax`}"); + + widgetDAO.create(widgetEntity); WidgetLayoutUserWidgetEntity widgetLayoutUserWidget = new WidgetLayoutUserWidgetEntity(); - widgetLayoutUserWidget.setUserWidget(userWidgetEntity); + widgetLayoutUserWidget.setWidget(widgetEntity); widgetLayoutUserWidget.setWidgetLayout(widgetLayoutEntity); widgetLayoutUserWidget.setWidgetOrder(0); widgetLayoutUserWidgetEntityList.add(widgetLayoutUserWidget); http://git-wip-us.apache.org/repos/asf/ambari/blob/4b3caee7/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog210Test.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog210Test.java b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog210Test.java index 513e98a..9d068f8 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog210Test.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog210Test.java @@ -268,11 +268,11 @@ public class UpgradeCatalog210Test { // Verify widget tables assertEquals(12, userWidgetColumnsCapture.getValue().size()); - assertEquals(4, widgetLayoutColumnsCapture.getValue().size()); + assertEquals(7, widgetLayoutColumnsCapture.getValue().size()); assertEquals(3, widgetLayoutUserWidgetColumnsCapture.getValue().size()); } } - + /** * Verify view changes */ @@ -289,7 +289,7 @@ public class UpgradeCatalog210Test { captures.put("viewinstance", viewInstanceColumnCapture); captures.put("viewparameter", viewParamColumnCapture); } - + /** * {@inheritDoc} */ @@ -301,7 +301,7 @@ public class UpgradeCatalog210Test { dbAccessor.addColumn(eq("viewinstance"), capture(viewInstanceColumnCapture)); dbAccessor.addColumn(eq("viewparameter"), capture(viewParamColumnCapture)); } - + /** * {@inheritDoc} */ @@ -310,7 +310,7 @@ public class UpgradeCatalog210Test { verifyViewInstance(captures.get("viewinstance")); verifyViewParameter(captures.get("viewparameter")); } - + private void verifyViewInstance(Capture<DBAccessor.DBColumnInfo> viewInstanceColumnCapture) { DBColumnInfo clusterIdColumn = viewInstanceColumnCapture.getValue(); Assert.assertEquals(String.class, clusterIdColumn.getType());