• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.google.android.libraries.mobiledatadownload.file.openers;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 import static org.junit.Assert.assertThrows;
20 
21 import android.net.Uri;
22 import com.google.android.libraries.mobiledatadownload.file.SynchronousFileStorage;
23 import com.google.android.libraries.mobiledatadownload.file.backends.JavaFileBackend;
24 import com.google.android.libraries.mobiledatadownload.file.common.testing.TemporaryUri;
25 import com.google.android.libraries.mobiledatadownload.file.transforms.CompressTransform;
26 import com.google.android.libraries.mobiledatadownload.file.transforms.TransformProtos;
27 import com.google.android.libraries.storage.file.common.testing.TestMessageProto.ExtendableProto;
28 import com.google.android.libraries.storage.file.common.testing.TestMessageProto.ExtensionProto;
29 import com.google.android.libraries.storage.file.common.testing.TestMessageProto.FooProto;
30 import com.google.protobuf.ExtensionRegistryLite;
31 import com.google.protobuf.InvalidProtocolBufferException;
32 import java.util.Arrays;
33 import org.junit.Before;
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.robolectric.RobolectricTestRunner;
38 
39 @RunWith(RobolectricTestRunner.class)
40 public final class ReadProtoOpenerTest {
41 
42   private static final FooProto TEST_PROTO =
43       FooProto.newBuilder().setText("foo text").setBoolean(true).build();
44 
45   private SynchronousFileStorage storage;
46 
47   @Rule public TemporaryUri tmpUri = new TemporaryUri();
48 
49   @Before
setUpStorage()50   public void setUpStorage() throws Exception {
51     storage =
52         new SynchronousFileStorage(
53             Arrays.asList(new JavaFileBackend()), Arrays.asList(new CompressTransform()));
54   }
55 
56   @Test
create_fromMessageParser_returnsOpenerWithCorrectGenericType()57   public void create_fromMessageParser_returnsOpenerWithCorrectGenericType() throws Exception {
58     Uri uri = tmpUri.newUri();
59     storage.open(uri, WriteProtoOpener.create(TEST_PROTO));
60 
61     ReadProtoOpener<FooProto> opener = ReadProtoOpener.create(FooProto.parser());
62     FooProto unusedToCheckCompilation = storage.open(uri, opener);
63 
64     // Ensure Java compiler can infer the correct generic when Opener is passed in directly
65     unusedToCheckCompilation = storage.open(uri, ReadProtoOpener.create(FooProto.parser()));
66   }
67 
68   @Test
create_fromMessageInstance_returnsOpenerWithCorrectGenericType()69   public void create_fromMessageInstance_returnsOpenerWithCorrectGenericType() throws Exception {
70     Uri uri = tmpUri.newUri();
71     storage.open(uri, WriteProtoOpener.create(TEST_PROTO));
72 
73     ReadProtoOpener<FooProto> opener = ReadProtoOpener.create(TEST_PROTO);
74     FooProto unusedToCheckCompilation = storage.open(uri, opener);
75 
76     // Ensure Java compiler can infer the correct generic when Opener is passed in directly
77     unusedToCheckCompilation = storage.open(uri, ReadProtoOpener.create(TEST_PROTO));
78   }
79 
80   @Test
open_readsFullProtoFromFile()81   public void open_readsFullProtoFromFile() throws Exception {
82     Uri uri = tmpUri.newUri();
83     storage.open(uri, WriteProtoOpener.create(TEST_PROTO));
84 
85     assertThat(storage.open(uri, ReadProtoOpener.create(FooProto.parser()))).isEqualTo(TEST_PROTO);
86     assertThat(storage.open(uri, ReadProtoOpener.create(TEST_PROTO))).isEqualTo(TEST_PROTO);
87   }
88 
89   @Test
open_readsEmptyProtoFromFile()90   public void open_readsEmptyProtoFromFile() throws Exception {
91     FooProto emptyProto = FooProto.getDefaultInstance();
92     Uri uri = tmpUri.newUri();
93     storage.open(uri, WriteProtoOpener.create(emptyProto));
94 
95     assertThat(storage.open(uri, ReadProtoOpener.create(FooProto.parser()))).isEqualTo(emptyProto);
96   }
97 
98   @Test
open_invokesTransforms()99   public void open_invokesTransforms() throws Exception {
100     Uri uri = tmpUri.newUriBuilder().withTransform(TransformProtos.DEFAULT_COMPRESS_SPEC).build();
101     storage.open(uri, WriteProtoOpener.create(TEST_PROTO));
102 
103     assertThat(storage.open(uri, ReadProtoOpener.create(FooProto.parser()))).isEqualTo(TEST_PROTO);
104   }
105 
106   @Test
open_throwsIOExceptionOnBadParse()107   public void open_throwsIOExceptionOnBadParse() throws Exception {
108     Uri uri = tmpUri.newUri();
109     storage.open(uri, WriteStringOpener.create("not a proto"));
110 
111     assertThrows(
112         InvalidProtocolBufferException.class,
113         () -> storage.open(uri, ReadProtoOpener.create(FooProto.parser())));
114   }
115 
116   @Test
withRegistry_readsExtension()117   public void withRegistry_readsExtension() throws Exception {
118     Uri uri = tmpUri.newUri();
119     ExtendableProto extendable = createProtoWithExtension();
120     storage.open(uri, WriteProtoOpener.create(extendable));
121 
122     ReadProtoOpener<ExtendableProto> opener =
123         ReadProtoOpener.create(ExtendableProto.parser())
124             .withExtensionRegistry(ExtensionRegistryLite.getGeneratedRegistry());
125 
126     ExtendableProto actualExtendable = storage.open(uri, opener);
127     assertThat(actualExtendable.hasExtension(ExtensionProto.extension)).isTrue();
128     assertThat(actualExtendable.getExtension(ExtensionProto.extension).getFoo().getText())
129         .isEqualTo("foo text");
130   }
131 
132   @Test
withOutRegistry_failsToReadsExtension()133   public void withOutRegistry_failsToReadsExtension() throws Exception {
134     Uri uri = tmpUri.newUri();
135     ExtendableProto extendable = createProtoWithExtension();
136     storage.open(uri, WriteProtoOpener.create(extendable));
137 
138     ReadProtoOpener<ExtendableProto> opener = ReadProtoOpener.create(ExtendableProto.parser());
139 
140     ExtendableProto actualExtendable = storage.open(uri, opener);
141     assertThat(actualExtendable.hasExtension(ExtensionProto.extension)).isFalse();
142   }
143 
createProtoWithExtension()144   private ExtendableProto createProtoWithExtension() {
145     ExtendableProto extendable =
146         ExtendableProto.newBuilder()
147             .setExtension(
148                 ExtensionProto.extension, ExtensionProto.newBuilder().setFoo(TEST_PROTO).build())
149             .build();
150     return extendable;
151   }
152 }
153