WhenAPUTRequestWithACollectionContainingOnlyConformingIncludedOrExcludedTypeValuesIsSubmittedToSchoolsWithARequestBodyContentTypeOfTheAppropriateValueForTheProfileInUse( ConformanceType conformanceType, IncludedOrExcluded includedOrExcluded, string typeOrDescriptor, string resourceCollectionName, string contentType) { if (string.IsNullOrEmpty(contentType)) { contentType = ProfilesContentTypeHelper.CreateContentType( resourceCollectionName, ScenarioContext.Current.Get <string>(ScenarioContextKeys.ProfileName), ContentTypeUsage.Writable); } var httpClient = FeatureContext.Current.Get <HttpClient>(); httpClient.DefaultRequestHeaders.Clear(); var container = FeatureContext.Current.Get <IWindsorContainer>(); HttpContent putRequestContent = null; Guid id = Guid.NewGuid(); switch (resourceCollectionName) { case "schools": putRequestContent = GetSchoolPutRequestContent(id, contentType, conformanceType, includedOrExcluded, container, httpClient); break; case "studentAssessments": putRequestContent = GetStudentAssessmentPutRequestContent( id, contentType, conformanceType, includedOrExcluded, container, httpClient); break; default: throw new NotSupportedException(); } httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue( "Bearer", Guid.NewGuid() .ToString()); // Post resource, using the Profile's content type var putResponseMessage = httpClient.PutAsync(OwinUriHelper.BuildOdsUri(resourceCollectionName + "/" + id), putRequestContent) .Sync(); ScenarioContext.Current.Set(putResponseMessage); }
private static HttpContent GetStudentAssessmentPutRequestContent( Guid id, string contentType, ConformanceType conformanceType, IncludedOrExcluded includedOrExcluded, IWindsorContainer container, HttpClient httpClient) { // Get the "GetById" repository operation var getStudentAssessmentEntityById = container.Resolve <IGetEntityById <StudentAssessment_Entity> >(); // Retrieve an "existing" entity var studentAssessmentForUpdate = getStudentAssessmentEntityById.GetByIdAsync(id, CancellationToken.None) .GetResultSafely(); // Map the "updated" entity to a full School resource var fullStudentAssessmentResourceForUpdate = new StudentAssessmentResource_Full(); studentAssessmentForUpdate.MapTo(fullStudentAssessmentResourceForUpdate, null); //empty the id for the Post operation fullStudentAssessmentResourceForUpdate.Id = Guid.Empty; var profileElt = ScenarioContext.Current.Get <XElement>("ProfileXElement"); string resourceName = "StudentAssessment"; bool inclusiveFilter = includedOrExcluded == IncludedOrExcluded.Included; // Assumption: For simplicity (until otherwise required) we're only supporting specific properties Func <string, bool> filterPropertyNamePredicate = x => x.EqualsIgnoreCase("AssessmentReportingMethodDescriptor") || x.EqualsIgnoreCase("PerformanceLevelDescriptor"); var filteredCollectionElts = GetFilteredCollectionElts( profileElt, resourceName, false, filterPropertyNamePredicate, inclusiveFilter) .ToList(); if (!filteredCollectionElts.Any()) { throw new InvalidOperationException( "Assumptions have been made for simplicity of the tests. No collection filtered on 'AssessmentReportingMethodType' or 'PerformanceLevelDescriptor' was found."); } bool conforming = conformanceType == ConformanceType.Conforming; bool valuesAreIncluded = includedOrExcluded == IncludedOrExcluded.Included; bool nonConforming = conformanceType == ConformanceType.NonConforming; bool valuesAreExcluded = includedOrExcluded == IncludedOrExcluded.Excluded; foreach (var filteredCollectionElt in filteredCollectionElts) { var filterElt = filteredCollectionElt.Element("Filter"); string filterPropertyName = filterElt.Attribute("propertyName") .Value; // Get the filter's values var filterValues = filterElt .Elements("Value") .Select(x => x.Value) .ToList(); var testObjectFactory = container.Resolve <ITestObjectFactory>(); // Defensive programming if (!(conforming || nonConforming) || !(valuesAreIncluded || valuesAreExcluded)) { throw new NotSupportedException( "Both members of conforming/non-conforming or included/excluded have been set to false, indicating a change of mutual exclusivity expectations."); } if (valuesAreIncluded && conforming || valuesAreExcluded && nonConforming) { if (filterPropertyName == "AssessmentReportingMethodDescriptor") { // Remove all items fullStudentAssessmentResourceForUpdate.StudentAssessmentStudentObjectiveAssessments.ForEach( x => x.StudentAssessmentStudentObjectiveAssessmentScoreResults.Clear()); studentAssessmentForUpdate.StudentAssessmentStudentObjectiveAssessments.ForEach( x => x.StudentAssessmentStudentObjectiveAssessmentScoreResults.Clear()); // Add only conforming/included (or non-conforming excluded) items to the resource filterValues .Select( fv => { var item = testObjectFactory.Create <ScoreResult_Full>(); item.AssessmentReportingMethodDescriptor = fv; return(item); }) .ForEach( a => fullStudentAssessmentResourceForUpdate.StudentAssessmentStudentObjectiveAssessments.First() .StudentAssessmentStudentObjectiveAssessmentScoreResults.Add(a)); // Add some values to entity that are disallowed by the filter to ensure they are ignored (rather than processed and deleted) during persistence if (conforming) { filterValues .Select( fv => { var item = testObjectFactory.Create <ScoreResult_Entity>(); item.AssessmentReportingMethodDescriptor = fv + "1111"; item.StudentAssessmentStudentObjectiveAssessment = studentAssessmentForUpdate.StudentAssessmentStudentObjectiveAssessments.First(); return(item); }) .ForEach( a => studentAssessmentForUpdate.StudentAssessmentStudentObjectiveAssessments.First() .StudentAssessmentStudentObjectiveAssessmentScoreResults.Add(a)); } } else if (filterPropertyName == "PerformanceLevelDescriptor") { // Remove all items fullStudentAssessmentResourceForUpdate.StudentAssessmentStudentObjectiveAssessments.ForEach( x => x.StudentAssessmentStudentObjectiveAssessmentPerformanceLevels.Clear()); studentAssessmentForUpdate.StudentAssessmentStudentObjectiveAssessments.ForEach( x => x.StudentAssessmentStudentObjectiveAssessmentPerformanceLevels.Clear()); // Add only conforming (included) items filterValues .Select( fv => { var item = testObjectFactory.Create <PerformanceLevel_Full>(); item.PerformanceLevelDescriptor = fv; return(item); }) .ForEach( a => fullStudentAssessmentResourceForUpdate.StudentAssessmentStudentObjectiveAssessments.First() .StudentAssessmentStudentObjectiveAssessmentPerformanceLevels.Add(a)); // Add some values to entity that are disallowed by the filter to ensure they are ignored (rather than processed and deleted) during persistence if (conforming) { filterValues .Select( fv => { var item = testObjectFactory.Create <PerformanceLevel_Entity> (); item.PerformanceLevelDescriptor = fv + "1111"; item.StudentAssessmentStudentObjectiveAssessment = studentAssessmentForUpdate.StudentAssessmentStudentObjectiveAssessments.First(); return(item); }) .ForEach( a => studentAssessmentForUpdate.StudentAssessmentStudentObjectiveAssessments.First() .StudentAssessmentStudentObjectiveAssessmentPerformanceLevels.Add(a)); } } } else if (valuesAreExcluded && conforming || valuesAreIncluded && nonConforming) { if (filterPropertyName == "AssessmentReportingMethodDescriptor") { // Remove all items fullStudentAssessmentResourceForUpdate.StudentAssessmentStudentObjectiveAssessments.ForEach( x => x.StudentAssessmentStudentObjectiveAssessmentScoreResults.Clear()); studentAssessmentForUpdate.StudentAssessmentStudentObjectiveAssessments.ForEach( x => x.StudentAssessmentStudentObjectiveAssessmentScoreResults.Clear()); // Add only non-conforming (not included) items filterValues .Select( fv => { var item = testObjectFactory.Create <ScoreResult_Full>(); item.AssessmentReportingMethodDescriptor = fv + "9999"; return(item); }) .ForEach( a => fullStudentAssessmentResourceForUpdate.StudentAssessmentStudentObjectiveAssessments.First() .StudentAssessmentStudentObjectiveAssessmentScoreResults.Add(a)); // Add some values to entity that are disallowed by the filter to ensure they are ignored (rather than processed and deleted) during persistence if (conforming) { filterValues .Select( fv => { var item = testObjectFactory.Create <ScoreResult_Entity>(); item.AssessmentReportingMethodDescriptor = fv; item.StudentAssessmentStudentObjectiveAssessment = studentAssessmentForUpdate.StudentAssessmentStudentObjectiveAssessments.First(); return(item); }) .ForEach( a => studentAssessmentForUpdate.StudentAssessmentStudentObjectiveAssessments.First() .StudentAssessmentStudentObjectiveAssessmentScoreResults.Add(a)); } } else if (filterPropertyName == "PerformanceLevelDescriptor") { // Remove all items fullStudentAssessmentResourceForUpdate.StudentAssessmentStudentObjectiveAssessments.ForEach( x => x.StudentAssessmentStudentObjectiveAssessmentPerformanceLevels.Clear()); studentAssessmentForUpdate.StudentAssessmentStudentObjectiveAssessments.ForEach( x => x.StudentAssessmentStudentObjectiveAssessmentPerformanceLevels.Clear()); // Add only conforming (included) items filterValues .Select( v => { var item = testObjectFactory.Create <PerformanceLevel_Full>(); item.PerformanceLevelDescriptor = v + "9999"; return(item); }) .ForEach( a => fullStudentAssessmentResourceForUpdate.StudentAssessmentStudentObjectiveAssessments.First() .StudentAssessmentStudentObjectiveAssessmentPerformanceLevels.Add(a)); // Add some values to entity that are disallowed by the filter to ensure they are ignored (rather than processed and deleted) during persistence if (conforming) { filterValues .Select( fv => { var item = testObjectFactory.Create <PerformanceLevel_Entity> (); item.PerformanceLevelDescriptor = fv; item.StudentAssessmentStudentObjectiveAssessment = studentAssessmentForUpdate.StudentAssessmentStudentObjectiveAssessments.First(); return(item); }) .ForEach( a => studentAssessmentForUpdate.StudentAssessmentStudentObjectiveAssessments.First() .StudentAssessmentStudentObjectiveAssessmentPerformanceLevels.Add(a)); } } } } if (conforming) { // Make a copy of the entity for subsequent comparison var originalStudentAssessmentEntity = new StudentAssessment_Entity(); studentAssessmentForUpdate.MapTo(originalStudentAssessmentEntity, null); // Save into the ScenarioContext the targeted entity and the prepared school resource for // subsequent inspection to ensure values are synched to entity correctly, and existing // values disallowed by the filter are intact on the entity. ScenarioContext.Current.Set( ((FakeRepository <StudentAssessment_Entity>)getStudentAssessmentEntityById).EntitiesById[id], "currentStudentAssessmentEntity"); ScenarioContext.Current.Set(originalStudentAssessmentEntity, "originalStudentAssessmentEntity"); ScenarioContext.Current.Set(fullStudentAssessmentResourceForUpdate, "studentAssessmentResourceForUpdate"); } // Modify the client to specify that we're sending a profile-specific resource httpClient.DefaultRequestHeaders.Add(HttpRequestHeader.ContentType.ToString(), contentType); //build content HttpContent putRequestContent = new StringContent( JsonConvert.SerializeObject(fullStudentAssessmentResourceForUpdate), Encoding.UTF8, contentType); return(putRequestContent); }