|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Runtime.Remoting; |
| 6 | +using System.Text; |
| 7 | +using net.openstack.Core.Exceptions.Response; |
| 8 | +using OpenStack; |
| 9 | +using Xunit; |
| 10 | +using Xunit.Abstractions; |
| 11 | + |
| 12 | +namespace net.openstack.Providers.Rackspace |
| 13 | +{ |
| 14 | + public class CloudFilesTests |
| 15 | + { |
| 16 | + private const string region = "RegionOne"; |
| 17 | + private readonly CloudFilesProvider _cloudFiles; |
| 18 | + |
| 19 | + public CloudFilesTests(ITestOutputHelper testLog) |
| 20 | + { |
| 21 | + var testOutput = new XunitTraceListener(testLog); |
| 22 | + Trace.Listeners.Add(testOutput); |
| 23 | + OpenStackNet.Tracing.Http.Listeners.Add(testOutput); |
| 24 | + |
| 25 | + var authenticationProvider = TestIdentityProvider.GetIdentityProvider(); |
| 26 | + authenticationProvider.Authenticate(); |
| 27 | + |
| 28 | + _cloudFiles = new CloudFilesProvider(null, authenticationProvider); |
| 29 | + } |
| 30 | + |
| 31 | + public void Dispose() |
| 32 | + { |
| 33 | + Trace.Listeners.Clear(); |
| 34 | + OpenStackNet.Tracing.Http.Listeners.Clear(); |
| 35 | + } |
| 36 | + |
| 37 | + [Fact] |
| 38 | + public void MoveObject() |
| 39 | + { |
| 40 | + const string container = "test"; |
| 41 | + const string fileName = "helloworld.html"; |
| 42 | + const string backupName = "helloworld.bak"; |
| 43 | + |
| 44 | + // Remove old test data |
| 45 | + try |
| 46 | + { |
| 47 | + _cloudFiles.DeleteObject(container, backupName, region: region); |
| 48 | + } |
| 49 | + catch (ItemNotFoundException) |
| 50 | + { |
| 51 | + } |
| 52 | + |
| 53 | + // Create test data |
| 54 | + _cloudFiles.CreateContainer(container, region: region); |
| 55 | + var testObject = new MemoryStream(Encoding.UTF8.GetBytes("hello world")); |
| 56 | + _cloudFiles.CreateObject(container, testObject, fileName, region: region); |
| 57 | + |
| 58 | + // Backup the file |
| 59 | + _cloudFiles.MoveObject(container, fileName, container, backupName, region: region); |
| 60 | + |
| 61 | + // Verify the file moved |
| 62 | + var files = _cloudFiles.ListObjects(container, region: region); |
| 63 | + Assert.False(files.Any(f => f.Name == fileName)); |
| 64 | + Assert.True(files.Any(f => f.Name == backupName)); |
| 65 | + } |
| 66 | + |
| 67 | + [Fact] |
| 68 | + public void MoveObject_ShouldDoNothing_WhenSourceAndDestinationAreTheSame() |
| 69 | + { |
| 70 | + const string container = "test"; |
| 71 | + const string filename = "helloworld.html"; |
| 72 | + |
| 73 | + // Create test data |
| 74 | + _cloudFiles.CreateContainer(container, region: region); |
| 75 | + var testObject = new MemoryStream(Encoding.UTF8.GetBytes("hello world")); |
| 76 | + _cloudFiles.CreateObject(container, testObject, filename, region: region); |
| 77 | + |
| 78 | + // Move the file to the same location |
| 79 | + _cloudFiles.MoveObject(container, filename, container, filename, region: region); |
| 80 | + |
| 81 | + // Verify the file wasn't removed |
| 82 | + var files = _cloudFiles.ListObjects(container, region: region); |
| 83 | + Assert.True(files.Any(f => f.Name == filename)); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments