public async void GetAll_Default_Parameters() { // Create a mock query that always returns the same result. Mock <ITermsQueryService> querySvc = new Mock <ITermsQueryService>(); querySvc.Setup( svc => svc.GetAll( It.IsAny <string>(), It.IsAny <AudienceType>(), It.IsAny <string>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <bool>() ) ) .Returns(Task.FromResult(new GlossaryTermResults())); // Call the controller, we don't care about the actual return value. TermsController controller = new TermsController(NullLogger <TermsController> .Instance, querySvc.Object); await controller.GetAll("glossary", AudienceType.HealthProfessional, "en"); // Verify that the query layer is called: // a) with the expected values. // b) exactly once. querySvc.Verify( svc => svc.GetAll("glossary", AudienceType.HealthProfessional, "en", 100, 0, false), Times.Once, "ITermsQueryService::getAll() should be called once, with default values for size, from, and requestedFields" ); }
public async void GetAll_Specified_Parameters() { // Create a mock query that always returns the same result. Mock <ITermsQueryService> querySvc = new Mock <ITermsQueryService>(); querySvc.Setup( svc => svc.getAll( It.IsAny <string>(), It.IsAny <AudienceType>(), It.IsAny <string>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string[]>() ) ) .Returns(Task.FromResult(new GlossaryTermResults())); // Call the controller, we don't care about the actual return value. TermsController controller = new TermsController(querySvc.Object); await controller.getAll("glossary", AudienceType.HealthProfessional, "es", 200, 2, new string[] { "Field1", "Field2", "Field3" }); // Verify that the query layer is called: // a) with the expected values. // b) exactly once. querySvc.Verify( svc => svc.getAll("glossary", AudienceType.HealthProfessional, "es", 200, 2, new string[] { "Field1", "Field2", "Field3" }), Times.Once, "ITermsQueryService::getAll() should be called once, with the specified values for size, from, and requestedFields" ); }
public void Initialize() { TermsRepository repository = new TermsRepository(testDataFile, cacheKey); controller = new TermsController(repository); controller.Request = new HttpRequestMessage(); }
public async void Expand_RequiredParametersOnly() { // Create a mock query that always returns the same result. Mock <ITermsQueryService> querySvc = new Mock <ITermsQueryService>(); querySvc.Setup( svc => svc.Expand( It.IsAny <string>(), It.IsAny <AudienceType>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <bool>() ) ) .Returns(Task.FromResult(new GlossaryTermResults())); // Call the controller, we don't care about the actual return value. TermsController controller = new TermsController(NullLogger <TermsController> .Instance, querySvc.Object); await controller.Expand("Cancer.gov", AudienceType.Patient, "en", "s"); // Verify that the query layer is called: // a) with the expected updated values for size, from, and requestedFields. // b) exactly once. querySvc.Verify( svc => svc.Expand("Cancer.gov", AudienceType.Patient, "en", "s", 100, 0, false), Times.Once, "ITermsQueryService::Expand() should be called once, with the updated value for size" ); }
public async void Count_QueryValues(string dictionary, AudienceType audience, string language) { Mock <ITermsQueryService> querySvc = new Mock <ITermsQueryService>(); TermsController controller = new TermsController(NullLogger <TermsController> .Instance, querySvc.Object); // Set up the mock query service to handle the GetCount() call. // The return value is unimportant for this test. querySvc.Setup( mock => mock.GetCount( It.IsAny <string>(), It.IsAny <AudienceType>(), It.IsAny <string>() ) ) .Returns(Task.FromResult(default(long))); long result = await controller.GetCount(dictionary, audience, language); // Verify that the service layer is called: // a) with the expected values. // b) exactly once. querySvc.Verify( svc => svc.GetCount(dictionary, audience, language), Times.Once ); }
public async void GetAll_Error_DictionaryMissing() { Mock <ITermsQueryService> querySvc = new Mock <ITermsQueryService>(); TermsController controller = new TermsController(querySvc.Object); APIErrorException ex = await Assert.ThrowsAsync <APIErrorException>(() => controller.getAll("", AudienceType.HealthProfessional, "en")); }
public async void GetAll_Error_LanguageBad() { Mock <ITermsQueryService> querySvc = new Mock <ITermsQueryService>(); TermsController controller = new TermsController(querySvc.Object); APIErrorException ex = await Assert.ThrowsAsync <APIErrorException>(() => controller.getAll("glossary", AudienceType.HealthProfessional, "turducken")); }
public async void GetById_ErrorMessage_Id() { Mock <ITermsQueryService> termQueryService = new Mock <ITermsQueryService>(); TermsController controller = new TermsController(NullLogger <TermsController> .Instance, termQueryService.Object); var exception = await Assert.ThrowsAsync <APIErrorException>(() => controller.GetById("Dictionary", AudienceType.Patient, "EN", 0L)); Assert.Equal("You must supply a valid dictionary, audience, language and id", exception.Message); }
public TermsControllerTests() : base() { Target = new TermsController( The <ILogger <TermsController> >().Object, The <ITermsRepository>().Object, The <IMapper>().Object ); }
public async void GetByName_WithFallback_ErrorMessage_UnknownCombination() { Mock <ITermsQueryService> termQueryService = new Mock <ITermsQueryService>(); TermsController controller = new TermsController(NullLogger <TermsController> .Instance, termQueryService.Object); var exception = await Assert.ThrowsAsync <APIErrorException>(() => controller.GetByName("UnknownDictionary", AudienceType.Patient, "EN", "s-phase-fraction", true)); Assert.Equal(404, exception.HttpStatusCode); Assert.Equal("Could not find initial fallback combination with dictionary 'unknowndictionary' and audience 'Patient'.", exception.Message); }
public async void GetById_ErrorMessage_InvalidLanguage() { Mock <ITermsQueryService> termQueryService = new Mock <ITermsQueryService>(); TermsController controller = new TermsController(NullLogger <TermsController> .Instance, termQueryService.Object); var exception = await Assert.ThrowsAsync <APIErrorException>(() => controller.GetById("Cancer.gov", AudienceType.Patient, "chicken", 10L) ); Assert.Equal("Unsupported Language. Please try either 'en' or 'es'", exception.Message); }
public async void GetByName_ErrorMessage_EmptyLanguage() { Mock <ITermsQueryService> termQueryService = new Mock <ITermsQueryService>(); TermsController controller = new TermsController(NullLogger <TermsController> .Instance, termQueryService.Object); var exception = await Assert.ThrowsAsync <APIErrorException>( () => controller.GetByName("Cancer.gov", AudienceType.Patient, "", "s-1") ); Assert.Equal("You must supply a valid dictionary, audience, and language.", exception.Message); }
public void Term_Withdraws_ID() { TermsController controller = new TermsController(_context); int id = 1; ViewResult result = controller.Withdraw(id) as ViewResult; // Assert Assert.IsNotNull(result); }
public async void Search_ErrorMessage_InvalidLanguage() { Mock <ITermsQueryService> termQueryService = new Mock <ITermsQueryService>(); TermsController controller = new TermsController(NullLogger <TermsController> .Instance, termQueryService.Object); var exception = await Assert.ThrowsAsync <APIErrorException>( () => controller.Search("Cancer.gov", AudienceType.Patient, "chicken", "chicken", MatchType.Begins, 10, 0) ); Assert.Equal("Unsupported Language. Valid values are 'en' and 'es'.", exception.Message); }
public async void Expand_ErrorMessage_Dictionary() { Mock <ITermsQueryService> termQueryService = new Mock <ITermsQueryService>(); TermsController controller = new TermsController(NullLogger <TermsController> .Instance, termQueryService.Object); var exception = await Assert.ThrowsAsync <APIErrorException>( () => controller.Expand("", AudienceType.Patient, "en", "s", 10, 0) ); Assert.Equal("You must supply a valid dictionary, audience and language", exception.Message); }
public async void Search_ErrorMessage_AudienceType() { Mock <ITermsQueryService> termsQueryService = new Mock <ITermsQueryService>(); TermsController controller = new TermsController(NullLogger <TermsController> .Instance, termsQueryService.Object); APIErrorException exception = await Assert.ThrowsAsync <APIErrorException>( () => controller.Search("Cancer.gov", (AudienceType)(-18), "en", "chicken", MatchType.Begins, 10, 0) ); Assert.Equal("You must supply a valid dictionary, audience and language.", exception.Message); }
public async void SearchForTerms_ErrorMessage_AudienceType() { Mock <ITermsQueryService> termsQueryService = new Mock <ITermsQueryService>(); TermsController controller = new TermsController(termsQueryService.Object); APIErrorException exception = await Assert.ThrowsAsync <APIErrorException>( () => controller.Search("Dictionary", (AudienceType)(-18), "EN", "Query", "contains", 0, 1, new string[] {}) ); Assert.Equal("You must supply a valid dictionary, audience and language.", exception.Message); }
public async void SearchForTerms_ErrorMessage_MatchType() { Mock <ITermsQueryService> termsQueryService = new Mock <ITermsQueryService>(); TermsController controller = new TermsController(termsQueryService.Object); APIErrorException exception = await Assert.ThrowsAsync <APIErrorException>( () => controller.Search("Dictionary", AudienceType.Patient, "EN", "Query", "doesnotcontain", 1, 1, new string[] {}) ); Assert.Equal("'matchType' can only be 'begins' or 'contains'", exception.Message); }
[InlineData(new object[] { "" })] // Empty string. public async void Count_ErrorMessage_Dictionary(string dictionary) { Mock <ITermsQueryService> querySvc = new Mock <ITermsQueryService>(); TermsController controller = new TermsController(NullLogger <TermsController> .Instance, querySvc.Object); APIErrorException ex = await Assert.ThrowsAsync <APIErrorException>( () => controller.GetCount(dictionary, AudienceType.HealthProfessional, "en") ); Assert.Equal(400, ex.HttpStatusCode); }
[InlineData(new object[] { "" })] // Empty string. public async void Count_ErrorMessage_Language(string badLanguage) { Mock <ITermsQueryService> querySvc = new Mock <ITermsQueryService>(); TermsController controller = new TermsController(NullLogger <TermsController> .Instance, querySvc.Object); APIErrorException ex = await Assert.ThrowsAsync <APIErrorException>( () => controller.GetCount("Cancer.gov", AudienceType.Patient, badLanguage) ); Assert.Equal(400, ex.HttpStatusCode); }
public async void GetByNameTerms() { Mock <ITermsQueryService> termsQueryService = new Mock <ITermsQueryService>(); TermsController controller = new TermsController(NullLogger <TermsController> .Instance, termsQueryService.Object); GlossaryTerm glossaryTerm = new GlossaryTerm() { TermId = 44771, Language = "en", Dictionary = "Cancer.gov", Audience = AudienceType.Patient, TermName = "S-phase fraction", FirstLetter = "s", PrettyUrlName = "s-phase-fraction", Definition = new Definition() { Text = "A measure of the percentage of cells in a tumor that are in the phase of the cell cycle during which DNA is synthesized. The S-phase fraction may be used with the proliferative index to give a more complete understanding of how fast a tumor is growing.", Html = "A measure of the percentage of cells in a tumor that are in the phase of the cell cycle during which DNA is synthesized. The S-phase fraction may be used with the proliferative index to give a more complete understanding of how fast a tumor is growing." }, Pronunciation = new Pronunciation() { Key = "(... fayz FRAK-shun)", Audio = "https://nci-media-dev.cancer.gov/audio/pdq/705947.mp3" }, Media = new IMedia[] {}, RelatedResources = new IRelatedResource[] { } }; termsQueryService.Setup( termQSvc => termQSvc.GetByName( It.IsAny <string>(), It.IsAny <AudienceType>(), It.IsAny <string>(), It.IsAny <string>() ) ) .Returns(Task.FromResult(glossaryTerm)); GlossaryTerm term = await controller.GetByName("Cancer.gov", AudienceType.Patient, "en", "s-phase-fraction"); JObject actual = JObject.Parse(JsonConvert.SerializeObject(term)); JObject expected = JObject.Parse(File.ReadAllText(TestingTools.GetPathToTestFile("TermsControllerData/TestData_GetByName.json"))); // Verify that the service layer is called: // a) with the expected values. // b) exactly once. termsQueryService.Verify( svc => svc.GetByName("cancer.gov", AudienceType.Patient, "en", "s-phase-fraction"), Times.Once ); Assert.Equal(glossaryTerm, term, new GlossaryTermComparer()); Assert.Equal(expected, actual, new JTokenEqualityComparer()); }
public async void GetAll_Error_LanguageMissing() { Mock <ITermsQueryService> querySvc = new Mock <ITermsQueryService>(); TermsController controller = new TermsController(NullLogger <TermsController> .Instance, querySvc.Object); var exception = await Assert.ThrowsAsync <APIErrorException>( () => controller.GetAll("glossary", AudienceType.HealthProfessional, "") ); Assert.Equal("You must supply a valid dictionary, audience and language.", exception.Message); }
public async void GetAll_Error_LanguageBad() { Mock <ITermsQueryService> querySvc = new Mock <ITermsQueryService>(); TermsController controller = new TermsController(NullLogger <TermsController> .Instance, querySvc.Object); var exception = await Assert.ThrowsAsync <APIErrorException>( () => controller.GetAll("glossary", AudienceType.HealthProfessional, "turducken") ); Assert.Equal("Unsupported Language. Valid values are 'en' and 'es'.", exception.Message); }
public async void GetByName_WithFallback_TermsPatient_NotTitleCase() { Mock <ITermsQueryService> termQueryService = new Mock <ITermsQueryService>(); GlossaryTerm glossaryTerm = new GlossaryTerm() { TermId = 44771, Language = "en", Dictionary = "Cancer.gov", Audience = AudienceType.Patient, TermName = "S-phase fraction", FirstLetter = "s", PrettyUrlName = "s-phase-fraction", Definition = new Definition() { Text = "A measure of the percentage of cells in a tumor that are in the phase of the cell cycle during which DNA is synthesized. The S-phase fraction may be used with the proliferative index to give a more complete understanding of how fast a tumor is growing.", Html = "A measure of the percentage of cells in a tumor that are in the phase of the cell cycle during which DNA is synthesized. The S-phase fraction may be used with the proliferative index to give a more complete understanding of how fast a tumor is growing." }, Pronunciation = new Pronunciation() { Key = "(... fayz FRAK-shun)", Audio = "https://nci-media-dev.cancer.gov/audio/pdq/705947.mp3" }, Media = new IMedia[] { }, RelatedResources = new IRelatedResource[] { } }; // "cancer.gov" (not the requested "Cancer.gov") and Patient would be the only call to the terms query service, returning the term. termQueryService.Setup( termQSvc => termQSvc.GetByName( It.Is <String>(dictionary => dictionary == "cancer.gov"), It.Is <AudienceType>(audience => audience == AudienceType.Patient), It.Is <string>(language => language == "en"), It.Is <string>(prettyUrlName => prettyUrlName == "s-phase-fraction") ) ) .Returns(Task.FromResult(glossaryTerm)); TermsController controller = new TermsController(NullLogger <TermsController> .Instance, termQueryService.Object); GlossaryTerm gsTerm = await controller.GetByName("Cancer.gov", AudienceType.Patient, "en", "s-phase-fraction", true); // Verify that the expected and actual Term are the same. JObject actual = JObject.Parse(JsonConvert.SerializeObject(gsTerm)); JObject expected = JObject.Parse(File.ReadAllText(TestingTools.GetPathToTestFile("TermsControllerData/TestData_GetWithFallback_TermsPatient.json"))); Assert.Equal(expected, actual, new JTokenEqualityComparer()); // Verify that the service layer is called correctly with the lowercased-dictionary fallback combination: termQueryService.Verify( svc => svc.GetByName("cancer.gov", AudienceType.Patient, "en", "s-phase-fraction"), Times.Once ); }
public async void Search_InvalidMatchType() { // Create a mock query that always returns the same result. Mock <ITermsQueryService> querySvc = getDumbSearchSvcMock(); // Call the controller, we don't care about the actual return value. TermsController controller = new TermsController(NullLogger <TermsController> .Instance, querySvc.Object); var exception = await Assert.ThrowsAsync <APIErrorException>( () => controller.Search("Cancer.gov", AudienceType.Patient, "en", "chicken", (MatchType)5, -1, 0) ); Assert.Equal("Invalid value for the 'matchType' parameter.", exception.Message); }
public async void GetByName_ErrorMessage_MissingPrettyUrl() { Mock <ITermsQueryService> termQueryService = new Mock <ITermsQueryService>(); TermsController controller = new TermsController(NullLogger <TermsController> .Instance, termQueryService.Object); var exception = await Assert.ThrowsAsync <APIErrorException>( () => controller.GetByName("Cancer.gov", AudienceType.Patient, "en", null) ); Assert.Equal("You must specify the prettyUrlName parameter.", exception.Message); exception = await Assert.ThrowsAsync <APIErrorException>( () => controller.GetByName("Cancer.gov", AudienceType.Patient, "en", "") ); Assert.Equal("You must specify the prettyUrlName parameter.", exception.Message); }
public async void Search_RequiredParametersOnly() { // Create a mock query that always returns the same result. Mock <ITermsQueryService> querySvc = getDumbSearchSvcMock(); // Call the controller, we don't care about the actual return value. TermsController controller = new TermsController(NullLogger <TermsController> .Instance, querySvc.Object); await controller.Search("Cancer.gov", AudienceType.Patient, "en", "chicken"); // Verify that the query layer is called: // a) with the expected updated values for size, from, and requestedFields. // b) exactly once. querySvc.Verify( svc => svc.Search("Cancer.gov", AudienceType.Patient, "en", "chicken", MatchType.Begins, 100, 0, false), Times.Once, "ITermsQueryService::Search() should be called once, with the updated value for size" ); }
[InlineData(new object[] { int.MaxValue })] // Utterly ridiculous. public async void Count_QueryReturn(long returnCount) { Mock <ITermsQueryService> querySvc = new Mock <ITermsQueryService>(); TermsController controller = new TermsController(NullLogger <TermsController> .Instance, querySvc.Object); // Set up the mock query service to handle the GetCount() call. // The return value is unimportant for this test. querySvc.Setup( mock => mock.GetCount( It.IsAny <string>(), It.IsAny <AudienceType>(), It.IsAny <string>() ) ) .Returns(Task.FromResult(returnCount)); // The arguments don't matter for this test. long actual = await controller.GetCount("Cancer.gov", AudienceType.HealthProfessional, "es"); Assert.Equal(returnCount, actual); }
public void GetCurrentTerm_WhenCalled_ReturnsCurrentTerm() { var spring2018 = TestDataApi.CloneTerm(TestDataApi.spring2018); // Arrange termService.Setup(m => m.GetCurrentTerm()) .Returns(spring2018); var controller = new TermsController(termService.Object); TestHelper.SetUpControllerRequest(controller, "terms"); // Act var actionResult = controller.GetCurrentTerm(); var contentResult = actionResult as OkNegotiatedContentResult <TermDto>; // Assert termService.Verify(m => m.GetCurrentTerm()); Assert.That(contentResult, Is.Not.Null); Assert.That(contentResult.Content, Is.Not.Null); }
public void GetById_WhenCalled_ReturnsTerm() { var id = 5; var spring2018 = TestDataApi.CloneTerm(TestDataApi.spring2018); // Arrange termService.Setup(m => m.Get(It.IsAny <int>())) .Returns(spring2018); var controller = new TermsController(termService.Object); TestHelper.SetUpControllerRequest(controller, "terms"); // Act var actionResult = controller.Get(id); var contentResult = actionResult as OkNegotiatedContentResult <TermDto>; // Assert termService.Verify(m => m.Get(It.IsAny <int>())); Assert.That(contentResult, Is.Not.Null); Assert.That(contentResult.Content, Is.Not.Null); Assert.That(contentResult.Content.Name, Is.EqualTo("Spring 2018")); }