Skip to content

Commit

Permalink
MAS-129 Reafctor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ediblecode committed Jan 16, 2020
1 parent dda5c61 commit 7804e5c
Show file tree
Hide file tree
Showing 22 changed files with 618 additions and 349 deletions.
File renamed without changes.
42 changes: 42 additions & 0 deletions lambda/MAS.Tests/Feeds/daily-items-single.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[
{
"_id": "5daf1aa18a34d4bb8405b5e0",
"slug": "testing-789",
"source": {
"_id": "5de526fc43e373581b7810c8",
"oldEPiServerId": 2,
"title": "ACP Journal Club",
"__v": 0
},
"shortSummary": "testing",
"resourceLinks": "http://localhost:3010/keystone/items",
"url": "https://www.nhs.uk/",
"title": "Wonder Drug",
"__v": 0,
"comment": "<p>A</p>",
"publicationDate": "2019-11-25T13:48:36.000Z",
"createdDate": "2019-10-22T15:05:05.927Z",
"specialities": [
{
"_id": "5e0e4331200a585f718e1ee5",
"key": "be1c2e2f-745e-4a82-b5aa-d4cef4d31a1b",
"title": "Anaesthesia and pain",
"__v": 0
},
{
"_id": "5e0e4331200a58dead8e1ee8",
"key": "53fc67a4-46d8-4171-9447-7fcf216c8749",
"title": "Complementary and alternative therapies",
"__v": 0
}
],
"evidenceType": {
"_id": "5df7abf383138898ee1f67ef",
"title": "Safety alerts",
"key": "mas_evidence_types:Safety%20alerts",
"oldEPiServerId": 778708,
"__v": 0
},
"isInitial": false
}
]
162 changes: 162 additions & 0 deletions lambda/MAS.Tests/Feeds/daily-items.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
[
{
"_id": "5daf1aa18a34d4bb8405b5e0",
"slug": "testing-789",
"source": {
"_id": "5de526fc43e373581b7810c8",
"oldEPiServerId": 2,
"title": "ACP Journal Club",
"__v": 0
},
"shortSummary": "testing",
"resourceLinks": "http://localhost:3010/keystone/items",
"url": "https://www.nhs.uk/",
"title": "Wonder Drug",
"__v": 0,
"comment": "<p>A</p>",
"publicationDate": "2019-11-25T13:48:36.000Z",
"createdDate": "2019-10-22T15:05:05.927Z",
"specialities": [
{
"_id": "5e0e4331200a585f718e1ee5",
"key": "be1c2e2f-745e-4a82-b5aa-d4cef4d31a1b",
"title": "Anaesthesia and pain",
"__v": 0
},
{
"_id": "5e0e4331200a58dead8e1ee8",
"key": "53fc67a4-46d8-4171-9447-7fcf216c8749",
"title": "Complementary and alternative therapies",
"__v": 0
}
],
"evidenceType": {
"_id": "5df7abf383138898ee1f67ef",
"title": "Safety alerts",
"key": "mas_evidence_types:Safety%20alerts",
"oldEPiServerId": 778708,
"__v": 0
},
"isInitial": false
},
{
"_id": "5db02d6d8a34d43bde05b5e1",
"slug": "my-medicine",
"source": {
"_id": "5de5261b8ce66f142e44cd08",
"oldEPiServerId": 1394,
"title": "ADHD Awareness Month",
"__v": 0
},
"shortSummary": "this is my medicine",
"resourceLinks": "http://localhost:3010/keystone/items",
"url": "https://www.nice.org.uk/",
"title": "MY MEDICINE",
"__v": 0,
"comment": "<p>this is my comment</p>",
"publicationDate": "2019-11-23T13:48:36.000Z",
"createdDate": "2019-10-23T10:38:43.000Z",
"specialities": [
{
"_id": "5e0e4331200a585f718e1ee5",
"key": "be1c2e2f-745e-4a82-b5aa-d4cef4d31a1b",
"title": "Anaesthesia and pain",
"__v": 0
},
{
"_id": "5e0e4331200a58dead8e1ee8",
"key": "53fc67a4-46d8-4171-9447-7fcf216c8749",
"title": "Complementary and alternative therapies",
"__v": 0
}
],
"evidenceType": {
"_id": "5df7abf383138898ee1f67ef",
"title": "Safety alerts",
"key": "mas_evidence_types:Safety%20alerts",
"oldEPiServerId": 778708,
"__v": 0
},
"isInitial": false
},
{
"_id": "5db031d78a34d4a69905b5e3",
"slug": "hello-world",
"source": {
"_id": "5de52a02c676eee677e97f41",
"oldEPiServerId": 2,
"title": "Substance Misuse Management in General Practice",
"__v": 0
},
"shortSummary": "test",
"resourceLinks": "http://localhost:3010/keystone/items",
"url": "https://www.bbc.co.uk/",
"title": "Hello world",
"__v": 0,
"comment": "<p>comment</p>",
"publicationDate": "2019-10-23T00:00:00.000Z",
"createdDate": "2019-10-23T10:56:40.000Z",
"specialities": [
{
"_id": "5e0e4331200a585f718e1ee5",
"key": "be1c2e2f-745e-4a82-b5aa-d4cef4d31a1b",
"title": "Anaesthesia and pain",
"__v": 0
},
{
"_id": "5e0e4331200a58dead8e1ee8",
"key": "53fc67a4-46d8-4171-9447-7fcf216c8749",
"title": "Complementary and alternative therapies",
"__v": 0
}
],
"evidenceType": {
"_id": "5df7abf383138898ee1f67ef",
"title": "Safety alerts",
"key": "mas_evidence_types:Safety%20alerts",
"oldEPiServerId": 778708,
"__v": 0
},
"isInitial": false
},
{
"_id": "5db6cbcc8a34d4ca5905b5e4",
"slug": "hello-there",
"source": {
"_id": "5de52a01c676ee589ae97d2a",
"oldEPiServerId": 2,
"title": "Journal of Surgical Oncology",
"__v": 0
},
"shortSummary": "hello",
"resourceLinks": "http://localhost:3010/keystone/items",
"url": "https://www.google.com/",
"title": "A Placebo",
"__v": 0,
"comment": "<p>test</p>",
"publicationDate": "2019-10-28T00:00:00.000Z",
"createdDate": "2019-10-28T11:07:11.000Z",
"specialities": [
{
"_id": "5e0e4331200a585f718e1ee5",
"key": "be1c2e2f-745e-4a82-b5aa-d4cef4d31a1b",
"title": "Anaesthesia and pain",
"__v": 0
},
{
"_id": "5e0e4331200a58dead8e1ee8",
"key": "53fc67a4-46d8-4171-9447-7fcf216c8749",
"title": "Complementary and alternative therapies",
"__v": 0
}
],
"evidenceType": {
"_id": "5df7abf383138898ee1f67ef",
"title": "Safety alerts",
"key": "mas_evidence_types:Safety%20alerts",
"oldEPiServerId": 778708,
"__v": 0
},
"isInitial": false
}
]
46 changes: 46 additions & 0 deletions lambda/MAS.Tests/Infrastructure/MASWebApplicationFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using MAS.Configuration;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;

