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 32e2c407f66b447a3b99725b694216b0839e06d8 Author: RĂ©mi Kowalski <rkowal...@linagora.com> AuthorDate: Thu Jul 25 17:02:15 2019 +0200 JAMES-2813 add contract test for serialization of task queries in webadmin --- .../WebadminApiQuerySerializationContractTest.java | 131 +++++++++++++++++++++ .../resources/query/contains_origin_mailbox.json | 14 +++ .../test/resources/query/contains_recipient.json | 14 +++ .../src/test/resources/query/has_attachment.json | 14 +++ .../test/resources/query/has_no_attachment.json | 14 +++ .../src/test/resources/query/has_sender.json | 14 +++ .../src/test/resources/query/string_contains.json | 14 +++ .../query/string_contains_ignore_case.json | 14 +++ .../src/test/resources/query/string_equals.json | 14 +++ .../resources/query/string_equals_ignore_case.json | 14 +++ .../query/zoned_date_time_after_or_equals.json | 14 +++ .../query/zoned_date_time_before_or_equals.json | 14 +++ 12 files changed, 285 insertions(+) diff --git a/server/protocols/webadmin/webadmin-mailbox-deleted-message-vault/src/test/java/org/apache/james/webadmin/vault/routes/WebadminApiQuerySerializationContractTest.java b/server/protocols/webadmin/webadmin-mailbox-deleted-message-vault/src/test/java/org/apache/james/webadmin/vault/routes/WebadminApiQuerySerializationContractTest.java new file mode 100644 index 0000000..32b3d66 --- /dev/null +++ b/server/protocols/webadmin/webadmin-mailbox-deleted-message-vault/src/test/java/org/apache/james/webadmin/vault/routes/WebadminApiQuerySerializationContractTest.java @@ -0,0 +1,131 @@ +/**************************************************************** + * 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.webadmin.vault.routes; + +import java.time.ZonedDateTime; +import java.util.stream.Stream; + +import org.apache.james.mailbox.model.MailboxId; +import org.apache.james.mailbox.model.TestId; +import org.apache.james.server.task.json.JsonTaskSerializer; +import org.apache.james.util.ClassLoaderUtils; +import org.apache.james.vault.dto.query.CriterionDTO; +import org.apache.james.vault.dto.query.QueryDTO; +import org.apache.james.vault.dto.query.QueryTranslator; +import org.apache.james.vault.search.FieldName; +import org.apache.james.vault.search.Operator; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.mockito.Mockito; + +import com.google.common.collect.ImmutableList; + +class WebadminApiQuerySerializationContractTest { + + private static final String AND = "and"; + private static final String USER_JAMES = "ja...@apache.org"; + private static final MailboxId MAILBOX_1_ID = TestId.of(1L); + private static final ZonedDateTime ZONED_DATE_TIME = ZonedDateTime.parse("2007-12-03T10:15:30+01:00[Europe/Paris]"); + private static final String SUBJECT = "James"; + + private final static String HAS_ATTACHMENT_FILE = "has_attachment.json"; + private final static QueryDTO HAS_ATTACHMENT_DTO = new QueryDTO(AND, + ImmutableList.of(new CriterionDTO(FieldName.HAS_ATTACHMENT.getValue(), Operator.EQUALS.getValue(), "true"))); + + private final static String HAS_NO_ATTACHMENT_FILE = "has_no_attachment.json"; + private final static QueryDTO HAS_NO_ATTACHMENT_DTO = new QueryDTO(AND, + ImmutableList.of(new CriterionDTO(FieldName.HAS_ATTACHMENT.getValue(), Operator.EQUALS.getValue(), "false"))); + + private final static String HAS_SENDER_FILE = "has_sender.json"; + private final static QueryDTO HAS_SENDER_DTO = new QueryDTO(AND, + ImmutableList.of(new CriterionDTO(FieldName.SENDER.getValue(), Operator.EQUALS.getValue(), USER_JAMES))); + + private final static String CONTAINS_RECIPIENT_FILE = "contains_recipient.json"; + private final static QueryDTO CONTAINS_RECIPIENT_DTO = new QueryDTO(AND, + ImmutableList.of(new CriterionDTO(FieldName.RECIPIENTS.getValue(), Operator.CONTAINS.getValue(), USER_JAMES))); + + private final static String CONTAINS_ORIGIN_MAILBOX_FILE = "contains_origin_mailbox.json"; + private final static QueryDTO CONTAINS_ORIGIN_MAILBOX_DTO = new QueryDTO(AND, + ImmutableList.of(new CriterionDTO(FieldName.ORIGIN_MAILBOXES.getValue(), Operator.CONTAINS.getValue(), MAILBOX_1_ID.serialize()))); + + private final static String DELIVERY_BEFORE_FILE = "zoned_date_time_before_or_equals.json"; + private final static QueryDTO DELIVERY_BEFORE_DTO = new QueryDTO(AND, + ImmutableList.of(new CriterionDTO(FieldName.DELIVERY_DATE.getValue(), Operator.BEFORE_OR_EQUALS.getValue(), ZONED_DATE_TIME.toString()))); + + private final static String DELETED_AFTER_FILE = "zoned_date_time_after_or_equals.json"; + private final static QueryDTO DELETED_AFTER_DTO = new QueryDTO(AND, + ImmutableList.of(new CriterionDTO(FieldName.DELETION_DATE.getValue(), Operator.AFTER_OR_EQUALS.getValue(), ZONED_DATE_TIME.toString()))); + + private final static String SUBJECT_CONTAINS_FILE = "string_contains.json"; + private final static QueryDTO SUBJECT_CONTAINS_DTO = new QueryDTO(AND, + ImmutableList.of(new CriterionDTO(FieldName.SUBJECT.getValue(), Operator.CONTAINS.getValue(), SUBJECT))); + + private final static String SUBJECT_CONTAINS_IGNORE_CASE_FILE = "string_contains_ignore_case.json"; + private final static QueryDTO SUBJECT_CONTAINS_IGNORE_CASE_DTO = new QueryDTO(AND, + ImmutableList.of(new CriterionDTO(FieldName.SUBJECT.getValue(), Operator.CONTAINS_IGNORE_CASE.getValue(), SUBJECT))); + + private final static String SUBJECT_EQUALS_FILE = "string_equals.json"; + private final static QueryDTO SUBJECT_EQUALS_DTO = new QueryDTO(AND, + ImmutableList.of(new CriterionDTO(FieldName.SUBJECT.getValue(), Operator.EQUALS.getValue(), SUBJECT))); + + private final static String SUBJECT_EQUALS_IGNORE_CASE_FILE = "string_equals_ignore_case.json"; + private final static QueryDTO SUBJECT_EQUALS_IGNORE_CASE_DTO = new QueryDTO(AND, + ImmutableList.of(new CriterionDTO(FieldName.SUBJECT.getValue(), Operator.EQUALS_IGNORE_CASE.getValue(), SUBJECT))); + + private final static TestId.Factory mailboxIdFactory = new TestId.Factory(); + private final static QueryTranslator queryTranslator = new QueryTranslator(mailboxIdFactory); + private final static RestoreService restoreService = Mockito.mock(RestoreService.class); + private final static DeletedMessagesVaultRestoreTask.Factory factory = new DeletedMessagesVaultRestoreTask.Factory(restoreService, queryTranslator); + private final static JsonTaskSerializer taskSerializer = new JsonTaskSerializer(DeletedMessagesVaultRestoreTask.MODULE.apply(factory)); + + /** + * Enforce that the format of the query serialized in json in the body of the request to the webadmin is stable. + * For the time being the query are serialized in the same way for the webadmin API requests and for the EventSystem for the + * distributed task manager. + * If you break on of this test. It's time to use different serialization for the internal representation in the Event System + * and for the user facing format. + * You should then ensure than a query in one part of the system is interpretable in the other part. + */ + @ParameterizedTest + @MethodSource + void respectAPIContract(String jsonFilePath, QueryDTO expectedDeserializedValue) throws Exception { + String jsonContent = ClassLoaderUtils.getSystemResourceAsString("query/" + jsonFilePath); + QueryDTO extractedQueryDTO = factory.createDTO((DeletedMessagesVaultRestoreTask) taskSerializer.deserialize(jsonContent), + DeletedMessagesVaultRestoreTask.TYPE).getQuery(); + Assertions.assertThat(extractedQueryDTO).isEqualTo(expectedDeserializedValue); + } + + static Stream<Arguments> respectAPIContract() { + return Stream.of( + Arguments.of(SUBJECT_EQUALS_FILE, SUBJECT_EQUALS_DTO), + Arguments.of(SUBJECT_EQUALS_IGNORE_CASE_FILE, SUBJECT_EQUALS_IGNORE_CASE_DTO), + Arguments.of(SUBJECT_CONTAINS_FILE, SUBJECT_CONTAINS_DTO), + Arguments.of(SUBJECT_CONTAINS_IGNORE_CASE_FILE, SUBJECT_CONTAINS_IGNORE_CASE_DTO), + Arguments.of(DELETED_AFTER_FILE, DELETED_AFTER_DTO), + Arguments.of(DELIVERY_BEFORE_FILE, DELIVERY_BEFORE_DTO), + Arguments.of(CONTAINS_ORIGIN_MAILBOX_FILE, CONTAINS_ORIGIN_MAILBOX_DTO), + Arguments.of(CONTAINS_RECIPIENT_FILE, CONTAINS_RECIPIENT_DTO), + Arguments.of(HAS_SENDER_FILE, HAS_SENDER_DTO), + Arguments.of(HAS_ATTACHMENT_FILE, HAS_ATTACHMENT_DTO), + Arguments.of(HAS_NO_ATTACHMENT_FILE, HAS_NO_ATTACHMENT_DTO) + ); + } +} diff --git a/server/protocols/webadmin/webadmin-mailbox-deleted-message-vault/src/test/resources/query/contains_origin_mailbox.json b/server/protocols/webadmin/webadmin-mailbox-deleted-message-vault/src/test/resources/query/contains_origin_mailbox.json new file mode 100644 index 0000000..14ba54c --- /dev/null +++ b/server/protocols/webadmin/webadmin-mailbox-deleted-message-vault/src/test/resources/query/contains_origin_mailbox.json @@ -0,0 +1,14 @@ +{ + "type": "deletedMessages/restore", + "userToRestore": "james", + "query": { + "combinator": "and", + "criteria": [ + { + "fieldName": "originMailboxes", + "operator": "contains", + "value": "1" + } + ] + } +} \ No newline at end of file diff --git a/server/protocols/webadmin/webadmin-mailbox-deleted-message-vault/src/test/resources/query/contains_recipient.json b/server/protocols/webadmin/webadmin-mailbox-deleted-message-vault/src/test/resources/query/contains_recipient.json new file mode 100644 index 0000000..4be0216 --- /dev/null +++ b/server/protocols/webadmin/webadmin-mailbox-deleted-message-vault/src/test/resources/query/contains_recipient.json @@ -0,0 +1,14 @@ +{ + "type": "deletedMessages/restore", + "userToRestore": "james", + "query": { + "combinator": "and", + "criteria": [ + { + "fieldName": "recipients", + "operator": "contains", + "value": "ja...@apache.org" + } + ] + } +} \ No newline at end of file diff --git a/server/protocols/webadmin/webadmin-mailbox-deleted-message-vault/src/test/resources/query/has_attachment.json b/server/protocols/webadmin/webadmin-mailbox-deleted-message-vault/src/test/resources/query/has_attachment.json new file mode 100644 index 0000000..c56056e --- /dev/null +++ b/server/protocols/webadmin/webadmin-mailbox-deleted-message-vault/src/test/resources/query/has_attachment.json @@ -0,0 +1,14 @@ +{ + "type": "deletedMessages/restore", + "userToRestore": "james", + "query": { + "combinator": "and", + "criteria": [ + { + "fieldName": "hasAttachment", + "operator": "equals", + "value": "true" + } + ] + } +} \ No newline at end of file diff --git a/server/protocols/webadmin/webadmin-mailbox-deleted-message-vault/src/test/resources/query/has_no_attachment.json b/server/protocols/webadmin/webadmin-mailbox-deleted-message-vault/src/test/resources/query/has_no_attachment.json new file mode 100644 index 0000000..2dcc716 --- /dev/null +++ b/server/protocols/webadmin/webadmin-mailbox-deleted-message-vault/src/test/resources/query/has_no_attachment.json @@ -0,0 +1,14 @@ +{ + "type": "deletedMessages/restore", + "userToRestore": "james", + "query": { + "combinator": "and", + "criteria": [ + { + "fieldName": "hasAttachment", + "operator": "equals", + "value": "false" + } + ] + } +} \ No newline at end of file diff --git a/server/protocols/webadmin/webadmin-mailbox-deleted-message-vault/src/test/resources/query/has_sender.json b/server/protocols/webadmin/webadmin-mailbox-deleted-message-vault/src/test/resources/query/has_sender.json new file mode 100644 index 0000000..91a0212 --- /dev/null +++ b/server/protocols/webadmin/webadmin-mailbox-deleted-message-vault/src/test/resources/query/has_sender.json @@ -0,0 +1,14 @@ +{ + "type": "deletedMessages/restore", + "userToRestore": "james", + "query": { + "combinator": "and", + "criteria": [ + { + "fieldName": "sender", + "operator": "equals", + "value": "ja...@apache.org" + } + ] + } +} \ No newline at end of file diff --git a/server/protocols/webadmin/webadmin-mailbox-deleted-message-vault/src/test/resources/query/string_contains.json b/server/protocols/webadmin/webadmin-mailbox-deleted-message-vault/src/test/resources/query/string_contains.json new file mode 100644 index 0000000..2cad5ec --- /dev/null +++ b/server/protocols/webadmin/webadmin-mailbox-deleted-message-vault/src/test/resources/query/string_contains.json @@ -0,0 +1,14 @@ +{ + "type": "deletedMessages/restore", + "userToRestore": "james", + "query": { + "combinator": "and", + "criteria": [ + { + "fieldName": "subject", + "operator": "contains", + "value": "James" + } + ] + } +} \ No newline at end of file diff --git a/server/protocols/webadmin/webadmin-mailbox-deleted-message-vault/src/test/resources/query/string_contains_ignore_case.json b/server/protocols/webadmin/webadmin-mailbox-deleted-message-vault/src/test/resources/query/string_contains_ignore_case.json new file mode 100644 index 0000000..f585f3a --- /dev/null +++ b/server/protocols/webadmin/webadmin-mailbox-deleted-message-vault/src/test/resources/query/string_contains_ignore_case.json @@ -0,0 +1,14 @@ +{ + "type": "deletedMessages/restore", + "userToRestore": "james", + "query": { + "combinator": "and", + "criteria": [ + { + "fieldName": "subject", + "operator": "containsIgnoreCase", + "value": "James" + } + ] + } +} \ No newline at end of file diff --git a/server/protocols/webadmin/webadmin-mailbox-deleted-message-vault/src/test/resources/query/string_equals.json b/server/protocols/webadmin/webadmin-mailbox-deleted-message-vault/src/test/resources/query/string_equals.json new file mode 100644 index 0000000..2f1f4e9 --- /dev/null +++ b/server/protocols/webadmin/webadmin-mailbox-deleted-message-vault/src/test/resources/query/string_equals.json @@ -0,0 +1,14 @@ +{ + "type": "deletedMessages/restore", + "userToRestore": "james", + "query": { + "combinator": "and", + "criteria": [ + { + "fieldName": "subject", + "operator": "equals", + "value": "James" + } + ] + } +} \ No newline at end of file diff --git a/server/protocols/webadmin/webadmin-mailbox-deleted-message-vault/src/test/resources/query/string_equals_ignore_case.json b/server/protocols/webadmin/webadmin-mailbox-deleted-message-vault/src/test/resources/query/string_equals_ignore_case.json new file mode 100644 index 0000000..d277c6c --- /dev/null +++ b/server/protocols/webadmin/webadmin-mailbox-deleted-message-vault/src/test/resources/query/string_equals_ignore_case.json @@ -0,0 +1,14 @@ +{ + "type": "deletedMessages/restore", + "userToRestore": "james", + "query": { + "combinator": "and", + "criteria": [ + { + "fieldName": "subject", + "operator": "equalsIgnoreCase", + "value": "James" + } + ] + } +} \ No newline at end of file diff --git a/server/protocols/webadmin/webadmin-mailbox-deleted-message-vault/src/test/resources/query/zoned_date_time_after_or_equals.json b/server/protocols/webadmin/webadmin-mailbox-deleted-message-vault/src/test/resources/query/zoned_date_time_after_or_equals.json new file mode 100644 index 0000000..3fff15c --- /dev/null +++ b/server/protocols/webadmin/webadmin-mailbox-deleted-message-vault/src/test/resources/query/zoned_date_time_after_or_equals.json @@ -0,0 +1,14 @@ +{ + "type": "deletedMessages/restore", + "userToRestore": "james", + "query": { + "combinator": "and", + "criteria": [ + { + "fieldName": "deletionDate", + "operator": "afterOrEquals", + "value": "2007-12-03T10:15:30+01:00[Europe/Paris]" + } + ] + } +} \ No newline at end of file diff --git a/server/protocols/webadmin/webadmin-mailbox-deleted-message-vault/src/test/resources/query/zoned_date_time_before_or_equals.json b/server/protocols/webadmin/webadmin-mailbox-deleted-message-vault/src/test/resources/query/zoned_date_time_before_or_equals.json new file mode 100644 index 0000000..ca89aa0 --- /dev/null +++ b/server/protocols/webadmin/webadmin-mailbox-deleted-message-vault/src/test/resources/query/zoned_date_time_before_or_equals.json @@ -0,0 +1,14 @@ +{ + "type": "deletedMessages/restore", + "userToRestore": "james", + "query": { + "combinator": "and", + "criteria": [ + { + "fieldName": "deliveryDate", + "operator": "beforeOrEquals", + "value": "2007-12-03T10:15:30+01:00[Europe/Paris]" + } + ] + } +} \ 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