Commit b722655c by lepdou

fix ut

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