Ejemplo n.º 1
0
        public void RequestWithWrongProtocol()
        {
            var uri = new Uri("http://www.silkveil.net");
            var fileContentSource = new FileContentSource();

            var called = 0;
            fileContentSource.ContentAvailable += stream => called++;

            fileContentSource.Request(uri);

            Assert.That(called, Is.EqualTo(0));
        }
Ejemplo n.º 2
0
        public void RequestNonExistentFileThrowsContentNotFoundException()
        {
            string nonExistentFile;
            do
            {
                nonExistentFile = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
            } while (System.IO.File.Exists(nonExistentFile));

            var fileContentSource = new FileContentSource();

            Assert.That(
                () => fileContentSource.Request(new Uri(nonExistentFile)),
                Throws.Exception.TypeOf(typeof(ContentNotFoundException)));
        }
Ejemplo n.º 3
0
    public async Task UpsertAssetAsync_DynamicallyTyped_WithFileContent_IdentifierIsNull_Throws()
    {
        var client = _fileSystemFixture.CreateMockClientWithoutResponse();

        var content = new FileContentSource(
            new MemoryStream(Encoding.UTF8.GetBytes("Hello world from CM API .NET SDK")),
            "Hello.txt",
            "text/plain");

        var updateModel = new AssetUpsertModel {
            Title = "xxx"
        };

        await client.Invoking(c => c.UpsertAssetAsync(null, content, updateModel))
        .Should().ThrowExactlyAsync <ArgumentNullException>();
    }
Ejemplo n.º 4
0
        public void RequestFile()
        {
            int    size    = 1024;
            string content = new string('\u0000', size);

            var uri = CreateRandomTempFile(content);
            var fileContentSource = new FileContentSource();

            fileContentSource.ContentAvailable += stream =>
            {
                Assert.That(stream.Length, Is.EqualTo(size));
                using (var streamReader = new StreamReader(stream))
                {
                    Assert.That(streamReader.ReadToEnd(), Is.EqualTo(content));
                }
            };
            fileContentSource.Request(uri);
        }
Ejemplo n.º 5
0
        public void RequestFile()
        {
            int size = 1024;
            string content = new string('\u0000', size);

            var uri = CreateRandomTempFile(content);
            var fileContentSource = new FileContentSource();

            fileContentSource.ContentAvailable += stream =>
                                                      {
                                                          Assert.That(stream.Length, Is.EqualTo(size));
                                                          using (var streamReader = new StreamReader(stream))
                                                          {
                                                              Assert.That(streamReader.ReadToEnd(), Is.EqualTo(content));
                                                          }
                                                      };
            fileContentSource.Request(uri);
        }
Ejemplo n.º 6
0
    public async Task UpsertAssetAsync_DynamicallyTyped_WithFileContent_UpsertsAsset()
    {
        var client = _fileSystemFixture.CreateMockClientWithResponse("File.json", "Asset.json");

        var expected = GetExpectedDynamicAssetModel();

        var stream      = new MemoryStream(Encoding.UTF8.GetBytes("Hello world from CM API .NET SDK"));
        var fileName    = "Hello.txt";
        var contentType = "text/plain";

        var updateModel = new AssetUpsertModel
        {
            Title    = expected.Title,
            Elements = expected.Elements
        };

        var content = new FileContentSource(stream, fileName, contentType);

        var response = await client.UpsertAssetAsync(Reference.ById(expected.Id), content, updateModel);

        response.Should().BeEquivalentTo(expected);
    }
Ejemplo n.º 7
0
    public async Task CreateAssetAsync_StronglyTyped_WithFileContent_CreatesAsset()
    {
        var client = _fileSystemFixture.CreateMockClientWithResponse("File.json", "Asset.json");

        var expected = GetExpectedStronglyTypedAssetModel();

        var stream      = new MemoryStream(Encoding.UTF8.GetBytes("Hello world from CM API .NET SDK"));
        var fileName    = "Hello.txt";
        var contentType = "text/plain";

        var updateModel = new AssetCreateModel <ComplexTestModel>
        {
            Title    = expected.Title,
            Elements = expected.Elements
        };

        var content = new FileContentSource(stream, fileName, contentType);

        var response = await client.CreateAssetAsync(content, updateModel);

        response.Should().BeEquivalentTo(expected);
    }
