-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: change expectation script model name
- Loading branch information
Showing
14 changed files
with
362 additions
and
114 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
39 changes: 39 additions & 0 deletions
39
...main/java/com/arextest/web/core/business/config/expectation/ExpectationScriptService.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,39 @@ | ||
package com.arextest.web.core.business.config.expectation; | ||
|
||
import com.arextest.web.core.repository.expectation.ExpectationScriptRepository; | ||
import com.arextest.web.model.contract.contracts.config.expectation.ExpectationScriptDeleteRequest; | ||
import com.arextest.web.model.contract.contracts.config.expectation.ExpectationScriptModel; | ||
import com.arextest.web.model.contract.contracts.config.expectation.ExpectationScriptQueryRequest; | ||
import java.util.List; | ||
import org.apache.commons.collections4.CollectionUtils; | ||
import org.springframework.stereotype.Service; | ||
|
||
/** | ||
* @since 2023/12/15 | ||
*/ | ||
@Service | ||
public class ExpectationScriptService { | ||
private final ExpectationScriptRepository repository; | ||
private final ScriptNormalizer normalizer; | ||
|
||
public ExpectationScriptService(ExpectationScriptRepository repository, ScriptNormalizer normalizer) { | ||
this.repository = repository; | ||
this.normalizer = normalizer; | ||
} | ||
|
||
public List<ExpectationScriptModel> query(ExpectationScriptQueryRequest request) { | ||
return repository.query(request); | ||
} | ||
|
||
public boolean save(ExpectationScriptModel model) { | ||
normalizer.normalize(model); | ||
if (CollectionUtils.isNotEmpty(model.getInvalidExtractAssertList())) { | ||
return false; | ||
} | ||
return model.getId() != null ? repository.update(model) : repository.insert(model); | ||
} | ||
|
||
public boolean delete(ExpectationScriptDeleteRequest request) { | ||
return repository.delete(request.getId(), request.getAppId()); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
.../src/main/java/com/arextest/web/core/business/config/expectation/ScriptAssertHandler.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.arextest.web.core.business.config.expectation; | ||
|
||
import com.arextest.web.model.contract.contracts.config.expectation.ScriptExtractAssertModel; | ||
import com.arextest.web.model.contract.contracts.config.expectation.ExpectationScriptModel; | ||
|
||
/** | ||
* @since 2023/11/29 | ||
*/ | ||
public interface ScriptAssertHandler { | ||
boolean support(ScriptExtractAssertModel model); | ||
void handle(ExpectationScriptModel scriptModel, ScriptExtractAssertModel assertModel); | ||
} |
79 changes: 79 additions & 0 deletions
79
...ore/src/main/java/com/arextest/web/core/business/config/expectation/ScriptNormalizer.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.arextest.web.core.business.config.expectation; | ||
|
||
import com.arextest.web.model.contract.contracts.config.expectation.ExpectationScriptModel; | ||
import com.arextest.web.model.contract.contracts.config.expectation.ScriptExtractAssertModel; | ||
import com.arextest.web.model.contract.contracts.config.expectation.ScriptExtractOperationModel; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* @since 2023/11/29 | ||
*/ | ||
@Component | ||
public class ScriptNormalizer { | ||
// (?:let (?<variable>\w+) = )?arex\.(?<category>[a-zA-Z]+){1}\[\"(?<operation>.+)\"] | ||
private static final String EXTRACT_OPERATION_REGEX = "(?:var (?<variable>\\w+) = )?arex\\.(?<category>[a-zA-Z]+){1}\\[\"(?<operation>.+)\"]"; | ||
private static final Pattern EXTRACT_OPERATION_PATTERN = Pattern.compile(EXTRACT_OPERATION_REGEX); | ||
// arex\.assert\.[a-zA-Z]+\(.*\); | ||
private static final String EXTRACT_ASSERT_REGEX = "arex\\.assert\\.[a-zA-Z]+\\(.*\\);"; | ||
private static final Pattern EXTRACT_ASSERT_PATTERN = Pattern.compile(EXTRACT_ASSERT_REGEX); | ||
|
||
private final List<ScriptAssertHandler> assertHandlerList; | ||
|
||
public ScriptNormalizer(List<ScriptAssertHandler> assertHandlerList) { | ||
this.assertHandlerList = assertHandlerList; | ||
} | ||
|
||
public void normalize(ExpectationScriptModel model) { | ||
model.setNormalizedContent(model.getContent()); | ||
List<ScriptExtractOperationModel> extactOpeartionList = extactOpeartionList(model.getNormalizedContent()); | ||
|
||
if (extactOpeartionList.isEmpty()) { | ||
return; | ||
} | ||
|
||
model.setExtractOperationList(extactOpeartionList); | ||
|
||
// match script with EXTRACT_ASSERT_PATTERN | ||
Matcher assertMatcher = EXTRACT_ASSERT_PATTERN.matcher(model.getNormalizedContent()); | ||
while (assertMatcher.find()) { | ||
ScriptExtractAssertModel assertModel = new ScriptExtractAssertModel(assertMatcher.group()); | ||
for (ScriptAssertHandler handler : assertHandlerList) { | ||
if (!handler.support(assertModel)) { | ||
continue; | ||
} | ||
handler.handle(model, assertModel); | ||
if (!assertModel.validate()) { | ||
continue; | ||
} | ||
model.setNormalizedContent(StringUtils.replace(model.getNormalizedContent(), assertModel.getOriginalText(), | ||
assertModel.rebuild())); | ||
} | ||
if (!assertModel.validate()) { | ||
model.setInvalidExtractAssertList(Collections.singletonList(assertModel)); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
private List<ScriptExtractOperationModel> extactOpeartionList(String script) { | ||
List<ScriptExtractOperationModel> operationList = new ArrayList<>(); | ||
Matcher matcher = EXTRACT_OPERATION_PATTERN.matcher(script); | ||
while (matcher.find()) { | ||
ScriptExtractOperationModel operationModel = new ScriptExtractOperationModel(); | ||
operationModel.setVariableName(matcher.group("variable")); | ||
operationModel.setCategoryName(matcher.group("category")); | ||
operationModel.setOperationName(matcher.group("operation")); | ||
operationModel.setOriginalText(matcher.group()); | ||
if (operationModel.validate() && !operationList.contains(operationModel)) { | ||
operationList.add(operationModel); | ||
} | ||
} | ||
return operationList; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
.../main/java/com/arextest/web/core/business/config/expectation/assertion/EqualsHandler.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,60 @@ | ||
package com.arextest.web.core.business.config.expectation.assertion; | ||
|
||
import com.arextest.web.core.business.config.expectation.ScriptAssertHandler; | ||
import com.arextest.web.model.contract.contracts.config.expectation.ScriptExtractAssertModel; | ||
import com.arextest.web.model.contract.contracts.config.expectation.ExpectationScriptModel; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* @since 2023/11/29 | ||
*/ | ||
@Component | ||
public class EqualsHandler implements ScriptAssertHandler { | ||
// arex\.assert\.equals\((?<expected>\"\w+\"), *(?<service>\w+)\.request.(?<path>.*)\); | ||
private static final String EQUALS_REGEX = "arex\\.assert\\.equals\\((?<expected>\"\\w+\"), *(?<service>.+)\\.(?:request|response)\\.(?<path>.*)\\);"; | ||
private static final Pattern EQUALS_PATTERN = Pattern.compile(EQUALS_REGEX); | ||
|
||
// arex\.(?<category>[a-zA-Z]+){1}\[\"(?<operation>.+)\"] | ||
private static final String OPERATION_REGEX = "arex\\.(?<category>[a-zA-Z]+){1}\\[\"(?<operation>.+)\"]"; | ||
private static final Pattern OPERATION_PATTERN = Pattern.compile(OPERATION_REGEX); | ||
|
||
@Override | ||
public boolean support(ScriptExtractAssertModel model) { | ||
return model.getOriginalText().startsWith("arex.assert.equals"); | ||
} | ||
|
||
@Override | ||
public void handle(ExpectationScriptModel scriptModel, ScriptExtractAssertModel assertModel) { | ||
Matcher equalsMatcher = EQUALS_PATTERN.matcher(assertModel.getOriginalText()); | ||
if (!equalsMatcher.find()) { | ||
return; | ||
} | ||
assertModel.setMethodName("equals"); | ||
assertModel.setExpected(equalsMatcher.group("expected")); | ||
assertModel.setShortServiceName(equalsMatcher.group("service")); | ||
assertModel.setPath(equalsMatcher.group("path")); | ||
|
||
if (StringUtils.startsWith(assertModel.getShortServiceName(), "arex")) { | ||
Matcher operationMatcher = OPERATION_PATTERN.matcher(assertModel.getShortServiceName()); | ||
if (operationMatcher.find()) { | ||
assertModel.setCategoryName(operationMatcher.group("category")); | ||
assertModel.setOperationName(operationMatcher.group("operation")); | ||
} | ||
} | ||
|
||
if (StringUtils.isNotEmpty(assertModel.getOperationName())) { | ||
return; | ||
} | ||
|
||
scriptModel.getExtractOperationList().stream() | ||
.filter(importsModel -> StringUtils.equals(importsModel.getVariableName(), assertModel.getShortServiceName())) | ||
.findFirst() | ||
.ifPresent(operation -> { | ||
assertModel.setOperationName(operation.getOperationName()); | ||
assertModel.setCategoryName(operation.getCategoryName()); | ||
}); | ||
} | ||
} |
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
52 changes: 52 additions & 0 deletions
52
...src/test/java/com/arextest/web/core/business/config/expectation/ScriptNormalizerTest.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,52 @@ | ||
package com.arextest.web.core.business.config.expectation; | ||
|
||
import com.arextest.web.core.business.config.expectation.assertion.EqualsHandler; | ||
import com.arextest.web.model.contract.contracts.config.expectation.ExpectationScriptModel; | ||
import com.arextest.web.model.contract.contracts.config.expectation.ScriptExtractOperationModel; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* @since 2023/12/15 | ||
*/ | ||
class ScriptNormalizerTest { | ||
static ScriptNormalizer scriptNormalizer; | ||
static List<ScriptAssertHandler> scriptAssertHandlerList; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
scriptAssertHandlerList = new ArrayList<>(1); | ||
scriptAssertHandlerList.add(new EqualsHandler()); | ||
scriptNormalizer = new ScriptNormalizer(scriptAssertHandlerList); | ||
} | ||
|
||
@AfterEach | ||
void tearDown() { | ||
} | ||
|
||
@Test | ||
void normalize() { | ||
String script = " var serviceConsumerA = arex.SoaConsumer[\"HelloService.ConsumerA\"];\n" | ||
+ " arex.assert.equals(\"serviceConsumerB\", serviceConsumerA.request.name);\n" | ||
+ "\n" | ||
+ " var serviceConsumerB = arex.SoaConsumer[\"HelloService.ConsumerB\"];\n" | ||
+ " arex.assert.equals(\"serviceConsumerB\", serviceConsumerB.request.name);\n" | ||
+ " \n" | ||
+ " arex.assert.equals(\"mark4\", arex.SoaProvider[\"HelloService\"].response.order.id);"; | ||
ExpectationScriptModel expectationScript = new ExpectationScriptModel(); | ||
expectationScript.setAppId("test-service"); | ||
expectationScript.setOperationId("test-operation-id"); | ||
expectationScript.setContent(script); | ||
expectationScript.setNormalizedContent(expectationScript.getContent()); | ||
scriptNormalizer.normalize(expectationScript); | ||
|
||
assert expectationScript.getExtractOperationList().size() == 3; | ||
|
||
for (ScriptExtractOperationModel operation : expectationScript.getExtractOperationList()) { | ||
System.out.println(operation); | ||
} | ||
} | ||
} |
Oops, something went wrong.