Example #1
0
        public async Task HandleAsync_SettingEASchemeIbisCustomerReferenceNumberToAValueThatAlreadyExistsForAnotherEAScheme_ReturnsFailureResult()
        {
            // Arrange
            UKCompetentAuthority environmentAgency = A.Dummy <UKCompetentAuthority>();

            typeof(UKCompetentAuthority).GetProperty("Id").SetValue(environmentAgency, new Guid("42D3130C-4CDB-4F74-866A-BFF839A347B5"));

            Scheme scheme = new Scheme(A.Dummy <Organisation>());

            typeof(Entity).GetProperty("Id").SetValue(scheme, new Guid("5AE25C37-88C8-4646-8793-DB4C2F4EF0E5"));
            scheme.UpdateScheme(
                "Scheme 1",
                "WEE/AA0000AA/SCH",
                "WEE7453846",
                Domain.Obligation.ObligationType.B2C,
                environmentAgency);
            scheme.SetStatus(Domain.Scheme.SchemeStatus.Approved);

            Scheme otherScheme = new Scheme(A.Dummy <Organisation>());

            typeof(Entity).GetProperty("Id").SetValue(otherScheme, new Guid("C78D98A9-E33A-4E20-88D3-F1C99D5165B1"));
            otherScheme.UpdateScheme(
                "Scheme 2",
                "WEE/BB1111BB/SCH",
                "WEE8643759",
                Domain.Obligation.ObligationType.B2C,
                environmentAgency);
            otherScheme.SetStatus(Domain.Scheme.SchemeStatus.Approved);

            A.CallTo(() => dataAccess.FetchSchemeAsync(new Guid("5AE25C37-88C8-4646-8793-DB4C2F4EF0E5"))).Returns(scheme);
            A.CallTo(() => dataAccess.FetchEnvironmentAgencyAsync()).Returns(environmentAgency);
            A.CallTo(() => dataAccess.FetchNonRejectedEnvironmentAgencySchemesAsync())
            .Returns(new List <Scheme>()
            {
                scheme, otherScheme
            });

            // Act
            CreateScheme request = new CreateScheme(
                scheme.Id,
                "Scheme 1",
                "WEE/AA0000AA/SCH",
                "WEE8643759",
                ObligationType.B2C,
                new Guid("42D3130C-4CDB-4F74-866A-BFF839A347B5"),
                SchemeStatus.Approved);

            CreateOrUpdateSchemeInformationResult result = await handler.HandleAsync(request);

            // Assert
            Assert.NotNull(result);
            Assert.Equal(CreateOrUpdateSchemeInformationResult.ResultType.IbisCustomerReferenceUniquenessFailure, result.Result);

            Assert.NotNull(result.IbisCustomerReferenceUniquenessFailure);
            Assert.Equal("WEE8643759", result.IbisCustomerReferenceUniquenessFailure.IbisCustomerReference);
            Assert.Equal("WEE/BB1111BB/SCH", result.IbisCustomerReferenceUniquenessFailure.OtherSchemeApprovalNumber);
            Assert.Equal("Scheme 2", result.IbisCustomerReferenceUniquenessFailure.OtherSchemeName);
        }