Ejemplo n.º 8
0
        public static async Task <AssetModel> CreateAssetAsync(this ContentManagementClient client, FileContentSource fileContent, IEnumerable <AssetDescription> descriptions)
        {
            if (fileContent == null)
            {
                throw new ArgumentNullException(nameof(fileContent));
            }

            if (descriptions == null)
            {
                throw new ArgumentNullException(nameof(descriptions));
            }

            var fileResult = await client.UploadFileAsync(fileContent);

            var asset = new AssetUpsertModel
            {
                FileReference = fileResult,
                Descriptions  = descriptions
            };

            var response = await client.CreateAssetAsync(asset);

            return(response);
        }
Ejemplo n.º 9
0
        public static async Task <AssetModel> UpsertAssetByExternalIdAsync(this ContentManagementClient client, string externalId, FileContentSource fileContent, IEnumerable <AssetDescription> descriptions)
        {
            if (string.IsNullOrEmpty(externalId))
            {
                throw new ArgumentException("The external id is not specified.", nameof(externalId));
            }

            if (fileContent == null)
            {
                throw new ArgumentNullException(nameof(fileContent));
            }

            if (descriptions == null)
            {
                throw new ArgumentNullException(nameof(descriptions));
            }

            var fileResult = await client.UploadFileAsync(fileContent);

            var asset = new AssetUpsertModel
            {
                FileReference = fileResult,
                Descriptions  = descriptions
            };

            var response = await client.UpsertAssetByExternalIdAsync(externalId, asset);

            return(response);
        }
Ejemplo n.º 10
0
    /// <summary>
    /// Creates asset.
    /// </summary>
    /// <param name="client"></param>
    /// <param name="fileContent">Represents the content of the file.</param>
    /// <param name="assetCreateModel">Updated values for the strongly typed asset.</param>
    public async static Task <AssetModel <T> > CreateAssetAsync <T>(this IManagementClient client, FileContentSource fileContent, AssetCreateModel <T> assetCreateModel) where T : new()
    {
        if (fileContent == null)
        {
            throw new ArgumentNullException(nameof(fileContent));
        }
        if (assetCreateModel == null)
        {
            throw new ArgumentNullException(nameof(assetCreateModel));
        }

        var fileResult = await client.UploadFileAsync(fileContent);

        assetCreateModel.FileReference = fileResult;

        var response = await client.CreateAssetAsync(assetCreateModel);

        return(response);
    }
Ejemplo n.º 11
0
    /// <summary>
    /// Creates or updates the given asset.
    /// </summary>
    /// <param name="client">Content management client instance.</param>
    /// <param name="identifier">The identifier of the asset.</param>
    /// <param name="fileContent">Represents the content of the file.</param>
    /// <param name="upsertModel">Updated values for the asset.</param>
    /// <returns>The <see cref="AssetModel{T}"/> instance that represents created or updated strongly typed asset.</returns>
    public async static Task <AssetModel <T> > UpsertAssetAsync <T>(this IManagementClient client, Reference identifier, FileContentSource fileContent, AssetUpsertModel <T> upsertModel) where T : new()
    {
        if (identifier == null)
        {
            throw new ArgumentNullException(nameof(identifier));
        }

        if (fileContent == null)
        {
            throw new ArgumentNullException(nameof(fileContent));
        }

        if (upsertModel == null)
        {
            throw new ArgumentNullException(nameof(upsertModel));
        }

        var fileResult = await client.UploadFileAsync(fileContent);

        upsertModel.FileReference = fileResult;

        var response = await client.UpsertAssetAsync(identifier, upsertModel);

        return(response);
    }
        /// <summary>
        /// Creates asset.
        /// </summary>
        /// <param name="client"></param>
        /// <param name="fileContent">Represents the content of the file.</param>
        /// <param name="assetUpdateModel">Updated values for asset.</param>
        public static async Task <AssetModel> CreateAssetAsync(this ManagementClient client, FileContentSource fileContent, AssetUpdateModel assetUpdateModel)
        {
            if (fileContent == null)
            {
                throw new ArgumentNullException(nameof(fileContent));
            }
            if (assetUpdateModel == null)
            {
                throw new ArgumentNullException(nameof(assetUpdateModel));
            }

            if (assetUpdateModel.Descriptions == null)
            {
                throw new ArgumentNullException(nameof(assetUpdateModel.Descriptions));
            }

            var fileResult = await client.UploadFileAsync(fileContent);

            var asset = new AssetUpsertModel
            {
                FileReference = fileResult,
                Descriptions  = assetUpdateModel.Descriptions,
                Title         = assetUpdateModel.Title
            };

            var response = await client.CreateAssetAsync(asset);

            return(response);
        }