Skip to content

Commit

Permalink
feat: add operationId
Browse files Browse the repository at this point in the history
  • Loading branch information
mr3 committed Nov 24, 2023
1 parent d673480 commit 958b708
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
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 com.arextest.web.model.dao.mongodb.expectation.ExpectationScriptEntity;
import com.arextest.web.model.dao.mongodb.expectation.ExpectationScriptEntity.Fields;
import com.arextest.web.model.dao.mongodb.expectation.ExpectationScriptCollection;
import com.arextest.web.model.dao.mongodb.expectation.ExpectationScriptCollection.Fields;
import com.arextest.web.model.mapper.expectation.ExpectationScriptMapper;
import com.mongodb.client.result.UpdateResult;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
Expand All @@ -23,24 +24,25 @@ public class ExpectationScriptRepository implements RepositoryProvider {
private MongoTemplate mongoTemplate;

public boolean insert(ExpectationScriptModel model) {
ExpectationScriptEntity entity = ExpectationScriptMapper.INSTANCE.toEntity(model);
ExpectationScriptCollection entity = ExpectationScriptMapper.INSTANCE.toCollection(model);
if (entity.getExpirationTime() == 0) {
// 2099-01-01 12:00:00
entity.setExpirationTime(4070923200000L);
}
entity.setValid(BooleanUtils.toBooleanDefaultIfNull(model.valid, true));
mongoTemplate.insert(entity);
return StringUtils.isNotBlank(entity.getId());
}
public boolean update(ExpectationScriptModel model) {
Query query = Query.query(Criteria.where(DASH_ID).is(model.getId()));
Update update = getConfigUpdate();
update.set(Fields.content, model.getContent());
update.set(Fields.valid, model.valid);
update.set(Fields.valid, BooleanUtils.toBooleanDefaultIfNull(model.valid, true));
update.set(Fields.expirationTime, model.getExpirationTime());
update.set(Fields.scope, model.getScope());
update.set(Fields.dataChangeUpdateBy, model.getDataChangeUpdateBy());

UpdateResult updateResult = mongoTemplate.updateFirst(query, update, ExpectationScriptEntity.class);
UpdateResult updateResult = mongoTemplate.updateFirst(query, update, ExpectationScriptCollection.class);
return updateResult.getModifiedCount() > 0;
}

Expand All @@ -52,24 +54,25 @@ public boolean delete(ExpectationScriptDeleteRequest request) {
Query query = new Query();
query.addCriteria(Criteria.where(DASH_ID).is(request.getId()));
query.addCriteria(Criteria.where(Fields.appId).is(request.getAppId()));
return mongoTemplate.remove(query, ExpectationScriptEntity.class).getDeletedCount() > 0;
return mongoTemplate.remove(query, ExpectationScriptCollection.class).getDeletedCount() > 0;
}

public List<ExpectationScriptModel> query(ExpectationScriptQueryRequest request){
Query query = new Query();
query.addCriteria(Criteria.where(Fields.appId).is(request.getAppId()));
if (StringUtils.isNotEmpty(request.getOperationId())) {
query.addCriteria(Criteria.where(Fields.operationId).is(request.getOperationId()));
}
if (request.getValid() != null) {
query.addCriteria(Criteria.where(Fields.valid).is(request.getValid()));
}
if (request.getExpirationTime() != null) {
query.addCriteria(Criteria.where(Fields.expirationTime).lt(request.getExpirationTime()));
query.addCriteria(Criteria.where(Fields.expirationTime).lte(request.getExpirationTime()));
}
if (request.getScope() != null) {
query.addCriteria(Criteria.where(Fields.scope).is(request.getScope()));
}
if (request.getTitle() != null) {
query.addCriteria(Criteria.where(Fields.title).regex(request.getTitle()));
}
return ExpectationScriptMapper.INSTANCE.toModelList(mongoTemplate.find(query, ExpectationScriptEntity.class));
List<ExpectationScriptCollection> collectionList = mongoTemplate.find(query, ExpectationScriptCollection.class);
return ExpectationScriptMapper.INSTANCE.toModelList(collectionList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ public class ExpectationScriptModel {
/**
* Script title, eg: classA.methodB
*/
@NotBlank(message = "title can not be blank")
private String title;
@NotBlank(message = "operationId can not be blank")
private String operationId;
/**
* Script alias
*/
private String alias;
/**
* Script content
*/
Expand All @@ -33,8 +37,8 @@ public class ExpectationScriptModel {
*/
public long expirationTime;
/**
* 0 specified: for specified operation
* 1 global: for all operation
* 0 for specified operation
* 1 for all operation
*/
private byte scope;
private String dataChangeCreateBy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,19 @@
public class ExpectationScriptQueryRequest {
@NotBlank(message = "appId can not be blank")
private String appId;
private String title;
/**
* operationId see Collection: ServiceOperation
*/
private String operationId;
/**
* null for all
*/
private Boolean valid;
private Long expirationTime;
/**
* null for all
* 0 for specified operation
* 1 for all operation
*/
private Byte scope;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@
@FieldNameConstants
@EqualsAndHashCode(callSuper = true)
@Document(collection = "ConfigExpectationScript")
public class ExpectationScriptEntity extends ModelBase {
public class ExpectationScriptCollection extends ModelBase {
/**
* app id
*/
private String appId;
/**
* Script title, eg: classA.methodB
* operation id
*/
private String title;
private String operationId;
/**
* Script alias
*/
private String alias;
/**
* Script content
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package com.arextest.web.model.mapper.expectation;

import com.arextest.web.model.contract.contracts.config.expectation.ExpectationScriptModel;
import com.arextest.web.model.dao.mongodb.expectation.ExpectationScriptEntity;
import com.arextest.web.model.dao.mongodb.expectation.ExpectationScriptCollection;
import java.util.List;
import org.mapstruct.Mapper;
import org.mapstruct.NullValueMappingStrategy;
import org.mapstruct.factory.Mappers;

@Mapper
@Mapper(nullValueIterableMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT)
public interface ExpectationScriptMapper {
ExpectationScriptMapper INSTANCE = Mappers.getMapper(ExpectationScriptMapper.class);
ExpectationScriptEntity toEntity(ExpectationScriptModel model);
ExpectationScriptModel toModel(ExpectationScriptEntity entity);
List<ExpectationScriptEntity> toEntityList(List<ExpectationScriptModel> modelList);
List<ExpectationScriptModel> toModelList(List<ExpectationScriptEntity> entityList);
ExpectationScriptCollection toCollection(ExpectationScriptModel model);
ExpectationScriptModel toModel(ExpectationScriptCollection entity);
List<ExpectationScriptCollection> toCollectionList(List<ExpectationScriptModel> modelList);
List<ExpectationScriptModel> toModelList(List<ExpectationScriptCollection> collectionList);
}

0 comments on commit 958b708

Please sign in to comment.