提交 80bd483b 编写于 作者: Y Yiming Liu

Merge pull request #8 from ctripcorp/portal_update

Update portal sample test case
package com.ctrip.apollo.portal.web;
package com.ctrip.apollo.portal.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.ctrip.apollo.portal.domain.App;
import com.ctrip.apollo.portal.entity.App;
import com.ctrip.apollo.portal.exception.NotFoundException;
import com.ctrip.apollo.portal.service.AppService;
@RestController
......@@ -19,18 +22,27 @@ public class AppController {
@Autowired
private AppService appService;
@RequestMapping(value = "", method = RequestMethod.POST)
public App create(App app) {
@RequestMapping(value = "", method = RequestMethod.POST, consumes = {"application/json"})
public App create(@RequestBody App app) {
return appService.save(app);
}
@RequestMapping("/{appid}")
public App detail(@PathVariable String appId) {
return appService.detail(appId);
App app = appService.detail(appId);
if (app == null) {
throw new NotFoundException();
}
return app;
}
@RequestMapping("")
public Page<App> list(@PageableDefault(size = 50) Pageable pageable) {
return appService.list(pageable);
public List<App> list(Pageable pageable) {
Page<App> page = appService.list(pageable);
if (pageable.getPageNumber() > page.getTotalPages()) {
throw new NotFoundException();
}
return page.getContent();
}
}
package com.ctrip.apollo.portal.controller;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import static org.springframework.http.HttpStatus.NOT_FOUND;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.HttpMediaTypeException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import com.ctrip.apollo.portal.exception.NotFoundException;
@ControllerAdvice
public class GlobalDefaultExceptionHandler {
@ExceptionHandler(Exception.class)
public ResponseEntity<Map<String, Object>> exception(HttpServletRequest request, Exception ex) {
return handleError(request, INTERNAL_SERVER_ERROR, ex);
}
private ResponseEntity<Map<String, Object>> handleError(HttpServletRequest request,
HttpStatus status, Throwable ex) {
return handleError(request, status, ex, ex.getMessage());
}
private ResponseEntity<Map<String, Object>> handleError(HttpServletRequest request,
HttpStatus status, Throwable ex, String message) {
ex = resolveError(ex);
Map<String, Object> errorAttributes = new LinkedHashMap<>();
errorAttributes.put("status", status.value());
errorAttributes.put("message", message);
errorAttributes.put("timestamp",
LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
errorAttributes.put("exception", resolveError(ex).getClass().getName());
HttpHeaders headers = new HttpHeaders();
headers.setContentType(APPLICATION_JSON);
return new ResponseEntity<>(errorAttributes, headers, status);
}
@ExceptionHandler({HttpRequestMethodNotSupportedException.class, HttpMediaTypeException.class})
public ResponseEntity<Map<String, Object>> methodNotSupportedException(HttpServletRequest request,
ServletException ex) {
return handleError(request, BAD_REQUEST, ex);
}
@ExceptionHandler(NotFoundException.class)
@ResponseStatus(value = NOT_FOUND)
public void notFound(HttpServletRequest req, NotFoundException ex) {}
private Throwable resolveError(Throwable ex) {
while (ex instanceof ServletException && ex.getCause() != null) {
ex = ((ServletException) ex).getCause();
}
return ex;
}
}
package com.ctrip.apollo.portal.domain;
package com.ctrip.apollo.portal.entity;
import java.io.Serializable;
import java.util.Date;
......
package com.ctrip.apollo.portal.exception;
public class NotFoundException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 7611357629749481796L;
}
package com.ctrip.apollo.portal.domain;
package com.ctrip.apollo.portal.repository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
public interface AppRepository extends CrudRepository<App, String> {
import com.ctrip.apollo.portal.entity.App;
public interface AppRepository extends PagingAndSortingRepository<App, String> {
Page<App> findAll(Pageable pageable);
}
......@@ -7,8 +7,8 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import com.ctrip.apollo.portal.domain.App;
import com.ctrip.apollo.portal.domain.AppRepository;
import com.ctrip.apollo.portal.entity.App;
import com.ctrip.apollo.portal.repository.AppRepository;
@Service
public class AppService {
......@@ -24,6 +24,10 @@ public class AppService {
return appRepository.findAll(pageable);
}
public Iterable<App> list() {
return appRepository.findAll();
}
public App save(App app) {
app.setCreateTimestamp(new Date());
return appRepository.save(app);
......
spring.datasource.url = jdbc:h2:file:~/fxapolloportaldb;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.url = jdbc:h2:file:~/fxapolloportaldb
spring.datasource.username = sa
spring.datasource.password = sa
package com.ctrip.apollo.portal;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = PortalApplicationTestConfiguration.class)
public abstract class AbstractPortalTest {
}
package com.ctrip.apollo.portal.controller;
import java.net.URI;
import java.net.URISyntaxException;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.TestRestTemplate;
import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import com.ctrip.apollo.portal.AbstractPortalTest;
import com.ctrip.apollo.portal.entity.App;
import com.ctrip.apollo.portal.repository.AppRepository;
@WebIntegrationTest
public class AppControllerTest extends AbstractPortalTest {
RestTemplate restTemplate = new TestRestTemplate();
@Autowired
AppRepository appRepository;
@Test
public void testCreate() throws URISyntaxException {
App newApp = new App();
newApp.setId(String.valueOf(System.currentTimeMillis()));
newApp.setName("new app " + System.currentTimeMillis());
newApp.setOwner("owner " + System.currentTimeMillis());
URI uri = new URI("http://localhost:8080/apps");
App createdApp = restTemplate.postForObject(uri, newApp, App.class);
Assert.assertEquals(newApp.getId(), createdApp.getId());
Assert.assertNull(newApp.getCreateTimestamp());
Assert.assertNotNull(createdApp.getCreateTimestamp());
App foundApp = appRepository.findOne(newApp.getId());
Assert.assertEquals(newApp.getId(), foundApp.getId());
}
@Test
public void testList() throws URISyntaxException {
App newApp = new App();
newApp.setId(String.valueOf(System.currentTimeMillis()));
newApp.setName("new app " + System.currentTimeMillis());
newApp.setOwner("owner " + System.currentTimeMillis());
appRepository.save(newApp);
URI uri = new URI("http://localhost:8080/apps");
App[] apps = restTemplate.getForObject(uri, App[].class);
Assert.assertEquals(1, apps.length);
Assert.assertEquals(newApp.getId(), apps[0].getId());
}
@Test
public void testListOutOfRange() throws URISyntaxException {
App newApp = new App();
newApp.setId(String.valueOf(System.currentTimeMillis()));
newApp.setName("new app " + System.currentTimeMillis());
newApp.setOwner("owner " + System.currentTimeMillis());
appRepository.save(newApp);
URI uri = new URI("http://localhost:8080/apps?page=2");
ResponseEntity<App[]> entity = restTemplate.getForEntity(uri, App[].class);
Assert.assertEquals(HttpStatus.NOT_FOUND, entity.getStatusCode());
Assert.assertNull(entity.getBody());
}
}
package com.ctrip.apollo.portal.repository;
import java.util.Date;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.ctrip.apollo.portal.PortalApplicationTestConfiguration;
import com.ctrip.apollo.portal.domain.App;
import com.ctrip.apollo.portal.domain.AppRepository;
import com.ctrip.apollo.portal.AbstractPortalTest;
import com.ctrip.apollo.portal.entity.App;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = PortalApplicationTestConfiguration.class)
public class AppRepositoryTest {
public class AppRepositoryTest extends AbstractPortalTest{
@Autowired
AppRepository repository;
......@@ -28,7 +21,6 @@ public class AppRepositoryTest {
ramdomApp.setId(String.valueOf(System.currentTimeMillis()));
ramdomApp.setName("new app " + System.currentTimeMillis());
ramdomApp.setOwner("owner " + System.currentTimeMillis());
ramdomApp.setCreateTimestamp(new Date());
repository.save(ramdomApp);
Assert.assertEquals(1, repository.count());
......
spring.datasource.url = jdbc:h2:file:~/fxapolloportaldb;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.url = jdbc:h2:mem:fxapolloportaldb
spring.datasource.username = sa
spring.datasource.password = sa
spring.datasource.password =
spring.jpa.show-sql: true
\ No newline at end of file
......@@ -121,8 +121,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>${java.source}</source>
<target>${java.target}</target>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
......@@ -151,6 +151,11 @@
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<artifactId>maven-source-plugin</artifactId>
</plugin>
</plugins>
</build>
<profiles>
<profile>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册