Skip to content

Commit

Permalink
增加规则文件导入测试
Browse files Browse the repository at this point in the history
  • Loading branch information
entropy-cloud committed Sep 2, 2023
1 parent fc6e40a commit ea27a9f
Show file tree
Hide file tree
Showing 25 changed files with 237 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
ext:mavenGroupId="io.github.entropy-cloud" ext:basePackageName="io.nop.auth" ext:appName="nop-auth"
ext:platformVersion="2.0.0-SNAPSHOT" ext:dialect="mysql,oracle,postgresql" ext:mavenVersion="2.0.0-SNAPSHOT"
x:schema="/nop/schema/orm/orm.xdef" xmlns:x="/nop/schema/xdsl.xdef" xmlns:i18n-en="i18n-en"
xmlns:ref-i18n-en="ref-i18n-en" xmlns:ext="ext" xmlns:orm-gen="orm-gen" xmlns:xpl="xpl">
xmlns:ref-i18n-en="ref-i18n-en" xmlns:ext="ext" xmlns:orm-gen="orm-gen" xmlns:xpl="xpl" xmlns:ui="ui">

<x:post-extends x:override="replace">
<orm-gen:DefaultPostExtends xpl:lib="/nop/orm/xlib/orm-gen.xlib"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -45,6 +46,7 @@ public void saveCollectedData() {

for (Map.Entry<IEntityModel, Map<String, EntityRow>> entry : dataMap.entrySet()) {
IEntityModel entityModel = entry.getKey();
// 按照实体主键的字符串顺序排序,便于输出确定性结果,也便于查看
Collection<EntityRow> rows = new TreeMap<>(entry.getValue()).values();

List<EntityRow> loadedRows = getLoadedRows(rows);
Expand All @@ -54,6 +56,10 @@ public void saveCollectedData() {
// 即使没有装载到数据,也需要保留空文件记录。这样数据初始化的时候可以知道需要新建对应的表。
// 有可能是执行查询但是没有查询到数据记录。
saveInputTable(entityModel, loadedRows);
} else if (ormHook.getLoadedTables().contains(entityModel.getName())) {
saveInputTable(entityModel, Collections.emptyList());
} else {
removeInputTable(entityModel);
}

if (!changedRows.isEmpty()) {
Expand All @@ -77,6 +83,11 @@ private void saveInputTable(IEntityModel entityModel, List<EntityRow> rows) {
}
}

private void removeInputTable(IEntityModel entityModel) {
File file = caseData.getInputTableFile(entityModel.getTableName(), null);
file.delete();
}

private List<String> getColNames(IEntityModel entityModel) {
List<String> ret = new ArrayList<>(entityModel.getColumns().size());
for (IColumnModel col : entityModel.getColumns()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,29 @@
import io.nop.orm.IOrmInterceptor;
import io.nop.orm.model.IEntityModel;

import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class AutoTestOrmHook implements IOrmDaoListener, IOrmInterceptor {
private final Map<IEntityModel, Map<String, EntityRow>> dataMap = new ConcurrentHashMap<>();
private final Set<String> loadedTables = Collections.newSetFromMap(new ConcurrentHashMap<>());

public Map<IEntityModel, Map<String, EntityRow>> getDataMap() {
return dataMap;
}

@Override
public void onRead(IEntityModel entityModel) {
loadedTables.add(entityModel.getName());
makeEntityData(entityModel);
}

public Set<String> getLoadedTables() {
return loadedTables;
}

@Override
public void onUpdate(IEntityModel entityModel) {
makeEntityData(entityModel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,19 @@ public long getWriteCount() {

@Override
public void writeBatch(Collection<? extends T> records) {
if (records == null || records.isEmpty())
if (records == null || records.isEmpty()) {
if (!headersWritten) {
if (!CollectionHelper.isEmpty(headers)) {
try {
writeHeaders(null);
} catch (IOException e) {
throw NopException.adapt(e);
}
headersWritten = true;
}
}
return;
}

try {
if (!headersWritten) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
ext:mavenGroupId="io.github.entropy-cloud" ext:basePackageName="io.nop.file" ext:appName="nop-file"
ext:platformVersion="2.0.0-SNAPSHOT" ext:dialect="mysql,oracle,postgresql" ext:mavenVersion="2.0.0-SNAPSHOT"
x:schema="/nop/schema/orm/orm.xdef" xmlns:x="/nop/schema/xdsl.xdef" xmlns:i18n-en="i18n-en"
xmlns:ref-i18n-en="ref-i18n-en" xmlns:ext="ext" xmlns:orm-gen="orm-gen" xmlns:xpl="xpl">
xmlns:ref-i18n-en="ref-i18n-en" xmlns:ext="ext" xmlns:orm-gen="orm-gen" xmlns:xpl="xpl" xmlns:ui="ui">

<x:post-extends x:override="replace">
<orm-gen:DefaultPostExtends xpl:lib="/nop/orm/xlib/orm-gen.xlib"/>
Expand Down
2 changes: 2 additions & 0 deletions nop-orm/src/main/java/io/nop/orm/IOrmEntityEnhancer.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public interface IOrmEntityEnhancer {

void internalLoadCollection(IOrmEntitySet coll);

Object initEntityId(IOrmEntity entity);

IOrmEntity newEntity(String entityName);

IEntityModel getEntityModel(String entityName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
Expand Down Expand Up @@ -48,7 +49,8 @@ static class EntityCache {
*/
boolean dirty;

Map<Object /* id */, IOrmEntity> idToEntities = new HashMap<>();
// 尽量保证实体的处理顺序,避免执行过程中的随机性,便于基于录制回放机制实现自动化测试
Map<Object /* id */, IOrmEntity> idToEntities = new LinkedHashMap<>();

// 是否某个实体被修改了
public boolean isDirty() {
Expand Down
11 changes: 8 additions & 3 deletions nop-orm/src/main/java/io/nop/orm/support/OrmEntitySet.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.nop.api.core.convert.ConvertHelper;
import io.nop.api.core.exceptions.ErrorCode;
import io.nop.api.core.exceptions.NopException;
import io.nop.api.core.util.Guard;
import io.nop.commons.util.ClassHelper;
import io.nop.orm.IOrmEntity;
import io.nop.orm.IOrmEntityEnhancer;
Expand Down Expand Up @@ -258,9 +259,12 @@ void beginModify() {

@Override
public void orm_onFlush() {
IOrmEntityEnhancer enhancer = orm_enhancer();
Guard.notNull(enhancer, "enhancer");

for (IOrmEntity entity : entities) {
if (entity.orm_enhancer() == null) {
entity.orm_attach(orm_enhancer());
entity.orm_attach(enhancer);
}
if (entity.orm_entityModel() == null) {
entity.orm_entityModel(orm_enhancer().getEntityModel(entity.orm_entityName()));
Expand Down Expand Up @@ -385,8 +389,9 @@ public boolean add(T e) {
beginModify();

IOrmEntityEnhancer enhancer = orm_enhancer();
if (enhancer != null && e.orm_enhancer() == null) {
e.orm_attach(enhancer);
if (enhancer != null) {
if (e.orm_enhancer() == null)
e.orm_attach(enhancer);
if (e.orm_entityModel() == null)
e.orm_entityModel(enhancer.getEntityModel(e.orm_entityName()));
}
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
data: {
data: {
ruleName: "test",
ruleGroup: "default",
ruleVersion: 1,
displayName: "Test Tree",
status: 1,
importFile: "@var:downloadPath"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FILE_ID,FILE_NAME,FILE_PATH,FILE_EXT,MIME_TYPE,FILE_LENGTH,FILE_LAST_MODIFIED,BIZ_OBJ_NAME,BIZ_OBJ_ID,FIELD_NAME,FILE_HASH,DEL_FLAG,CREATED_BY,CREATE_TIME,REMARK
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
RULE_ID,RULE_NAME,RULE_VERSION,DISPLAY_NAME,RULE_GROUP,RULE_TYPE,DESCRIPTION,MODEL_TEXT,STATUS,VERSION,CREATED_BY,CREATE_TIME,UPDATED_BY,UPDATE_TIME,REMARK
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SEQ_NAME,SEQ_TYPE,IS_UUID,NEXT_VALUE,STEP_SIZE,CACHE_SIZE,MAX_VALUE,RESET_TYPE,DEL_FLAG,VERSION,CREATED_BY,CREATE_TIME,UPDATED_BY,UPDATE_TIME,REMARK
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"data": {
"ruleId": "@var:NopRuleDefinition@ruleId",
"ruleName": "test",
"ruleVersion": 1,
"displayName": "Test Tree",
"ruleGroup": "default",
"ruleType": "TREE",
"ruleType_label": "TREE-决策树",
"modelText": "<rule displayName=\"测试规则\" x:schema=\"/nop/schema/rule.xdef\" xmlns:x=\"/nop/schema/xdsl.xdef\">\n <inputs>\n <input displayName=\"季度\" mandatory=\"true\" name=\"season\" type=\"java.lang.String\">\n <schema/>\n </input>\n <input displayName=\"客人数\" mandatory=\"true\" name=\"guestCount\" type=\"java.lang.Integer\">\n <schema/>\n </input>\n </inputs>\n <outputs>\n <output displayName=\"食物\" name=\"dish\" type=\"java.lang.String\">\n <schema/>\n </output>\n </outputs>\n</rule>",
"status": 1,
"status_label": "1-启用",
"version": 0,
"createdBy": "autotest-ref",
"createTime": "@var:NopRuleDefinition@updateTime",
"updatedBy": "autotest-ref",
"updateTime": "@var:NopRuleDefinition@updateTime",
"ruleInputs": [
{
"name": "season",
"displayName": "季度",
"type": "java.lang.String",
"mandatory": true,
"computed": false,
"defaultExpr": null,
"description": null,
"schema": {}
},
{
"name": "guestCount",
"displayName": "客人数",
"type": "java.lang.Integer",
"mandatory": true,
"computed": false,
"defaultExpr": null,
"description": null,
"schema": {}
}
],
"ruleOutputs": [
{
"name": "dish",
"displayName": "食物",
"type": "java.lang.String",
"aggregate": null,
"description": null,
"schema": {}
}
],
"beforeExecute": null,
"id": "@var:NopRuleDefinition@ruleId",
"description": null,
"remark": null
},
"status": 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
_chgType,FILE_ID,FILE_NAME,FILE_PATH,FILE_EXT,MIME_TYPE,FILE_LENGTH,FILE_LAST_MODIFIED,BIZ_OBJ_NAME,BIZ_OBJ_ID,FIELD_NAME,FILE_HASH,DEL_FLAG,CREATED_BY,CREATE_TIME,REMARK
A,@var:NopFileRecord@fileId,decision-tree.rule.xlsx,@var:NopFileRecord@filePath,xlsx,binary,1000,,NopRuleDefinition,__TEMP__,importFile,,1,autotest-ref,*,
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
_chgType,RULE_ID,RULE_NAME,RULE_VERSION,DISPLAY_NAME,RULE_GROUP,RULE_TYPE,DESCRIPTION,MODEL_TEXT,STATUS,VERSION,CREATED_BY,CREATE_TIME,UPDATED_BY,UPDATE_TIME,REMARK
A,@var:NopRuleDefinition@ruleId,test,1,Test Tree,default,TREE,,"<rule displayName=""测试规则"" x:schema=""/nop/schema/rule.xdef"" xmlns:x=""/nop/schema/xdsl.xdef"">
<inputs>
<input displayName=""季度"" mandatory=""true"" name=""season"" type=""java.lang.String"">
<schema/>
</input>
<input displayName=""客人数"" mandatory=""true"" name=""guestCount"" type=""java.lang.Integer"">
<schema/>
</input>
</inputs>
<outputs>
<output displayName=""食物"" name=""dish"" type=""java.lang.String"">
<schema/>
</output>
</outputs>
</rule>",1,0,autotest-ref,*,autotest-ref,*,
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
_chgType,SID,RULE_ID,LABEL,SORT_NO,PREDICATE,OUTPUTS,PARENT_ID,IS_LEAF,VERSION,CREATED_BY,CREATE_TIME,UPDATED_BY,UPDATE_TIME,REMARK
A,@var:NopRuleNode@sid_8,@var:NopRuleDefinition@ruleId,,1,"{""$type"":""gt"",""name"":""guestCount"",""value"":8}","{
""dish"": ""\""Stew\""""
}",@var:NopRuleNode@sid_7,true,0,autotest-ref,*,autotest-ref,*,
A,@var:NopRuleNode@sid_9,@var:NopRuleDefinition@ruleId,,5,"{""$type"":""eq"",""name"":""season"",""value"":""Summer""}",,,true,0,autotest-ref,*,autotest-ref,*,
A,@var:NopRuleNode@sid_10,@var:NopRuleDefinition@ruleId,,1,"{""$type"":""alwaysTrue""}","{
""dish"": ""\""Light Salad and nice Steak\""""
}",@var:NopRuleNode@sid_9,true,0,autotest-ref,*,autotest-ref,*,
A,@var:NopRuleNode@sid,@var:NopRuleDefinition@ruleId,,1,"{""$type"":""eq"",""name"":""season"",""value"":""Fall""}",,,true,0,autotest-ref,*,autotest-ref,*,
A,@var:NopRuleNode@sid_1,@var:NopRuleDefinition@ruleId,,1,"{""$type"":""le"",""name"":""guestCount"",""value"":8}","{
""dish"": ""\""Spareribs\""""
}",@var:NopRuleNode@sid,true,0,autotest-ref,*,autotest-ref,*,
A,@var:NopRuleNode@sid_2,@var:NopRuleDefinition@ruleId,,2,"{""$type"":""eq"",""name"":""season"",""value"":""Winter""}",,,true,0,autotest-ref,*,autotest-ref,*,
A,@var:NopRuleNode@sid_3,@var:NopRuleDefinition@ruleId,,1,"{""$type"":""le"",""name"":""guestCount"",""value"":8}","{
""dish"": ""\""Roastbeef\""""
}",@var:NopRuleNode@sid_2,true,0,autotest-ref,*,autotest-ref,*,
A,@var:NopRuleNode@sid_4,@var:NopRuleDefinition@ruleId,,3,"{""$type"":""eq"",""name"":""season"",""value"":""Spring""}",,,true,0,autotest-ref,*,autotest-ref,*,
A,@var:NopRuleNode@sid_5,@var:NopRuleDefinition@ruleId,,1,"{""$type"":""le"",""name"":""guestCount"",""value"":4}","{
""dish"": ""\""Dry Aged Gourmet Steak\""""
}",@var:NopRuleNode@sid_4,true,0,autotest-ref,*,autotest-ref,*,
A,@var:NopRuleNode@sid_6,@var:NopRuleDefinition@ruleId,,2,"{""$type"":""and"",""$body"":[{""$type"":""le"",""name"":""guestCount"",""value"":8},{""$type"":""ge"",""name"":""guestCount"",""value"":5}]}","{
""dish"": ""\""Steak\""""
}",@var:NopRuleNode@sid_4,true,0,autotest-ref,*,autotest-ref,*,
A,@var:NopRuleNode@sid_7,@var:NopRuleDefinition@ruleId,,4,"{""$type"":""in"",""name"":""season"",""value"":[""Fall"",""Winter"",""Spring""]}",,,true,0,autotest-ref,*,autotest-ref,*,
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
_chgType,SEQ_NAME,SEQ_TYPE,IS_UUID,NEXT_VALUE,STEP_SIZE,CACHE_SIZE,MAX_VALUE,RESET_TYPE,DEL_FLAG,VERSION,CREATED_BY,CREATE_TIME,UPDATED_BY,UPDATE_TIME,REMARK
A,default,seq,0,101,1,100,,,0,1,autotest-ref,*,autotest-ref,*,
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"data": {
"filename": "decision-tree.rule.xlsx",
"url": null,
"value": "@var:downloadPath"
},
"status": 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package io.nop.rule.service.entity;

import io.nop.api.core.annotations.autotest.EnableSnapshot;
import io.nop.api.core.annotations.autotest.NopTestConfig;
import io.nop.api.core.beans.ApiRequest;
import io.nop.api.core.beans.ApiResponse;
import io.nop.autotest.junit.JunitAutoTestCase;
import io.nop.commons.util.IoHelper;
import io.nop.core.reflect.bean.BeanTool;
import io.nop.core.resource.IResource;
import io.nop.file.core.UploadRequestBean;
import io.nop.graphql.core.IGraphQLExecutionContext;
import io.nop.graphql.core.ast.GraphQLOperationType;
import io.nop.graphql.core.engine.IGraphQLEngine;
import io.nop.rule.dao.entity.NopRuleDefinition;
import org.junit.jupiter.api.Test;

import javax.inject.Inject;
import java.io.InputStream;
import java.util.Map;

@NopTestConfig(localDb = true, initDatabaseSchema = true)
public class TestNopRuleDefinitionBizModel extends JunitAutoTestCase {

@Inject
IGraphQLEngine graphQLEngine;

@EnableSnapshot
@Test
public void testImport() {
IResource resource = inputResource("decision-tree.rule.xlsx");
InputStream is = resource.getInputStream();

try {
UploadRequestBean request = new UploadRequestBean();
request.setFileName("decision-tree.rule.xlsx");
request.setBizObjName(NopRuleDefinition.class.getSimpleName());
request.setFieldName("importFile");
request.setLength(1000);
request.setMimeType("binary");
request.setLastModified(1000);
request.setInputStream(is);

IGraphQLExecutionContext ctx = graphQLEngine.newRpcContext(GraphQLOperationType.mutation,
"NopFileStore__upload", ApiRequest.build(request));
ApiResponse<?> response = graphQLEngine.executeRpc(ctx);
// 每次生成的下载路径都是一个随机值,所以需要注册为变量
setVar("downloadPath", BeanTool.getComplexProperty(response, "data.value"));
output("upload-result.json5", response);
} finally {
IoHelper.safeCloseObject(is);
}

ApiRequest<?> request = request("request.json5", Map.class);
IGraphQLExecutionContext ctx = graphQLEngine.newRpcContext(GraphQLOperationType.mutation,
"NopRuleDefinition__save", request);
ApiResponse<?> response = graphQLEngine.executeRpc(ctx);
output("response.json5", response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ static class SeqItem {
}

public void update(NopSysSequence seq) {
this.cacheSize = seq.getCacheSize() == null ? seq.getCacheSize() : 0;
this.stepSize = seq.getStepSize() == null ? seq.getStepSize() : 1;
this.cacheSize = 0;
this.stepSize = seq.getStepSize() != null ? seq.getStepSize() : 1;
if (this.stepSize <= 0) {
this.stepSize = 1;
}
Expand Down Expand Up @@ -163,6 +163,9 @@ long syncFromDb(SeqItem item) {

item.update(seq);

item.cacheSize = seq.getCacheSize();
item.useUuid = StringHelper.isYes(seq.getIsUuid());

long ret = seq.getNextValue();

long next = item.nextValue + (item.cacheSize > 0 ? (long) item.cacheSize * item.stepSize : item.stepSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
ext:mavenGroupId="io.github.entropy-cloud" ext:basePackageName="io.nop.sys" ext:appName="nop-sys"
ext:platformVersion="2.0.0-SNAPSHOT" ext:dialect="mysql,oracle,postgresql" ext:mavenVersion="2.0.0-SNAPSHOT"
x:schema="/nop/schema/orm/orm.xdef" xmlns:x="/nop/schema/xdsl.xdef" xmlns:i18n-en="i18n-en"
xmlns:ref-i18n-en="ref-i18n-en" xmlns:ext="ext" xmlns:orm-gen="orm-gen" xmlns:xpl="xpl">
xmlns:ref-i18n-en="ref-i18n-en" xmlns:ext="ext" xmlns:orm-gen="orm-gen" xmlns:xpl="xpl" xmlns:ui="ui">

<x:post-extends x:override="replace">
<orm-gen:DefaultPostExtends xpl:lib="/nop/orm/xlib/orm-gen.xlib"/>
Expand Down
Loading

0 comments on commit ea27a9f

Please sign in to comment.