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.behaviors.SyncingBehavior; 24 import com.google.android.libraries.mobiledatadownload.file.common.testing.FakeFileBackend; 25 import com.google.android.libraries.mobiledatadownload.file.common.testing.TemporaryUri; 26 import com.google.android.libraries.mobiledatadownload.file.transforms.CompressTransform; 27 import com.google.android.libraries.storage.file.common.testing.TestMessageProto.FooProto; 28 import java.io.IOException; 29 import java.util.Arrays; 30 import org.junit.Before; 31 import org.junit.Rule; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.mockito.Mockito; 35 import org.robolectric.RobolectricTestRunner; 36 37 @RunWith(RobolectricTestRunner.class) 38 public final class WriteProtoOpenerTest { 39 40 private static final FooProto TEST_PROTO = 41 FooProto.newBuilder() 42 .setText("all work and no play makes jack a dull boy all work and no play makes jack a") 43 .setBoolean(true) 44 .build(); 45 46 private SynchronousFileStorage storage; 47 private final FakeFileBackend backend = new FakeFileBackend(); 48 49 @Rule public TemporaryUri tmpUri = new TemporaryUri(); 50 51 @Before setUpStorage()52 public void setUpStorage() throws Exception { 53 storage = 54 new SynchronousFileStorage(Arrays.asList(backend), Arrays.asList(new CompressTransform())); 55 } 56 57 @Test writesProto()58 public void writesProto() throws Exception { 59 Uri uri = tmpUri.newUri(); 60 storage.open(uri, WriteProtoOpener.create(TEST_PROTO)); 61 62 FooProto actual = storage.open(uri, ReadProtoOpener.create(FooProto.parser())); 63 assertThat(actual).isEqualTo(TEST_PROTO); 64 } 65 66 @Test writesProto_withTransform()67 public void writesProto_withTransform() throws Exception { 68 Uri uri = tmpUri.newUri().buildUpon().encodedFragment("transform=compress").build(); 69 storage.open(uri, WriteProtoOpener.create(TEST_PROTO)); 70 71 FooProto actual = storage.open(uri, ReadProtoOpener.create(FooProto.parser())); 72 assertThat(actual).isEqualTo(TEST_PROTO); 73 assertThat(storage.fileSize(uri)).isLessThan(TEST_PROTO.getSerializedSize()); 74 } 75 76 @Test failedWrite_noChange()77 public void failedWrite_noChange() throws Exception { 78 Uri uri = tmpUri.newUri(); 79 storage.open(uri, WriteProtoOpener.create(TEST_PROTO)); 80 81 FooProto modifiedProto = TEST_PROTO.toBuilder().setBoolean(false).build(); 82 IOException expected = new IOException("expected"); 83 backend.setFailure(FakeFileBackend.OperationType.WRITE_STREAM, expected); 84 85 assertThrows( 86 IOException.class, () -> storage.open(uri, WriteProtoOpener.create(modifiedProto))); 87 88 FooProto actual = storage.open(uri, ReadProtoOpener.create(FooProto.parser())); 89 assertThat(actual).isEqualTo(TEST_PROTO); 90 } 91 92 @Test failedRename_noChange()93 public void failedRename_noChange() throws Exception { 94 Uri uri = tmpUri.newUri(); 95 storage.open(uri, WriteProtoOpener.create(TEST_PROTO)); 96 97 FooProto modifiedProto = TEST_PROTO.toBuilder().setBoolean(false).build(); 98 IOException expected = new IOException("expected"); 99 backend.setFailure(FakeFileBackend.OperationType.MANAGE, expected); 100 101 assertThrows( 102 IOException.class, () -> storage.open(uri, WriteProtoOpener.create(modifiedProto))); 103 104 FooProto actual = storage.open(uri, ReadProtoOpener.create(FooProto.parser())); 105 assertThat(actual).isEqualTo(TEST_PROTO); 106 } 107 108 @Test invokes_autoSync()109 public void invokes_autoSync() throws Exception { 110 Uri uri1 = tmpUri.newUri(); 111 SyncingBehavior syncing = Mockito.spy(new SyncingBehavior()); 112 storage.open(uri1, WriteProtoOpener.create(TEST_PROTO).withBehaviors(syncing)); 113 Mockito.verify(syncing).sync(); 114 } 115 } 116