public static byte[] Bytify(ProjectModel[] projects)
		{
			var hash = Hashify(projects);
			var json = JsonSerializer.SerializeObject(hash);
			var bytes = Encoding.UTF8.GetBytes(json);
			return bytes;
		}
		public void BeforeEachTest()
		{
			_display = new CharDisplaySpy();
			_config = new Mock<IConfigurationService>();
			_projectOne = new ProjectModel("succ-1", "Project 1", "Subtitle 1", 1, BuildServiceProvider.Succeeding);
			_projectTwo = new ProjectModel("fail-1", "Project 2", "Subtitle 2", 2, BuildServiceProvider.Failing);
			_sut = new ProjectSelector(_display, _config.Object);
		}
		public static Hashtable Hashify(ProjectModel[] projects)
		{
			var hashList = new Hashtable();
			foreach(var proj in projects)
			{
				var hash = Hashify(proj);
				hashList.Add(proj.Slug, hash);
			}
			return hashList;
		}
		public static Hashtable Hashify(ProjectModel proj)
		{
			var hash = new Hashtable();

			hash["title"] = proj.Title;
			hash["subtitle"] = proj.Subtitle;
			hash["rank"] = proj.Rank;
			hash["provider"] = ((int) proj.Provider);

			return hash;
		}
		public void SaveProject(ProjectModel newProject)
		{
			var projects = ReadConfigFile();
			if(newProject.Slug == "")
			{
				var existingSlugs = GetSlugs(projects);
				newProject.Slug = _slugCreator.CreateSlug(existingSlugs);
			}
			else
			{
				RemoveBySlug(projects, newProject.Slug);
			}
			projects.Add(newProject);
			WriteConfigFile(projects);
		}
		public void BeforeEachTest()
		{
			_jsonPersist = new Mock<IJsonPersistence>();
			_slugCreator = new Mock<ISlugCreator>();

			_newProject = new ProjectModel("", "New project", "New subtitle", 5, BuildServiceProvider.Succeeding);
			_survivingProject = new ProjectModel("slug-1",
			                                     "Surviving project",
			                                     "Surviving subtitle", 3, BuildServiceProvider.Failing);
			_doomedProject = new ProjectModel("slug-2",
			                                  "Doomed project",
			                                  "Doomed subtitle",
			                                  5, BuildServiceProvider.Succeeding);

			_jsonPersist.Setup(x => x.Read(@"\root\config\projects.json")).Returns(new Hashtable());

			_sut = new RealConfigurationService(@"\root\", _jsonPersist.Object, _slugCreator.Object);
		}
		public void BeforeEachTest()
		{
			_projectOne = new ProjectModel("proj-1", "Project One", "Subtitle One", 1, BuildServiceProvider.AppVeyor);
			_projectTwo = new ProjectModel("proj-2", "Project Two", "Subtitle Two", 2, BuildServiceProvider.TeamCity);
			_jsonOne = JsonSerializer.SerializeObject(ConfigHashifier.Hashify(_projectOne));

			_readBody = new FakeApiReadBody();
			_socket = new Mock<IApiSocket>();
			_config = new Mock<IConfigurationService>();
			_garbage = new Mock<IGarbage>();

			_req = new ApiRequest
				{
					Headers = new Hashtable(),
					GetArguments = new Hashtable(),
					Body = _readBody,
					HttpMethod = "GET",
					Url = "projects/",
					Client = _socket.Object,
				};
			_sut = new ConfigApiService(_config.Object, _garbage.Object);
		}
        private void HandleDefault(ApiRequest request)
        {
            if (request.HttpMethod == "GET")
            {
                var projects = _configurationService.GetProjects();
                var bytes = ConfigHashifier.Bytify(projects);
                request.Client.Send200_OK("application/json", bytes.Length);
                request.Client.Send(bytes, bytes.Length);
                return;
            }

            if (request.HttpMethod == "PUT")
            {
                var proj = new ProjectModel();
                UnpackProject(request, proj);
                proj.Slug = "";
                _configurationService.SaveProject(proj);
                request.Client.Send200_OK("application/json");
                return;
            }

            request.Client.Send405_MethodNotAllowed();
        }
        private static void UnpackProject(ApiRequest request, ProjectModel proj)
        {
            var buffer = new byte[BufferSize];
            var countBytes = ShortBodyReader.ReadBody(request.Body, buffer);
            var chars = Encoding.UTF8.GetChars(buffer, 0, countBytes);
            var json = new string(chars);
            var project = JsonSerializer.DeserializeString(json) as Hashtable;
            if (project == null) return;

            proj.Title = project["title"] as string;
            proj.Subtitle = project["subtitle"] as string;
            proj.Rank = Int32.Parse(project["rank"].ToString());
            proj.Provider = (BuildServiceProvider) Int32.Parse(project["provider"].ToString());
        }
		public void One_project_endpoint_saves_on_put()
		{
			var modifiedProject = new ProjectModel
				{
					Title = "Modified title",
					Subtitle = "Modified subtitle",
					Rank = 55,
					Provider = BuildServiceProvider.TeamCity
				};
			var hash = ConfigHashifier.Hashify(modifiedProject);
			var json = JsonSerializer.SerializeObject(hash);
			var bytesModifiedProject = Encoding.UTF8.GetBytes(json);

			_config.Setup(x => x.GetProject(_projectOne.Slug)).Returns(_projectOne);
			_config.Setup(x => x.SaveProject(It.IsAny<ProjectModel>()))
			       .Callback((ProjectModel proj) =>
				       {
					       Assert.AreEqual(_projectOne.Slug, proj.Slug);
					       Assert.AreEqual(modifiedProject.Title, proj.Title);
					       Assert.AreEqual(modifiedProject.Subtitle, proj.Subtitle);
					       Assert.AreEqual(modifiedProject.Rank, proj.Rank);
					       Assert.AreEqual(modifiedProject.Provider, proj.Provider);
				       });

			_req.HttpMethod = "PUT";
			_req.Url = "projects/" + _projectOne.Slug;
			_readBody.SetUp(bytesModifiedProject);

			SutSendResponse();

			_config.Verify(x => x.SaveProject(It.IsAny<ProjectModel>()), Times.Once);
			_socket.Verify(x => x.Send200_OK("application/json"), Times.Once);

			_socket.Verify(x => x.Send404_NotFound(), Times.Never);
			_socket.Verify(x => x.Send405_MethodNotAllowed(), Times.Never);
			_socket.Verify(x => x.Send500_Failure(It.IsAny<string>()), Times.Never);
		}
		private void MockConfigZeroProjects()
		{
			var models = new ProjectModel[] {};
			_config.Setup(x => x.GetProjects()).Returns(models);
		}
		private ProjectModel MockReadExistingProject(string slug)
		{
			var existingProject = new ProjectModel(slug,
			                                       "Existing project",
			                                       "Existing subtitle",
			                                       3, BuildServiceProvider.Failing);

			var existingHash = ConfigHashifier.Hashify(existingProject);
			_jsonPersist.Setup(x => x.Read(@"\root\config\projects.json"))
			            .Returns(new Hashtable
				            {
					            {existingProject.Slug, existingHash}
				            });
			return existingProject;
		}
		private static void AssertContainsProject(IDictionary hash, string slug, ProjectModel project)
		{
			Assert.IsTrue(hash.Contains(slug), "Slug exists in hash");
			var pro = (Hashtable) hash[slug];
			Assert.IsNotNull(pro, "Project exists");
			Assert.AreEqual(project.Title, pro["title"], "title");
			Assert.AreEqual(project.Subtitle, pro["subtitle"], "subtitle");
			Assert.AreEqual(project.Rank, pro["rank"], "rank");
			Assert.AreEqual((int) project.Provider, pro["provider"], "provider");
		}
		private void MockConfigTwoProjects()
		{
			var models = new ProjectModel[]
				{
					_projectOne,
					_projectTwo
				};
			_config.Setup(x => x.GetProjects()).Returns(models);
		}
		private void AssertProjectSelected(ProjectModel proj)
		{
			Assert.IsTrue(_sut.IsProjectSelected, "Is selected");
			Assert.AreEqual(proj.Title, _sut.SelectedProjectName, "Name");
			Assert.AreEqual(proj, _sut.SelectedProject, "Model");
			Assert.AreEqual(proj.Title, _display.Line1, "Display line 1");
			Assert.AreEqual(proj.Subtitle, _display.Line2, "Display line 2");
		}
		public void SaveProject(ProjectModel newProject)
		{
		}
		private ArrayList ReadConfigFile()
		{
			var list = _persistence.Read(ProjectListFilePath);
			var projects = new ArrayList();
			foreach(string slug in list.Keys)
			{
				var projectHash = list[slug] as Hashtable;
				if(projectHash == null) continue;
				var provider = Int32.Parse(projectHash["provider"].ToString());
				var prov = (BuildServiceProvider) provider;
				var proj = new ProjectModel(slug,
				                            projectHash["title"] as string,
				                            projectHash["subtitle"] as string,
				                            Int32.Parse(projectHash["rank"].ToString()),
				                            prov);
				projects.Add(proj);
			}
			return projects;
		}