-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
435 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
hsweb-tests/src/main/groovy/org/hswebframework/web/tests/HswebCrudWebApiSpecification.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package org.hswebframework.web.tests | ||
|
||
import com.alibaba.fastjson.JSON | ||
import org.hswebframework.ezorm.core.dsl.Query | ||
import org.hswebframework.web.WebUtil | ||
import org.hswebframework.web.commons.entity.param.QueryParamEntity | ||
import org.springframework.http.MediaType | ||
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.* | ||
|
||
|
||
/** | ||
* @author zhouhao | ||
* @since 3.0.2 | ||
*/ | ||
abstract class HswebCrudWebApiSpecification extends HswebSpecification { | ||
|
||
protected abstract String getBaseApi(); | ||
|
||
def doAddRequest(String requestBody) { | ||
def response = mockMvc.perform(post(getBaseApi()) | ||
.content(requestBody) | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andReturn() | ||
.response | ||
.contentAsString; | ||
return JSON.parseObject(response); | ||
} | ||
|
||
def doUpdateRequest(String id, String requestBody) { | ||
def response = mockMvc.perform(put("${getBaseApi()}/{id}", id) | ||
.content(requestBody) | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andReturn() | ||
.response | ||
.contentAsString; | ||
return JSON.parseObject(response); | ||
} | ||
|
||
def doDeleteRequest(String id) { | ||
def response = mockMvc | ||
.perform(delete("${getBaseApi()}/{id}", id)) | ||
.andReturn() | ||
.response | ||
.contentAsString; | ||
return JSON.parseObject(response); | ||
} | ||
|
||
def doGetRequest(String id) { | ||
def response = mockMvc | ||
.perform(get("${getBaseApi()}/{id}", id)) | ||
.andReturn() | ||
.response | ||
.contentAsString; | ||
return JSON.parseObject(response); | ||
} | ||
|
||
|
||
Query createQuery() { | ||
return Query.empty(new QueryParamEntity()); | ||
} | ||
|
||
def doQueryRequest(Query query) { | ||
MockHttpServletRequestBuilder get = get("${getBaseApi()}") | ||
WebUtil.objectToHttpParameters(query.param) | ||
.forEach({ k, v -> get.param(k, v) }) | ||
def response = mockMvc | ||
.perform(get) | ||
.andReturn() | ||
.response | ||
.contentAsString; | ||
return JSON.parseObject(response); | ||
} | ||
|
||
def doQueryByIdsRequest(String ids) { | ||
def response = mockMvc | ||
.perform(get("${getBaseApi()}/ids").param("ids", ids)) | ||
.andReturn() | ||
.response | ||
.contentAsString; | ||
return JSON.parseObject(response); | ||
} | ||
|
||
def doTotalRequest(Query query) { | ||
MockHttpServletRequestBuilder get = get("${getBaseApi()}/total") | ||
WebUtil.objectToHttpParameters(query.param) | ||
.forEach({ k, v -> get.param(k, v) }) | ||
|
||
def response = mockMvc | ||
.perform(get) | ||
.andReturn() | ||
.response | ||
.contentAsString; | ||
return JSON.parseObject(response); | ||
} | ||
|
||
def doCountRequest(Query query) { | ||
MockHttpServletRequestBuilder get = get("${getBaseApi()}/count") | ||
WebUtil.objectToHttpParameters(query.param) | ||
.forEach({ k, v -> get.param(k, v) }) | ||
def response = mockMvc | ||
.perform(get) | ||
.andReturn() | ||
.response | ||
.contentAsString; | ||
return JSON.parseObject(response); | ||
} | ||
|
||
def doNoPagingRequest(Query query) { | ||
MockHttpServletRequestBuilder get = get("${getBaseApi()}/no-paging") | ||
WebUtil.objectToHttpParameters(query.param) | ||
.forEach({ k, v -> get.param(k, v) }) | ||
def response = mockMvc | ||
.perform(get) | ||
.andReturn() | ||
.response | ||
.contentAsString; | ||
return JSON.parseObject(response); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
hsweb-tests/src/main/groovy/org/hswebframework/web/tests/HswebSpecification.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.hswebframework.web.tests | ||
|
||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.springframework.context.ConfigurableApplicationContext | ||
import org.springframework.test.context.ContextConfiguration | ||
import org.springframework.test.context.web.WebAppConfiguration | ||
import org.springframework.test.web.servlet.MockMvc | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders | ||
import spock.lang.Shared | ||
import spock.lang.Specification | ||
|
||
/** | ||
* @author zhouhao | ||
* @since 3.0.2 | ||
*/ | ||
@WebAppConfiguration | ||
@ContextConfiguration | ||
@SpringBootTest(classes = [HswebTestApplication.class], properties = ["classpath:application.yml"]) | ||
class HswebSpecification extends Specification { | ||
@Autowired | ||
protected ConfigurableApplicationContext context; | ||
|
||
@Shared | ||
protected MockMvc mockMvc; | ||
|
||
void setup() { | ||
mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
hsweb-tests/src/main/groovy/org/hswebframework/web/tests/HswebTestApplication.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.hswebframework.web.tests; | ||
|
||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.test.context.web.WebAppConfiguration; | ||
|
||
/** | ||
* @author zhouhao | ||
* @since 3.0.2 | ||
*/ | ||
@SpringBootApplication | ||
@WebAppConfiguration | ||
@Configuration | ||
class HswebTestApplication { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.