我正在使用框架Olingo用Java语言实现尊重OData标准版本4的Web服务。我需要自定义$filter
的响应。我已经实现了一个访客as documented in the quick start。我需要实施一个集成测试,以尝试使用另一种语法,并防止将来维护时出现回归错误。
对于V2,我发现了the following tutorial和以下代码:
@Test
public void printExpressionWithProperty() throws Exception {
//Use a mocked edmProvider for this tutorial
TestEdmProvider provider = new TestEdmProvider();
Edm edm = RuntimeDelegate.createEdm(provider);
EdmEntityType entityType = edm.getEntityType(TestEdmProvider.NAMESPACE_1, TestEdmProvider.ENTITY_TYPE_1_1.getName());
String rawExpression = "EmployeeId eq '1'";
FilterExpression expression = UriParser.parseFilter (null, entityType, rawExpression);
String whereClause = (String) expression.accept(new JdbcSimpleStringVisitor());
System.out.println("Raw: " + rawExpression + " ------> Whereclause: " + whereClause);
System.out.println();
}
不幸的是,UriParser.parseFilter
在v4中不存在。
我尝试过:
public class MyVisitorTest {
private final FullQualifiedName NAME1 = new FullQualifiedName("testNamespace1", "myfield");
private final OData odata = OData.newInstance();
public EdmEntityType createEntityType(final FullQualifiedName fqn) {
if (NAME1.equals(fqn)) {
EdmEntityType entityType = mock(EdmEntityType.class);
when(entityType.getNamespace()).thenReturn(fqn.getNamespace());
when(entityType.getName()).thenReturn(fqn.getName());
return entityType;
}
return null;
}
private Expression parseExpression(final String expressionString)
throws UriParserException, UriValidationException {
UriTokenizer tokenizer = new UriTokenizer(expressionString);
EdmEntityType entityType = createEntityType(NAME1);
Edm edm = mock(Edm.class);
when(edm.getEntityType(NAME1)).thenReturn(entityType);
final Expression expression = new ExpressionParser(edm, odata).parse(tokenizer, null, null, null);
assertNotNull(expression);
assertTrue(tokenizer.next(UriTokenizer.TokenKind.EOF));
return expression;
}
@Test
public void simpleTest() throws UriParserException, UriValidationException, ODataApplicationException, ExpressionVisitException {
String exp = "myfield gt 2019-01-01T00:00:00Z";
Expression e = parseExpression(exp);
MyVisitor myVisitor = new MyVisitor();
String result = (String) e.accept(startEndMeasureVisitor);
assertEquals(result.toString(), "MyResult");
}
}
它不起作用,它向我发送以下消息:
属性路径必须遵循结构化类型。
因此,我正在寻找使我的单元测试生效的想法,或者您是否有可以分享的工作示例...
答案 0 :(得分:1)
关于单元测试,我将集中精力分别测试每种过滤器方法。就像visitBinaryOperator
根据输入返回期望值,依此类推。
我不是事物命名专家,但是我将您的测试称为集成测试。在这里,我将重点介绍测试内部准备的测试FilterOption
。在您的原始应用中,Apache Olingo将创建此FilterOption
,并且这部分(应该)应在Olingo中进行测试。
使用Apache Olingo时,我经常提到its repository,尤其是 server-test 和 server-tecsvc 。验证在Olingo本身中如何进行测试。
我建议您看一下FilterValidator,因为它可能对您的集成测试有用。
答案 1 :(得分:0)
在这里,我们要感谢原型的跟踪功能(对遗留代码非常有用),我有了parseExpression
方法的新版本。
class A implements EdmStructuredType, EdmPrimitiveType {
@Override
public boolean isCompatible(EdmPrimitiveType edmPrimitiveType) {
return false;
}
@Override
public Class<?> getDefaultType() {
return null;
}
@Override
public boolean validate(String s, Boolean aBoolean, Integer integer, Integer integer1, Integer integer2, Boolean aBoolean1) {
return false;
}
@Override
public <T> T valueOfString(String s, Boolean aBoolean, Integer integer, Integer integer1, Integer integer2, Boolean aBoolean1, Class<T> aClass) throws EdmPrimitiveTypeException {
return null;
}
@Override
public String valueToString(Object o, Boolean aBoolean, Integer integer, Integer integer1, Integer integer2, Boolean aBoolean1) throws EdmPrimitiveTypeException {
return null;
}
@Override
public String toUriLiteral(String s) {
return null;
}
@Override
public String fromUriLiteral(String s) throws EdmPrimitiveTypeException {
return null;
}
@Override
public EdmElement getProperty(String s) {
return null;
}
@Override
public List<String> getPropertyNames() {
return null;
}
@Override
public EdmProperty getStructuralProperty(String s) {
return null;
}
@Override
public EdmNavigationProperty getNavigationProperty(String s) {
return null;
}
@Override
public List<String> getNavigationPropertyNames() {
return null;
}
@Override
public EdmStructuredType getBaseType() {
return null;
}
@Override
public boolean compatibleTo(EdmType edmType) {
return false;
}
@Override
public boolean isOpenType() {
return false;
}
@Override
public boolean isAbstract() {
return false;
}
@Override
public EdmAnnotation getAnnotation(EdmTerm edmTerm, String s) {
return null;
}
@Override
public List<EdmAnnotation> getAnnotations() {
return null;
}
@Override
public FullQualifiedName getFullQualifiedName() {
return null;
}
@Override
public String getNamespace() {
return null;
}
@Override
public EdmTypeKind getKind() {
return null;
}
@Override
public String getName() {
return null;
}
}
private Expression parseExpression(final String expressionString)
throws UriParserException, UriValidationException {
UriTokenizer tokenizer = new UriTokenizer(expressionString);
Edm edm = mock(A.class, withSettings().defaultAnswer(RETURNS_SMART_NULLS));
EdmProperty employeeIdTypeEdmElement = mock(EdmProperty.class, RETURNS_SMART_NULLS);
when(edmType.getProperty("EmployeeId")).thenReturn(measureTypeEdmElement);
when(edmType.getKind()).thenReturn(EdmTypeKind.PRIMITIVE);
when(edmType.isCompatible(new EdmDate())).thenReturn(true);
when(employeeIdTypeEdmElement.getName()).thenReturn("EmployeeId");
when(employeeIdTypeEdmElement.getType()).thenReturn(edmType);
when(employeeIdTypeEdmElement.isCollection()).thenReturn(false);
final Expression expression = new ExpressionParser(edm, odata).parse(tokenizer, edmType, null, null);
assertNotNull(expression);
assertTrue(tokenizer.next(UriTokenizer.TokenKind.EOF));
return expression;
}
万一它可以帮助某人,也许可以对其进行优化,并提出自己的建议。