From 35966926907c017436a472acc3c92905caf03d52 Mon Sep 17 00:00:00 2001 From: skdong Date: Mon, 27 Apr 2020 20:55:05 +0800 Subject: [PATCH 1/7] Modify unit test to generate API documentation --- services/api_gateway/pom.xml | 11 +++ .../AlcorApiGatewayApplicationTest.java | 68 ------------------- services/mac_manager/pom.xml | 13 ++-- .../controller/MacControllerTest.java | 24 +++++-- services/private_ip_manager/pom.xml | 7 ++ .../controller/IpAddrControllerTest.java | 4 ++ services/subnet_manager/pom.xml | 12 ++-- .../alcor/subnet/SubnetControllerTests.java | 20 +++++- 8 files changed, 71 insertions(+), 88 deletions(-) delete mode 100644 services/api_gateway/src/test/com/futurewei/alcor/apigateway/AlcorApiGatewayApplicationTest.java diff --git a/services/api_gateway/pom.xml b/services/api_gateway/pom.xml index 8fe9b1d52..a79baaea8 100644 --- a/services/api_gateway/pom.xml +++ b/services/api_gateway/pom.xml @@ -87,6 +87,17 @@ 0.1.0-SNAPSHOT compile + + org.springframework.restdocs + spring-restdocs-webtestclient + test + + + org.springframework.restdocs + spring-restdocs-asciidoctor + test + + diff --git a/services/api_gateway/src/test/com/futurewei/alcor/apigateway/AlcorApiGatewayApplicationTest.java b/services/api_gateway/src/test/com/futurewei/alcor/apigateway/AlcorApiGatewayApplicationTest.java deleted file mode 100644 index 895defd8f..000000000 --- a/services/api_gateway/src/test/com/futurewei/alcor/apigateway/AlcorApiGatewayApplicationTest.java +++ /dev/null @@ -1,68 +0,0 @@ -/* -Copyright 2019 The Alcor Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package com.futurewei.alcor.apigateway; - -import org.junit.Test; -import org.junit.runner.RunWith; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock; -import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.test.web.reactive.server.WebTestClient; - -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) -public class AlcorApiGatewayApplicationTest { - - @Autowired - private WebTestClient webClient; - - @Test - public void contextLoads() throws Exception { - //Stubs - stubFor(get(urlEqualTo("/get")) - .willReturn(aResponse() - .withBody("{\"headers\":{\"Hello\":\"Alcor\"}}") - .withHeader("Content-Type", "application/json"))); -// stubFor(get(urlEqualTo("/delay/3")) -// .willReturn(aResponse() -// .withBody("no fallback") -// .withFixedDelay(3000))); - - webClient - .get().uri("/get") - .exchange() - .expectStatus().isOk() - .expectBody() - .jsonPath("$.headers.Hello").isEqualTo("Alcor"); - -// webClient -// .get().uri("/delay/3") -// .header("Host", "www.hystrix.com") -// .exchange() -// .expectStatus().isOk() -// .expectBody() -// .consumeWith( -// response -> assertThat(response.getResponseBody()).isEqualTo("fallback".getBytes())); - } -} \ No newline at end of file diff --git a/services/mac_manager/pom.xml b/services/mac_manager/pom.xml index becf7f0cb..dfd8592be 100644 --- a/services/mac_manager/pom.xml +++ b/services/mac_manager/pom.xml @@ -64,13 +64,14 @@ org.springframework.boot spring-boot-starter-test test - - - org.junit.vintage - junit-vintage-engine - - + + + org.springframework.restdocs + spring-restdocs-mockmvc + test + + com.futurewei.alcor.common AlcorCommonLib diff --git a/services/mac_manager/src/test/java/com/futurewei/alcor/macmanager/controller/MacControllerTest.java b/services/mac_manager/src/test/java/com/futurewei/alcor/macmanager/controller/MacControllerTest.java index e1e198b0d..07f908462 100644 --- a/services/mac_manager/src/test/java/com/futurewei/alcor/macmanager/controller/MacControllerTest.java +++ b/services/mac_manager/src/test/java/com/futurewei/alcor/macmanager/controller/MacControllerTest.java @@ -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; @@ -42,10 +43,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; @@ -108,7 +111,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")); } @Test @@ -122,7 +126,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 @@ -131,7 +136,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 @@ -140,7 +146,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 @@ -158,7 +165,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 @@ -176,7 +184,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 @@ -185,7 +194,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{ diff --git a/services/private_ip_manager/pom.xml b/services/private_ip_manager/pom.xml index b4ef6dcf8..9beda7d51 100644 --- a/services/private_ip_manager/pom.xml +++ b/services/private_ip_manager/pom.xml @@ -47,6 +47,13 @@ AlcorCommonLib 0.1.0-SNAPSHOT + + + org.springframework.restdocs + spring-restdocs-mockmvc + test + + diff --git a/services/private_ip_manager/src/test/java/com/futurewei/alcor/privateipmanager/controller/IpAddrControllerTest.java b/services/private_ip_manager/src/test/java/com/futurewei/alcor/privateipmanager/controller/IpAddrControllerTest.java index 4e075f026..b51e0c277 100644 --- a/services/private_ip_manager/src/test/java/com/futurewei/alcor/privateipmanager/controller/IpAddrControllerTest.java +++ b/services/private_ip_manager/src/test/java/com/futurewei/alcor/privateipmanager/controller/IpAddrControllerTest.java @@ -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; @@ -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; diff --git a/services/subnet_manager/pom.xml b/services/subnet_manager/pom.xml index b79de5c1c..623b61b81 100644 --- a/services/subnet_manager/pom.xml +++ b/services/subnet_manager/pom.xml @@ -60,12 +60,12 @@ org.springframework.boot spring-boot-starter-test test - - - org.junit.vintage - junit-vintage-engine - - + + + + org.springframework.restdocs + spring-restdocs-mockmvc + test diff --git a/services/subnet_manager/src/test/java/com/futurewei/alcor/subnet/SubnetControllerTests.java b/services/subnet_manager/src/test/java/com/futurewei/alcor/subnet/SubnetControllerTests.java index aa788c547..f6716fc7a 100644 --- a/services/subnet_manager/src/test/java/com/futurewei/alcor/subnet/SubnetControllerTests.java +++ b/services/subnet_manager/src/test/java/com/futurewei/alcor/subnet/SubnetControllerTests.java @@ -31,11 +31,14 @@ import java.util.HashMap; import java.util.Map; +import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs; +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = {"httpbin=http://localhost:${wiremock.server.port}"}) @AutoConfigureMockMvc +@AutoConfigureRestDocs(outputDir = "target/generated-snippets") public class SubnetControllerTests { @Autowired @@ -62,6 +65,7 @@ public void subnetGetById_canFindSubnet_pass () throws Exception { this.mockMvc.perform(get(getByIdUri)) .andDo(print()) .andExpect(status().isOk()) + .andDo(document("subnet_get_pass")) .andExpect(MockMvcResultMatchers.jsonPath("$.subnet.id").value(UnitTestConfig.subnetId)); } @@ -71,6 +75,7 @@ public void subnetGetById_canNotFindSubnet_notPass () throws Exception { String response = this.mockMvc.perform(get(getByIdUri)) .andDo(print()) .andExpect(status().isOk()) + .andDo(document("subnet_get_nopass")) .andReturn().getResponse().getContentAsString(); System.out.println("-----json returned = " + response); assertEquals("{\"subnet\":null}", response); @@ -104,6 +109,7 @@ public void createSubnetState_create_pass () throws Exception { .content(UnitTestConfig.resource)) .andDo(print()) .andExpect(status().is(201)) + .andDo(document("subnet_post_pass")) .andExpect(MockMvcResultMatchers.jsonPath("$.subnet.id").value(UnitTestConfig.subnetId)); } @@ -136,6 +142,7 @@ public void createSubnetState_canNotFindVpcState_notPass () throws Exception { this.mockMvc.perform(post(creatwUri).contentType(MediaType.APPLICATION_JSON) .content(UnitTestConfig.resource)) .andDo(print()) + .andDo(document("subnet_post_novpc")) .andExpect(status().is(201)) .andExpect(MockMvcResultMatchers.jsonPath("$.subnet.id").value(UnitTestConfig.subnetId)); }catch (Exception ex) { @@ -173,6 +180,7 @@ public void createSubnetState_canNotFindRoute_notPass () throws Exception { this.mockMvc.perform(post(creatwUri).contentType(MediaType.APPLICATION_JSON) .content(UnitTestConfig.resource)) .andDo(print()) + .andDo(document("subnet_post_noroute")) .andExpect(status().is(201)) .andExpect(MockMvcResultMatchers.jsonPath("$.subnet.id").value(UnitTestConfig.subnetId)); }catch (Exception ex) { @@ -210,6 +218,7 @@ public void createSubnetState_canNotFindMac_notPass () throws Exception { this.mockMvc.perform(post(creatwUri).contentType(MediaType.APPLICATION_JSON) .content(UnitTestConfig.resource)) .andDo(print()) + .andDo(document("subnet_get_nomac")) .andExpect(status().is(201)) .andExpect(MockMvcResultMatchers.jsonPath("$.subnet.id").value(UnitTestConfig.subnetId)); }catch (Exception ex) { @@ -249,6 +258,7 @@ public void createSubnetState_canNotFindIP_notPass () throws Exception { this.mockMvc.perform(post(creatwUri).contentType(MediaType.APPLICATION_JSON) .content(UnitTestConfig.resource)) .andDo(print()) + .andDo(document("subnet_get_noip")) .andExpect(status().is(201)) .andExpect(MockMvcResultMatchers.jsonPath("$.subnet.id").value(UnitTestConfig.subnetId)); }catch (Exception ex) { @@ -266,6 +276,7 @@ public void updateSubnetState_noUpdate_pass () throws Exception { this.mockMvc.perform(put(putUri).contentType(MediaType.APPLICATION_JSON) .content(UnitTestConfig.resource)) .andDo(print()) + .andDo(document("subnet_put_noupdate")) .andExpect(status().isOk()) .andExpect(MockMvcResultMatchers.jsonPath("$.subnet.id").value(UnitTestConfig.subnetId)); } @@ -282,6 +293,7 @@ public void updateSubnetState_update_pass () throws Exception { this.mockMvc.perform(put(putUri).contentType(MediaType.APPLICATION_JSON) .content(UnitTestConfig.updateResource)) .andDo(print()) + .andDo(document("subnet_puts")) .andExpect(status().isOk()) .andExpect(MockMvcResultMatchers.jsonPath("$.subnet.name").value(UnitTestConfig.updateName)); } @@ -293,6 +305,7 @@ public void updateSubnetState_canNotFindSubnet_notPass () throws Exception { try { this.mockMvc.perform(put(putUri).contentType(MediaType.APPLICATION_JSON).content(UnitTestConfig.resource)) .andDo(print()) + .andDo(document("subnet_put_nosubnet")) .andExpect(status().isOk()); }catch (Exception ex) { //System.out.println(ex.getMessage()); @@ -310,7 +323,9 @@ public void getSubnetStatesByProjectIdAndVpcId_getMap_pass () throws Exception { UnitTestConfig.name,UnitTestConfig.cidr); subnetStates.put("SubnetState", subnetState); Mockito.when(subnetDatabaseService.getAllSubnets()).thenReturn(subnetStates); - this.mockMvc.perform(get(getByProjectIdAndVpcIdUri)).andDo(print()) + this.mockMvc.perform(get(getByProjectIdAndVpcIdUri)) + .andDo(print()) + .andDo(document("subnet_get_project_vpc")) .andExpect(status().isOk()); } @@ -319,6 +334,7 @@ public void getSubnetStatesByProjectIdAndVpcId_getEmptyMap_pass () throws Except Map subnetStates = new HashMap<>(); Mockito.when(subnetDatabaseService.getAllSubnets()).thenReturn(subnetStates); this.mockMvc.perform(get(getByProjectIdAndVpcIdUri)).andDo(print()) + .andDo(document("subnet_get_project_vpc_empty")) .andExpect(status().isOk()); } @@ -331,6 +347,7 @@ public void deleteSubnetState_deleteWhenIdExist_pass () throws Exception { UnitTestConfig.name,UnitTestConfig.cidr)); this.mockMvc.perform(delete(deleteUri)) .andDo(print()) + .andDo(document("subnet_delete")) .andExpect(status().isOk()) .andExpect(MockMvcResultMatchers.jsonPath("$.id").value(UnitTestConfig.subnetId)); } @@ -341,6 +358,7 @@ public void deleteSubnetState_deleteWhenIdNotExist_pass () throws Exception { .thenReturn(null); String response = this.mockMvc.perform(delete(deleteUri)) .andDo(print()) + .andDo(document("subnet_delete_noexist")) .andExpect(status().isOk()) .andReturn().getResponse().getContentAsString(); System.out.println("-----json returned = " + response); From d40d5c4e5af9255c3b0d750849e2e2b05779d41e Mon Sep 17 00:00:00 2001 From: skdong Date: Mon, 27 Apr 2020 20:55:27 +0800 Subject: [PATCH 2/7] Modify unit test to generate API documentation --- .../AlcorApiGatewayApplicationTest.java | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 services/api_gateway/src/test/java/futurewei/alcor/apigateway/AlcorApiGatewayApplicationTest.java diff --git a/services/api_gateway/src/test/java/futurewei/alcor/apigateway/AlcorApiGatewayApplicationTest.java b/services/api_gateway/src/test/java/futurewei/alcor/apigateway/AlcorApiGatewayApplicationTest.java new file mode 100644 index 000000000..43593b555 --- /dev/null +++ b/services/api_gateway/src/test/java/futurewei/alcor/apigateway/AlcorApiGatewayApplicationTest.java @@ -0,0 +1,80 @@ +/* +Copyright 2019 The Alcor Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package com.futurewei.alcor.apigateway; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock; +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") +public class AlcorApiGatewayApplicationTest { + + @Autowired + private WebTestClient webClient; + + @Rule + public final JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation(); + + @Test + public void contextLoads() throws Exception { + //Stubs + stubFor(get(urlEqualTo("/get")) + .willReturn(aResponse() + .withBody("{\"headers\":{\"Hello\":\"Alcor\"}}") + .withHeader("Content-Type", "application/json"))); +// stubFor(get(urlEqualTo("/delay/3")) +// .willReturn(aResponse() +// .withBody("no fallback") +// .withFixedDelay(3000))); + + WebTestClient client = this.webClient.mutate().filter(documentationConfiguration(restDocumentation)).build(); + client + .get().uri("/get") + .exchange() + .expectStatus().isOk() + .expectBody() + .jsonPath("$.headers.Hello").isEqualTo("Alcor") + .consumeWith(document("admin_get")); + +// webClient +// .get().uri("/delay/3") +// .header("Host", "www.hystrix.com") +// .exchange() +// .expectStatus().isOk() +// .expectBody() +// .consumeWith( +// response -> assertThat(response.getResponseBody()).isEqualTo("fallback".getBytes())); + } +} \ No newline at end of file From 834eae0005007b8541576f52e83a21c4c4a52a53 Mon Sep 17 00:00:00 2001 From: skdong Date: Tue, 28 Apr 2020 15:40:11 +0800 Subject: [PATCH 3/7] add generate spring rest docs code for private ip --- .../AlcorApiGatewayApplicationTest.java | 11 ++++++++-- .../controller/IpAddrControllerTest.java | 20 ++++++++++++++++--- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/services/api_gateway/src/test/java/futurewei/alcor/apigateway/AlcorApiGatewayApplicationTest.java b/services/api_gateway/src/test/java/futurewei/alcor/apigateway/AlcorApiGatewayApplicationTest.java index 43593b555..6fa91fbd3 100644 --- a/services/api_gateway/src/test/java/futurewei/alcor/apigateway/AlcorApiGatewayApplicationTest.java +++ b/services/api_gateway/src/test/java/futurewei/alcor/apigateway/AlcorApiGatewayApplicationTest.java @@ -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; @@ -44,9 +45,16 @@ 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 @@ -59,8 +67,7 @@ public void contextLoads() throws Exception { // .withBody("no fallback") // .withFixedDelay(3000))); - WebTestClient client = this.webClient.mutate().filter(documentationConfiguration(restDocumentation)).build(); - client + this.client .get().uri("/get") .exchange() .expectStatus().isOk() diff --git a/services/private_ip_manager/src/test/java/com/futurewei/alcor/privateipmanager/controller/IpAddrControllerTest.java b/services/private_ip_manager/src/test/java/com/futurewei/alcor/privateipmanager/controller/IpAddrControllerTest.java index b51e0c277..8eef294f9 100644 --- a/services/private_ip_manager/src/test/java/com/futurewei/alcor/privateipmanager/controller/IpAddrControllerTest.java +++ b/services/private_ip_manager/src/test/java/com/futurewei/alcor/privateipmanager/controller/IpAddrControllerTest.java @@ -57,6 +57,7 @@ public void createIpAddrRangeTest() throws Exception { .content(ipAddrRangeJson) .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)) .andExpect(status().isCreated()) + .andDo(document("ips_range_post")) .andDo(print()); } @@ -64,6 +65,7 @@ public void createIpAddrRangeTest() throws Exception { public void deleteIpAddrRange() throws Exception { this.mockMvc.perform(delete("/ips/range/range1")) .andDo(print()) + .andDo(document("ips_range_delete")) .andExpect(status().isOk()); } @@ -71,6 +73,7 @@ public void deleteIpAddrRange() throws Exception { public void getIpAddrRangeTest() throws Exception { this.mockMvc.perform(get("/ips/range/range1")) .andDo(print()) + .andDo(document("ips_range_get")) .andExpect(status().isOk()); } @@ -78,6 +81,7 @@ public void getIpAddrRangeTest() throws Exception { public void listIpAddrRangeTest() throws Exception { this.mockMvc.perform(get("/ips/range/")) .andDo(print()) + .andDo(document("ips_range_list")) .andExpect(status().isOk()); } @@ -91,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()); } @@ -115,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()); } @@ -128,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()); } @@ -150,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()); } @@ -163,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()); } @@ -185,6 +194,7 @@ public void deactivateIpAddrStateBulkTest() throws Exception { .content(ipAddrRequestBulkJson) .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) + .andDo(document("ips_bulk_deactivate")) .andDo(print()); } @@ -192,7 +202,8 @@ public void deactivateIpAddrStateBulkTest() throws Exception { 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 @@ -214,6 +225,7 @@ public void releaseIpAddrBulkTest() throws Exception { .content(ipAddrRequestBulkJson) .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) + .andDo(document("ips_bulk_delete")) .andDo(print()); } @@ -221,13 +233,15 @@ public void releaseIpAddrBulkTest() throws Exception { 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")); } } From abcc3bb2cb21f2d84d3b0f0c3b2a98356dfbf6c0 Mon Sep 17 00:00:00 2001 From: skdong Date: Wed, 29 Apr 2020 10:20:53 +0800 Subject: [PATCH 4/7] add vpc,route spirng rest docs generate codes --- services/route_manager/pom.xml | 6 ++++++ .../alcor/route/RouteControllerTests.java | 15 +++++++++++++-- services/vpc_manager/pom.xml | 6 ++++++ .../alcor/vpcmanager/VpcControllerTests.java | 15 +++++++++++++++ 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/services/route_manager/pom.xml b/services/route_manager/pom.xml index d8649a291..ec509a631 100644 --- a/services/route_manager/pom.xml +++ b/services/route_manager/pom.xml @@ -67,6 +67,12 @@ + + org.springframework.restdocs + spring-restdocs-mockmvc + test + + com.futurewei.alcor.common AlcorCommonLib diff --git a/services/route_manager/src/test/java/com/futurewei/alcor/route/RouteControllerTests.java b/services/route_manager/src/test/java/com/futurewei/alcor/route/RouteControllerTests.java index 5a30a5de1..be69b0448 100644 --- a/services/route_manager/src/test/java/com/futurewei/alcor/route/RouteControllerTests.java +++ b/services/route_manager/src/test/java/com/futurewei/alcor/route/RouteControllerTests.java @@ -24,11 +24,14 @@ import java.io.IOException; +import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs; +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = {"httpbin=http://localhost:${wiremock.server.port}"}) @AutoConfigureMockMvc +@AutoConfigureRestDocs(outputDir = "target/generated-snippets") public class RouteControllerTests { @Autowired @@ -49,7 +52,8 @@ public void routeGetById_canFindRoute_pass () throws Exception { this.mockMvc.perform(get(getByIdUri)) .andDo(print()) .andExpect(status().isOk()) - .andExpect(MockMvcResultMatchers.jsonPath("$.route.id").value(UnitTestConfig.routeId)); + .andExpect(MockMvcResultMatchers.jsonPath("$.route.id").value(UnitTestConfig.routeId)) + .andDo(document("route_get_byid")); } @Test @@ -57,6 +61,7 @@ public void routeGetById_canNotFindRoute_notPass () throws Exception { Mockito.when(routeDatabaseService.getByRouteId(UnitTestConfig.routeId)).thenReturn(null); String response = this.mockMvc.perform(get(getByIdUri)) .andDo(print()) + .andDo(document("route_get_byid_notpass")) .andExpect(status().isOk()) .andReturn().getResponse().getContentAsString(); System.out.println("-----json returned = " + response); @@ -69,7 +74,8 @@ public void createVpcRoute_create_pass () throws Exception { .content(UnitTestConfig.vpcResource)) .andDo(print()) .andExpect(status().is(201)) - .andExpect(MockMvcResultMatchers.jsonPath("$.route.destination").value(UnitTestConfig.cidr)); + .andExpect(MockMvcResultMatchers.jsonPath("$.route.destination").value(UnitTestConfig.cidr)) + .andDo(document("vpc_route_post")); } @Test @@ -78,6 +84,7 @@ public void createVpcRoute_parameterNullOrEmpty_notPass () throws Exception { this.mockMvc.perform(post(createVpcUri).contentType(MediaType.APPLICATION_JSON) .content(UnitTestConfig.vpcResource)) .andDo(print()) + .andDo(document("vpc_route_post_notpass")) .andExpect(status().is(201)) .andExpect(MockMvcResultMatchers.jsonPath("$.route.destination").value(UnitTestConfig.cidr)); } catch (Exception e) { @@ -90,6 +97,7 @@ public void createSubnetRoute_create_pass () throws Exception { this.mockMvc.perform(post(createSubnetUri).contentType(MediaType.APPLICATION_JSON) .content(UnitTestConfig.resource)) .andDo(print()) + .andDo(document("subnet_route_post")) .andExpect(status().is(201)) .andExpect(MockMvcResultMatchers.jsonPath("$.route.destination").value(UnitTestConfig.cidr)); } @@ -100,6 +108,7 @@ public void createSubnetRoute_parameterNullOrEmpty_notPass () throws Exception { this.mockMvc.perform(post(createSubnetUri).contentType(MediaType.APPLICATION_JSON) .content(UnitTestConfig.resource)) .andDo(print()) + .andDo(document("subnet_route_post_paramerror")) .andExpect(status().is(201)) .andExpect(MockMvcResultMatchers.jsonPath("$.route.destination").value(UnitTestConfig.cidr)); } catch (Exception e) { @@ -113,6 +122,7 @@ public void deleteRuleById_deleteWhenIdExist_pass () throws Exception { .thenReturn(new RouteState(){{setId(UnitTestConfig.routeId);}}); this.mockMvc.perform(delete(deleteUri)) .andDo(print()) + .andDo(document("rule_delete")) .andExpect(status().isOk()) .andExpect(MockMvcResultMatchers.jsonPath("$.id").value(UnitTestConfig.routeId)); } @@ -123,6 +133,7 @@ public void deleteRuleById_deleteWhenIdNotExist_notPass () throws Exception { .thenReturn(null); String response = this.mockMvc.perform(delete(deleteUri)) .andDo(print()) + .andDo(document("rule_delete_notexist")) .andExpect(status().isOk()) .andReturn().getResponse().getContentAsString(); assertEquals("{\"id\":null}", response); diff --git a/services/vpc_manager/pom.xml b/services/vpc_manager/pom.xml index 7db9dd9c5..7722bc960 100644 --- a/services/vpc_manager/pom.xml +++ b/services/vpc_manager/pom.xml @@ -69,6 +69,12 @@ + + org.springframework.restdocs + spring-restdocs-mockmvc + test + + com.futurewei.alcor.common AlcorCommonLib diff --git a/services/vpc_manager/src/test/java/com/futurewei/alcor/vpcmanager/VpcControllerTests.java b/services/vpc_manager/src/test/java/com/futurewei/alcor/vpcmanager/VpcControllerTests.java index 8e64a08ab..a74a2c455 100644 --- a/services/vpc_manager/src/test/java/com/futurewei/alcor/vpcmanager/VpcControllerTests.java +++ b/services/vpc_manager/src/test/java/com/futurewei/alcor/vpcmanager/VpcControllerTests.java @@ -34,10 +34,15 @@ import java.util.List; import java.util.Map; + +import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs; +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; + @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = {"httpbin=http://localhost:${wiremock.server.port}"}) @AutoConfigureMockMvc +@AutoConfigureRestDocs(outputDir = "target/generated-snippets") public class VpcControllerTests { @Autowired @@ -63,6 +68,7 @@ public void vpcGetById_canFindVpc_pass () throws Exception { UnitTestConfig.cidr, null)); this.mockMvc.perform(get(getByIdUri)) .andDo(print()) + .andDo(document("vpc_get_byid")) .andExpect(status().isOk()) .andExpect(MockMvcResultMatchers.jsonPath("$.vpc.id").value(UnitTestConfig.vpcId)); } @@ -72,6 +78,7 @@ public void vpcGetById_canNotFindVpc_notPass () throws Exception { Mockito.when(vpcDatabaseService.getByVpcId(UnitTestConfig.vpcId)).thenReturn(null); String response = this.mockMvc.perform(get(getByIdUri)) .andDo(print()) + .andDo(document("vpc_get_nofind")) .andExpect(status().isOk()) .andReturn().getResponse().getContentAsString(); System.out.println("-----json returned = " + response); @@ -89,6 +96,7 @@ public void createVpcState_create_pass () throws Exception { .thenReturn(routeWebJson); this.mockMvc.perform(post(createUri).contentType(MediaType.APPLICATION_JSON).content(UnitTestConfig.vpcResource)) .andDo(print()) + .andDo(document("vpcstate_post")) .andExpect(status().is(201)) .andExpect(MockMvcResultMatchers.jsonPath("$.vpc.id").value(UnitTestConfig.vpcId)); } @@ -110,6 +118,7 @@ public void createVpcState_canNotFindRoute_notPass () throws Exception { try { this.mockMvc.perform(post(createUri).contentType(MediaType.APPLICATION_JSON).content(UnitTestConfig.vpcResource)) .andDo(print()) + .andDo(document("vpcstate_post_noroute")) .andExpect(status().is(201)) .andExpect(MockMvcResultMatchers.jsonPath("$.vpc.routes[0].destination").value(UnitTestConfig.cidr)); } catch (Exception e) { @@ -125,6 +134,7 @@ public void updateVpcStateByVpcId_noUpdate_pass () throws Exception { UnitTestConfig.cidr, null)); this.mockMvc.perform(put(updateUri).contentType(MediaType.APPLICATION_JSON).content(UnitTestConfig.vpcResource)) .andDo(print()) + .andDo(document("vpcstate_update_no")) .andExpect(status().isOk()) .andExpect(MockMvcResultMatchers.jsonPath("$.vpc.id").value(UnitTestConfig.vpcId)); } @@ -140,6 +150,7 @@ public void updateVpcStateByVpcId_update_pass () throws Exception { UnitTestConfig.cidr, null)); this.mockMvc.perform(put(updateUri).contentType(MediaType.APPLICATION_JSON).content(UnitTestConfig.vpcResource)) .andDo(print()) + .andDo(document("vpcstate_update")) .andExpect(status().isOk()) .andExpect(MockMvcResultMatchers.jsonPath("$.vpc.name").value(UnitTestConfig.updateName)); } @@ -152,6 +163,7 @@ public void deleteVpcStateByVpcId_deleteWhenIdExist_pass () throws Exception { UnitTestConfig.cidr, null)); this.mockMvc.perform(delete(deleteUri)) .andDo(print()) + .andDo(document("vpcsate_delete")) .andExpect(status().isOk()) .andExpect(MockMvcResultMatchers.jsonPath("$.id").value(UnitTestConfig.vpcId)); } @@ -162,6 +174,7 @@ public void deleteVpcStateByVpcId_deleteWhenIdNotExist_pass () throws Exception .thenReturn(null); String response = this.mockMvc.perform(delete(deleteUri)) .andDo(print()) + .andDo(document("vpcstate_delete_noexist")) .andExpect(status().isOk()) .andReturn().getResponse().getContentAsString(); System.out.println("-----json returned = " + response); @@ -177,6 +190,7 @@ public void getVpcStatesByProjectId_getMap_pass () throws Exception { vpcStates.put("VpcState", vpcState); Mockito.when(vpcDatabaseService.getAllVpcs()).thenReturn(vpcStates); this.mockMvc.perform(get(getByProjectIdUri)).andDo(print()) + .andDo(document("vpcstate_get_by_projectid")) .andExpect(status().isOk()); } @@ -185,6 +199,7 @@ public void getVpcStatesByProjectId_getEmptyMap_pass () throws Exception { Map vpcStates = new HashMap<>(); Mockito.when(vpcDatabaseService.getAllVpcs()).thenReturn(vpcStates); this.mockMvc.perform(get(getByProjectIdUri)).andDo(print()) + .andDo(document("vpcstate_get_by_projectid_empty")) .andExpect(status().isOk()); } From 332d373e99573c626b87bab1b43dacb4e3ab485b Mon Sep 17 00:00:00 2001 From: skdong Date: Wed, 6 May 2020 11:58:45 +0800 Subject: [PATCH 5/7] skip unitest --- services/mac_manager/pom.xml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/services/mac_manager/pom.xml b/services/mac_manager/pom.xml index dfd8592be..968874ba2 100644 --- a/services/mac_manager/pom.xml +++ b/services/mac_manager/pom.xml @@ -64,6 +64,12 @@ org.springframework.boot spring-boot-starter-test test + + + org.junit.vintage + junit-vintage-engine + + From 6ea6c796ba57709d9ae375b4ff35eece2303b048 Mon Sep 17 00:00:00 2001 From: skdong Date: Fri, 8 May 2020 11:24:48 +0800 Subject: [PATCH 6/7] enable route manager UTs --- services/route_manager/pom.xml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/services/route_manager/pom.xml b/services/route_manager/pom.xml index d6aebf472..dd43a4e37 100644 --- a/services/route_manager/pom.xml +++ b/services/route_manager/pom.xml @@ -59,12 +59,6 @@ org.springframework.boot spring-boot-starter-test test - - - org.junit.vintage - junit-vintage-engine - - From 3965e06bd1c071abd0a6877f72e801b429b9d16e Mon Sep 17 00:00:00 2001 From: skdong Date: Fri, 8 May 2020 12:40:23 +0800 Subject: [PATCH 7/7] fix conflict --- services/route_manager/pom.xml | 2 +- services/subnet_manager/pom.xml | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/services/route_manager/pom.xml b/services/route_manager/pom.xml index e5e1c4d43..c0af5b5e5 100644 --- a/services/route_manager/pom.xml +++ b/services/route_manager/pom.xml @@ -77,7 +77,7 @@ org.springframework.restdocs spring-restdocs-mockmvc - 2.6.1 + io.springfox springfox-swagger2 diff --git a/services/subnet_manager/pom.xml b/services/subnet_manager/pom.xml index 883ee6f90..b8863a825 100644 --- a/services/subnet_manager/pom.xml +++ b/services/subnet_manager/pom.xml @@ -73,15 +73,12 @@ org.springframework.boot spring-boot-starter-test test -<<<<<<< HEAD org.springframework.restdocs spring-restdocs-mockmvc test -======= ->>>>>>> master commons-net