Commit b722655c authored by lepdou's avatar lepdou

fix ut

parent 04383980
package com.ctrip.framework.apollo.biz; package com.ctrip.framework.apollo.biz;
import com.ctrip.framework.apollo.common.ApolloCommonConfig;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@Configuration @Configuration
@EnableAutoConfiguration @EnableAutoConfiguration
@ComponentScan(basePackages = "com.ctrip.framework.apollo.biz") @ComponentScan(basePackageClasses = {ApolloCommonConfig.class, ApolloBizConfig.class})
public class BizTestConfiguration { public class BizTestConfiguration {
} }
...@@ -9,6 +9,7 @@ import org.springframework.test.annotation.Rollback; ...@@ -9,6 +9,7 @@ import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import com.ctrip.framework.apollo.biz.BizTestConfiguration; import com.ctrip.framework.apollo.biz.BizTestConfiguration;
import com.ctrip.framework.apollo.common.entity.App; import com.ctrip.framework.apollo.common.entity.App;
......
...@@ -60,6 +60,11 @@ public class AppController { ...@@ -60,6 +60,11 @@ public class AppController {
return response; return response;
} }
/**
* 创建App流程: 1.先在portal db中创建 2.再保存到各个环境的apollo db中
*
* 只要第一步成功,就算这次创建app是成功操作,如果某个环境的apollo db创建失败,可通过portal db中的app信息再次创建.
*/
@RequestMapping(value = "", method = RequestMethod.POST) @RequestMapping(value = "", method = RequestMethod.POST)
public ResponseEntity<Void> create(@RequestBody App app) { public ResponseEntity<Void> create(@RequestBody App app) {
......
...@@ -68,19 +68,6 @@ public class AppService { ...@@ -68,19 +68,6 @@ public class AppService {
return appAPI.loadApp(env, appId); return appAPI.loadApp(env, appId);
} }
/**
* 创建App流程: 1.先在portal db中创建 2.再保存到各个环境的apollo db中
*
* 只要第一步成功,就算这次创建app是成功操作,如果某个环境的apollo db创建失败,可通过portal db中的app信息再次创建.
*/
public void createApp(App app) {
enrichUserInfo(app);
App localApp = createOrUpdateAppInLocal(app);
publisher.publishEvent(new AppCreationEvent(localApp));
}
public void createApp(Env env, App app) { public void createApp(Env env, App app) {
enrichUserInfo(app); enrichUserInfo(app);
try { try {
...@@ -109,9 +96,6 @@ public class AppService { ...@@ -109,9 +96,6 @@ public class AppService {
return appRepository.save(managedApp); return appRepository.save(managedApp);
} else { } else {
App createdApp = appRepository.save(app); App createdApp = appRepository.save(app);
if (app.getName().equals("xx")){
throw new RuntimeException("xxxx");
}
namespaceService.createDefaultAppNamespace(appId); namespaceService.createDefaultAppNamespace(appId);
//role //role
roleInitializationService.initAppRoles(createdApp); roleInitializationService.initAppRoles(createdApp);
......
...@@ -3,7 +3,6 @@ package com.ctrip.framework.apollo.portal.util; ...@@ -3,7 +3,6 @@ package com.ctrip.framework.apollo.portal.util;
import com.ctrip.framework.apollo.core.exception.BadRequestException; import com.ctrip.framework.apollo.core.exception.BadRequestException;
import com.ctrip.framework.apollo.core.utils.StringUtils; import com.ctrip.framework.apollo.core.utils.StringUtils;
import com.sun.istack.internal.Nullable;
public class RequestPrecondition { public class RequestPrecondition {
...@@ -19,7 +18,7 @@ public class RequestPrecondition { ...@@ -19,7 +18,7 @@ public class RequestPrecondition {
checkArgument(valid, ILLEGAL_MODEL); checkArgument(valid, ILLEGAL_MODEL);
} }
public static void checkArgument(boolean expression, @Nullable Object errorMessage) { public static void checkArgument(boolean expression, Object errorMessage) {
if (!expression) { if (!expression) {
throw new BadRequestException(String.valueOf(errorMessage)); throw new BadRequestException(String.valueOf(errorMessage));
} }
......
...@@ -119,9 +119,10 @@ ...@@ -119,9 +119,10 @@
<div class="btn-group" role="group" aria-label="..."> <div class="btn-group" role="group" aria-label="...">
<button type="button" <button type="button"
class="btn btn-default btn-sm J_tableview_btn" class="btn btn-default btn-sm J_tableview_btn"
ng-show="namespace.hasReleasePermission || namespace.hasModifyPermission"
ng-disabled="namespace.isTextEditing" ng-disabled="namespace.isTextEditing"
ng-click="prepareReleaseNamespace(namespace)">发布 ng-click="prepareReleaseNamespace(namespace)">发布
<!--ng-show="namespace.hasReleasePermission"-->
</button> </button>
<button type="button" <button type="button"
class="btn btn-default btn-sm J_tableview_btn" disabled class="btn btn-default btn-sm J_tableview_btn" disabled
......
...@@ -21,11 +21,11 @@ import org.springframework.test.util.ReflectionTestUtils; ...@@ -21,11 +21,11 @@ import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.client.HttpServerErrorException; import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.HttpStatusCodeException; import org.springframework.web.client.HttpStatusCodeException;
import com.ctrip.framework.apollo.common.entity.App;
import com.ctrip.framework.apollo.common.utils.ExceptionUtils; import com.ctrip.framework.apollo.common.utils.ExceptionUtils;
import com.ctrip.framework.apollo.core.dto.AppDTO; import com.ctrip.framework.apollo.core.dto.AppDTO;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.core.exception.ServiceException; import com.ctrip.framework.apollo.core.exception.ServiceException;
import com.ctrip.framework.apollo.portal.api.AdminServiceAPI; import com.ctrip.framework.apollo.portal.controller.AppController;
import com.ctrip.framework.apollo.portal.service.AppService; import com.ctrip.framework.apollo.portal.service.AppService;
import com.google.gson.Gson; import com.google.gson.Gson;
...@@ -33,26 +33,25 @@ import com.google.gson.Gson; ...@@ -33,26 +33,25 @@ import com.google.gson.Gson;
public class ServiceExceptionTest extends AbstractPortalTest { public class ServiceExceptionTest extends AbstractPortalTest {
@Autowired @Autowired
private AppService appService; private AppController appController;
@Mock @Mock
private AdminServiceAPI.AppAPI appAPI; private AppService appService;
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
ReflectionTestUtils.setField(appService, "appAPI", appAPI); ReflectionTestUtils.setField(appController, "appService", appService);
} }
private String getBaseAppUrl() { private String getBaseAppUrl() {
return "http://localhost:" + port + "/apps/envs/ALL"; return "http://localhost:" + port + "/apps";
} }
@Test @Test
public void testAdminServiceException() { public void testAdminServiceException() {
Map<String, Object> errorAttributes = new LinkedHashMap<>(); Map<String, Object> errorAttributes = new LinkedHashMap<>();
errorAttributes.put("status", 500); errorAttributes.put("status", 500);
errorAttributes.put("message", "admin server error"); errorAttributes.put("message", "No available admin service");
errorAttributes.put("timestamp", errorAttributes.put("timestamp",
LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)); LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
errorAttributes.put("exception", ServiceException.class.getName()); errorAttributes.put("exception", ServiceException.class.getName());
...@@ -61,26 +60,27 @@ public class ServiceExceptionTest extends AbstractPortalTest { ...@@ -61,26 +60,27 @@ public class ServiceExceptionTest extends AbstractPortalTest {
HttpStatusCodeException adminException = HttpStatusCodeException adminException =
new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR, "admin server error", new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR, "admin server error",
new Gson().toJson(errorAttributes).getBytes(), Charset.defaultCharset()); new Gson().toJson(errorAttributes).getBytes(), Charset.defaultCharset());
when(appAPI.createApp(any(Env.class), any(AppDTO.class))).thenThrow(adminException);
AppDTO dto = generateSampleDTOData(); when(appService.createOrUpdateAppInLocal(any(App.class))).thenThrow(adminException);
App app = generateSampleApp();
try { try {
restTemplate.postForEntity(getBaseAppUrl(), dto, AppDTO.class); restTemplate.postForEntity(getBaseAppUrl(), app, AppDTO.class);
} catch (HttpStatusCodeException e) { } catch (HttpStatusCodeException e) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, String> attr = new Gson().fromJson(e.getResponseBodyAsString(), Map.class); Map<String, String> attr = new Gson().fromJson(e.getResponseBodyAsString(), Map.class);
System.out.println(ExceptionUtils.toString(e)); System.out.println(ExceptionUtils.toString(e));
Assert.assertEquals("admin server error", attr.get("message")); Assert.assertEquals("No available admin service", attr.get("message"));
Assert.assertEquals("8848", attr.get("errorCode")); Assert.assertEquals("8848", attr.get("errorCode"));
} }
} }
private AppDTO generateSampleDTOData() { private App generateSampleApp() {
AppDTO dto = new AppDTO(); App app = new App();
dto.setAppId("someAppId"); app.setAppId("someAppId");
dto.setName("someName"); app.setName("someName");
dto.setOwnerName("someOwner"); app.setOwnerName("someOwner");
dto.setOwnerEmail("someOwner@ctrip.com"); app.setOwnerEmail("someOwner@ctrip.com");
return dto; return app;
} }
} }
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