public void User_receives_success_message_when_trying_to_lock_DSLAM_when_the_port_state_is_Unknown()
        {
            using (ShimsContext.Create())
            {
                // ARRANGE
                const DslamPortEnable newLockState = DslamPortEnable.PortStateUnknown;
                var servicesControllerForTest = ServicesControllerForTests;
                var testContext = new SIMPLTestContext();

                // fake local current subscriber object
                var fakeCurrentSubscriber = new FakeCurrentSubscriber();

                // fake SubscriberDto
                var fakeSubscriberData = TestContext.DataRow["MapToSubscriberDtoAsJSON"];
                Assert.IsNotNull(fakeSubscriberData);
                var fakeSubscriberDtoAsJson = fakeSubscriberData.ToString().Replace("\r\n", "");
                var fakeSubscriberDto = JsonConvert.DeserializeObject<SubscriberDto>(fakeSubscriberDtoAsJson, new JsonSerializerSettings
                {
                    MissingMemberHandling = MissingMemberHandling.Ignore
                });

                // Create copy of minimal DSL provisioned services list for use as expected services reference.
                var expectedServicesList = fakeSubscriberDto.Accounts.First().Services.ToList();
                // Add DSLOFF to expected as this is the only additional service expected.
                expectedServicesList.Add(new ServiceDto
                {
                    ClassName = "OVERRIDE",
                    Name = "DSLOFF",
                    Description = "SUSPEND DSL PORT"
                });

                // fake CurrentSubscriber and Rosettian method calls
                ShimCurrentSubscriber.GetInstance = () => new ShimCurrentSubscriber();
                ShimSubscriberExtension.MapToSubscriberCurrentSubscriber = delegate { return fakeSubscriberDto; };
                ShimCurrentUser.AsUserDto = testContext.GetFakeUserDtoObject;
                ShimRosettianClient.AllInstances.UpdateSubscriberSubscriberDtoBooleanUserDto = delegate { return true; };
                ShimCurrentSubscriber.UpdateSubscriberDto = delegate(SubscriberDto subscriber)
                {
                    fakeCurrentSubscriber.SubIpType = subscriber.GetIPType();
                    fakeCurrentSubscriber.ProvisionedServicesList = subscriber.Accounts.First().Services;
                };
                ShimSubscriberExtension.MapToSubDetailsModelCurrentSubscriber = delegate
                {
                    return new SubscriberDetailsModel
                    {
                        IPType = fakeCurrentSubscriber.SubIpType
                    };
                };

                // ACT
                var actualResult = servicesControllerForTest.UpdateDslamPortGate(newLockState);

                // ASSERT
                Assert.IsNotNull(actualResult);
                var actualJsonResult = actualResult as JsonResult;
                Assert.IsNotNull(actualJsonResult);
                // verify the returned Json
                dynamic actualJson = actualJsonResult.Data;
                var status = actualJson.status as string;
                Assert.AreEqual("success", status, "UpdateDslamPortGate() returned Json has unexpected failing result status");
                Assert.IsTrue(expectedServicesList.SequenceEqual(fakeCurrentSubscriber.ProvisionedServicesList, new ServiceComparer()));
            }
        }
        public void While_trying_to_unlock_a_DSLAM_user_receives_error_message_when_a_FaultException_is_raised()
        {
            using (ShimsContext.Create())
            {
                // ARRANGE
                const DslamPortEnable newLockState = DslamPortEnable.PortStateUnknown;
                var servicesControllerForTest = ServicesControllerForTests;
                var testContext = new SIMPLTestContext();

                // fake local current subscriber object
                var fakeCurrentSubscriber = new FakeCurrentSubscriber();

                // fake SubscriberDto
                var fakeSubscriberData = TestContext.DataRow["MapToSubscriberDtoAsJSON"];
                Assert.IsNotNull(fakeSubscriberData);
                var fakeSubscriberDtoAsJson = fakeSubscriberData.ToString().Replace("\r\n", "");
                var fakeSubscriberDto = JsonConvert.DeserializeObject<SubscriberDto>(fakeSubscriberDtoAsJson, new JsonSerializerSettings
                {
                    MissingMemberHandling = MissingMemberHandling.Ignore
                });

                // Create copy of minimal DSL provisioned services list for use as expected services reference.
                var expectedServicesList = fakeSubscriberDto.Accounts.First().Services.ToList();
                // Add DSLOFF to expected as this is the only additional service expected.
                expectedServicesList.Add(new ServiceDto
                {
                    ClassName = "OVERRIDE",
                    Name = "DSLOFF",
                    Description = "SUSPEND DSL PORT"
                });

                // fake CurrentSubscriber and Rosettian method calls
                ShimCurrentSubscriber.GetInstance = () => new ShimCurrentSubscriber();
                ShimSubscriberExtension.MapToSubscriberCurrentSubscriber = delegate { return fakeSubscriberDto; };
                ShimCurrentUser.AsUserDto = testContext.GetFakeUserDtoObject;

                const string expectedErrorMessage = "This is a fault exception message";
                var faultDetail = new ValidationDetailDto()
                {
                    Message = expectedErrorMessage
                };

                var faultDetails = new List<ValidationDetailDto>();
                faultDetails.Add(faultDetail);

                var validationFault = new ValidationFaultDto()
                {
                    Details = faultDetails
                };

                var fault = new FaultException<ValidationFaultDto>(validationFault);
                ShimRosettianClient.AllInstances.UpdateSubscriberSubscriberDtoBooleanUserDto = delegate { throw fault; };
                ShimCurrentSubscriber.UpdateSubscriberDto = delegate(SubscriberDto subscriber)
                {
                    fakeCurrentSubscriber.SubIpType = subscriber.GetIPType();
                    fakeCurrentSubscriber.ProvisionedServicesList = subscriber.Accounts.First().Services;
                };
                ShimSubscriberExtension.MapToSubDetailsModelCurrentSubscriber = delegate
                {
                    return new SubscriberDetailsModel
                    {
                        IPType = fakeCurrentSubscriber.SubIpType
                    };
                };

                var wasLoggingCalled = false;
                ShimErrorLoggingService.AllInstances.LogErrorException = (myClient, myException) =>
                {
                    wasLoggingCalled = true;
                };

                // ACT
                var actualResult = servicesControllerForTest.UpdateDslamPortGate(newLockState);

                // ASSERT
                Assert.IsNotNull(actualResult);
                var actualJsonResult = actualResult as JsonResult;
                Assert.IsNotNull(actualJsonResult);
                Assert.IsTrue(wasLoggingCalled, "An Exception was expected and the logger was expected to have been called");
                // verify the returned Json
                dynamic actualJson = actualJsonResult.Data;
                var actualStatus = actualJson.status as string;
                var actualErrorMessage = actualJson.errorMessage as string;
                Assert.AreEqual("error", actualStatus, "UpdateDslamPortGate() returned Json has unexpected failing result status");
                Assert.AreEqual(expectedErrorMessage, actualErrorMessage, "UpdateDslamPortGate() returned errorMessage that doesn't have the expected value");
            }
        }
        public void UpdateIpType_ReturnsCorrectData_WhenChangingFromStaticToDynamic()
        {
            using (ShimsContext.Create())
            {
                // ARRANGE
                const string expectedViewName = "~/Areas/SIMPLNET/Views/Subscriber/SubDetails_Partial.cshtml";
                const IPTypeDto newIpType = IPTypeDto.Dynamic;
                var servicesControllerForTest = ServicesControllerForTests;
                var testContext = new SIMPLTestContext();

                // fake local current subscriber object
                var fakeCurrentSubscriber = new FakeCurrentSubscriber();

                // fake SubscriberDto
                var fakeSubscriberData = TestContext.DataRow["MapToSubscriberDtoAsJSON"];
                Assert.IsNotNull(fakeSubscriberData);
                var fakeSubscriberDtoAsJson = fakeSubscriberData.ToString().Replace("\r\n", "");
                var fakeSubscriberDto = JsonConvert.DeserializeObject<SubscriberDto>(fakeSubscriberDtoAsJson, new JsonSerializerSettings
                {
                    MissingMemberHandling = MissingMemberHandling.Ignore
                });

                // set expected provisioned services list
                var expectedServicesList = fakeSubscriberDto.Accounts.First().Services.ToList();
                var staticService = expectedServicesList.FirstOrDefault(s => s.Name.Equals("STATIC", StringComparison.OrdinalIgnoreCase));
                Assert.IsNotNull(staticService);
                expectedServicesList.Remove(staticService);

                // fake CurrentSubscriber and Rosettian method calls
                ShimCurrentSubscriber.GetInstance = () => new ShimCurrentSubscriber();
                ShimSubscriberExtension.MapToSubscriberCurrentSubscriber = delegate { return fakeSubscriberDto; };
                ShimCurrentUser.AsUserDto = testContext.GetFakeUserDtoObject;
                ShimRosettianClient.AllInstances.UpdateSubscriberSubscriberDtoBooleanUserDto = delegate { return true; };
                ShimCurrentSubscriber.UpdateSubscriberDto = delegate(SubscriberDto subscriber)
                {
                    fakeCurrentSubscriber.SubIpType = subscriber.GetIPType();
                    fakeCurrentSubscriber.ProvisionedServicesList = subscriber.Accounts.First().Services;
                };
                ShimSubscriberExtension.MapToSubDetailsModelCurrentSubscriber = delegate
                {
                    return new SubscriberDetailsModel
                    {
                        IPType = fakeCurrentSubscriber.SubIpType
                    };
                };

                // ACT
                var actualResult = servicesControllerForTest.UpdateIpType(newIpType);

                // ASSERT
                Assert.IsNotNull(actualResult);
                var actualPartialView = actualResult as PartialViewResult;
                Assert.IsNotNull(actualPartialView);
                Assert.AreEqual(expectedViewName, actualPartialView.ViewName);
                var actualViewModel = actualPartialView.Model as SubscriberDetailsModel;
                Assert.IsNotNull(actualViewModel);
                Assert.AreEqual(newIpType, actualViewModel.IPType);
                Assert.IsTrue(expectedServicesList.SequenceEqual(fakeCurrentSubscriber.ProvisionedServicesList, new ServiceComparer()));
            }
        }