|
| 1 | +/** |
| 2 | + * (C) Copyright IBM Corp. 2019. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +package com.ibm.cloud.sdk.core.test.model; |
| 15 | + |
| 16 | +import com.ibm.cloud.sdk.core.service.model.FileWithMetadata; |
| 17 | +import org.testng.annotations.Test; |
| 18 | + |
| 19 | +import java.io.ByteArrayInputStream; |
| 20 | +import java.io.File; |
| 21 | +import java.io.FileNotFoundException; |
| 22 | +import java.io.InputStream; |
| 23 | + |
| 24 | +import static org.testng.Assert.assertEquals; |
| 25 | +import static org.testng.Assert.assertNotNull; |
| 26 | +import static org.testng.Assert.assertNull; |
| 27 | + |
| 28 | +/** |
| 29 | + * A few simple tests that exercise the FileWithMetadata model. |
| 30 | + */ |
| 31 | +public class FileWithMetadataTest { |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testBuilderWithOnlyRequired() { |
| 35 | + byte[] fileBytes = {(byte) 0xde, (byte) 0xad, (byte) 0xbe, (byte) 0xef}; |
| 36 | + InputStream inputStream = new ByteArrayInputStream(fileBytes); |
| 37 | + FileWithMetadata.Builder builder = new FileWithMetadata.Builder(); |
| 38 | + builder.data(inputStream); |
| 39 | + FileWithMetadata fileWithMetadata = builder.build(); |
| 40 | + assertNotNull(fileWithMetadata); |
| 41 | + assertNotNull(fileWithMetadata.data()); |
| 42 | + assertNull(fileWithMetadata.filename()); |
| 43 | + assertNull(fileWithMetadata.contentType()); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testBuilderWithFile() throws FileNotFoundException { |
| 48 | + String filename = "my-credentials.env"; |
| 49 | + final File myFile = new File("src/test/resources/" + filename); |
| 50 | + FileWithMetadata.Builder builder = new FileWithMetadata.Builder(); |
| 51 | + builder.data(myFile); |
| 52 | + FileWithMetadata fileWithMetadata = builder.build(); |
| 53 | + assertNotNull(fileWithMetadata); |
| 54 | + assertNotNull(fileWithMetadata.data()); |
| 55 | + assertEquals(filename, fileWithMetadata.filename()); |
| 56 | + assertNull(fileWithMetadata.contentType()); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testBuilderWithAllProps() { |
| 61 | + byte[] fileBytes = {(byte) 0xca, (byte) 0xfe, (byte) 0xba, (byte) 0xbe}; |
| 62 | + String filename = "my-awesome-file.txt"; |
| 63 | + String contentType = "text/plain"; |
| 64 | + InputStream inputStream = new ByteArrayInputStream(fileBytes); |
| 65 | + FileWithMetadata.Builder builder = new FileWithMetadata.Builder(inputStream); |
| 66 | + builder.filename(filename); |
| 67 | + builder.contentType(contentType); |
| 68 | + FileWithMetadata fileWithMetadata = builder.build(); |
| 69 | + assertNotNull(fileWithMetadata); |
| 70 | + assertNotNull(fileWithMetadata.data()); |
| 71 | + assertEquals(filename, fileWithMetadata.filename()); |
| 72 | + assertEquals(contentType, fileWithMetadata.contentType()); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testBuilderConstructorWithFile() throws FileNotFoundException { |
| 77 | + String filename = "my-credentials.env"; |
| 78 | + final File myFile = new File("src/test/resources/" + filename); |
| 79 | + FileWithMetadata fileWithMetadata = new FileWithMetadata.Builder(myFile).build(); |
| 80 | + assertNotNull(fileWithMetadata); |
| 81 | + assertNotNull(fileWithMetadata.data()); |
| 82 | + assertEquals(filename, fileWithMetadata.filename()); |
| 83 | + assertNull(fileWithMetadata.contentType()); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void testNewBuilder() { |
| 88 | + byte[] fileBytes = {(byte) 0xca, (byte) 0xfe, (byte) 0xba, (byte) 0xbe}; |
| 89 | + String filename = "my-awesome-file.txt"; |
| 90 | + String contentType = "text/plain"; |
| 91 | + FileWithMetadata orig = new FileWithMetadata.Builder() |
| 92 | + .data(new ByteArrayInputStream(fileBytes)) |
| 93 | + .filename("foo.txt") |
| 94 | + .contentType("text/plain") |
| 95 | + .build(); |
| 96 | + FileWithMetadata.Builder builder = orig.newBuilder(); |
| 97 | + builder.filename(filename); |
| 98 | + builder.contentType(contentType); |
| 99 | + FileWithMetadata fileWithMetadata = builder.build(); |
| 100 | + assertNotNull(fileWithMetadata); |
| 101 | + assertNotNull(fileWithMetadata.data()); |
| 102 | + assertEquals(filename, fileWithMetadata.filename()); |
| 103 | + assertEquals(contentType, fileWithMetadata.contentType()); |
| 104 | + } |
| 105 | + |
| 106 | + @Test(expectedExceptions = {IllegalArgumentException.class}) |
| 107 | + public void testValidation() { |
| 108 | + FileWithMetadata.Builder builder = new FileWithMetadata.Builder(); |
| 109 | + FileWithMetadata fileWithMetadata = builder.build(); |
| 110 | + } |
| 111 | +} |
0 commit comments