Commit 66c4f954 by lepdou

unit test

parent d13244c8
......@@ -71,7 +71,7 @@ public class NamespaceLockAspect {
acquireLock(item.getNamespaceId(), operator);
}
private void acquireLock(String appId, String clusterName, String namespaceName,
void acquireLock(String appId, String clusterName, String namespaceName,
String currentUser) {
if (apolloSwitcher.isNamespaceLockSwitchOff()) {
return;
......@@ -82,7 +82,7 @@ public class NamespaceLockAspect {
acquireLock(namespace, currentUser);
}
private void acquireLock(long namespaceId, String currentUser) {
void acquireLock(long namespaceId, String currentUser) {
Namespace namespace = namespaceService.findOne(namespaceId);
acquireLock(namespace, currentUser);
......
......@@ -8,7 +8,6 @@ import com.ctrip.framework.apollo.biz.utils.ApolloSwitcher;
import com.ctrip.framework.apollo.common.utils.BeanUtils;
import com.ctrip.framework.apollo.core.dto.NamespaceLockDTO;
import com.ctrip.framework.apollo.core.exception.BadRequestException;
import com.ctrip.framework.apollo.core.exception.NotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
......
......@@ -4,6 +4,7 @@ import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import com.ctrip.framework.apollo.adminservice.aop.NamespaceLockTest;
import com.ctrip.framework.apollo.adminservice.controller.AppControllerTest;
import com.ctrip.framework.apollo.adminservice.controller.ControllerExceptionTest;
import com.ctrip.framework.apollo.adminservice.controller.ControllerIntegrationExceptionTest;
......@@ -12,7 +13,7 @@ import com.ctrip.framework.apollo.adminservice.controller.ReleaseControllerTest;
@RunWith(Suite.class)
@SuiteClasses({AppControllerTest.class, ReleaseControllerTest.class, ItemSetControllerTest.class,
ControllerExceptionTest.class, ControllerIntegrationExceptionTest.class})
ControllerExceptionTest.class, ControllerIntegrationExceptionTest.class, NamespaceLockTest.class})
public class AllTests {
}
package com.ctrip.framework.apollo.adminservice.aop;
import com.ctrip.framework.apollo.biz.entity.Namespace;
import com.ctrip.framework.apollo.biz.entity.NamespaceLock;
import com.ctrip.framework.apollo.biz.service.ItemService;
import com.ctrip.framework.apollo.biz.service.NamespaceLockService;
import com.ctrip.framework.apollo.biz.service.NamespaceService;
import com.ctrip.framework.apollo.biz.utils.ApolloSwitcher;
import com.ctrip.framework.apollo.core.exception.BadRequestException;
import com.ctrip.framework.apollo.core.exception.ServiceException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.dao.DataIntegrityViolationException;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class NamespaceLockTest {
private static final String APP = "app-test";
private static final String CLUSTER = "cluster-test";
private static final String NAMESPACE = "namespace-test";
private static final String CURRENT_USER = "user-test";
private static final String ANOTHER_USER = "user-test2";
private static final long NAMESPACE_ID = 100;
@Mock
private NamespaceLockService namespaceLockService;
@Mock
private NamespaceService namespaceService;
@Mock
private ItemService itemService;
@Mock
private ApolloSwitcher apolloSwitcher;
@InjectMocks
NamespaceLockAspect namespaceLockAspect;
@Test
public void acquireLockWithNotLockedAndSwitchON() {
when(apolloSwitcher.isNamespaceLockSwitchOff()).thenReturn(true);
namespaceLockAspect.acquireLock(APP, CLUSTER, NAMESPACE, CURRENT_USER);
verify(namespaceService, times(0)).findOne(APP, CLUSTER, NAMESPACE);
}
@Test
public void acquireLockWithNotLockedAndSwitchOFF() {
when(apolloSwitcher.isNamespaceLockSwitchOff()).thenReturn(false);
when(namespaceService.findOne(APP, CLUSTER, NAMESPACE)).thenReturn(mockNamespace());
when(namespaceLockService.findLock(anyLong())).thenReturn(null);
namespaceLockAspect.acquireLock(APP, CLUSTER, NAMESPACE, CURRENT_USER);
verify(apolloSwitcher).isNamespaceLockSwitchOff();
verify(namespaceService).findOne(APP, CLUSTER, NAMESPACE);
verify(namespaceLockService).findLock(anyLong());
verify(namespaceLockService).tryLock(any());
}
@Test(expected = BadRequestException.class)
public void acquireLockWithAlreadyLockedByOtherGuy() {
when(apolloSwitcher.isNamespaceLockSwitchOff()).thenReturn(false);
when(namespaceService.findOne(APP, CLUSTER, NAMESPACE)).thenReturn(mockNamespace());
when(namespaceLockService.findLock(NAMESPACE_ID)).thenReturn(mockNamespaceLock(ANOTHER_USER));
namespaceLockAspect.acquireLock(APP, CLUSTER, NAMESPACE, CURRENT_USER);
verify(apolloSwitcher).isNamespaceLockSwitchOff();
verify(namespaceService).findOne(APP, CLUSTER, NAMESPACE);
verify(namespaceLockService).findLock(NAMESPACE_ID);
}
@Test
public void acquireLockWithAlreadyLockedBySelf() {
when(apolloSwitcher.isNamespaceLockSwitchOff()).thenReturn(false);
when(namespaceService.findOne(APP, CLUSTER, NAMESPACE)).thenReturn(mockNamespace());
when(namespaceLockService.findLock(NAMESPACE_ID)).thenReturn(mockNamespaceLock(CURRENT_USER));
namespaceLockAspect.acquireLock(APP, CLUSTER, NAMESPACE, CURRENT_USER);
verify(apolloSwitcher).isNamespaceLockSwitchOff();
verify(namespaceService).findOne(APP, CLUSTER, NAMESPACE);
verify(namespaceLockService).findLock(NAMESPACE_ID);
}
@Test
public void acquireLockWithNamespaceIdSwitchOn(){
when(apolloSwitcher.isNamespaceLockSwitchOff()).thenReturn(false);
when(namespaceService.findOne(NAMESPACE_ID)).thenReturn(mockNamespace());
when(namespaceLockService.findLock(NAMESPACE_ID)).thenReturn(null);
namespaceLockAspect.acquireLock(NAMESPACE_ID, CURRENT_USER);
verify(apolloSwitcher).isNamespaceLockSwitchOff();
verify(namespaceService).findOne(NAMESPACE_ID);
verify(namespaceLockService).findLock(NAMESPACE_ID);
verify(namespaceLockService).tryLock(any());
}
@Test(expected = ServiceException.class)
public void testDuplicateLock(){
when(apolloSwitcher.isNamespaceLockSwitchOff()).thenReturn(false);
when(namespaceService.findOne(NAMESPACE_ID)).thenReturn(mockNamespace());
when(namespaceLockService.findLock(NAMESPACE_ID)).thenReturn(null);
when(namespaceLockService.tryLock(any())).thenThrow(DataIntegrityViolationException.class);
namespaceLockAspect.acquireLock(NAMESPACE_ID, CURRENT_USER);
verify(apolloSwitcher).isNamespaceLockSwitchOff();
verify(namespaceService).findOne(NAMESPACE_ID);
verify(namespaceLockService, times(2)).findLock(NAMESPACE_ID);
verify(namespaceLockService).tryLock(any());
}
private Namespace mockNamespace() {
Namespace namespace = new Namespace();
namespace.setId(NAMESPACE_ID);
namespace.setAppId(APP);
namespace.setClusterName(CLUSTER);
namespace.setNamespaceName(NAMESPACE);
return namespace;
}
private NamespaceLock mockNamespaceLock(String locedUser) {
NamespaceLock lock = new NamespaceLock();
lock.setNamespaceId(NAMESPACE_ID);
lock.setDataChangeCreatedBy(locedUser);
return lock;
}
}
......@@ -3,4 +3,6 @@ DELETE FROM Namespace;
DELETE FROM AppNamespace;
DELETE FROM Cluster;
DELETE FROM App;
DELETE FROM NamespaceLock;
DELETE FROM ServerConfig;
......@@ -65,8 +65,7 @@ public class AppNamespaceService {
@Transactional
public AppNamespace createAppNamespaceInLocal(AppNamespace appNamespace) {
// unique check
if (appNamespace.isPublic() &&
appNamespaceRepository.findByNameAndIsPublic(appNamespace.getName(), true) != null) {
if (appNamespace.isPublic() && findPublicAppNamespace(appNamespace.getName()) != null) {
throw new BadRequestException(appNamespace.getName() + "已存在");
}
......
......@@ -102,13 +102,12 @@ public class RoleInitializationService {
private void createDefaultNamespaceRole(String appId, String namespaceName, String permissionType, String roleName) {
Permission
modifyDefaultNsPermission =
Permission permisson =
createPermisson(RoleUtils.buildNamespaceTargetId(appId, namespaceName), permissionType);
Permission createdModifyDefaultNsPermission = rolePermissionService.createPermission(modifyDefaultNsPermission);
Permission createdPermission = rolePermissionService.createPermission(permisson);
Role role = createRole(roleName);
rolePermissionService
.createRoleWithPermissions(role, Sets.newHashSet(createdModifyDefaultNsPermission.getId()));
.createRoleWithPermissions(role, Sets.newHashSet(createdPermission.getId()));
}
}
......@@ -99,9 +99,11 @@ application_module.controller("ConfigNamespaceController",
toastr.success("更新成功, 如需生效请发布");
//refresh all namespace items
$rootScope.refreshNamespaces();
return true;
}, function (result) {
toastr.error(AppUtil.errorMsg(result), "更新失败");
return false;
}
);
}
......
......@@ -119,10 +119,10 @@ directive_module.directive('apollonspanel',
}
function modifyByText(namespace) {
scope.commitChange(namespace);
namespace.commited = true;
toggleTextEditStatus(namespace);
if (scope.commitChange(namespace)){
namespace.commited = true;
toggleTextEditStatus(namespace);
}
}
//local method
......
......@@ -15,7 +15,7 @@ import javax.annotation.PostConstruct;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = PortalApplication.class)
@WebIntegrationTest(randomPort = true)
public abstract class AbstractPortalTest {
public abstract class AbstractIntegrationTest {
RestTemplate restTemplate = new TestRestTemplate("apollo", "");
......
package com.ctrip.framework.apollo.portal;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public abstract class AbstractUnitTest {
}
package com.ctrip.framework.apollo.portal;
import com.ctrip.framework.apollo.portal.service.AppNamespaceServiceTest;
import com.ctrip.framework.apollo.portal.service.ConfigServiceTest;
import com.ctrip.framework.apollo.portal.service.NamespaceServiceTest;
import com.ctrip.framework.apollo.portal.service.RoleInitializationServiceTest;
import com.ctrip.framework.apollo.portal.service.RolePermissionServiceTest;
import com.ctrip.framework.apollo.portal.service.txtresolver.FileTextResolverTest;
import com.ctrip.framework.apollo.portal.service.txtresolver.PropertyResolverTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
......@@ -10,7 +16,8 @@ import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({
ConfigServiceTest.class, PropertyResolverTest.class,
NamespaceServiceTest.class, ServiceExceptionTest.class, RolePermissionServiceTest.class
NamespaceServiceTest.class, ServiceExceptionTest.class, RolePermissionServiceTest.class,
AppNamespaceServiceTest.class, RoleInitializationServiceTest.class, FileTextResolverTest.class
})
public class AllTests {
......
......@@ -30,7 +30,7 @@ import com.ctrip.framework.apollo.portal.service.UserService;
import com.google.gson.Gson;
public class ServiceExceptionTest extends AbstractPortalTest {
public class ServiceExceptionTest extends AbstractIntegrationTest {
@Autowired
private AppController appController;
......
package com.ctrip.framework.apollo.portal.service;
import com.ctrip.framework.apollo.common.entity.AppNamespace;
import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.core.exception.BadRequestException;
import com.ctrip.framework.apollo.portal.AbstractIntegrationTest;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.jdbc.Sql;
import java.util.List;
public class AppNamespaceServiceTest extends AbstractIntegrationTest {
@Autowired
private AppNamespaceService appNamespaceService;
private final String APP = "app-test";
@Test
@Sql(scripts = "/sql/appnamespaceservice/init-appnamespace.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testFindPublicAppNamespace() {
List<AppNamespace> appNamespaceList = appNamespaceService.findPublicAppNamespaces();
Assert.assertNotNull(appNamespaceList);
Assert.assertEquals(5, appNamespaceList.size());
}
@Test
@Sql(scripts = "/sql/appnamespaceservice/init-appnamespace.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testFindPublicAppNamespaceByName() {
Assert.assertNotNull(appNamespaceService.findPublicAppNamespace("datasourcexml"));
Assert.assertNull(appNamespaceService.findPublicAppNamespace("TFF.song0711-02"));
}
@Test
@Sql(scripts = "/sql/appnamespaceservice/init-appnamespace.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testFindPublicAppNamespaceByAppAndName() {
Assert.assertNotNull(appNamespaceService.findByAppIdAndName("100003173", "datasourcexml"));
Assert.assertNull(appNamespaceService.findByAppIdAndName("100003173", "TFF.song0711-02"));
}
@Test
@Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testCreateDefaultAppNamespace() {
appNamespaceService.createDefaultAppNamespace(APP);
AppNamespace appNamespace = appNamespaceService.findByAppIdAndName(APP, ConfigConsts.NAMESPACE_APPLICATION);
Assert.assertNotNull(appNamespace);
Assert.assertEquals(ConfigFileFormat.Properties.getValue(), appNamespace.getFormat());
}
@Test(expected = BadRequestException.class)
@Sql(scripts = "/sql/appnamespaceservice/init-appnamespace.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testCreatePublicAppNamespaceExisted() {
AppNamespace appNamespace = assmbleBaseAppNamespace();
appNamespace.setPublic(true);
appNamespace.setName("FX.old");
appNamespaceService.createAppNamespaceInLocal(appNamespace);
}
@Test
@Sql(scripts = "/sql/appnamespaceservice/init-appnamespace.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testCreatePublicAppNamespaceNotExisted() {
AppNamespace appNamespace = assmbleBaseAppNamespace();
appNamespace.setPublic(true);
appNamespaceService.createAppNamespaceInLocal(appNamespace);
AppNamespace createdAppNamespace = appNamespaceService.findPublicAppNamespace(appNamespace.getName());
Assert.assertNotNull(createdAppNamespace);
Assert.assertEquals(appNamespace.getName(), createdAppNamespace.getName());
}
@Test(expected = BadRequestException.class)
@Sql(scripts = "/sql/appnamespaceservice/init-appnamespace.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testCreatePrivateAppNamespaceExisted() {
AppNamespace appNamespace = assmbleBaseAppNamespace();
appNamespace.setPublic(false);
appNamespace.setName("datasource.xml");
appNamespace.setAppId("100003173");
appNamespaceService.createAppNamespaceInLocal(appNamespace);
}
@Test
@Sql(scripts = "/sql/appnamespaceservice/init-appnamespace.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testCreatePrivateAppNamespaceNotExisted() {
AppNamespace appNamespace = assmbleBaseAppNamespace();
appNamespace.setPublic(false);
appNamespaceService.createAppNamespaceInLocal(appNamespace);
AppNamespace createdAppNamespace =
appNamespaceService.findByAppIdAndName(appNamespace.getAppId(), appNamespace.getName());
Assert.assertNotNull(createdAppNamespace);
Assert.assertEquals(appNamespace.getName(), createdAppNamespace.getName());
}
private AppNamespace assmbleBaseAppNamespace() {
AppNamespace appNamespace = new AppNamespace();
appNamespace.setName("appNamespace");
appNamespace.setAppId("1000");
appNamespace.setFormat(ConfigFileFormat.XML.getValue());
return appNamespace;
}
}
package com.ctrip.framework.apollo.portal;
package com.ctrip.framework.apollo.portal.service;
import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.core.dto.ItemChangeSets;
......
package com.ctrip.framework.apollo.portal;
package com.ctrip.framework.apollo.portal.service;
import com.ctrip.framework.apollo.common.entity.AppNamespace;
import com.ctrip.framework.apollo.core.dto.ItemDTO;
......
package com.ctrip.framework.apollo.portal.service;
import com.google.common.collect.Sets;
import com.ctrip.framework.apollo.common.entity.App;
import com.ctrip.framework.apollo.portal.AbstractUnitTest;
import com.ctrip.framework.apollo.portal.auth.UserInfoHolder;
import com.ctrip.framework.apollo.portal.constant.PermissionType;
import com.ctrip.framework.apollo.portal.entity.po.Permission;
import com.ctrip.framework.apollo.portal.entity.po.Role;
import com.ctrip.framework.apollo.portal.entity.po.UserInfo;
import com.ctrip.framework.apollo.portal.util.RoleUtils;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anySet;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class RoleInitializationServiceTest extends AbstractUnitTest {
private final String APP_ID = "1000";
private final String APP_NAME = "app-test";
private final String CLUSTER = "cluster-test";
private final String NAMESPACE = "namespace-test";
private final String CURRENT_USER = "user";
@Mock
private RolePermissionService rolePermissionService;
@Mock
private UserInfoHolder userInfoHolder;
@InjectMocks
private RoleInitializationService roleInitializationService;
@Test
public void testInitAppRoleHasInitBefore(){
when(rolePermissionService.findRoleByRoleName(anyString())).thenReturn(mockRole(RoleUtils.buildAppMasterRoleName(APP_ID)));
roleInitializationService.initAppRoles(mockApp());
verify(rolePermissionService, times(1)).findRoleByRoleName(RoleUtils.buildAppMasterRoleName(APP_ID));
verify(rolePermissionService, times(0)).assignRoleToUsers(anyString(), anySet(), anyString());
}
@Test
public void testInitAppRole(){
when(rolePermissionService.findRoleByRoleName(anyString())).thenReturn(null);
when(userInfoHolder.getUser()).thenReturn(mockUser());
when(rolePermissionService.createPermission(any())).thenReturn(mockPermission());
roleInitializationService.initAppRoles(mockApp());
verify(rolePermissionService, times(3)).findRoleByRoleName(anyString());
verify(rolePermissionService, times(1)).assignRoleToUsers(
RoleUtils.buildAppMasterRoleName(APP_ID), Sets.newHashSet(CURRENT_USER), CURRENT_USER);
verify(rolePermissionService, times(2)).createPermission(any());
verify(rolePermissionService, times(3)).createRoleWithPermissions(any(), anySet());
}
@Test
public void testInitNamespaceRoleHasExisted(){
String modifyNamespaceRoleName = RoleUtils.buildModifyNamespaceRoleName(APP_ID, NAMESPACE);
when(rolePermissionService.findRoleByRoleName(modifyNamespaceRoleName)).
thenReturn(mockRole(modifyNamespaceRoleName));
String releaseNamespaceRoleName = RoleUtils.buildReleaseNamespaceRoleName(APP_ID, NAMESPACE);
when(rolePermissionService.findRoleByRoleName(releaseNamespaceRoleName)).
thenReturn(mockRole(releaseNamespaceRoleName));
roleInitializationService.initNamespaceRoles(APP_ID, NAMESPACE);
verify(rolePermissionService, times(2)).findRoleByRoleName(anyString());
verify(rolePermissionService, times(0)).createPermission(any());
verify(rolePermissionService, times(0)).createRoleWithPermissions(any(), anySet());
}
@Test
public void testInitNamespaceRoleNotExisted(){
String modifyNamespaceRoleName = RoleUtils.buildModifyNamespaceRoleName(APP_ID, NAMESPACE);
when(rolePermissionService.findRoleByRoleName(modifyNamespaceRoleName)).
thenReturn(null);
String releaseNamespaceRoleName = RoleUtils.buildReleaseNamespaceRoleName(APP_ID, NAMESPACE);
when(rolePermissionService.findRoleByRoleName(releaseNamespaceRoleName)).
thenReturn(null);
when(userInfoHolder.getUser()).thenReturn(mockUser());
when(rolePermissionService.createPermission(any())).thenReturn(mockPermission());
roleInitializationService.initNamespaceRoles(APP_ID, NAMESPACE);
verify(rolePermissionService, times(2)).findRoleByRoleName(anyString());
verify(rolePermissionService, times(2)).createPermission(any());
verify(rolePermissionService, times(2)).createRoleWithPermissions(any(), anySet());
}
@Test
public void testInitNamespaceRoleModifyNSExisted(){
String modifyNamespaceRoleName = RoleUtils.buildModifyNamespaceRoleName(APP_ID, NAMESPACE);
when(rolePermissionService.findRoleByRoleName(modifyNamespaceRoleName)).
thenReturn(mockRole(modifyNamespaceRoleName));
String releaseNamespaceRoleName = RoleUtils.buildReleaseNamespaceRoleName(APP_ID, NAMESPACE);
when(rolePermissionService.findRoleByRoleName(releaseNamespaceRoleName)).
thenReturn(null);
when(userInfoHolder.getUser()).thenReturn(mockUser());
when(rolePermissionService.createPermission(any())).thenReturn(mockPermission());
roleInitializationService.initNamespaceRoles(APP_ID, NAMESPACE);
verify(rolePermissionService, times(2)).findRoleByRoleName(anyString());
verify(rolePermissionService, times(1)).createPermission(any());
verify(rolePermissionService, times(1)).createRoleWithPermissions(any(), anySet());
}
private App mockApp(){
App app = new App();
app.setAppId(APP_ID);
app.setName(APP_NAME);
app.setOrgName("xx");
app.setOrgId("1");
app.setOwnerName(CURRENT_USER);
app.setDataChangeCreatedBy(CURRENT_USER);
return app;
}
private Role mockRole(String roleName){
Role role = new Role();
role.setRoleName(roleName);
return role;
}
private UserInfo mockUser(){
UserInfo userInfo = new UserInfo();
userInfo.setUserId(CURRENT_USER);
return userInfo;
}
private Permission mockPermission(){
Permission permission = new Permission();
permission.setPermissionType(PermissionType.MODIFY_NAMESPACE);
permission.setTargetId(RoleUtils.buildNamespaceTargetId(APP_ID, NAMESPACE));
return permission;
}
}
......@@ -4,7 +4,7 @@ import com.google.common.collect.FluentIterable;
import com.google.common.collect.Sets;
import com.ctrip.framework.apollo.common.entity.BaseEntity;
import com.ctrip.framework.apollo.portal.AbstractPortalTest;
import com.ctrip.framework.apollo.portal.AbstractIntegrationTest;
import com.ctrip.framework.apollo.portal.entity.po.Permission;
import com.ctrip.framework.apollo.portal.entity.po.Role;
import com.ctrip.framework.apollo.portal.entity.po.RolePermission;
......@@ -30,7 +30,7 @@ import static org.junit.Assert.assertTrue;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public class RolePermissionServiceTest extends AbstractPortalTest {
public class RolePermissionServiceTest extends AbstractIntegrationTest {
@Autowired
private RolePermissionService rolePermissionService;
......@@ -73,7 +73,7 @@ public class RolePermissionServiceTest extends AbstractPortalTest {
}
@Test(expected = IllegalStateException.class)
@Sql(scripts = "/sql/insert-test-permissions.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/permission/insert-test-permissions.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testCreatePermissionWithPermissionExisted() throws Exception {
String someTargetId = "someTargetId";
......@@ -119,7 +119,7 @@ public class RolePermissionServiceTest extends AbstractPortalTest {
}
@Test(expected = IllegalStateException.class)
@Sql(scripts = "/sql/insert-test-permissions.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/permission/insert-test-permissions.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testCreatePermissionsWithPermissionsExisted() throws Exception {
String someTargetId = "someTargetId";
......@@ -134,7 +134,7 @@ public class RolePermissionServiceTest extends AbstractPortalTest {
}
@Test
@Sql(scripts = "/sql/insert-test-permissions.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/permission/insert-test-permissions.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testCreateRoleWithPermissions() throws Exception {
String someRoleName = "someRoleName";
......@@ -158,7 +158,7 @@ public class RolePermissionServiceTest extends AbstractPortalTest {
}
@Test(expected = IllegalStateException.class)
@Sql(scripts = "/sql/insert-test-roles.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/permission/insert-test-roles.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testCreateRoleWithPermissionsWithRoleExisted() throws Exception {
String someRoleName = "someRoleName";
......@@ -168,7 +168,7 @@ public class RolePermissionServiceTest extends AbstractPortalTest {
}
@Test
@Sql(scripts = "/sql/insert-test-roles.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/permission/insert-test-roles.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testAssignRoleToUsers() throws Exception {
String someRoleName = "someRoleName";
......@@ -208,8 +208,8 @@ public class RolePermissionServiceTest extends AbstractPortalTest {
}
@Test
@Sql(scripts = "/sql/insert-test-roles.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/insert-test-userroles.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/permission/insert-test-roles.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/permission/insert-test-userroles.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testAssignRoleToUsersWithUserRolesExisted() throws Exception {
String someRoleName = "someRoleName";
......@@ -236,8 +236,8 @@ public class RolePermissionServiceTest extends AbstractPortalTest {
}
@Test
@Sql(scripts = "/sql/insert-test-roles.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/insert-test-userroles.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/permission/insert-test-roles.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/permission/insert-test-userroles.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testRemoveRoleFromUsers() throws Exception {
String someRoleName = "someRoleName";
......@@ -257,7 +257,7 @@ public class RolePermissionServiceTest extends AbstractPortalTest {
}
@Test(expected = IllegalStateException.class)
@Sql(scripts = "/sql/insert-test-userroles.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/permission/insert-test-userroles.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testRemoveRoleFromUsersWithRoleNotExisted() throws Exception {
String someRoleName = "someRoleName";
......@@ -270,8 +270,8 @@ public class RolePermissionServiceTest extends AbstractPortalTest {
}
@Test
@Sql(scripts = "/sql/insert-test-roles.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/insert-test-userroles.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/permission/insert-test-roles.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/permission/insert-test-userroles.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testQueryUsersWithRole() throws Exception {
String someRoleName = "someRoleName";
......@@ -284,10 +284,10 @@ public class RolePermissionServiceTest extends AbstractPortalTest {
}
@Test
@Sql(scripts = "/sql/insert-test-roles.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/insert-test-permissions.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/insert-test-userroles.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/insert-test-rolepermissions.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/permission/insert-test-roles.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/permission/insert-test-permissions.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/permission/insert-test-userroles.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/permission/insert-test-rolepermissions.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testUserHasPermission() throws Exception {
String someTargetId = "someTargetId";
......
package com.ctrip.framework.apollo.portal.service.txtresolver;
import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.core.dto.ItemChangeSets;
import com.ctrip.framework.apollo.core.dto.ItemDTO;
import com.ctrip.framework.apollo.portal.AbstractIntegrationTest;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import java.util.Arrays;
import java.util.Collections;
public class FileTextResolverTest extends AbstractIntegrationTest {
@Qualifier("fileTextResolver")
@Autowired
private ConfigTextResolver resolver;
private final String CONFIG_TEXT = "config_text";
private final long NAMESPACE = 1000;
@Test
public void testCreateItem(){
ItemChangeSets changeSets = resolver.resolve(NAMESPACE, CONFIG_TEXT, Collections.emptyList());
Assert.assertEquals(1, changeSets.getCreateItems().size());
Assert.assertEquals(0, changeSets.getUpdateItems().size());
Assert.assertEquals(0, changeSets.getDeleteItems().size());
ItemDTO createdItem = changeSets.getCreateItems().get(0);
Assert.assertEquals(CONFIG_TEXT, createdItem.getValue());
}
@Test
public void testUpdateItem(){
ItemDTO existedItem = new ItemDTO();
existedItem.setId(1000);
existedItem.setKey(ConfigConsts.CONFIG_FILE_CONTENT_KEY);
existedItem.setValue("before");
ItemChangeSets changeSets = resolver.resolve(NAMESPACE, CONFIG_TEXT, Arrays.asList(existedItem));
Assert.assertEquals(0, changeSets.getCreateItems().size());
Assert.assertEquals(1, changeSets.getUpdateItems().size());
Assert.assertEquals(0, changeSets.getDeleteItems().size());
ItemDTO updatedItem = changeSets.getUpdateItems().get(0);
Assert.assertEquals(CONFIG_TEXT, updatedItem.getValue());
}
}
package com.ctrip.framework.apollo.portal;
package com.ctrip.framework.apollo.portal.service.txtresolver;
import com.ctrip.framework.apollo.core.dto.ItemChangeSets;
import com.ctrip.framework.apollo.core.dto.ItemDTO;
import com.ctrip.framework.apollo.core.exception.BadRequestException;
import com.ctrip.framework.apollo.portal.service.txtresolver.ConfigTextResolver;
import com.ctrip.framework.apollo.portal.AbstractIntegrationTest;
import org.junit.Assert;
import org.junit.Test;
......@@ -15,7 +15,7 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class PropertyResolverTest extends AbstractPortalTest {
public class PropertyResolverTest extends AbstractIntegrationTest {
@Autowired
@Qualifier("propertyResolver")
......
INSERT INTO `appnamespace` (`Id`, `Name`, `AppId`, `Format`, `IsPublic`, `Comment`, `IsDeleted`, `DataChange_CreatedBy`, `DataChange_CreatedTime`, `DataChange_LastModifiedBy`, `DataChange_LastTime`)
VALUES
(139, 'FX.old', '100003173', 'properties', 1, '', 0, 'zhanglea', '2016-07-11 10:00:58', 'zhanglea', '2016-07-11 10:00:58'),
(140, 'SCC.song0711-03', 'song0711-01', 'properties', 1, '', 0, 'song_s', '2016-07-11 10:04:09', 'song_s', '2016-07-11 10:04:09'),
(141, 'SCC.song0711-04', 'song0711-01', 'properties', 1, '', 0, 'song_s', '2016-07-11 10:06:29', 'song_s', '2016-07-11 10:06:29'),
(142, 'application', 'song0711-02', 'properties', 1, 'default app namespace', 0, 'song_s', '2016-07-11 11:18:24', 'song_s', '2016-07-11 11:18:24'),
(143, 'TFF.song0711-02', 'song0711-02', 'properties', 0, '', 0, 'song_s', '2016-07-11 11:15:11', 'song_s', '2016-07-11 11:15:11'),
(144, 'datasourcexml', '100003173', 'xml', 1, '', 0, 'apollo', '2016-07-11 12:08:29', 'apollo', '2016-07-11 12:08:29'),
(145, 'datasource.xml', '100003173', 'xml', 0, '', 0, 'apollo', '2016-07-11 12:09:30', 'apollo', '2016-07-11 12:09:30');
......@@ -2,3 +2,4 @@ delete from Permission;
delete from Role;
delete from RolePermission;
delete from UserRole;
delete from AppNamespace;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment