@@ -0,0 +1,1865 @@
+// Copyright (C) 2022 The Android Open Source Project%0A//%0A// Licensed under the Apache License, Version 2.0 (the %22License%22);%0A// you may not use this file except in compliance with the License.%0A// You may obtain a copy of the License at%0A//%0A// http://www.apache.org/licenses/LICENSE-2.0%0A//%0A// Unless required by applicable law or agreed to in writing, software%0A// distributed under the License is distributed on an %22AS IS%22 BASIS,%0A// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A// See the License for the specific language governing permissions and%0A// limitations under the License.%0A%0Apackage com.google.gerrit.server.notedb;%0A%0Aimport static com.google.common.truth.Truth.assertThat;%0Aimport static com.google.common.truth.Truth8.assertThat;%0A%0Aimport com.google.gson.Gson;%0Aimport com.google.inject.TypeLiteral;%0Aimport java.util.Optional;%0Aimport org.junit.Test;%0A%0Apublic class ChangeNoteJsonTest %7B%0A  private final Gson gson = new ChangeNoteJson().getGson();%0A%0A  @Test%0A  public void shouldSerializeAndDeserializeEmptyOptional() %7B%0A    // given%0A    Optional%3C?%3E empty = Optional.empty();%0A%0A    // when%0A    String json = gson.toJson(empty);%0A%0A    // then%0A    assertThat(json).isEqualTo(%22%7B%7D%22);%0A%0A    // and when%0A    Optional%3C?%3E result = gson.fromJson(json, Optional.class);%0A%0A    // and then%0A    assertThat(result).isEmpty();%0A  %7D%0A%0A  @Test%0A  public void shouldSerializeAndDeserializeNonEmptyOptional() %7B%0A    // given%0A    String value = %22foo%22;%0A    Optional%3CString%3E nonEmpty = Optional.of(value);%0A%0A    // when%0A    String json = gson.toJson(nonEmpty);%0A%0A    // then%0A    assertThat(json).isEqualTo(%22%7B%5Cn  %5C%22value%5C%22: %5C%22%22 + value + %22%5C%22%5Cn%7D%22);%0A%0A    // and when%0A    Optional%3CString%3E result = gson.fromJson(json, new TypeLiteral%3COptional%3CString%3E%3E() %7B%7D.getType());%0A%0A    // and then%0A    assertThat(result).isPresent();%0A    assertThat(result.get()).isEqualTo(value);%0A  %7D%0A%7D%0A

Add Optional type JSON (de)serialization unit tests