Commit 3da14c8c by lepdou

update

parent cd4e4759
......@@ -24,6 +24,7 @@ public class AppControllerTest extends AbstractControllerTest {
}
@Test
@Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
public void testCheckIfAppIdUnique() {
AppDTO dto = generateSampleDTOData();
ResponseEntity<AppDTO> response =
......@@ -72,16 +73,12 @@ public class AppControllerTest extends AbstractControllerTest {
Assert.assertEquals(dto.getAppId(), savedApp.getAppId());
Assert.assertNotNull(savedApp.getDataChangeCreatedTime());
response = restTemplate.postForEntity(getBaseAppUrl(), dto, AppDTO.class);
AppDTO second = response.getBody();
Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
Assert.assertEquals(dto.getAppId(), second.getAppId());
Assert.assertEquals(first.getId(), second.getId());
try {
restTemplate.postForEntity(getBaseAppUrl(), dto, AppDTO.class);
}catch (HttpClientErrorException e){
Assert.assertEquals(HttpStatus.BAD_REQUEST, e.getStatusCode());
}
savedApp = appRepository.findOne(second.getId());
Assert.assertEquals(dto.getAppId(), savedApp.getAppId());
Assert.assertNotNull(savedApp.getDataChangeCreatedTime());
Assert.assertNotNull(savedApp.getDataChangeLastModifiedTime());
}
@Test
......@@ -115,27 +112,14 @@ public class AppControllerTest extends AbstractControllerTest {
Assert.assertNull(deletedApp);
}
@Test
@Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
public void testUpdate() {
AppDTO dto = generateSampleDTOData();
App app = BeanUtils.transfrom(App.class, dto);
app = appRepository.save(app);
dto.setName("newName");
restTemplate.postForObject(getBaseAppUrl(), dto, AppDTO.class);
App updatedApp = appRepository.findOne(app.getId());
Assert.assertEquals(dto.getName(), updatedApp.getName());
Assert.assertNotNull(updatedApp.getDataChangeLastModifiedTime());
}
private AppDTO generateSampleDTOData() {
AppDTO dto = new AppDTO();
dto.setAppId("someAppId");
dto.setName("someName");
dto.setOwnerName("someOwner");
dto.setOwnerEmail("someOwner@ctrip.com");
dto.setDataChangeCreatedBy("apollo");
dto.setDataChangeLastModifiedBy("apollo");
return dto;
}
}
......@@ -24,6 +24,7 @@ public class AppNamespaceControllerTest extends AbstractControllerTest{
dto.setAppId(appId);
dto.setName(name);
dto.setComment(comment);
dto.setDataChangeCreatedBy("apollo");
AppNamespaceDTO resultDto = restTemplate.postForEntity(
String.format("http://localhost:%d/apps/%s/appnamespaces", port, appId),dto, AppNamespaceDTO.class).getBody();
......
......@@ -82,7 +82,7 @@ public class ControllerExceptionTest {
when(adminService.createNewApp(any(App.class)))
.thenThrow(new ServiceException("create app failed"));
appController.createOrUpdate(dto);
appController.create(dto);
}
private AppDTO generateSampleDTOData() {
......
......@@ -37,7 +37,7 @@ public class AppNamespaceService {
return Objects.isNull(appNamespaceRepository.findByAppIdAndName(appId, namespaceName));
}
public AppNamespace findPublicByNamespaceName(String namespaceName) {
public AppNamespace findPublicNamespaceByName(String namespaceName) {
Preconditions.checkArgument(namespaceName != null, "Namespace must not be null");
return appNamespaceRepository.findByNameAndIsPublicTrue(namespaceName);
}
......
package com.ctrip.framework.apollo.biz.service;
import com.ctrip.framework.apollo.core.exception.BadRequestException;
import com.ctrip.framework.apollo.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Validator {
@Autowired
private ServerConfigService serverConfigService;
public boolean checkItemValueLength(String value){
int lengthLimit = Integer.valueOf(serverConfigService.getValue("item.value.length.limit", "65536"));
if (!StringUtils.isEmpty(value) && value.length() > lengthLimit){
throw new BadRequestException("value too long. length limit:" + lengthLimit);
}
return true;
}
}
......@@ -3,7 +3,6 @@ package com.ctrip.framework.apollo.common.entity;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import org.bouncycastle.util.Strings;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
......@@ -64,7 +63,7 @@ public class AppNamespace extends BaseEntity {
isPublic = aPublic;
}
public ConfigFileFormat getFormatAsEnum() {
public ConfigFileFormat formatAsEnum() {
return ConfigFileFormat.fromString(this.format);
}
......
......@@ -10,7 +10,7 @@ import java.util.regex.Pattern;
*/
public class InputValidator {
public static final String INVALID_CLUSTER_NAMESPACE_MESSAGE = "只允许输入数字,字母和符号 - _ .";
public static final String INVALID_NAMESPACE_NAMESPACE_MESSAGE = "不允许以.json|.yml|.yaml|.xml|.properties结尾";
public static final String INVALID_NAMESPACE_NAMESPACE_MESSAGE = "不允许以.json, .yml, .yaml, .xml, .properties结尾";
public static final String CLUSTER_NAMESPACE_VALIDATOR = "[0-9a-zA-z_.-]+";
public static final String APP_NAMESPACE_VALIDATOR = "[a-zA-z0-9._-]+(?<!\\.(json|yml|yaml|xml|properties))$";
private static final Pattern CLUSTER_NAMESPACE_PATTERN =
......
......@@ -127,7 +127,7 @@ public class ConfigController {
* @param dataCenter the datacenter
*/
private Release findPublicConfig(String applicationId, String clusterName, String namespace, String dataCenter) {
AppNamespace appNamespace = appNamespaceService.findPublicByNamespaceName(namespace);
AppNamespace appNamespace = appNamespaceService.findPublicNamespaceByName(namespace);
//check whether the namespace's appId equals to current one
if (Objects.isNull(appNamespace) || Objects.equals(applicationId, appNamespace.getAppId())) {
......
......@@ -131,7 +131,7 @@ public class NotificationController implements ReleaseMessageListener {
private Set<String> findPublicConfigWatchKey(String applicationId, String clusterName,
String namespace,
String dataCenter) {
AppNamespace appNamespace = appNamespaceService.findPublicByNamespaceName(namespace);
AppNamespace appNamespace = appNamespaceService.findPublicNamespaceByName(namespace);
//check whether the namespace's appId equals to current one
if (Objects.isNull(appNamespace) || Objects.equals(applicationId, appNamespace.getAppId())) {
......
......@@ -247,7 +247,7 @@ public class ConfigControllerTest {
when(configService.findRelease(someAppId, someClusterName, someAppOwnNamespaceName))
.thenReturn(someRelease);
when(appNamespaceService.findPublicByNamespaceName(someAppOwnNamespaceName))
when(appNamespaceService.findPublicNamespaceByName(someAppOwnNamespaceName))
.thenReturn(someAppOwnNamespace);
when(someRelease.getReleaseKey()).thenReturn(someServerSideReleaseKey);
when(namespaceUtil.filterNamespaceName(someAppOwnNamespaceName))
......@@ -276,7 +276,7 @@ public class ConfigControllerTest {
when(configService.findRelease(someAppId, someClusterName, somePublicNamespaceName))
.thenReturn(null);
when(appNamespaceService.findPublicByNamespaceName(somePublicNamespaceName))
when(appNamespaceService.findPublicNamespaceByName(somePublicNamespaceName))
.thenReturn(somePublicAppNamespace);
when(configService.findRelease(somePublicAppId, someDataCenter, somePublicNamespaceName))
.thenReturn(somePublicRelease);
......@@ -305,7 +305,7 @@ public class ConfigControllerTest {
when(configService.findRelease(someAppId, someClusterName, somePublicNamespaceName))
.thenReturn(null);
when(appNamespaceService.findPublicByNamespaceName(somePublicNamespaceName))
when(appNamespaceService.findPublicNamespaceByName(somePublicNamespaceName))
.thenReturn(somePublicAppNamespace);
when(configService.findRelease(somePublicAppId, someDataCenter, somePublicNamespaceName))
.thenReturn(somePublicRelease);
......@@ -335,7 +335,7 @@ public class ConfigControllerTest {
when(configService.findRelease(someAppId, someClusterName, somePublicNamespaceName))
.thenReturn(null);
when(appNamespaceService.findPublicByNamespaceName(somePublicNamespaceName))
when(appNamespaceService.findPublicNamespaceByName(somePublicNamespaceName))
.thenReturn(somePublicAppNamespace);
when(configService.findRelease(somePublicAppId, someDataCenter, somePublicNamespaceName))
.thenReturn(null);
......@@ -373,7 +373,7 @@ public class ConfigControllerTest {
when(configService.findRelease(someAppId, someClusterName, somePublicNamespaceName))
.thenReturn(someRelease);
when(someRelease.getReleaseKey()).thenReturn(someAppSideReleaseKey);
when(appNamespaceService.findPublicByNamespaceName(somePublicNamespaceName))
when(appNamespaceService.findPublicNamespaceByName(somePublicNamespaceName))
.thenReturn(somePublicAppNamespace);
when(configService.findRelease(somePublicAppId, someDataCenter, somePublicNamespaceName))
.thenReturn(somePublicRelease);
......
......@@ -221,7 +221,7 @@ public class NotificationControllerTest {
AppNamespace somePublicAppNamespace =
assmbleAppNamespace(somePublicAppId, somePublicNamespace);
when(appNamespaceService.findPublicByNamespaceName(somePublicNamespace))
when(appNamespaceService.findPublicNamespaceByName(somePublicNamespace))
.thenReturn(somePublicAppNamespace);
DeferredResult<ResponseEntity<ApolloConfigNotification>>
......@@ -258,7 +258,7 @@ public class NotificationControllerTest {
when(namespaceUtil.filterNamespaceName(somePublicNamespaceAsFile))
.thenReturn(somePublicNamespace);
when(appNamespaceService.findPublicByNamespaceName(somePublicNamespace))
when(appNamespaceService.findPublicNamespaceByName(somePublicNamespace))
.thenReturn(somePublicAppNamespace);
when(appNamespaceService.findOne(someAppId, somePublicNamespace)).thenReturn(null);
......@@ -302,7 +302,7 @@ public class NotificationControllerTest {
AppNamespace somePublicAppNamespace =
assmbleAppNamespace(somePublicAppId, somePublicNamespace);
when(appNamespaceService.findPublicByNamespaceName(somePublicNamespace))
when(appNamespaceService.findPublicNamespaceByName(somePublicNamespace))
.thenReturn(somePublicAppNamespace);
DeferredResult<ResponseEntity<ApolloConfigNotification>>
......@@ -349,7 +349,7 @@ public class NotificationControllerTest {
AppNamespace somePublicAppNamespace =
assmbleAppNamespace(somePublicAppId, somePublicNamespace);
when(appNamespaceService.findPublicByNamespaceName(somePublicNamespace))
when(appNamespaceService.findPublicNamespaceByName(somePublicNamespace))
.thenReturn(somePublicAppNamespace);
DeferredResult<ResponseEntity<ApolloConfigNotification>>
......
......@@ -5,5 +5,5 @@ public interface ConfigConsts {
String CLUSTER_NAME_DEFAULT = "default";
String CLUSTER_NAMESPACE_SEPARATOR = "+";
String APOLLO_CLUSTER_KEY = "apollo.cluster";
String FILE_NAMESPACE_KEY_NAME = "content";
String CONFIG_FILE_CONTENT_KEY = "content";
}
package com.ctrip.framework.apollo.core.dto;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
public class AppNamespaceDTO extends BaseDTO{
private long id;
......@@ -40,10 +38,6 @@ public class AppNamespaceDTO extends BaseDTO{
this.appId = appId;
}
public ConfigFileFormat getFormatAsEnum() {
return ConfigFileFormat.fromString(this.format);
}
public String getFormat() {
return format;
}
......
package com.ctrip.framework.apollo.core.enums;
import com.ctrip.framework.apollo.core.utils.StringUtils;
/**
* @author Jason Song(song_s@ctrip.com)
*/
......@@ -17,6 +19,9 @@ public enum ConfigFileFormat {
}
public static ConfigFileFormat fromString(String value){
if (StringUtils.isEmpty(value)){
throw new IllegalArgumentException("value can not be empty");
}
switch (value){
case "properties":
return Properties;
......
......@@ -91,7 +91,7 @@ public class NamespaceController {
//add app org id as prefix
App app = appService.load(appId);
if (appNamespace.getFormatAsEnum() == ConfigFileFormat.Properties) {
if (appNamespace.formatAsEnum() == ConfigFileFormat.Properties) {
appNamespace.setName(String.format("%s.%s", app.getOrgId(), appNamespace.getName()));
} else {
appNamespace.setName(String.format("%s.%s.%s", app.getOrgId(), appNamespace.getName(), appNamespace.getFormat()));
......
......@@ -14,8 +14,6 @@ public interface AppNamespaceRepository extends PagingAndSortingRepository<AppNa
AppNamespace findByNameAndIsPublic(String namespaceName, boolean isPublic);
List<AppNamespace> findByNameNot(String namespaceName);
List<AppNamespace> findByNameNotAndIsPublic(String namespaceName, boolean isPublic);
List<AppNamespace> findByIsPublicTrue();
}
package com.ctrip.framework.apollo.portal.service;
import com.ctrip.framework.apollo.common.entity.AppNamespace;
import com.ctrip.framework.apollo.common.utils.BeanUtils;
import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.core.exception.BadRequestException;
import com.ctrip.framework.apollo.core.exception.ServiceException;
......@@ -26,17 +25,16 @@ public class AppNamespaceService {
/**
* 公共的app ns,能被其它项目关联到的app ns
* @return
*/
public List<AppNamespace> findPublicAppNamespaces() {
return appNamespaceRepository.findByNameNotAndIsPublic(ConfigConsts.NAMESPACE_APPLICATION, true);
return appNamespaceRepository.findByIsPublicTrue();
}
public AppNamespace findPublicAppNamespace(String namespaceName){
public AppNamespace findPublicAppNamespace(String namespaceName) {
return appNamespaceRepository.findByNameAndIsPublic(namespaceName, true);
}
public AppNamespace findByAppIdAndName(String appId, String namespaceName){
public AppNamespace findByAppIdAndName(String appId, String namespaceName) {
return appNamespaceRepository.findByAppIdAndName(appId, namespaceName);
}
......@@ -64,18 +62,18 @@ public class AppNamespaceService {
@Transactional
public AppNamespace createAppNamespaceInLocal(AppNamespace appNamespace) {
//not unique
if (appNamespaceRepository.findByName(appNamespace.getName()) != null){
// unique check
if (appNamespace.isPublic() &&
appNamespaceRepository.findByNameAndIsPublic(appNamespace.getName(), true) != null) {
throw new BadRequestException(appNamespace.getName() + "已存在");
}
AppNamespace managedAppNamespace = appNamespaceRepository.findByAppIdAndName(appNamespace.getAppId(), appNamespace.getName());
//update
if (managedAppNamespace != null){
BeanUtils.copyEntityProperties(appNamespace, managedAppNamespace);
return appNamespaceRepository.save(managedAppNamespace);
}else {
return appNamespaceRepository.save(appNamespace);
if (!appNamespace.isPublic() &&
appNamespaceRepository.findByAppIdAndName(appNamespace.getAppId(), appNamespace.getName()) != null) {
throw new BadRequestException(appNamespace.getName() + "已存在");
}
return appNamespaceRepository.save(appNamespace);
}
}
......@@ -36,7 +36,7 @@ public class FileTextResolver implements ConfigTextResolver {
ItemDTO item = new ItemDTO();
item.setNamespaceId(namespaceId);
item.setValue(value);
item.setKey(ConfigConsts.FILE_NAMESPACE_KEY_NAME);
item.setKey(ConfigConsts.CONFIG_FILE_CONTENT_KEY);
return item;
}
}
......@@ -24,7 +24,7 @@
<div class="row">
<div class="col-md-6">新建Namespace</div>
<div class="col-md-6 text-right">
<button type="button" class="btn btn-info" ng-show="step == 2" ng-click="back()">返回到项目首页
<button type="button" class="btn btn-info" ng-click="back()">返回到项目首页
</button>
</div>
</div>
......
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