Skip to content
This repository has been archived by the owner on Mar 31, 2023. It is now read-only.

Restii #177

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
11 changes: 11 additions & 0 deletions services/api_gateway/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@
<version>0.1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We just merged new UTs to VPC and Route Controllers. Could you please add the docs accordingly. Thank you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok let me add it

Now there is a problem, most microservices mvn test need to connect to the database, I currently deploy the database can be executed, but it is best to be able to leave the database

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to have a flag which could switch off DB access when generating the docs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The document is generated after the unit test is completed. As long as the unit test does not depend on the database during execution, the document can be generated.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Please work with @chenpiaoping to see if we can support DB-related UTs by Ignite mock.

<groupId>org.springframework.restdocs</groupId>
<artifactId>spring-restdocs-webtestclient</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.restdocs</groupId>
<artifactId>spring-restdocs-asciidoctor</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.futurewei.alcor.apigateway;

import org.junit.Test;
import org.junit.Before;
import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -25,18 +26,35 @@
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;

import org.junit.Rule;
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs;
import org.springframework.restdocs.JUnitRestDocumentation;
import static org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation.document;
import static org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation.documentationConfiguration;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.assertj.core.api.Assertions.*;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
properties = {"httpbin=http://localhost:${wiremock.server.port}"})
@AutoConfigureWireMock(port = 0)
@AutoConfigureRestDocs(outputDir = "target/generated-snippets")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If most of the 126 files on the other PR could be found in the target/generated-snippets directory, that would be great. This means that we could safely remove those files in the /docs/apis/ directory as we could always find the latest by running "mvn test". right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick question: are we able to find the html files like alcor/docs/apis/mac-manager/swagger/html/index.html in the target/generated-snippets directory?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Yes, we can generate those files under the target / generated-snippets directory by executing mvn test

  • The code for the swagger part has not been submitted. Currently, Swagger documentation can be viewed by visiting the /swagger-ui.html path after the microservice is started. Before, I used a separate html generated by a third-party tool. I tried to generate html automatically when I tried mvn test.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! How can we access the /swagger-ui.html if the microservice is not started?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found some ways to generate swagger html files during mvn test, I am now verifying