Example #2
0
        public async Task HandleAsync_SettingNonEASchemeIbisCustomerReferenceNumberToEmptyString_ReturnsSuccessResult()
        {
            // Arrange
            UKCompetentAuthority environmentAgency = A.Dummy <UKCompetentAuthority>();

            typeof(UKCompetentAuthority).GetProperty("Id").SetValue(environmentAgency, new Guid("42D3130C-4CDB-4F74-866A-BFF839A347B5"));

            Scheme scheme = new Scheme(A.Dummy <Organisation>());

            typeof(Entity).GetProperty("Id").SetValue(scheme, new Guid("5AE25C37-88C8-4646-8793-DB4C2F4EF0E5"));
            scheme.UpdateScheme(
                "Scheme 1",
                "WEE/AA0000AA/SCH",
                "WEE8643759",
                Domain.Obligation.ObligationType.B2C,
                environmentAgency);
            scheme.SetStatus(Domain.Scheme.SchemeStatus.Approved);

            IUpdateSchemeInformationDataAccess dataAccess = A.Fake <IUpdateSchemeInformationDataAccess>();

            A.CallTo(() => dataAccess.FetchSchemeAsync(new Guid("5AE25C37-88C8-4646-8793-DB4C2F4EF0E5"))).Returns(scheme);
            A.CallTo(() => dataAccess.FetchEnvironmentAgencyAsync()).Returns(environmentAgency);
            A.CallTo(() => dataAccess.FetchNonRejectedEnvironmentAgencySchemesAsync())
            .Returns(new List <Scheme>()
            {
                scheme
            });

            IWeeeAuthorization             authorization = AuthorizationBuilder.CreateUserWithAllRights();
            UpdateSchemeInformationHandler handler       = new UpdateSchemeInformationHandler(
                authorization,
                dataAccess);

            // Act
            UpdateSchemeInformation request = new UpdateSchemeInformation(
                scheme.Id,
                "Scheme 1",
                "WEE/AA0000AA/SCH",
                string.Empty,
                ObligationType.B2C,
                new Guid("61D93F16-A478-4F45-AE6B-2A581F0C0648"),
                SchemeStatus.Approved);

            CreateOrUpdateSchemeInformationResult result = await handler.HandleAsync(request);

            // Assert
            Assert.NotNull(result);
            Assert.Equal(CreateOrUpdateSchemeInformationResult.ResultType.Success, result.Result);
        }
Example #3
0
        public async Task HandleAsync_WhereApprovalNumberIsChangingToAValueThatAlreadyExists_ReturnsFailureResult()
        {
            // Arrange
            UKCompetentAuthority environmentAgency = A.Dummy <UKCompetentAuthority>();

            typeof(UKCompetentAuthority).GetProperty("Id").SetValue(environmentAgency, new Guid("42D3130C-4CDB-4F74-866A-BFF839A347B5"));

            Scheme scheme = new Scheme(A.Dummy <Organisation>());

            typeof(Entity).GetProperty("Id").SetValue(scheme, new Guid("5AE25C37-88C8-4646-8793-DB4C2F4EF0E5"));
            scheme.UpdateScheme(
                "Scheme 1",
                "WEE/AA0000AA/SCH",
                "WEE7453846",
                Domain.Obligation.ObligationType.B2C,
                environmentAgency);
            scheme.SetStatus(Domain.Scheme.SchemeStatus.Approved);

            IUpdateSchemeInformationDataAccess dataAccess = A.Fake <IUpdateSchemeInformationDataAccess>();

            A.CallTo(() => dataAccess.FetchSchemeAsync(new Guid("5AE25C37-88C8-4646-8793-DB4C2F4EF0E5"))).Returns(scheme);
            A.CallTo(() => dataAccess.CheckSchemeApprovalNumberInUseAsync("WEE/ZZ9999ZZ/SCH")).Returns(true);

            IWeeeAuthorization             authorization = AuthorizationBuilder.CreateUserWithAllRights();
            UpdateSchemeInformationHandler handler       = new UpdateSchemeInformationHandler(
                authorization,
                dataAccess);

            // Act
            UpdateSchemeInformation request = new UpdateSchemeInformation(
                scheme.Id,
                "Scheme 1",
                "WEE/ZZ9999ZZ/SCH",
                "WEE7453846",
                ObligationType.B2C,
                new Guid("42D3130C-4CDB-4F74-866A-BFF839A347B5"),
                SchemeStatus.Approved);

            CreateOrUpdateSchemeInformationResult result = await handler.HandleAsync(request);

            // Assert
            Assert.NotNull(result);
            Assert.Equal(CreateOrUpdateSchemeInformationResult.ResultType.ApprovalNumberUniquenessFailure, result.Result);
        }
