1
+ /*
2
+ * Copyright 2015 Google Inc. All Rights Reserved.
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
+
17
+ package com .google .gcloud .storage ;
18
+
19
+ import static com .google .gcloud .storage .Storage .PredefinedAcl .PUBLIC_READ ;
20
+ import static org .junit .Assert .assertEquals ;
21
+
22
+ import com .google .common .collect .ImmutableList ;
23
+ import com .google .gcloud .storage .Storage .BlobSourceOption ;
24
+ import com .google .gcloud .storage .Storage .BlobTargetOption ;
25
+
26
+ import org .junit .Rule ;
27
+ import org .junit .Test ;
28
+ import org .junit .rules .ExpectedException ;
29
+
30
+ public class CopyRequestTest {
31
+
32
+ private static final String SOURCE_BUCKET_NAME = "b0" ;
33
+ private static final String SOURCE_BLOB_NAME = "o0" ;
34
+ private static final String TARGET_BUCKET_NAME = "b1" ;
35
+ private static final String TARGET_BLOB_NAME = "o1" ;
36
+ private static final String TARGET_BLOB_CONTENT_TYPE = "contentType" ;
37
+ private static final BlobId SOURCE_BLOB_ID = BlobId .of (SOURCE_BUCKET_NAME , SOURCE_BLOB_NAME );
38
+ private static final BlobId TARGET_BLOB_ID = BlobId .of (TARGET_BUCKET_NAME , TARGET_BLOB_NAME );
39
+ private static final BlobInfo TARGET_BLOB_INFO = BlobInfo .builder (TARGET_BLOB_ID )
40
+ .contentType (TARGET_BLOB_CONTENT_TYPE ).build ();
41
+
42
+ @ Rule
43
+ public ExpectedException thrown = ExpectedException .none ();
44
+
45
+ @ Test
46
+ public void testCopyRequest () {
47
+ Storage .CopyRequest copyRequest1 = Storage .CopyRequest .builder ()
48
+ .source (SOURCE_BLOB_ID )
49
+ .sourceOptions (BlobSourceOption .generationMatch (1 ))
50
+ .target (TARGET_BLOB_INFO , BlobTargetOption .predefinedAcl (PUBLIC_READ ))
51
+ .build ();
52
+ assertEquals (SOURCE_BLOB_ID , copyRequest1 .source ());
53
+ assertEquals (1 , copyRequest1 .sourceOptions ().size ());
54
+ assertEquals (BlobSourceOption .generationMatch (1 ), copyRequest1 .sourceOptions ().get (0 ));
55
+ assertEquals (TARGET_BLOB_INFO , copyRequest1 .target ());
56
+ assertEquals (1 , copyRequest1 .targetOptions ().size ());
57
+ assertEquals (BlobTargetOption .predefinedAcl (PUBLIC_READ ), copyRequest1 .targetOptions ().get (0 ));
58
+
59
+ Storage .CopyRequest copyRequest2 = Storage .CopyRequest .builder ()
60
+ .source (SOURCE_BUCKET_NAME , SOURCE_BLOB_NAME )
61
+ .target (TARGET_BLOB_ID )
62
+ .build ();
63
+ assertEquals (SOURCE_BLOB_ID , copyRequest2 .source ());
64
+ assertEquals (BlobInfo .builder (TARGET_BLOB_ID ).build (), copyRequest2 .target ());
65
+
66
+ Storage .CopyRequest copyRequest3 = Storage .CopyRequest .builder ()
67
+ .source (SOURCE_BLOB_ID )
68
+ .target (TARGET_BLOB_INFO , ImmutableList .of (BlobTargetOption .predefinedAcl (PUBLIC_READ )))
69
+ .build ();
70
+ assertEquals (SOURCE_BLOB_ID , copyRequest3 .source ());
71
+ assertEquals (TARGET_BLOB_INFO , copyRequest3 .target ());
72
+ assertEquals (ImmutableList .of (BlobTargetOption .predefinedAcl (PUBLIC_READ )),
73
+ copyRequest3 .targetOptions ());
74
+ }
75
+
76
+ @ Test
77
+ public void testCopyRequestOf () {
78
+ Storage .CopyRequest copyRequest1 = Storage .CopyRequest .of (SOURCE_BLOB_ID , TARGET_BLOB_INFO );
79
+ assertEquals (SOURCE_BLOB_ID , copyRequest1 .source ());
80
+ assertEquals (TARGET_BLOB_INFO , copyRequest1 .target ());
81
+
82
+ Storage .CopyRequest copyRequest2 = Storage .CopyRequest .of (SOURCE_BLOB_ID , TARGET_BLOB_NAME );
83
+ assertEquals (SOURCE_BLOB_ID , copyRequest2 .source ());
84
+ assertEquals (BlobInfo .builder (SOURCE_BUCKET_NAME , TARGET_BLOB_NAME ).build (),
85
+ copyRequest2 .target ());
86
+
87
+ Storage .CopyRequest copyRequest3 =
88
+ Storage .CopyRequest .of (SOURCE_BUCKET_NAME , SOURCE_BLOB_NAME , TARGET_BLOB_INFO );
89
+ assertEquals (SOURCE_BLOB_ID , copyRequest3 .source ());
90
+ assertEquals (TARGET_BLOB_INFO , copyRequest3 .target ());
91
+
92
+ Storage .CopyRequest copyRequest4 =
93
+ Storage .CopyRequest .of (SOURCE_BUCKET_NAME , SOURCE_BLOB_NAME , TARGET_BLOB_NAME );
94
+ assertEquals (SOURCE_BLOB_ID , copyRequest4 .source ());
95
+ assertEquals (BlobInfo .builder (SOURCE_BUCKET_NAME , TARGET_BLOB_NAME ).build (),
96
+ copyRequest4 .target ());
97
+
98
+ Storage .CopyRequest copyRequest5 = Storage .CopyRequest .of (SOURCE_BLOB_ID , TARGET_BLOB_ID );
99
+ assertEquals (SOURCE_BLOB_ID , copyRequest5 .source ());
100
+ assertEquals (BlobInfo .builder (TARGET_BLOB_ID ).build (), copyRequest5 .target ());
101
+
102
+ Storage .CopyRequest copyRequest6 =
103
+ Storage .CopyRequest .of (SOURCE_BUCKET_NAME , SOURCE_BLOB_NAME , TARGET_BLOB_ID );
104
+ assertEquals (SOURCE_BLOB_ID , copyRequest6 .source ());
105
+ assertEquals (BlobInfo .builder (TARGET_BLOB_ID ).build (), copyRequest6 .target ());
106
+ }
107
+
108
+ @ Test
109
+ public void testCopyRequestFail () {
110
+ thrown .expect (IllegalArgumentException .class );
111
+ Storage .CopyRequest .builder ()
112
+ .source (SOURCE_BLOB_ID )
113
+ .target (BlobInfo .builder (TARGET_BLOB_ID ).build ())
114
+ .build ();
115
+ }
116
+
117
+ @ Test
118
+ public void testCopyRequestOfBlobInfoFail () {
119
+ thrown .expect (IllegalArgumentException .class );
120
+ Storage .CopyRequest .of (SOURCE_BLOB_ID , BlobInfo .builder (TARGET_BLOB_ID ).build ());
121
+ }
122
+
123
+ @ Test
124
+ public void testCopyRequestOfStringFail () {
125
+ thrown .expect (IllegalArgumentException .class );
126
+ Storage .CopyRequest .of (
127
+ SOURCE_BUCKET_NAME , SOURCE_BLOB_NAME , BlobInfo .builder (TARGET_BLOB_ID ).build ());
128
+ }
129
+ }
0 commit comments