This is an automated email from the ASF dual-hosted git repository. rouazana pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit d53fef27ad58921acbab6e112b92b82287c24f98 Author: Rémi Kowalski <rkowal...@linagora.com> AuthorDate: Thu Jul 18 11:55:55 2019 +0200 JAMES-2813 add serializer for QueryElement --- .../vault/dto/query/QueryElementSerializer.java | 40 ++++++++++ .../dto/query/QueryElementSerializerTest.java | 90 ++++++++++++++++++++++ 2 files changed, 130 insertions(+) diff --git a/mailbox/plugin/deleted-messages-vault/src/main/java/org/apache/james/vault/dto/query/QueryElementSerializer.java b/mailbox/plugin/deleted-messages-vault/src/main/java/org/apache/james/vault/dto/query/QueryElementSerializer.java new file mode 100644 index 0000000..87608e7 --- /dev/null +++ b/mailbox/plugin/deleted-messages-vault/src/main/java/org/apache/james/vault/dto/query/QueryElementSerializer.java @@ -0,0 +1,40 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.vault.dto.query; + +import java.io.IOException; + +import javax.inject.Inject; + +import com.fasterxml.jackson.databind.ObjectMapper; + +public class QueryElementSerializer { + + private final ObjectMapper objectMapper; + + @Inject + public QueryElementSerializer(ObjectMapper objectMapper) { + this.objectMapper = objectMapper; + } + + public String serialize(QueryElement queryElement) throws IOException { + return objectMapper.writeValueAsString(queryElement); + } +} diff --git a/mailbox/plugin/deleted-messages-vault/src/test/java/org/apache/james/vault/dto/query/QueryElementSerializerTest.java b/mailbox/plugin/deleted-messages-vault/src/test/java/org/apache/james/vault/dto/query/QueryElementSerializerTest.java new file mode 100644 index 0000000..37c98b8 --- /dev/null +++ b/mailbox/plugin/deleted-messages-vault/src/test/java/org/apache/james/vault/dto/query/QueryElementSerializerTest.java @@ -0,0 +1,90 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.vault.dto.query; + +import static org.apache.james.vault.DeletedMessageFixture.SUBJECT; +import static org.apache.mailet.base.MailAddressFixture.SENDER; +import static org.assertj.core.api.Assertions.assertThat; + +import org.apache.james.vault.dto.query.QueryTranslator.FieldName; +import org.apache.james.vault.dto.query.QueryTranslator.Operator; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import com.fasterxml.jackson.databind.ObjectMapper; +import net.javacrumbs.jsonunit.assertj.JsonAssertions; + +class QueryElementSerializerTest { + + private QueryElementSerializer queryElementSerializer; + + @BeforeEach + void beforeEach() { + ObjectMapper objectMapper = new ObjectMapper(); + queryElementSerializer = new QueryElementSerializer(objectMapper); + } + + @Test + void shouldSerializeNestedStructure() throws Exception { + + QueryDTO queryDTO = QueryDTO.and( + QueryDTO.and( + CriterionDTO.from(FieldName.SUBJECT, Operator.CONTAINS, SUBJECT), + CriterionDTO.from(FieldName.SENDER, Operator.EQUALS, SENDER.asString())), + CriterionDTO.from(FieldName.HAS_ATTACHMENT, Operator.EQUALS, "true") + ); + + JsonAssertions.assertThatJson(queryElementSerializer.serialize(queryDTO)) + .isEqualTo("{ " + + " \"combinator\": \"and\", " + + " \"criteria\": [ " + + " { " + + " \"combinator\": \"and\", " + + " \"criteria\": [ " + + " {\"fieldName\": \"subject\", \"operator\": \"contains\", \"value\": \"" + SUBJECT + "\"}," + + " {\"fieldName\": \"sender\", \"operator\": \"equals\", \"value\": \"" + SENDER.asString() + "\"}" + + " ] " + + " }, " + + " {\"fieldName\": \"hasAttachment\", \"operator\": \"equals\", \"value\": \"true\"}" + + " ] " + + "} "); + } + + @Test + void shouldSerializeFlattenStructure() throws Exception { + + QueryDTO queryDTO = QueryDTO.and( + CriterionDTO.from(FieldName.SUBJECT, Operator.CONTAINS, SUBJECT), + CriterionDTO.from(FieldName.SENDER, Operator.EQUALS, SENDER.asString()), + CriterionDTO.from(FieldName.HAS_ATTACHMENT, Operator.EQUALS, "true") + ); + + JsonAssertions.assertThatJson(queryElementSerializer.serialize(queryDTO)) + .isEqualTo("{ " + + " \"combinator\": \"and\", " + + " \"criteria\": [ " + + " {\"fieldName\": \"subject\", \"operator\": \"contains\", \"value\": \"" + SUBJECT + "\"}," + + " {\"fieldName\": \"sender\", \"operator\": \"equals\", \"value\": \"" + SENDER.asString() + "\"}," + + " {\"fieldName\": \"hasAttachment\", \"operator\": \"equals\", \"value\": \"true\"}" + + " ] " + + "} " + ); + } +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org