namespace MAS.Tests.Infrastructure
{
/// <summary>
/// See https://github.com/aspnet/AspNetCore.Docs/issues/7063#issuecomment-414661566
/// </summary>
public class MASWebApplicationFactory : WebApplicationFactory<Startup>
{
protected override IWebHostBuilder CreateWebHostBuilder()
{
return WebHost.CreateDefaultBuilder()
.UseStartup<Startup>()
.UseLambdaServer();
}

protected override void ConfigureWebHost(IWebHostBuilder builder)
{
var projectDir = Directory.GetCurrentDirectory();

builder.ConfigureAppConfiguration((context, conf) =>
{
conf.AddJsonFile(Path.Combine(projectDir, "appsettings.test.json"));
});

builder.ConfigureTestServices(services => {
AppSettings.CMSConfig.BaseUrl = new Uri("file://" + projectDir + "/Feeds").ToString();
});

base.ConfigureWebHost(builder);
}
}
}
36 changes: 25 additions & 11 deletions lambda/MAS.Tests/Infrastructure/TestAppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,36 @@

namespace MAS.Tests.Infrastructure
{
public class TestAppSettings
/// <summary>
/// Use this in integration tests to override CMS config per test e.g. AppSettings.CMSConfig = TestAppSettings.CMS.InvalidURI;
/// </summary>
public static class TestAppSettings
{
public static CMSConfig GetInvalidURI()
public static class CMS
{
return new CMSConfig()
public static CMSConfig Default
{
BaseUrl = new Uri("file://" + Directory.GetCurrentDirectory() + "/Feeds/nonexistanturl").ToString()
};
}
get
{
return new CMSConfig()
{
BaseUrl = new Uri("file://" + Directory.GetCurrentDirectory() + "/Feeds").ToString(),
AllItemsPath = "/all-items.json",
DailyItemsPath = "/daily-items.json",
};
}
}

public static CMSConfig GetMultipleItemsFeed()
{
return new CMSConfig()
public static CMSConfig InvalidURI
{
BaseUrl = new Uri("file://" + Directory.GetCurrentDirectory() + "/Feeds/multiple-items.json").ToString()
};
get
{
return new CMSConfig()
{
BaseUrl = new Uri("file://" + Directory.GetCurrentDirectory() + "/Feeds/nonexistanturl").ToString()
};
}
}
}
}
}
54 changes: 20 additions & 34 deletions lambda/MAS.Tests/Infrastructure/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,41 @@
using System;
using System.Net.Http;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace MAS.Tests.Infrastructure
{
public class TestBase : IDisposable
public abstract class TestBase : IDisposable
{
protected readonly IWebHostBuilder _builder;
protected readonly TestServer _server;
protected readonly HttpClient _client;
//protected readonly IConfigurationRoot _config;
protected readonly MASWebApplicationFactory _factory;

public TestBase()
{
//_config = new ConfigurationBuilder()
// .AddJsonFile("appsettings.json")
// //.AddUserSecrets("adafe3d8-65fb-49fd-885e-03341a36dc88")
// .Build();

//var builder = new WebHostBuilder()
_builder = WebHost.CreateDefaultBuilder(new string[0])
.UseContentRoot("../../../../MAS")
.ConfigureServices(services =>
{
//AppSettings.Configure(services, _config);
})
.UseEnvironment("Production")
.UseStartup(typeof(Startup));

_server = new TestServer(_builder);
_client = _server.CreateClient();
//_client.BaseAddress = new Uri("http://localhost:5000");
_factory = new MASWebApplicationFactory();
}

protected void WithImplementation<TService, TImplementation>()
where TService : class where TImplementation : class, TService
protected WebApplicationFactory<Startup> WithImplementation<TService>(TService implementation)
{
_builder.ConfigureServices(serviceCollection => serviceCollection.AddTransient<TService, TImplementation>());
}
return _factory.WithWebHostBuilder(builder =>
{
builder.ConfigureTestServices(services =>
{
var serviceProvider = services.BuildServiceProvider();
protected void WithImplementation<TService>(TService implementation)
where TService : class
{
_builder.ConfigureServices(serviceCollection => serviceCollection.AddTransient(serviceProvier => implementation));
var descriptor =
new ServiceDescriptor(
typeof(TService), implementation);
services.Replace(descriptor);
});
});
}

public virtual void Dispose()
{
// Do "global" teardown here; Called after every test method.
_client.Dispose();
_server.Dispose();
_factory.Dispose();
}
}
}
Loading

2 comments on commit 7804e5c

@NICE-TeamCity
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TeamCity MAS / MAS Build 1.1.0.562 outcome was FAILURE
Summary: Artifacts size 0 B is 100% different from 3.6 MB in build #1.1.0.557-r893BC73; exit code 1 (Step: Set build number (Command Line)) (new) Build time: 00:00:15

@NICE-TeamCity
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TeamCity MAS / MAS Build 1.1.0.563-MAS-129_121__112_Dai outcome was FAILURE
Summary: Artifacts size 0 B is 100% different from 3.6 MB in build #1.1.0.557-r893BC73 Build time: 00:00:38

Please sign in to comment.