using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Configuration;
using System.Net;
using System.IO;

namespace RestApiTest
{
    [TestClass]
    public class QueryTest
    {
        const string SIMPLE_QUERY_REQUEST = "{\"Query\":\"SELECT 1 AS a\"}";
        const string SIMPLE_QUERY_RESULT = "a\n1";
        const string REQUEST_URI = "/contexts/mydb/query";

        [TestMethod]
        public void AuthorizedQuickQuery()
        {
            string requestUri = ConfigurationManager.AppSettings["RestApiUri"] + REQUEST_URI;
            var request = (HttpWebRequest)WebRequest.Create(requestUri);
            request.Method = "POST";
            request.ContentType = "application/json";
            request.Headers.Add("X-Auth-Token", CredentialsManager.RegisteredUser.Token.Id);
            StreamWriter writer = new StreamWriter(request.GetRequestStream());
            writer.Write(SIMPLE_QUERY_REQUEST);
            writer.Close();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            
            StreamReader reader = new StreamReader(response.GetResponseStream());
            string s = reader.ReadToEnd();
            reader.Close();

            Assert.AreEqual(SIMPLE_QUERY_RESULT,s);
        }

        [TestMethod]
        public void AnonymousQuickQuery()
        {
            string requestUri = ConfigurationManager.AppSettings["RestApiUri"] + REQUEST_URI;
            var request = (HttpWebRequest)WebRequest.Create(requestUri);
            request.Method = "POST";
            request.ContentType = "application/json";
            StreamWriter writer = new StreamWriter(request.GetRequestStream());
            writer.Write(SIMPLE_QUERY_REQUEST);
            writer.Close();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);

            StreamReader reader = new StreamReader(response.GetResponseStream());
            string s = reader.ReadToEnd();
            reader.Close();

            Assert.AreEqual(SIMPLE_QUERY_RESULT, s);
        }

        [TestMethod]
        public void UnauthorizedQuickQuery()
        {
            string requestUri = ConfigurationManager.AppSettings["RestApiUri"] + REQUEST_URI;
            var request = (HttpWebRequest)WebRequest.Create(requestUri);
            request.Method = "POST";
            request.ContentType = "application/json";
            request.Headers.Add("X-Auth-Token", "_invalid token_");
            StreamWriter writer = new StreamWriter(request.GetRequestStream());
            writer.Write("");
            writer.Close();
            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Assert.Fail();
            }
            catch (WebException ex)
            {
                using (HttpWebResponse response = (HttpWebResponse)ex.Response)
                {
                    Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode);
                }
            }
        }
    }
}
