public PatchRequest2 ComposeReferencePatch(
            string referenceAttributeName,
            string referencedObjectUniqueIdentifier,
            OperationName operationName)
        {
            Assert.IsFalse(string.IsNullOrWhiteSpace(referenceAttributeName));
            Assert.IsFalse(string.IsNullOrWhiteSpace(referencedObjectUniqueIdentifier));

            IPath path;

            Assert.IsTrue(Path.TryParse(referenceAttributeName, out path));
            OperationValue operationValue =
                new OperationValue()
            {
                Value = referencedObjectUniqueIdentifier
            };
            PatchOperation operation =
                new PatchOperation()
            {
                Name = operationName,
                Path = path
            };

            operation.AddValue(operationValue);

            PatchRequest2 result = new PatchRequest2();

            result.AddOperation(operation);
            return(result);
        }
        public PatchRequest2 ComposeUserPatch()
        {
            string value = Guid.NewGuid().ToString(SampleComposer.FormatUniqueIdentifierCompressed);

            IPath          path;
            PatchOperation operation;
            OperationValue operationValue;

            PatchRequest2 result = new PatchRequest2();

            Assert.IsTrue(Path.TryParse(AttributeNames.Active, out path));
            operationValue =
                new OperationValue()
            {
                Value = bool.FalseString
            };
            operation =
                new PatchOperation()
            {
                Name = OperationName.Replace,
                Path = path
            };
            operation.AddValue(operationValue);
            result.AddOperation(operation);

            Assert.IsTrue(Path.TryParse(AttributeNames.DisplayName, out path));
            operationValue =
                new OperationValue()
            {
                Value = value
            };
            operation =
                new PatchOperation()
            {
                Name = OperationName.Replace,
                Path = path
            };
            operation.AddValue(operationValue);
            result.AddOperation(operation);

            Assert.IsTrue(Path.TryParse(SampleComposer.PathExpressionPrimaryWorkElectronicMailAddress, out path));
            string electronicMailAddressValue =
                string.Format(
                    CultureInfo.InvariantCulture,
                    SampleComposer.ElectronicMailAddressTemplate,
                    value);

            operationValue =
                new OperationValue()
            {
                Value = electronicMailAddressValue
            };
            operation =
                new PatchOperation()
            {
                Name = OperationName.Replace,
                Path = path
            };
            operation.AddValue(operationValue);
            result.AddOperation(operation);

            Assert.IsTrue(Path.TryParse(SampleComposer.PathExpressionPostalCode, out path));
            operationValue =
                new OperationValue()
            {
                Value = value
            };
            operation =
                new PatchOperation()
            {
                Name = OperationName.Replace,
                Path = path
            };
            operation.AddValue(operationValue);
            result.AddOperation(operation);

            return(result);
        }
        public void TestLifecycleGroup()
        {
            Uri addressBase = new Uri(WebServiceUnitTest.AddressBase);

            IMonitor monitor = new ConsoleMonitor();

            IAmazonWebServicesIdentityAnchoringBehavior anchoringBehavior =
                new AnchoringByIdentifierBehavior();
            AmazonWebServicesProviderBase provider = new AmazonWebServicesProvider(WebServiceUnitTest.CredentialsProfileName, anchoringBehavior);
            Service webService = null;
            try
            {
                webService = new WebService(monitor, provider);
                webService.Start(addressBase);

                string identifierGroup;
                string identifierGroupExternal;
                string identifierMemberOne;
                string identifierMemberTwo;
                
                Uri resource;

                WebClient client = null;
                try
                {
                    IDictionary<string, object> json;
                    string characters;
                    byte[] bytes;
                    byte[] response;
                    string responseCharacters;
                    IReadOnlyDictionary<string, object> responseJson;
                    Core2EnterpriseUser user;
                    Member member;
                    IReadOnlyCollection<Member> members;

                    client = new WebClient();
                    
                    identifierMemberOne = Guid.NewGuid().ToString();
                    string identifierMemberOneExternal = Guid.NewGuid().ToString();
                    user =
                        new Core2EnterpriseUser()
                        {
                            Identifier = identifierMemberOne,
                            ExternalIdentifier = identifierMemberOneExternal
                        };

                    json = user.ToJson();
                    characters = WebServiceUnitTest.Serializer.Value.Serialize(json);
                    bytes = Encoding.UTF8.GetBytes(characters);
                    resource = new Uri(addressBase, WebServiceUnitTest.AddressRelativeUsers);
                    client.Headers.Clear();
                    client.Headers.Add(HttpRequestHeader.ContentType, WebServiceUnitTest.ContentTypeJson);
                    response = client.UploadData(resource.AbsoluteUri, WebRequestMethods.Http.Post, bytes);
                    responseCharacters = Encoding.UTF8.GetString(response);
                    responseJson =
                        WebServiceUnitTest.Serializer.Value.Deserialize<Dictionary<string, object>>(responseCharacters);
                    user = new Core2EnterpriseUserJsonDeserializingFactory().Create(responseJson);
                    identifierMemberOne = user.Identifier;

                    try
                    {
                        member = 
                            new Member()
                            {
                                Value = identifierMemberOne
                            };
                        members =
                            new Member[]
                                {
                                    member
                                };

                        identifierGroup = Guid.NewGuid().ToString();
                        identifierGroupExternal = Guid.NewGuid().ToString();

                        WindowsAzureActiveDirectoryGroup group =
                            new WindowsAzureActiveDirectoryGroup()
                            {
                                Identifier = identifierGroup,
                                ExternalIdentifier = identifierGroupExternal,
                                Members = members
                            };

                        json = group.ToJson();
                        characters = WebServiceUnitTest.Serializer.Value.Serialize(json);
                        bytes = Encoding.UTF8.GetBytes(characters);
                        resource = new Uri(addressBase, WebServiceUnitTest.AddressRelativeGroups);
                        client.Headers.Clear();
                        client.Headers.Add(HttpRequestHeader.ContentType, WebServiceUnitTest.ContentTypeJson);
                        response = client.UploadData(resource.AbsoluteUri, WebRequestMethods.Http.Post, bytes);
                        responseCharacters = Encoding.UTF8.GetString(response);
                        responseJson =
                            WebServiceUnitTest.Serializer.Value.Deserialize<Dictionary<string, object>>(responseCharacters);
                        group = new WindowsAzureActiveDirectoryGroupJsonDeserializingFactory().Create(responseJson);
                        Assert.IsNotNull(group);
                        Assert.IsNotNull(
                            group
                            .Schemas
                            .SingleOrDefault(
                                (string item) =>
                                    string.Equals(
                                        SchemaIdentifiers.WindowsAzureActiveDirectoryGroup,
                                        item,
                                        StringComparison.Ordinal)));
                        Assert.IsFalse(string.IsNullOrWhiteSpace(group.Identifier));

                        string identifierGroupAmazon = group.Identifier;

                        try
                        {
                            Assert.IsNotNull(group.Metadata);
                            Assert.IsFalse(string.IsNullOrWhiteSpace(group.Metadata.ResourceType));
                            Assert.IsFalse(string.Equals(identifierGroup, identifierGroupAmazon, StringComparison.OrdinalIgnoreCase));

                            string resourcePath = 
                                string.Format(
                                    CultureInfo.InvariantCulture, 
                                    WebServiceUnitTest.AddressRelativeGroupTemplate, 
                                    identifierGroupAmazon);
                            resource = new Uri(addressBase, resourcePath);

                            response = client.DownloadData(resource);
                            responseCharacters = Encoding.UTF8.GetString(response);
                            responseJson =
                                WebServiceUnitTest.Serializer.Value.Deserialize<Dictionary<string, object>>(responseCharacters);
                            group = new WindowsAzureActiveDirectoryGroupJsonDeserializingFactory().Create(responseJson);
                            Assert.IsNotNull(group);
                            Assert.IsNotNull(
                                group
                                .Schemas
                                .SingleOrDefault(
                                    (string item) =>
                                        string.Equals(
                                            SchemaIdentifiers.Core2Group,
                                            item,
                                            StringComparison.Ordinal)));
                            Assert.IsFalse(string.IsNullOrWhiteSpace(group.Identifier));
                            Assert.IsTrue(string.Equals(group.Identifier, identifierGroupAmazon, StringComparison.OrdinalIgnoreCase));

                            Assert.IsFalse(string.IsNullOrWhiteSpace(group.ExternalIdentifier));
                            Assert.IsTrue(string.Equals(group.ExternalIdentifier, identifierGroupExternal, StringComparison.OrdinalIgnoreCase));

                            identifierMemberTwo = Guid.NewGuid().ToString();
                            string identifierMemberTwoExternal = Guid.NewGuid().ToString();
                            user =
                                new Core2EnterpriseUser()
                                {
                                    Identifier = identifierMemberTwo,
                                    ExternalIdentifier = identifierMemberTwoExternal
                                };

                            json = user.ToJson();
                            characters = WebServiceUnitTest.Serializer.Value.Serialize(json);
                            bytes = Encoding.UTF8.GetBytes(characters);
                            resource = new Uri(addressBase, WebServiceUnitTest.AddressRelativeUsers);
                            client.Headers.Clear();
                            client.Headers.Add(HttpRequestHeader.ContentType, WebServiceUnitTest.ContentTypeJson);
                            response = client.UploadData(resource.AbsoluteUri, WebRequestMethods.Http.Post, bytes);
                            responseCharacters = Encoding.UTF8.GetString(response);
                            responseJson =
                                WebServiceUnitTest.Serializer.Value.Deserialize<Dictionary<string, object>>(responseCharacters);
                            user = new Core2EnterpriseUserJsonDeserializingFactory().Create(responseJson);
                            identifierMemberTwo = user.Identifier;

                            try
                            {
                                IResourceIdentifier resourceIdentifier =
                                    new ResourceIdentifier()
                                    {
                                        Identifier = identifierGroupAmazon,
                                        SchemaIdentifier = SchemaIdentifiers.WindowsAzureActiveDirectoryGroup
                                    };

                                IPath path = Microsoft.SystemForCrossDomainIdentityManagement.Path.Create(AttributeNames.Members);

                                OperationValue operationValue;
                                PatchOperation operation;
                                IReadOnlyCollection<PatchOperation> operations;
                                PatchRequest2 patch;
                                                                
                                operationValue =
                                    new OperationValue()
                                    {
                                        Value = identifierMemberTwo
                                    };
                                operation =
                                    new PatchOperation()
                                    {
                                        Name = OperationName.Add,
                                        Path = path
                                    };
                                operations =
                                    new PatchOperation[] 
                                        { 
                                            operation 
                                        };
                                operation.AddValue(operationValue);                                
                                
                                patch = new PatchRequest2();
                                patch.AddOperation(operation);
                                json = patch.ToJson();
                                characters = WebServiceUnitTest.Serializer.Value.Serialize(json);
                                bytes = Encoding.UTF8.GetBytes(characters);
                                resourcePath = string.Concat(WebServiceUnitTest.AddressRelativeGroup, identifierGroupAmazon);
                                resource = new Uri(addressBase, resourcePath);
                                client.Headers.Clear();
                                client.Headers.Add(HttpRequestHeader.ContentType, WebServiceUnitTest.ContentTypeJson);
                                response = client.UploadData(resource.AbsoluteUri, WebServiceUnitTest.MethodPatch, bytes);

                                operationValue =
                                   new OperationValue()
                                   {
                                       Value = identifierMemberTwo
                                   };
                                operation =
                                    new PatchOperation()
                                    {
                                        Name = OperationName.Remove,
                                        Path = path
                                    };
                                operations =
                                    new PatchOperation[] 
                                        { 
                                            operation 
                                        };
                                operation.AddValue(operationValue);

                                patch = new PatchRequest2();
                                patch.AddOperation(operation);
                                json = patch.ToJson();
                                characters = WebServiceUnitTest.Serializer.Value.Serialize(json);
                                bytes = Encoding.UTF8.GetBytes(characters);
                                resourcePath = string.Concat(WebServiceUnitTest.AddressRelativeGroup, identifierGroupAmazon);
                                resource = new Uri(addressBase, resourcePath);
                                client.Headers.Clear();
                                client.Headers.Add(HttpRequestHeader.ContentType, WebServiceUnitTest.ContentTypeJson);
                                response = client.UploadData(resource.AbsoluteUri, WebServiceUnitTest.MethodPatch, bytes);

                                operationValue =
                                   new OperationValue()
                                   {
                                       Value = identifierMemberOne
                                   };
                                operation =
                                    new PatchOperation()
                                    {
                                        Name = OperationName.Remove,
                                        Path = path
                                    };
                                operations =
                                    new PatchOperation[] 
                                        { 
                                            operation 
                                        };
                                operation.AddValue(operationValue);

                                patch = new PatchRequest2();
                                patch.AddOperation(operation);
                                json = patch.ToJson();
                                characters = WebServiceUnitTest.Serializer.Value.Serialize(json);
                                bytes = Encoding.UTF8.GetBytes(characters);
                                resourcePath = string.Concat(WebServiceUnitTest.AddressRelativeGroup, identifierGroupAmazon);
                                resource = new Uri(addressBase, resourcePath);
                                client.Headers.Clear();
                                client.Headers.Add(HttpRequestHeader.ContentType, WebServiceUnitTest.ContentTypeJson);
                                response = client.UploadData(resource.AbsoluteUri, WebServiceUnitTest.MethodPatch, bytes);
                            }
                            finally
                            {
                                resourcePath = string.Concat(WebServiceUnitTest.AddressRelativeUser, identifierMemberTwo);
                                resource = new Uri(addressBase, resourcePath);
                                bytes = new byte[0];
                                client.UploadData(resource, WebServiceUnitTest.MethodDelete, bytes);
                            }
                        }
                        finally
                        {
                            string resourcePath = string.Concat(WebServiceUnitTest.AddressRelativeGroup, identifierGroupAmazon);
                            resource = new Uri(addressBase, resourcePath);
                            bytes = new byte[0];
                            client.UploadData(resource, WebServiceUnitTest.MethodDelete, bytes);
                        }
                    }
                    finally
                    {
                        string resourcePath = string.Concat(WebServiceUnitTest.AddressRelativeUser, identifierMemberOne);
                        resource = new Uri(addressBase, 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;
                }
            }
        }