ServiceConfigDAO { @RequiresSession public List<ServiceConfigEntity> getLastServiceConfigsForService(Long clusterId, String serviceName) { TypedQuery<ServiceConfigEntity> query = entityManagerProvider.get().createNamedQuery( "ServiceConfigEntity.findLatestServiceConfigsByService", ServiceConfigEntity.class); query.setParameter("clusterId", clusterId); query.setParameter("serviceName", serviceName); return daoUtils.selectList(query); }  @RequiresSession ServiceConfigEntity find(Long serviceConfigId); @RequiresSession ServiceConfigEntity findByServiceAndVersion(String serviceName, Long version); @RequiresSession List<ServiceConfigEntity> findByService(Long clusterId, String serviceName); @RequiresSession List<ServiceConfigEntity> getLastServiceConfigVersionsForGroups(Collection<Long> configGroupIds); @RequiresSession List<Long> getServiceConfigVersionsByConfig(Long clusterId, String configType, Long configVersion); @RequiresSession List<ServiceConfigEntity> getLastServiceConfigs(Long clusterId); @RequiresSession List<ServiceConfigEntity> getLastServiceConfigsForService(Long clusterId, String serviceName); @RequiresSession List<ServiceConfigEntity> getAllServiceConfigsForClusterAndStack(Long clusterId,
      StackId stackId); @RequiresSession List<ServiceConfigEntity> getLatestServiceConfigs(Long clusterId,
      StackId stackId); @RequiresSession ServiceConfigEntity getLastServiceConfig(Long clusterId, String serviceName); @RequiresSession ServiceConfigEntity findMaxVersion(Long clusterId, String serviceName); @RequiresSession List<ServiceConfigEntity> getServiceConfigs(Long clusterId); @RequiresSession List<ServiceConfigEntity> findAll(); @RequiresSession Long findNextServiceConfigVersion(long clusterId, String serviceName); @Transactional void removeHostFromServiceConfigs(final Long hostId); @Transactional void create(ServiceConfigEntity serviceConfigEntity); @Transactional ServiceConfigEntity merge(ServiceConfigEntity serviceConfigEntity); @Transactional void remove(ServiceConfigEntity serviceConfigEntity);  }
@Test public void testGetLastServiceConfigsForService() throws Exception { String serviceName = "HDFS"; Clusters clusters = injector.getInstance(Clusters.class); clusters.addCluster("c1", HDP_01); ConfigGroupEntity configGroupEntity1 = new ConfigGroupEntity(); ClusterEntity clusterEntity = clusterDAO.findByName("c1"); configGroupEntity1.setClusterEntity(clusterEntity); configGroupEntity1.setClusterId(clusterEntity.getClusterId()); configGroupEntity1.setGroupName("group1"); configGroupEntity1.setDescription("group1_desc"); configGroupEntity1.setTag("HDFS"); configGroupEntity1.setServiceName("HDFS"); configGroupDAO.create(configGroupEntity1); ConfigGroupEntity group1 = configGroupDAO.findByName("group1"); ConfigGroupEntity configGroupEntity2 = new ConfigGroupEntity(); configGroupEntity2.setClusterEntity(clusterEntity); configGroupEntity2.setClusterId(clusterEntity.getClusterId()); configGroupEntity2.setGroupName("group2"); configGroupEntity2.setDescription("group2_desc"); configGroupEntity2.setTag("HDFS"); configGroupEntity2.setServiceName("HDFS"); configGroupDAO.create(configGroupEntity2); ConfigGroupEntity group2 = configGroupDAO.findByName("group2"); createServiceConfig(serviceName, "admin", 1L, 1L, 1111L, null); createServiceConfig(serviceName, "admin", 2L, 2L, 1010L, null); createServiceConfigWithGroup(serviceName, "admin", 3L, 3L, 2222L, null, group1.getGroupId()); createServiceConfigWithGroup(serviceName, "admin", 5L, 5L, 3333L, null, group2.getGroupId()); createServiceConfigWithGroup(serviceName, "admin", 4L, 4L, 3330L, null, group2.getGroupId()); List<ServiceConfigEntity> serviceConfigEntities = serviceConfigDAO .getLastServiceConfigsForService(clusterDAO.findByName("c1").getClusterId(), serviceName); Assert.assertNotNull(serviceConfigEntities); Assert.assertEquals(3, serviceConfigEntities.size()); for (ServiceConfigEntity sce : serviceConfigEntities) { if (sce.getGroupId() != null && sce.getGroupId().equals(group2.getGroupId())) { Assert.assertEquals(sce.getVersion(), Long.valueOf(5L)); } } }