public void Get_MultipleReleasesFound_MultipleReleasesReturned()
        {
            //Arrange
            var request = new GetReleasesHttpRequestMessageBuilder().Build();

            var expectedRelease1 = new ReleaseBuilder().Build();
            var expectedRelease2 = new ReleaseBuilder().Build();
            var expectedRelease3 = new ReleaseBuilder().Build();

            var dataModel = new DataModelBuilder()
                .WithRelease(expectedRelease1)
                .WithRelease(expectedRelease2)
                .WithRelease(expectedRelease3)
                .Build();

            _releaseRepositoryMock.Setup((stub) => stub.GetReleaseData(It.IsAny<string>(), It.IsAny<int>()))
                .Returns(dataModel);

            //Act
            dynamic result = _sut.Get(request);

            //Assert
            Assert.IsNotNull(result, "Unexpected result");
            Assert.IsInstanceOfType(result.releases, typeof(List<dynamic>), "Unexpected type for releases collection");

            List<dynamic> actualReleases = (List<dynamic>)result.releases;
            Assert.AreEqual(3, actualReleases.Count, "Unexpected number of releases");
            AssertReleasesCollectionContainsRelease(actualReleases, expectedRelease1);
            AssertReleasesCollectionContainsRelease(actualReleases, expectedRelease2);
            AssertReleasesCollectionContainsRelease(actualReleases, expectedRelease3);
        }
        public void Get_InvalidIncludedReleasePathIdsHeaderSpecified_BadRequestStatusReturned()
        {
            //Arrange
            var invalidRequest = new GetReleasesHttpRequestMessageBuilder()
                .WithIncludedReleasePathIdsHeader("invalid")
                .Build();

            //Act
            var result = _sut.Get(invalidRequest);

            //Assert
            AssertionHelper.AssertHttpResponseMessageStatus(result, HttpStatusCode.BadRequest);
        }
        public void Get_NoHeadersSpecified_DefaultFiltersUsedToRetrieveReleaseData()
        {
            //Arrange
            var request = new GetReleasesHttpRequestMessageBuilder().Build();

            _releaseRepositoryMock.Setup((stub) => stub.GetReleaseData(It.IsAny<string>(), It.IsAny<int>()))
                .Returns(new DataModelBuilder().Build());

            //Act
            _sut.Get(request);

            //Assert
            _releaseRepositoryMock.Verify((mock) => mock.GetReleaseData(DEFAULT_INCLUDE_RELEASE_PATH_IDS, DEFAULT_RELEASE_COUNT));
        }
        public void Get_ErrorOccursRetrievingReleaseData_InternalServerErrorReturned()
        {
            //Arrange
            var request = new GetReleasesHttpRequestMessageBuilder().Build();

            _releaseRepositoryMock.Setup((stub) => stub.GetReleaseData(It.IsAny<string>(), It.IsAny<int>()))
                .Throws<Exception>();

            //Act
            var result = _sut.Get(request);

            //Assert
            AssertionHelper.AssertHttpResponseMessageStatus(result, HttpStatusCode.InternalServerError);
        }
        public void Get_IncludedReleasePathIdsSpecified_IncludedReleasePathIdsFilterUsedToRetrieveReleaseData()
        {
            //Arrange
            string includeReleasePathIds = "1";
            var request = new GetReleasesHttpRequestMessageBuilder()
                .WithIncludedReleasePathIdsHeader(includeReleasePathIds)
                .Build();

            _releaseRepositoryMock.Setup((stub) => stub.GetReleaseData(It.IsAny<string>(), It.IsAny<int>()))
                .Returns(new DataModelBuilder().Build());

            //Act
            _sut.Get(request);

            //Assert
            _releaseRepositoryMock.Verify((mock) => mock.GetReleaseData(includeReleasePathIds, DEFAULT_RELEASE_COUNT));
        }
        public void Get_ValidRequest_VersionReturned()
        {
            //Arrange
            var request = new GetReleasesHttpRequestMessageBuilder().Build();
            var dataModel = new DataModelBuilder().WithDefaultDataSet().Build();

            _releaseRepositoryMock.Setup((stub) => stub.GetReleaseData(It.IsAny<string>(), It.IsAny<int>()))
                .Returns(dataModel);

            //Act
            dynamic result = _sut.Get(request);

            //Assert
            Assert.IsNotNull(result, "Unexpected result");
            Assert.IsNotNull(result.version, "Unexpected version");
            Assert.AreNotEqual("", result.version, "Unexpected version");
        }
        public void Get_ValidRequest_LastRefreshIsNow()
        {
            //Arrange
            var request = new GetReleasesHttpRequestMessageBuilder().Build();
            var dataModel = new DataModelBuilder().WithDefaultDataSet().Build();

            _releaseRepositoryMock.Setup((stub) => stub.GetReleaseData(It.IsAny<string>(), It.IsAny<int>()))
                .Returns(dataModel);

            //Act
            dynamic result = _sut.Get(request);

            //Assert
            Assert.IsNotNull(result, "Unexpected result");
            AssertionHelper.AssertDateTimeIsNow(result.lastRefresh);
        }
        public void Get_StageWithMultipleSteps_StepsAreOrderdByAttemptAndThenByRank()
        {
            //Arrange
            var request = new GetReleasesHttpRequestMessageBuilder().Build();

            var expectedRelease = new ReleaseBuilder().Build();
            var expectedEnvironment = new EnvironmentBuilder().Build();
            var expectedStage = new StageBuilder().ForEnvironment(expectedEnvironment).Build();

            var expectedStepBuilder = new StepBuilder().ForRelease(expectedRelease).ForStage(expectedStage);
            var expectedStepWithAttempt1AndRank1 = expectedStepBuilder.WithAttempt(1).WithRank(1).Build();
            var expectedStepWithAttempt1AndRank2 = expectedStepBuilder.WithAttempt(1).WithRank(2).Build();
            var expectedStepWithAttempt1AndRank3 = expectedStepBuilder.WithAttempt(1).WithRank(3).Build();
            var expectedStepWithAttempt2AndRank1 = expectedStepBuilder.WithAttempt(2).WithRank(1).Build();
            var expectedStepWithAttempt2AndRank2 = expectedStepBuilder.WithAttempt(2).WithRank(2).Build();

            var dataModel = new DataModelBuilder()
                .WithRelease(expectedRelease)
                .WithEnvironment(expectedEnvironment)
                .WithStage(expectedStage)
                .WithStageWorkflowFor(expectedRelease, expectedStage)

                //add steps in 'random' order to check sorting of steps by attempt and then by rank
                .WithStep(expectedStepWithAttempt2AndRank2)
                .WithStep(expectedStepWithAttempt1AndRank2)
                .WithStep(expectedStepWithAttempt1AndRank1)
                .WithStep(expectedStepWithAttempt1AndRank3)
                .WithStep(expectedStepWithAttempt2AndRank1)
                .Build();

            _releaseRepositoryMock.Setup((stub) => stub.GetReleaseData(It.IsAny<string>(), It.IsAny<int>()))
                .Returns(dataModel);

            //Act
            dynamic result = _sut.Get(request);

            //Assert
            Assert.IsNotNull(result, "Unexpected result");

            var stage = result.releases[0].stages[0];
            Assert.AreEqual(expectedStepWithAttempt1AndRank1.Id, stage.steps[0].id, "Unexpected first step");
            Assert.AreEqual(expectedStepWithAttempt1AndRank2.Id, stage.steps[1].id, "Unexpected second step");
            Assert.AreEqual(expectedStepWithAttempt1AndRank3.Id, stage.steps[2].id, "Unexpected third step");
            Assert.AreEqual(expectedStepWithAttempt2AndRank1.Id, stage.steps[3].id, "Unexpected fourth step");
            Assert.AreEqual(expectedStepWithAttempt2AndRank2.Id, stage.steps[4].id, "Unexpected fifth step");
        }
        public void Get_ShowComponentsHeaderIsTrue_ComponentReturned()
        {
            //Arrange
            var requestWithShowComponentsHeaderIsTrue = new GetReleasesHttpRequestMessageBuilder()
                .WithShowComponentsHeader(true)
                .Build();

            var expectedRelease = new ReleaseBuilder().Build();
            var expectedComponent = new ComponentBuilder().ForRelease(expectedRelease).Build();

            var dataModel = new DataModelBuilder()
                .WithRelease(expectedRelease)
                .WithComponent(expectedComponent)
                .Build();

            _releaseRepositoryMock.Setup((stub) => stub.GetReleaseData(It.IsAny<string>(), It.IsAny<int>()))
                .Returns(dataModel);

            //Act
            dynamic result = _sut.Get(requestWithShowComponentsHeaderIsTrue);

            //Assert
            Assert.IsNotNull(result, "Unexpected result");

            var actualRelease = result.releases[0];
            Assert.IsInstanceOfType(actualRelease.components, typeof(List<dynamic>), "Unexpected type for components collection");
            Assert.AreEqual(1, actualRelease.components.Count, "Unexpected number of components for release");
            Assert.AreEqual(expectedComponent.Build, actualRelease.components[0].build);
        }
        public void Get_ReleaseWithStageAndStepFound_ReleaseWithStageAndStepReturned()
        {
            //Arrange
            var request = new GetReleasesHttpRequestMessageBuilder().Build();

            var expectedRelease = new ReleaseBuilder().Build();
            var expectedEnvironment = new EnvironmentBuilder().Build();
            var expectedStage = new StageBuilder().ForEnvironment(expectedEnvironment).Build();
            var expectedStep = new StepBuilder()
                .ForRelease(expectedRelease)
                .ForStage(expectedStage)
                .Build();
            var expectedDeploymentStep = new DeploymentStepBuilder()
                .ForRelease(expectedRelease)
                .ForStage(expectedStage)
                .ForStep(expectedStep)
                .Build();

            var dataModel = new DataModelBuilder()
                .WithRelease(expectedRelease)
                .WithEnvironment(expectedEnvironment)
                .WithStage(expectedStage)
                .WithStageWorkflowFor(expectedRelease, expectedStage)
                .WithStep(expectedStep)
                .WithDeploymentStep(expectedDeploymentStep)
                .Build();

            _releaseRepositoryMock.Setup((stub) => stub.GetReleaseData(It.IsAny<string>(), It.IsAny<int>()))
                .Returns(dataModel);

            //Act
            dynamic result = _sut.Get(request);

            //Assert
            Assert.IsNotNull(result, "Unexpected result");

            Assert.IsInstanceOfType(result.releases, typeof(List<dynamic>), "Unexpected type for releases collection");
            Assert.AreEqual(1, result.releases.Count, "Unexpected number of releases");
            var actualRelease = result.releases[0];
            AssertAreReleasesEqual(expectedRelease, actualRelease);

            Assert.IsInstanceOfType(actualRelease.stages, typeof(List<dynamic>), "Unexpected type for stages collection");
            Assert.AreEqual(1, actualRelease.stages.Count, "Unexpected number of stages for release");
            var actualStage = actualRelease.stages[0];
            AssertAreStagesEqual(expectedStage, expectedEnvironment, actualStage);

            Assert.IsInstanceOfType(actualStage.steps, typeof(List<dynamic>), "Unexpected type for steps collection");
            Assert.AreEqual(1, actualStage.steps.Count, "Unexpected number of steps for stage");
            var actualStep = actualStage.steps[0];
            AssertAreStepsEqual(expectedStep, actualStep);

            Assert.IsInstanceOfType(actualStep.deploymentSteps, typeof(List<dynamic>), "Unexpected type for deploymentStep collection");
            Assert.AreEqual(1, actualStep.deploymentSteps.Count, "Unexpected number of steps for deploymentSteps");
            AssertAreDeploymentStepsEqual(expectedDeploymentStep, actualStep.deploymentSteps[0]);
        }
        public void Get_ReleaseWithMultipleComponents_ComponentsAreOrderdByBuildDefinition()
        {
            //Arrange
            var request = new GetReleasesHttpRequestMessageBuilder().WithShowComponentsHeader(true).Build();

            var expectedRelease = new ReleaseBuilder().Build();

            var expectedComponentBuilder = new ComponentBuilder().ForRelease(expectedRelease);
            var expectedComponentWithBuildDefinitionA = expectedComponentBuilder.WithBuildDefinition("A").Build();
            var expectedComponentWithBuildDefinitionB = expectedComponentBuilder.WithBuildDefinition("B").Build();
            var expectedComponentWithBuildDefinitionC = expectedComponentBuilder.WithBuildDefinition("C").Build();

            var dataModel = new DataModelBuilder()
                .WithRelease(expectedRelease)

                //add components in 'random' order to check sorting of components by build definition
                .WithComponent(expectedComponentWithBuildDefinitionB)
                .WithComponent(expectedComponentWithBuildDefinitionA)
                .WithComponent(expectedComponentWithBuildDefinitionC)
                .Build();

            _releaseRepositoryMock.Setup((stub) => stub.GetReleaseData(It.IsAny<string>(), It.IsAny<int>()))
                .Returns(dataModel);

            //Act
            dynamic result = _sut.Get(request);

            //Assert
            Assert.IsNotNull(result, "Unexpected result");
            Assert.AreEqual(expectedComponentWithBuildDefinitionA.Build, result.releases[0].components[0].build, "Unexpected first component");
            Assert.AreEqual(expectedComponentWithBuildDefinitionB.Build, result.releases[0].components[1].build, "Unexpected second component");
            Assert.AreEqual(expectedComponentWithBuildDefinitionC.Build, result.releases[0].components[2].build, "Unexpected third component");
        }
        public void Get_OverviewWithSingleItems()
        {
            //Arrange
            var request = new GetReleasesHttpRequestMessageBuilder().Build();

            var now = DateTime.Now;

            var expectedPath = new ReleasePathBuilder().Build();
            var expectedComponent = new ComponentBuilder().Build();
            var expectedOverviewStage = new OverviewStageBuilder().ForReleasePath(expectedPath).Build();
            var expectedOverviewStageDetails = new OverviewStageDetailsBuilder().ForOverviewStage(expectedOverviewStage).Build();

            var dataModel = new OverviewDataModelBuilder()
                .WithLastRefresh(now)
                .WithReleasePath(expectedPath)
                .WithComponent(expectedComponent)
                .WithOverviewStage(expectedOverviewStage)
                .WithOverviewStageDetails(expectedOverviewStageDetails)
                .Build();

            _releaseRepositoryMock.Setup((stub) => stub.GetReleaseOverviewData(It.IsAny<string>()))
                .Returns(dataModel);

            //Act
            dynamic result = _sut.Get(request);

            //Assert
            Assert.IsNotNull(result, "Unexpected result");
            Assert.IsInstanceOfType(result.applications, typeof(List<dynamic>), "Unexpected type for applications collection");

            List<dynamic> actualApplications = (List<dynamic>)result.applications;
            Assert.AreEqual(1, actualApplications.Count, "Unexpected number of releasePaths/applications");
            AssertDynamicCollectionContainsReleasePath(actualApplications, expectedPath);
            var actualStage = actualApplications.First();
            List<dynamic> actualStages = (List<dynamic>)actualApplications.First().stages;
            Assert.AreEqual(1, actualStages.Count, "Unexpected number of releasePaths/applications");
            AssertDynamicCollectionContainsStage(actualStages, expectedOverviewStage);
        }