Example #4
0
        public async Task HandleAsync_HappyPath_UpdatesSchemeCallsSaveAndReturnsSuccess()
        {
            // Arrange
            IWeeeAuthorization authorization = AuthorizationBuilder.CreateUserWithAllRights();

            IUpdateSchemeInformationDataAccess dataAccess = A.Fake <IUpdateSchemeInformationDataAccess>();

            Scheme scheme = new Scheme(A.Dummy <Organisation>());

            A.CallTo(() => dataAccess.FetchSchemeAsync(A <Guid> ._)).Returns(scheme);

            UpdateSchemeInformationHandler handler = new UpdateSchemeInformationHandler(
                authorization,
                dataAccess);

            // Act
            UpdateSchemeInformation request = new UpdateSchemeInformation(
                A.Dummy <Guid>(),
                "New scheme name",
                "WEE/AB8888CD/SCH",
                "WEE7453956",
                ObligationType.B2B,
                new Guid("559B69CE-865C-465F-89ED-D6A58AA8B0B9"),
                SchemeStatus.Approved);

            CreateOrUpdateSchemeInformationResult result = await handler.HandleAsync(request);

            // Assert
            Assert.Equal("New scheme name", scheme.SchemeName);
            Assert.Equal("WEE/AB8888CD/SCH", scheme.ApprovalNumber);
            Assert.Equal("WEE7453956", scheme.IbisCustomerReference);
            Assert.Equal(Domain.Obligation.ObligationType.B2B, scheme.ObligationType);
            Assert.Equal(new Guid("559B69CE-865C-465F-89ED-D6A58AA8B0B9"), scheme.CompetentAuthorityId);
            Assert.Equal(Domain.Scheme.SchemeStatus.Approved, scheme.SchemeStatus);

            A.CallTo(() => dataAccess.SaveAsync()).MustHaveHappened();

            Assert.NotNull(result);
            Assert.Equal(CreateOrUpdateSchemeInformationResult.ResultType.Success, result.Result);
        }
