This is an automated email from the ASF dual-hosted git repository. rcordier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit b73348ca0d22431477625358ff916601066dafcf Author: Tran Tien Duc <[email protected]> AuthorDate: Thu Nov 21 17:08:25 2019 +0700 JAMES-2989 Preview POJO The model is used in MessagePreviewStore, representing message previews of JMAP getMessages responses --- .../org/apache/james/jmap/api/preview/Preview.java | 64 +++++++++++++++++++ .../apache/james/jmap/api/preview/PreviewTest.java | 73 ++++++++++++++++++++++ 2 files changed, 137 insertions(+) diff --git a/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/preview/Preview.java b/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/preview/Preview.java new file mode 100644 index 0000000..0d4a208 --- /dev/null +++ b/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/preview/Preview.java @@ -0,0 +1,64 @@ +/**************************************************************** + * 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.jmap.api.preview; + +import java.util.Objects; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Preconditions; + +public class Preview { + + private static final int MAX_LENGTH = 256; + + public static Preview from(String value) { + return new Preview(value); + } + + private final String value; + + @VisibleForTesting + Preview(String value) { + Preconditions.checkNotNull(value); + Preconditions.checkArgument(value.length() <= MAX_LENGTH, + String.format("the preview value '%s' has length longer than %d", value, MAX_LENGTH)); + + this.value = value; + } + + public String getValue() { + return value; + } + + @Override + public final boolean equals(Object o) { + if (o instanceof Preview) { + Preview preview = (Preview) o; + + return Objects.equals(this.value, preview.value); + } + return false; + } + + @Override + public final int hashCode() { + return Objects.hash(value); + } +} diff --git a/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/preview/PreviewTest.java b/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/preview/PreviewTest.java new file mode 100644 index 0000000..8298f5d --- /dev/null +++ b/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/preview/PreviewTest.java @@ -0,0 +1,73 @@ +/**************************************************************** + * 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.jmap.api.preview; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.junit.jupiter.api.Test; + +import com.google.common.base.Strings; + +import nl.jqno.equalsverifier.EqualsVerifier; + +class PreviewTest { + + private static final String PREVIEW_RAW_VALUE = "Hello James!"; + + @Test + void shouldMatchBeanContract() { + EqualsVerifier.forClass(Preview.class) + .verify(); + } + + @Test + void getValueShouldReturnCorrectPreviewString() { + assertThat(new Preview(PREVIEW_RAW_VALUE).getValue()) + .isEqualTo(PREVIEW_RAW_VALUE); + } + + @Test + void fromShouldReturnACorrectPreview() { + assertThat(Preview.from(PREVIEW_RAW_VALUE)) + .isEqualTo(new Preview(PREVIEW_RAW_VALUE)); + } + + @Test + void fromShouldThrowWhenNullValue() { + assertThatThrownBy(() -> Preview.from(null)) + .isInstanceOf(NullPointerException.class); + } + + @Test + void fromShouldThrowWhenValueLengthIsLongerThanMaximum256() { + String errorMessageRegex = "the preview value '.*' has length longer than 256"; + assertThatThrownBy(() -> Preview.from(Strings.repeat("a", 257))) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageMatching(errorMessageRegex); + } + + @Test + void fromShouldNotThrowWhenValueLengthIsEqualsToMaximum256() { + assertThatCode(() -> Preview.from(Strings.repeat("a", 256))) + .doesNotThrowAnyException(); + } +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
