/
Helpers.cs
170 lines (139 loc) · 5.83 KB
/
Helpers.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Dispatcher;
using System.Web.Http.Hosting;
using System.Web.Http.Routing;
using NHibernate;
using NSubstitute;
using Streamus_Web_API.Domain;
using Streamus_Web_API.Domain.Interfaces;
using Streamus_Web_API.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Streamus_Web_API_Tests
{
/// <summary>
/// Stores common methods used by tests. Just useful for keeping things DRY between test cases.
/// </summary>
public class Helpers
{
private readonly IUserManager UserManager;
private readonly IPlaylistItemManager PlaylistItemManager;
public Helpers(ISession session, IManagerFactory managerFactory)
{
UserManager = managerFactory.GetUserManager(session);
PlaylistItemManager = managerFactory.GetPlaylistItemManager(session);
}
/// <summary>
/// Creates a new Video and PlaylistItem, puts item in the database and then returns
/// the item. Just a nice utility method to keep things DRY.
/// </summary>
public PlaylistItem CreateItemInPlaylist(Playlist playlist)
{
// Create a new PlaylistItem and write it to the database.
var playlistItem = CreateUnsavedPlaylistItem();
playlist.AddItem(playlistItem);
PlaylistItemManager.Save(playlistItem);
return playlistItem;
}
/// <summary>
/// Creates a new Video with a random Id, or a given Id if specified, saves it to the database and returns it.
/// </summary>
public PlaylistItem CreateUnsavedPlaylistItem(string videoIdOverride = "", string titleOverride = "")
{
// Create a random video ID to ensure the Video doesn't exist in the database currently.
string randomVideoId = videoIdOverride == string.Empty ? Guid.NewGuid().ToString().Substring(0, 11) : videoIdOverride;
string title = titleOverride == string.Empty ? string.Format("Video {0}", randomVideoId) : titleOverride;
var playlistItem = new PlaylistItem(title, randomVideoId, SourceType.YouTube, title, 999, "Author");
return playlistItem;
}
public User CreateUser()
{
User user = UserManager.CreateUser();
return user;
}
/// <summary>
/// Generate a PlaylistDto which has the User as its parent.
/// </summary>
/// <returns></returns>
public PlaylistDto CreatePlaylistDto(Guid userIdOverride)
{
var playlistDto = new PlaylistDto
{
UserId = userIdOverride
};
return playlistDto;
}
/// <summary>
/// Create a new Playlist, save it to the DB, then generate a PlaylistItemDto
/// which has the Playlist as its parent.
/// </summary>
public PlaylistItemDto CreatePlaylistItemDto()
{
User user = CreateUser();
PlaylistItem playlistItem = CreateUnsavedPlaylistItem();
var playlistItemDto = PlaylistItemDto.Create(playlistItem);
playlistItemDto.PlaylistId = user.Playlists.First().Id;
return playlistItemDto;
}
/// <summary>
/// Create a new Playlist, save it to the DB, then generate N PlaylistItemDtos
/// which have the Playlist as their parent.
/// </summary>
public List<PlaylistItemDto> CreatePlaylistItemsDto(int itemsToCreate, Guid playlistId = default(Guid))
{
if (playlistId == default(Guid))
{
User user = CreateUser();
playlistId = user.Playlists.First().Id;
}
PlaylistItem playlistItem = CreateUnsavedPlaylistItem();
List<PlaylistItemDto> playlistItemDtos = new List<PlaylistItemDto>();
for (int i = 0; i < itemsToCreate; i++)
{
var playlistItemDto = PlaylistItemDto.Create(playlistItem);
playlistItemDto.PlaylistId = playlistId;
playlistItemDtos.Add(playlistItemDto);
}
return playlistItemDtos;
}
public static RouteInfo RouteRequest(HttpConfiguration config, HttpRequestMessage request)
{
// create context
var controllerContext = new HttpControllerContext(config, Substitute.For<IHttpRouteData>(), request);
// get route data
var routeData = config.Routes.GetRouteData(request);
RemoveOptionalRoutingParameters(routeData.Values);
request.Properties[HttpPropertyKeys.HttpRouteDataKey] = routeData;
controllerContext.RouteData = routeData;
// get controller type
var controllerDescriptor = new DefaultHttpControllerSelector(config).SelectController(request);
controllerContext.ControllerDescriptor = controllerDescriptor;
// get action name
var actionMapping = new ApiControllerActionSelector().SelectAction(controllerContext);
return new RouteInfo
{
Controller = controllerDescriptor.ControllerType,
Action = actionMapping.ActionName
};
}
private static void RemoveOptionalRoutingParameters(IDictionary<string, object> routeValues)
{
var optionalParams = routeValues
.Where(x => x.Value == RouteParameter.Optional)
.Select(x => x.Key)
.ToList();
foreach (var key in optionalParams)
{
routeValues.Remove(key);
}
}
}
public class RouteInfo
{
public Type Controller { get; set; }
public string Action { get; set; }
}
}