Example #5
0
        public async Task HandleAsync_SettingNonEASchemeIbisCustomerReferenceNumberToAValueThatAlreadyExistsForAnotherScheme_ReturnsSuccessResult()
        {
            // Arrange
            UKCompetentAuthority environmentAgency = A.Dummy <UKCompetentAuthority>();

            typeof(UKCompetentAuthority).GetProperty("Id").SetValue(environmentAgency, new Guid("42D3130C-4CDB-4F74-866A-BFF839A347B5"));

            UKCompetentAuthority devlovedAgency = A.Dummy <UKCompetentAuthority>();

            typeof(UKCompetentAuthority).GetProperty("Id").SetValue(devlovedAgency, new Guid("76BE711C-191D-4648-AFE7-4404D287535C"));

            Scheme scheme = new Scheme(A.Dummy <Organisation>());

            typeof(Entity).GetProperty("Id").SetValue(scheme, new Guid("5AE25C37-88C8-4646-8793-DB4C2F4EF0E5"));
            scheme.UpdateScheme(
                "Scheme 1",
                "WEE/AA0000AA/SCH",
                "WEE7453846",
                Domain.Obligation.ObligationType.B2C,
                devlovedAgency);
            scheme.SetStatus(Domain.Scheme.SchemeStatus.Approved);

            Scheme otherScheme = new Scheme(A.Dummy <Organisation>());

            typeof(Entity).GetProperty("Id").SetValue(otherScheme, new Guid("C78D98A9-E33A-4E20-88D3-F1C99D5165B1"));
            otherScheme.UpdateScheme(
                "Scheme 2",
                "WEE/BB1111BB/SCH",
                "WEE8643759",
                Domain.Obligation.ObligationType.B2C,
                devlovedAgency);
            otherScheme.SetStatus(Domain.Scheme.SchemeStatus.Approved);

            IUpdateSchemeInformationDataAccess dataAccess = A.Fake <IUpdateSchemeInformationDataAccess>();

            A.CallTo(() => dataAccess.FetchSchemeAsync(new Guid("5AE25C37-88C8-4646-8793-DB4C2F4EF0E5"))).Returns(scheme);
            A.CallTo(() => dataAccess.FetchEnvironmentAgencyAsync()).Returns(environmentAgency);
            A.CallTo(() => dataAccess.FetchNonRejectedEnvironmentAgencySchemesAsync())
            .Returns(new List <Scheme>());

            IWeeeAuthorization             authorization = AuthorizationBuilder.CreateUserWithAllRights();
            UpdateSchemeInformationHandler handler       = new UpdateSchemeInformationHandler(
                authorization,
                dataAccess);

            // Act
            UpdateSchemeInformation request = new UpdateSchemeInformation(
                scheme.Id,
                "Scheme 1",
                "WEE/AA0000AA/SCH",
                "WEE8643759",
                ObligationType.B2C,
                new Guid("76BE711C-191D-4648-AFE7-4404D287535C"),
                SchemeStatus.Approved);

            CreateOrUpdateSchemeInformationResult result = await handler.HandleAsync(request);

            // Assert
            Assert.NotNull(result);
            Assert.Equal(CreateOrUpdateSchemeInformationResult.ResultType.Success, result.Result);
        }
        public async Task <CreateOrUpdateSchemeInformationResult> HandleAsync(UpdateSchemeInformation message)
        {
            authorization.EnsureCanAccessInternalArea();

            Scheme scheme = await dataAccess.FetchSchemeAsync(message.SchemeId);

            /*
             * Check the uniqueness of the approval number if the value is being changed.
             */
            if (scheme.ApprovalNumber != message.ApprovalNumber)
            {
                if (await dataAccess.CheckSchemeApprovalNumberInUseAsync(message.ApprovalNumber))
                {
                    return(new CreateOrUpdateSchemeInformationResult()
                    {
                        Result = CreateOrUpdateSchemeInformationResult.ResultType.ApprovalNumberUniquenessFailure
                    });
                }
            }

            UKCompetentAuthority environmentAgency = await dataAccess.FetchEnvironmentAgencyAsync();

            if (environmentAgency.Id == message.CompetentAuthorityId)
            {
                // The 1B1S customer reference is mandatory for schemes in the Environmetn Agency.
                if (string.IsNullOrEmpty(message.IbisCustomerReference))
                {
                    return(new CreateOrUpdateSchemeInformationResult()
                    {
                        Result = CreateOrUpdateSchemeInformationResult.ResultType.IbisCustomerReferenceMandatoryForEAFailure,
                    });
                }
                else
                {
                    /*
                     * The 1B1S customer refernece must be unique across schemes within the Environment Agency.
                     *
                     * Try and find another non-rejected scheme for the Environment Agency with the same
                     * 1B1S customer reference. In production, this should at most only ever return one result.
                     *
                     * As the check for uniqueness has not always existed, it is possible that other
                     * environments may contain multiple schemes with the same 1B1S customer reference
                     * so we are using FirstOrDefault rather than SingleOrDefault.
                     */
                    List <Scheme> nonRejectedEnvironmentAgencySchemes = await dataAccess.FetchNonRejectedEnvironmentAgencySchemesAsync();

                    Scheme otherScheme = nonRejectedEnvironmentAgencySchemes
                                         .Where(s => s.Id != scheme.Id)
                                         .Where(s => s.IbisCustomerReference == message.IbisCustomerReference)
                                         .FirstOrDefault();

                    if (otherScheme != null)
                    {
                        return(new CreateOrUpdateSchemeInformationResult()
                        {
                            Result = CreateOrUpdateSchemeInformationResult.ResultType.IbisCustomerReferenceUniquenessFailure,
                            IbisCustomerReferenceUniquenessFailure = new CreateOrUpdateSchemeInformationResult.IbisCustomerReferenceUniquenessFailureInfo()
                            {
                                IbisCustomerReference = message.IbisCustomerReference,
                                OtherSchemeApprovalNumber = otherScheme.ApprovalNumber,
                                OtherSchemeName = otherScheme.SchemeName
                            }
                        });
                    }
                }
            }

            Domain.Obligation.ObligationType obligationType = ValueObjectInitializer.GetObligationType(message.ObligationType);
            scheme.UpdateScheme(
                message.SchemeName,
                message.ApprovalNumber,
                message.IbisCustomerReference,
                obligationType,
                message.CompetentAuthorityId);

            SchemeStatus status = message.Status.ToDomainEnumeration <SchemeStatus>();

            scheme.SetStatus(status);

            await dataAccess.SaveAsync();

            return(new CreateOrUpdateSchemeInformationResult()
            {
                Result = CreateOrUpdateSchemeInformationResult.ResultType.Success
            });
        }