-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
b73747d
commit 769d21f
Showing
10 changed files
with
193 additions
and
66 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
62 changes: 62 additions & 0 deletions
62
pact-demo-consumer/src/main/java/com/lmx/pactdemoconsumer/PactDslJsonBodyUtil.java
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,62 @@ | ||
package com.lmx.pactdemoconsumer; | ||
|
||
import au.com.dius.pact.consumer.dsl.PactDslJsonBody; | ||
|
||
import java.lang.reflect.Field; | ||
import java.math.BigDecimal; | ||
import java.util.Date; | ||
|
||
public class PactDslJsonBodyUtil { | ||
/** | ||
* 以req对象来构建期望的请求 | ||
* <p> | ||
* 定义格式规范:如日期 | ||
* </p> | ||
* | ||
* @param o | ||
* @param pactDslJsonBody | ||
*/ | ||
public static PactDslJsonBody buildReqJson(PactDslJsonBody pactDslJsonBody, Object o) throws Exception { | ||
for (Field field : o.getClass().getDeclaredFields()) { | ||
if (field.getType().isInstance(new String())) { | ||
field.setAccessible(true); | ||
pactDslJsonBody.stringValue(field.getName(), (String) field.get(o)); | ||
} else if (field.getType().isInstance(new Integer(1)) | ||
|| field.getType().isInstance(new Long(1)) | ||
|| field.getType().isInstance(new BigDecimal(1))) | ||
pactDslJsonBody.numberType(field.getName()); | ||
else if (field.getType().isInstance(new Date())) | ||
pactDslJsonBody.date(field.getName(), "yyyy-MM-dd HH:mm:ss"); | ||
else if (field.getType().newInstance() instanceof Object) { | ||
PactDslJsonBody innerBodyObj = pactDslJsonBody.object(field.getName()); | ||
field.setAccessible(true); | ||
Object innerObj = field.get(o); | ||
return buildReqJson(innerBodyObj, innerObj); | ||
} | ||
} | ||
return pactDslJsonBody; | ||
|
||
} | ||
|
||
/** | ||
* 正则匹配响应值 | ||
* <p> | ||
* 定义格式规范:如日期和初始值 | ||
* </p> | ||
* | ||
* @param cls | ||
* @param pactDslJsonBody | ||
*/ | ||
public static void buildRespJson(Class cls, PactDslJsonBody pactDslJsonBody) { | ||
for (Field field : cls.getDeclaredFields()) { | ||
if (field.getType().isInstance(new String())) | ||
pactDslJsonBody.stringMatcher(field.getName(), "^[A-Za-z0-9]+$", "nZroXQogwHTRfpsyCZ98"); | ||
if (field.getType().isInstance(new Integer(1)) | ||
|| field.getType().isInstance(new Long(1)) | ||
|| field.getType().isInstance(new BigDecimal(1))) | ||
pactDslJsonBody.numberType(field.getName()); | ||
if (field.getType().isInstance(new Date())) | ||
pactDslJsonBody.date(field.getName(), "yyyy-MM-dd HH:mm:ss", new Date()); | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
pact-demo-consumer/src/main/java/com/lmx/pactdemoconsumer/PactFeignClient.java
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,12 @@ | ||
package com.lmx.pactdemoconsumer; | ||
|
||
import org.springframework.cloud.netflix.feign.FeignClient; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
|
||
@FeignClient("sc.provider") | ||
public interface PactFeignClient { | ||
|
||
@RequestMapping(value = "/api/pact",method = RequestMethod.POST) | ||
PactHttp.Resp hello(PactHttp.Req body); | ||
} |
79 changes: 79 additions & 0 deletions
79
pact-demo-consumer/src/main/java/com/lmx/pactdemoconsumer/PactFeignClientInvoker.java
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,79 @@ | ||
package com.lmx.pactdemoconsumer; | ||
|
||
import au.com.dius.pact.consumer.ConsumerPactBuilder; | ||
import au.com.dius.pact.consumer.PactVerificationResult; | ||
import au.com.dius.pact.consumer.dsl.PactDslJsonBody; | ||
import au.com.dius.pact.model.MockProviderConfig; | ||
import au.com.dius.pact.model.RequestResponsePact; | ||
import com.alibaba.fastjson.JSONObject; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.cloud.netflix.feign.FeignClient; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Proxy; | ||
|
||
import static au.com.dius.pact.consumer.ConsumerPactRunnerKt.runConsumerTest; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* 契约测试执行器 | ||
* <p> | ||
* 目标类:feignClient | ||
*/ | ||
public class PactFeignClientInvoker implements InvocationHandler { | ||
private Logger logger = LoggerFactory.getLogger(getClass()); | ||
|
||
private static PactFeignClientInvoker pactInvoker = new PactFeignClientInvoker(); | ||
|
||
@Override | ||
public Object invoke(Object proxy, Method method, Object[] args) { | ||
FeignClient cdc = method.getDeclaringClass().getDeclaredAnnotation(FeignClient.class); | ||
RequestMapping cdcInfo = method.getDeclaredAnnotation(RequestMapping.class); | ||
Object reqBody = args[0]; | ||
PactDslJsonBody req = new PactDslJsonBody(); | ||
PactDslJsonBody req_ = null; | ||
try { | ||
req_ = PactDslJsonBodyUtil.buildReqJson(req, reqBody); | ||
} catch (Exception e) { | ||
logger.error("", e); | ||
} | ||
PactDslJsonBody resp = new PactDslJsonBody(); | ||
PactDslJsonBodyUtil.buildRespJson(method.getReturnType(), resp); | ||
RequestResponsePact pact = ConsumerPactBuilder | ||
.consumer(System.getProperty("spring.application.name")) | ||
.hasPactWith(cdc.value()) | ||
.uponReceiving("it's a feign api") | ||
.path(cdcInfo.value()[0]) | ||
.method(cdcInfo.method() == null ? "POST" : cdcInfo.method()[0].toString()) | ||
.body(req_) | ||
.willRespondWith() | ||
.status(200) | ||
.body(resp) | ||
.toPact(); | ||
MockProviderConfig config = MockProviderConfig.createDefault(); | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
PactVerificationResult result = runConsumerTest(pact, config, mockServer -> { | ||
try { | ||
org.json.JSONObject jsonObject = new PactProviderClient(mockServer.getUrl()).pactMock((org.json.JSONObject) req.getBody(), cdcInfo.value()[0]); | ||
assertEquals(jsonObject.toString(), resp.getBody().toString()); | ||
stringBuilder.append(jsonObject.toString()); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
}); | ||
|
||
if (result instanceof PactVerificationResult.Error) { | ||
throw new RuntimeException(((PactVerificationResult.Error) result).getError()); | ||
} | ||
assertEquals(PactVerificationResult.Ok.INSTANCE, result); | ||
return JSONObject.parseObject(stringBuilder.toString(), method.getReturnType()); | ||
} | ||
|
||
public static Object getProxyObj(Class interface_) { | ||
return Proxy.newProxyInstance(PactFeignClientInvoker.class.getClassLoader(), new Class[]{interface_}, pactInvoker); | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ | |
@Slf4j | ||
public class PactConsumerTest { | ||
/** | ||
* 契约测试遵循字段严格匹配的约定 | ||
* simple demo | ||
*/ | ||
@Test | ||
public void testPact() { | ||
|
@@ -72,12 +72,25 @@ public void testPact() { | |
} | ||
|
||
/** | ||
* 契约测试遵循字段类型即可,当然也可以使用正则等 | ||
* support restTemplate/httpClient pact testing | ||
* | ||
*/ | ||
@Test | ||
public void testProxyPact() { | ||
PactHttp pactHttp = (PactHttp) PactInvoker.getProxyObj(PactHttp.class); | ||
PactHttp.Resp resp = pactHttp.hello(new PactHttp.Req("james", "123", 100L, new Date(), new PactHttp.InnerReq("15821303235", "[email protected]"))); | ||
PactHttp.Resp resp = pactHttp.hello(new PactHttp.Req("james", "123", 100L, new Date(), | ||
new PactHttp.InnerReq("15821303235", "[email protected]"))); | ||
log.info("cdc resp={}", resp); | ||
} | ||
|
||
/** | ||
* support feignClient pact testing | ||
*/ | ||
@Test | ||
public void testProxyFeignPact() { | ||
PactFeignClient feignClient = (PactFeignClient) PactFeignClientInvoker.getProxyObj(PactFeignClient.class); | ||
PactHttp.Resp resp = feignClient.hello(new PactHttp.Req("james", "123", 100L, new Date(), | ||
new PactHttp.InnerReq("15821303235", "[email protected]"))); | ||
log.info("cdc resp={}", resp); | ||
} | ||
} |
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