UpgradeCatalog242 extends AbstractUpgradeCatalog { @Transactional protected void createRoleAuthorizations() throws SQLException { addRoleAuthorization("CLUSTER.RUN_CUSTOM_COMMAND", "Perform custom cluster-level actions", Arrays.asList("AMBARI.ADMINISTRATOR:AMBARI", "CLUSTER.ADMINISTRATOR:CLUSTER")); addRoleAuthorization("AMBARI.RUN_CUSTOM_COMMAND", "Perform custom administrative actions", Collections.singletonList("AMBARI.ADMINISTRATOR:AMBARI")); } @Inject  UpgradeCatalog242(Injector injector); @Override String getTargetVersion(); @Override String getSourceVersion();  }
@Test public void testCreateRoleAuthorizations() throws AmbariException, SQLException { EasyMockSupport easyMockSupport = new EasyMockSupport(); ResourceTypeEntity ambariResourceTypeEntity = easyMockSupport.createMock(ResourceTypeEntity.class); ResourceTypeEntity clusterResourceTypeEntity = easyMockSupport.createMock(ResourceTypeEntity.class); PermissionEntity clusterAdministratorPermissionEntity = new PermissionEntity(); clusterAdministratorPermissionEntity.setId(1); PermissionEntity ambariAdministratorPermissionEntity = new PermissionEntity(); ambariAdministratorPermissionEntity.setId(2); PermissionDAO permissionDAO = easyMockSupport.createMock(PermissionDAO.class); expect(permissionDAO.findPermissionByNameAndType("AMBARI.ADMINISTRATOR", ambariResourceTypeEntity)) .andReturn(ambariAdministratorPermissionEntity) .times(2); expect(permissionDAO.findPermissionByNameAndType("CLUSTER.ADMINISTRATOR", clusterResourceTypeEntity)) .andReturn(clusterAdministratorPermissionEntity) .times(1); expect(permissionDAO.merge(ambariAdministratorPermissionEntity)) .andReturn(ambariAdministratorPermissionEntity) .times(2); expect(permissionDAO.merge(clusterAdministratorPermissionEntity)) .andReturn(clusterAdministratorPermissionEntity) .times(1); ResourceTypeDAO resourceTypeDAO = easyMockSupport.createMock(ResourceTypeDAO.class); expect(resourceTypeDAO.findByName("AMBARI")).andReturn(ambariResourceTypeEntity).times(2); expect(resourceTypeDAO.findByName("CLUSTER")).andReturn(clusterResourceTypeEntity).times(1); RoleAuthorizationDAO roleAuthorizationDAO = easyMockSupport.createMock(RoleAuthorizationDAO.class); expect(roleAuthorizationDAO.findById("CLUSTER.RUN_CUSTOM_COMMAND")).andReturn(null).times(1); expect(roleAuthorizationDAO.findById("AMBARI.RUN_CUSTOM_COMMAND")).andReturn(null).times(1); Capture<RoleAuthorizationEntity> captureClusterRunCustomCommandEntity = newCapture(); roleAuthorizationDAO.create(capture(captureClusterRunCustomCommandEntity)); expectLastCall().times(1); Capture<RoleAuthorizationEntity> captureAmbariRunCustomCommandEntity = newCapture(); roleAuthorizationDAO.create(capture(captureAmbariRunCustomCommandEntity)); expectLastCall().times(1); Injector injector = easyMockSupport.createNiceMock(Injector.class); expect(injector.getInstance(RoleAuthorizationDAO.class)).andReturn(roleAuthorizationDAO).atLeastOnce(); expect(injector.getInstance(PermissionDAO.class)).andReturn(permissionDAO).atLeastOnce(); expect(injector.getInstance(ResourceTypeDAO.class)).andReturn(resourceTypeDAO).atLeastOnce(); easyMockSupport.replayAll(); new UpgradeCatalog242(injector).createRoleAuthorizations(); easyMockSupport.verifyAll(); RoleAuthorizationEntity ambariRunCustomCommandEntity = captureAmbariRunCustomCommandEntity.getValue(); RoleAuthorizationEntity clusterRunCustomCommandEntity = captureClusterRunCustomCommandEntity.getValue(); Assert.assertEquals("AMBARI.RUN_CUSTOM_COMMAND", ambariRunCustomCommandEntity.getAuthorizationId()); Assert.assertEquals("Perform custom administrative actions", ambariRunCustomCommandEntity.getAuthorizationName()); Assert.assertEquals("CLUSTER.RUN_CUSTOM_COMMAND", clusterRunCustomCommandEntity.getAuthorizationId()); Assert.assertEquals("Perform custom cluster-level actions", clusterRunCustomCommandEntity.getAuthorizationName()); Assert.assertEquals(2, ambariAdministratorPermissionEntity.getAuthorizations().size()); Assert.assertTrue(ambariAdministratorPermissionEntity.getAuthorizations().contains(clusterRunCustomCommandEntity)); Assert.assertTrue(ambariAdministratorPermissionEntity.getAuthorizations().contains(ambariRunCustomCommandEntity)); Assert.assertEquals(1, clusterAdministratorPermissionEntity.getAuthorizations().size()); Assert.assertTrue(clusterAdministratorPermissionEntity.getAuthorizations().contains(clusterRunCustomCommandEntity)); }