public class AlcorApiGatewayApplicationTest {

@Autowired
private WebTestClient webClient;

private WebTestClient client;

@Rule
public final JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation();

@Before
public void init(){
this.client = this.webClient.mutate().filter(documentationConfiguration(restDocumentation)).build();
}

@Test
public void contextLoads() throws Exception {
//Stubs
Expand All @@ -49,12 +67,13 @@ public void contextLoads() throws Exception {
// .withBody("no fallback")
// .withFixedDelay(3000)));

webClient
this.client
.get().uri("/get")
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.headers.Hello").isEqualTo("Alcor");
.jsonPath("$.headers.Hello").isEqualTo("Alcor")
.consumeWith(document("admin_get"));

// webClient
// .get().uri("/delay/3")
Expand Down
7 changes: 7 additions & 0 deletions services/mac_manager/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.springframework.restdocs</groupId>
<artifactId>spring-restdocs-mockmvc</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpHeaders;
Expand All @@ -38,10 +39,12 @@
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
@AutoConfigureRestDocs(outputDir = "target/generated-snippets")
public class MacControllerTest {
private static final ObjectMapper om = new ObjectMapper();
public MacState testMacState;
Expand Down Expand Up @@ -104,7 +107,8 @@ public String createMacRange(MacRange macRange) {
public void test_index() throws Exception {
this.mockMvc.perform(get("/start.html"))
.andDo(print())
.andExpect(status().isOk());
.andExpect(status().isOk())
.andDo(document("start"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we use "mac_range_post"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • This corresponds to the directory name of the generated document. I think the name and the request are the same for easy search. I can change it if necessary, but it cannot conflict with other requests, otherwise it will overwrite the file

}

@Test
Expand All @@ -118,7 +122,8 @@ public void test_createMacState() throws Exception {
.content(json)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON))
.andExpect(status().isCreated())
.andDo(print());
.andDo(print())
.andDo(document("macs_post"));
}

@Test
Expand All @@ -127,7 +132,8 @@ public void test_getMacStateByMacAddress() throws Exception {
String strTestMac = createMacState(macState);
this.mockMvc.perform(get("/macs/" + strTestMac))
.andDo(print())
.andExpect(status().isOk());
.andExpect(status().isOk())
.andDo(document("macs_get"));
}

@Test
Expand All @@ -136,7 +142,8 @@ public void test_releaseMacStateByMacAddress() throws Exception {
String strTestMac = createMacState(macState);
this.mockMvc.perform(delete("/macs/" + strTestMac))
.andDo(print())
.andExpect(status().isOk());
.andExpect(status().isOk())
.andDo(document("macs_delete"));
}

@Test
Expand All @@ -154,7 +161,8 @@ public void test_activateMacState() throws Exception {
.content(json)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(print());
.andDo(print())
.andDo(document("macs_activate"));
}

@Test
Expand All @@ -172,7 +180,8 @@ public void test_deactivateMacState() throws Exception {
.content(json)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(print());
.andDo(print())
.andDo(document("macs_deactive"));
}

@Test
Expand All @@ -181,7 +190,8 @@ public void test_getMacRangeByMacRangeId() throws Exception {
String strRangeId = createMacRange(macRange);
this.mockMvc.perform(get("/macs/ranges/" + strRangeId))
.andDo(print())
.andExpect(status().isOk());
.andExpect(status().isOk())
.andDo(document("macs_range_get"));
}

public void test_getAllMacRanges(@PathVariable String rangeid) throws Exception {
Expand Down
7 changes: 7 additions & 0 deletions services/private_ip_manager/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@
<artifactId>AlcorCommonLib</artifactId>
<version>0.1.0-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>org.springframework.restdocs</groupId>
<artifactId>spring-restdocs-mockmvc</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
Expand All @@ -36,9 +37,12 @@
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;

@SpringBootTest
@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
@AutoConfigureRestDocs(outputDir = "target/generated-snippets")
public class IpAddrControllerTest {
@Autowired
private MockMvc mockMvc;
Expand All @@ -53,27 +57,31 @@ public void createIpAddrRangeTest() throws Exception {
.content(ipAddrRangeJson)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON))
.andExpect(status().isCreated())
.andDo(document("ips_range_post"))
.andDo(print());
}

@Test
public void deleteIpAddrRange() throws Exception {
this.mockMvc.perform(delete("/ips/range/range1"))
.andDo(print())
.andDo(document("ips_range_delete"))
.andExpect(status().isOk());
}

@Test
public void getIpAddrRangeTest() throws Exception {
this.mockMvc.perform(get("/ips/range/range1"))
.andDo(print())
.andDo(document("ips_range_get"))
.andExpect(status().isOk());
}

@Test
public void listIpAddrRangeTest() throws Exception {
this.mockMvc.perform(get("/ips/range/"))
.andDo(print())
.andDo(document("ips_range_list"))
.andExpect(status().isOk());
}

Expand All @@ -87,6 +95,7 @@ public void allocateIpAddrTest() throws Exception {
.content(ipAddrRequestJson)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON))
.andExpect(status().isCreated())
.andDo(document("ips_post"))
.andDo(print());
}

Expand All @@ -111,6 +120,7 @@ public void allocateIpAddrBulkTest() throws Exception {
.content(ipAddrRequestBulkJson)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON))
.andExpect(status().isCreated())
.andDo(document("ips_bulk_post"))
.andDo(print());
}

Expand All @@ -124,6 +134,7 @@ public void activateIpAddrStateTest() throws Exception {
.content(ipAddrRequestJson)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(document("ips_activate"))
.andDo(print());
}

Expand All @@ -146,6 +157,7 @@ public void activateIpAddrStateBulkTest() throws Exception {
.content(ipAddrRequestBulkJson)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(document("ips_bulk_activate"))
.andDo(print());
}

Expand All @@ -159,6 +171,7 @@ public void deactivateIpAddrStateTest() throws Exception {
.content(ipAddrRequestJson)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(document("ips_deactivate"))
.andDo(print());
}

Expand All @@ -181,14 +194,16 @@ public void deactivateIpAddrStateBulkTest() throws Exception {
.content(ipAddrRequestBulkJson)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(document("ips_bulk_deactivate"))
.andDo(print());
}

@Test
public void releaseIpAddrTest() throws Exception {
this.mockMvc.perform(delete("/ips/4/range1/11.11.11.1"))
.andDo(print())
.andExpect(status().isOk());
.andExpect(status().isOk())
.andDo(document("ips_delete"));
}

@Test
Expand All @@ -210,20 +225,23 @@ public void releaseIpAddrBulkTest() throws Exception {
.content(ipAddrRequestBulkJson)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(document("ips_bulk_delete"))
.andDo(print());
}

@Test
public void getIpAddrTest() throws Exception {
this.mockMvc.perform(get("/ips/4/range1/11.11.11.1"))
.andDo(print())
.andExpect(status().isOk());
.andExpect(status().isOk())
.andDo(document("ips_get"));
}

@Test
public void listIpAddrTest() throws Exception {
this.mockMvc.perform(get("/ips/4/range1"))
.andDo(print())
.andExpect(status().isOk());
.andExpect(status().isOk())
.andDo(document("ips_list"));
}
}
5 changes: 5 additions & 0 deletions services/route_manager/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.restdocs</groupId>
<artifactId>spring-restdocs-mockmvc</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
Expand Down
Loading