示例#1
0
        public override FileProviderBase CreateProvider()
        {
            FileProviderBase result = null;

            try
            {
                string   fileName = CommaDelimitedFileUnitTest.ComposeFileName();
                IMonitor monitor  = new ConsoleMonitor();
                result = new AccessConnectivityEngineFileProviderFactory(fileName, monitor).CreateProvider();
                return(result);
            }
            catch
            {
                if (result != null)
                {
                    result.Dispose();
                    result = null;
                }

                throw;
            }
        }
        public void TestCreateUser()
        {
            Uri resourceBase  = new Uri(WebServiceUnitTest.AddressBase);
            Uri resourceUsers = new Uri(WebServiceUnitTest.AddressRelativeUsers, UriKind.Relative);

            IMonitor monitor  = new ConsoleMonitor();
            string   fileName = CommaDelimitedFileUnitTest.ComposeFileName();

            FileProviderBase provider = null;

            try
            {
                provider = new AccessConnectivityEngineFileProviderFactory(fileName, monitor).CreateProvider();
                Service webService = null;
                try
                {
                    webService = new WebService(monitor, provider);
                    webService.Start(resourceBase);

                    IDictionary <string, object> json = SampleComposer.Instance.ComposeUserResource().ToJson();
                    string characters = WebServiceUnitTest.Serializer.Value.Serialize(json);
                    byte[] bytes      = Encoding.UTF8.GetBytes(characters);

                    Uri resource = new Uri(resourceBase, resourceUsers);

                    WebClient client = null;
                    try
                    {
                        client = new WebClient();
                        client.Headers.Add(HttpRequestHeader.ContentType, WebServiceUnitTest.ContentTypeJson);
                        byte[] response           = client.UploadData(resource.AbsoluteUri, WebRequestMethods.Http.Post, bytes);
                        string responseCharacters = Encoding.UTF8.GetString(response);
                        IReadOnlyDictionary <string, object> responseJson =
                            WebServiceUnitTest.Serializer.Value.Deserialize <Dictionary <string, object> >(responseCharacters);
                        Core2EnterpriseUser user = new Core2EnterpriseUserJsonDeserializingFactory().Create(responseJson);
                        Assert.IsNotNull(user);
                        Assert.IsNotNull(
                            user
                            .Schemas
                            .SingleOrDefault(
                                (string item) =>
                                string.Equals(
                                    SchemaIdentifiers.Core2EnterpriseUser,
                                    item,
                                    StringComparison.Ordinal)));
                        Assert.IsFalse(string.IsNullOrWhiteSpace(user.Identifier));
                        Assert.IsNotNull(user.Metadata);
                        Assert.IsFalse(string.IsNullOrWhiteSpace(user.Metadata.ResourceType));

                        string resourcePathValue =
                            string.Concat(WebServiceUnitTest.AddressRelativeUser, user.Identifier);
                        Uri resourcePath = new Uri(resourcePathValue, UriKind.Relative);
                        resource = new Uri(resourceBase, resourcePath);
                        bytes    = new byte[0];
                        client.UploadData(resource, WebServiceUnitTest.MethodDelete, bytes);
                    }
                    finally
                    {
                        if (client != null)
                        {
                            client.Dispose();
                            client = null;
                        }
                    }
                }
                finally
                {
                    if (webService != null)
                    {
                        webService.Dispose();
                        webService = null;
                    }
                }
            }
            finally
            {
                if (provider != null)
                {
                    provider.Dispose();
                    provider = null;
                }
            }
        }
        public async Task TestInserting()
        {
            string fileName = CommaDelimitedFileUnitTest.ComposeFileName();

            try
            {
                IReadOnlyCollection <string> columnNames =
                    Enumerable.Range(0, CommaDelimitedFileUnitTest.CountColumns)
                    .Select(
                        (int item) =>
                        item.ToString(CultureInfo.InvariantCulture))
                    .ToArray();

                IList <string> keys = new List <string>(CommaDelimitedFileUnitTest.CountRows);

                for
                (
                    int indexIterations = 0;
                    indexIterations < CommaDelimitedFileUnitTest.CountInsertIterations;
                    indexIterations++
                )
                {
                    ITabularFileAdapter fileStore = null;
                    try
                    {
                        fileStore =
                            new AccessConnectivityEngineCommaDelimitedFileAdapterFactory(fileName)
                            .CreateFileAdapter(columnNames);
                        Assert.IsTrue(File.Exists(fileName));

                        for (int indexRows = 0; indexRows < CommaDelimitedFileUnitTest.CountRows; indexRows++)
                        {
                            Dictionary <string, string> columns =
                                columnNames
                                .ToDictionary(
                                    (string item) =>
                                    item,
                                    (string item) =>
                                    Guid.NewGuid().ToString());

                            IRow row = await fileStore.InsertRow(columns);

                            Assert.IsNotNull(row);
                            Assert.IsFalse(string.IsNullOrWhiteSpace(row.Key));
                            keys.Add(row.Key);
                        }
                    }
                    finally
                    {
                        if (fileStore != null)
                        {
                            fileStore.Dispose();
                            fileStore = null;
                        }
                    }
                }

                Assert.AreEqual(keys.LongCount(), keys.Distinct().LongCount());
                Assert.AreEqual(keys.LongCount() + 1, File.ReadAllLines(fileName).Count());
            }
            finally
            {
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }
            }
        }
        public async Task TestReplacing()
        {
            string fileName = CommaDelimitedFileUnitTest.ComposeFileName();

            try
            {
                IReadOnlyCollection <string> columnNames =
                    Enumerable.Range(0, CommaDelimitedFileUnitTest.CountColumns)
                    .Select(
                        (int item) =>
                        item.ToString(CultureInfo.InvariantCulture))
                    .ToArray();

                IRow rowOne;
                ITabularFileAdapter fileStore = null;
                string replacementValue;
                try
                {
                    fileStore =
                        new AccessConnectivityEngineCommaDelimitedFileAdapterFactory(fileName)
                        .CreateFileAdapter(columnNames);
                    Assert.IsTrue(File.Exists(fileName));

                    Dictionary <string, string> columnsWritten =
                        columnNames
                        .ToDictionary(
                            (string item) =>
                            item,
                            (string item) =>
                            Guid.NewGuid().ToString());

                    rowOne = await fileStore.InsertRow(columnsWritten);

                    Assert.IsNotNull(rowOne);
                    Assert.IsFalse(string.IsNullOrWhiteSpace(rowOne.Key));

                    IRow rowTwo = await fileStore.InsertRow(columnsWritten);

                    Assert.IsNotNull(rowTwo);
                    Assert.IsFalse(string.IsNullOrWhiteSpace(rowTwo.Key));

                    Assert.IsFalse(string.Equals(rowOne.Key, rowTwo.Key, StringComparison.OrdinalIgnoreCase));

                    string columnName = columnsWritten.Keys.Last();
                    replacementValue           = Guid.NewGuid().ToString();
                    columnsWritten[columnName] = replacementValue;

                    IRow rowReplacment = new Row(rowOne.Key, columnsWritten);
                    await fileStore.ReplaceRow(rowReplacment);
                }
                finally
                {
                    if (fileStore != null)
                    {
                        fileStore.Dispose();
                        fileStore = null;
                    }
                }

                IReadOnlyCollection <string> lines = File.ReadAllLines(fileName);
                Assert.AreEqual(3, lines.LongCount());
                Assert.AreEqual(
                    1,
                    lines
                    .Where(
                        (string item) =>
                        item.StartsWith(rowOne.Key, StringComparison.OrdinalIgnoreCase) &&
                        item.EndsWith(replacementValue, StringComparison.OrdinalIgnoreCase))
                    .LongCount());
            }
            finally
            {
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }
            }
        }
        public async Task TestReading()
        {
            string fileName = CommaDelimitedFileUnitTest.ComposeFileName();

            try
            {
                IReadOnlyCollection <string> columnNames =
                    Enumerable.Range(0, CommaDelimitedFileUnitTest.CountColumns)
                    .Select(
                        (int item) =>
                        item.ToString(CultureInfo.InvariantCulture))
                    .ToArray();

                ITabularFileAdapter fileStore = null;
                try
                {
                    fileStore =
                        new AccessConnectivityEngineCommaDelimitedFileAdapterFactory(fileName)
                        .CreateFileAdapter(columnNames);
                    Assert.IsTrue(File.Exists(fileName));

                    Dictionary <string, string> columnsWritten =
                        columnNames
                        .ToDictionary(
                            (string item) =>
                            item,
                            (string item) =>
                            Guid.NewGuid().ToString());

                    IRow rowWritten = await fileStore.InsertRow(columnsWritten);

                    Assert.IsNotNull(rowWritten);
                    Assert.IsFalse(string.IsNullOrWhiteSpace(rowWritten.Key));

                    IRow rowRead = await fileStore.ReadRow(rowWritten.Key);

                    Assert.IsNotNull(rowRead.Columns);
                    Assert.AreEqual(CommaDelimitedFileUnitTest.CountColumns, rowRead.Columns.Count);

                    foreach (string columnName in columnsWritten.Keys)
                    {
                        Assert.IsTrue(
                            string.Equals(
                                columnsWritten[columnName],
                                rowRead.Columns[columnName],
                                StringComparison.OrdinalIgnoreCase));
                    }
                }
                finally
                {
                    if (fileStore != null)
                    {
                        fileStore.Dispose();
                        fileStore = null;
                    }
                }
            }
            finally
            {
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }
            }
        }
        public async Task TestQuerying()
        {
            string fileName = CommaDelimitedFileUnitTest.ComposeFileName();

            try
            {
                IReadOnlyCollection <string> columnNames =
                    Enumerable.Range(0, CommaDelimitedFileUnitTest.CountColumns)
                    .Select(
                        (int item) =>
                        item.ToString(CultureInfo.InvariantCulture))
                    .ToArray();

                ITabularFileAdapter fileStore = null;
                try
                {
                    fileStore =
                        new AccessConnectivityEngineCommaDelimitedFileAdapterFactory(fileName)
                        .CreateFileAdapter(columnNames);
                    Assert.IsTrue(File.Exists(fileName));

                    Dictionary <string, string> columnsWritten =
                        columnNames
                        .ToDictionary(
                            (string item) =>
                            item,
                            (string item) =>
                            Guid.NewGuid().ToString());

                    IList <string> keys = new List <string>(CommaDelimitedFileUnitTest.CountRows);
                    for (int rowIndex = 0; rowIndex < CommaDelimitedFileUnitTest.CountRows; rowIndex++)
                    {
                        IRow rowWritten = await fileStore.InsertRow(columnsWritten);

                        Assert.IsNotNull(rowWritten);
                        Assert.IsFalse(string.IsNullOrWhiteSpace(rowWritten.Key));
                        Assert.IsFalse(
                            keys
                            .Any(
                                (string item) =>
                                string.Equals(item, rowWritten.Key, StringComparison.OrdinalIgnoreCase)));
                        keys.Add(rowWritten.Key);
                    }

                    IReadOnlyCollection <IRow> rowsRead = await fileStore.Query(columnsWritten);

                    Assert.IsNotNull(rowsRead);
                    Assert.AreEqual(3, rowsRead.Count);
                    Assert.IsTrue(
                        keys
                        .All(
                            (string keyItem) =>
                            rowsRead
                            .Any(
                                (IRow rowItem) =>
                                string.Equals(keyItem, rowItem.Key, StringComparison.OrdinalIgnoreCase))));
                }
                finally
                {
                    if (fileStore != null)
                    {
                        fileStore.Dispose();
                        fileStore = null;
                    }
                }
            }
            finally
            {
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }
            }
        }
        public void TestRetrieveGroup()
        {
            Uri resourceBase = new Uri(WebServiceUnitTest.AddressBase);

            IMonitor monitor = new ConsoleMonitor();

            string fileName = CommaDelimitedFileUnitTest.ComposeFileName();

            FileProviderBase provider = null;

            try
            {
                provider = new AccessConnectivityEngineFileProviderFactory(fileName, monitor).CreateProvider();
                Service webService = null;
                try
                {
                    webService = new WebService(monitor, provider);
                    webService.Start(resourceBase);

                    Guid   groupIdentifier       = Guid.NewGuid();
                    string resourceRelativeValue =
                        string.Format(
                            CultureInfo.InvariantCulture,
                            WebServiceUnitTest.AddressRelativeGroupTemplate,
                            groupIdentifier);
                    Uri resourceRelative = new Uri(resourceRelativeValue, UriKind.Relative);
                    Uri resource         = new Uri(resourceBase, resourceRelative);

                    HttpWebResponse response = null;

                    WebClient client = null;
                    try
                    {
                        client = new WebClient();
                        try
                        {
                            client.DownloadData(resource);
                        }
                        catch (WebException exception)
                        {
                            response = exception.Response as HttpWebResponse;
                        }
                    }
                    finally
                    {
                        if (client != null)
                        {
                            client.Dispose();
                            client = null;
                        }
                    }

                    Assert.IsNotNull(response);
                    Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode);
                }
                finally
                {
                    if (webService != null)
                    {
                        webService.Dispose();
                        webService = null;
                    }
                }
            }
            finally
            {
                if (provider != null)
                {
                    provider.Dispose();
                    provider = null;
                }
            }
        }