1. 程式人生 > >A Microservice Architecture with Spring Boot and Spring Cloud(五)

A Microservice Architecture with Spring Boot and Spring Cloud(五)

測試 REST API

最後,我們將測試我們的REST API。

首先,一個簡單的設定:

private final String ROOT_URI = "http://localhost:8080";

private FormAuthConfig formConfig  = new FormAuthConfig("/login", "username", "password");

@Before public void setup() {    
    RestAssured.config = config().redirect(RedirectConfig.redirectConfig().followRedirects(false
)); }

接下來,讓我們得到所有books:

@Test 
public void whenGetAllBooks_thenSuccess() {    
    Response response = RestAssured.get(ROOT_URI + "/book-service/books");
    Assert.assertEquals(HttpStatus.OK.value(), response.getStatusCode());  
    Assert.assertNotNull(response.getBody()); 
}

然後,嘗試不登入來訪問受保護的資源:

@Test 
public void whenAccessProtectedResourceWithoutLogin_thenRedirectToLogin() {    
    Response response = RestAssured.get(ROOT_URI + "/book-service/books/1");
    Assert.assertEquals(HttpStatus.FOUND.value(), response.getStatusCode()); 
    Assert.assertEquals("http://localhost:8080/login",response.getHeader
("Location")); }

然後,登入並建立新的book物件:

@Test 
public void whenAddNewBook_thenSuccess() {    
    Book book = new Book("Baeldung", "How to spring cloud");    
    Response bookResponse = RestAssured.given().auth()
        .form("admin", "admin", formConfig).and()      
        .body(book)      
    Book result = bookResponse.as(Book.class);
    Assert.assertEquals(HttpStatus.OK.value(), bookResponse.getStatusCode()); 
    Assert.assertEquals(book.getAuthor(), result.getAuthor());   
}

然後在身份驗證後訪問受保護的資源:

@Test 
public void whenAccessProtectedResourceAfterLogin_thenSuccess() {    
    Response response = RestAssured.given().auth()      
        .form("user", "password", formConfig)      
        .get(ROOT_URI + "/book-service/books/1");
    Assert.assertEquals(HttpStatus.OK.value(), response.getStatusCode());    
 }

現在,我們將建立新的Rating物件:

@Test 
public void whenAddNewRating_thenSuccess() {    
    Rating rating = new Rating(1L, 4);    
    Response ratingResponse = RestAssured.given().auth()      
        .form("admin", "admin", formConfig).and()      
        .contentType(ContentType.JSON)      
        .body(rating)      
    Rating result = ratingResponse.as(Rating.class);
    Assert.assertEquals(HttpStatus.OK.value(), ratingResponse.getStatusCode());   
    Assert.assertEquals(rating.getBookId(), result.getBookId());    
}

現在嘗試訪問管理員保護的Rating資源:

@Test 
public void whenAccessAdminProtectedResource_thenForbidden() {    
    Response response = RestAssured.given().auth()      
        .form("user", "password", formConfig)      
        .get(ROOT_URI + "/rating-service/ratings");
}

通過使用admin登入訪問受保護的Rating

@Test 
public void whenAdminAccessProtectedResource_thenSuccess() {  
    Response response = RestAssured.given().auth()      
        .form("admin", "admin", formConfig)      
        .get(ROOT_URI + "/rating-service/ratings");
    Assert.assertEquals(HttpStatus.OK.value(), 
    response.getStatusCode()); 
    Assert.assertNotNull(response.getBody()); 
}

最後,以管理員身份訪問discovery資源:

@Test 
public void whenAdminAccessDiscoveryResource_thenSuccess() { 
    Response response = RestAssured.given().auth() 
        .form("admin", "admin", formConfig)      
        .get(ROOT_URI + "/discovery");  
    Assert.assertEquals(HttpStatus.OK.value(),response.getStatusCode()); }

執行

  • 首先,在您的HOME目錄中建立一個本地Git儲存庫application-config。
  • 新增配置屬性檔案。
  • 確保在執行服務之前提交本地Git儲存庫中的所有更改。
git add -A
git commit -m "the initial configuration"
  • 確保按以下順序執行模組:
  • 在埠8081執行配置服務
  • 接著,在埠8082執行發現服務
  • 之後,在埠埠8080執行閘道器服務
  • 最後,分別在埠8083和8084執行資源服務

結論

我們已經完成了 - 使用Spring Boot構建的全功能微服務架構Spring Cloud。
GitHub上提供了所有模組的原始碼。

END