Exemplo n.º 1
0
        static void Main(string[] args)
        {
            if (args.Length < 4)
            {
                Console.Error.WriteLine("Usage:");
                Console.Error.WriteLine("    Gototags.Platform.Data.Test host domain username password");
                return;
            }
            string host     = args[0];
            string domain   = args[1];
            string username = args[2];
            string password = args[3];

            //  Once we have the application then we must switch to our application specific API
            Client client = new Client(host, domain, username, password);

            //  load the client application (every call to this method does a query on the server, so you should cache a copy locally)
            Application app = client.GetApplication();

            //  change user permissions and update
            app.UserPermissions.Device.Create = true;
            app.UserPermissions.Device.Read   = true;
            app.UserPermissions.Device.Write  = true;
            app.UserPermissions.Device.Delete = true;
            //  now update the Application
            app = client.UpdateApplication(app);
            if (app == null)
            {
                throw new Exception("Failed to update Application");
            }
            ApplicationUserPermissionsDevice p = app.UserPermissions.Device;

            if (!(p.Create.Value && p.Read.Value && p.Write.Value && p.Delete.Value))
            {
                throw new Exception("User Device Permissions were not all true: " + p);
            }

            Device device = client.CreateDevice(new Device()
            {
                Name = "Device One", Identity = "Q 36 space modulator"
            });

            if (device == null)
            {
                throw new Exception("Failed to create Device");
            }
            if (device.Name != "Device One")
            {
                throw new Exception("Device should have had Name = 'Device One': " + device);
            }
            if (device.ApplicationId != app.Id)
            {
                throw new Exception("Device.ApplicationId (" + device.ApplicationId + ")should have been same as Application.Id: " + app.Id);
            }
            Console.WriteLine("------------------------------------Device--------------------------------------");
            Console.WriteLine(device);

            //	test queries for the device
            QueryOptions options = new QueryOptions()
            {
                Skip = 0
            }.Sort(Device.NAME, true);
            TypedArray <Device> devices = client.GetDevices(options, new Device.Query()
            {
                Name = "Device One",
                //  not a realistic query, but it tests compound queries and should only return one result.
                CreatedDateGreaterThanOrEquals = device.CreatedDate,
                CreatedDateLessThanOrEquals    = device.CreatedDate.AddMinutes(1)
            });

            Console.WriteLine("------------------------------------Query--------------------------------------");
            Console.WriteLine(devices.ToString());
            if (devices.Count != 1 || !device.Id.Equals(devices.First.Id))
            {
                throw new Exception("Device Query should have returned the device we previously created.");
            }
            if (options.Total != 1)
            {
                throw new Exception("Options.total should have been 1 but was: " + options.Total);
            }
            Console.WriteLine(devices);

            //  try to create another device with the same identity.  This should throw a 409 Conflict WebException
            WebException error = null;

            try
            {
                client.CreateDevice(new Device()
                {
                    Identity = device.Identity
                });
            }
            catch (WebException e)
            {
                error = e;
            }
            if (error == null || error.GetStatusCode() != HttpStatusCode.Conflict)
            {
                Console.WriteLine("Error: CreatDevice should have thrown a 409 Conflict Exception: " + error);
            }

            //  now check that getting the device by id works
            device = client.GetDevice(device.Id);
            if (device == null)
            {
                throw new Exception("Failed to get Device");
            }

            //	touch the device
            HttpWebRequest request = client.Touch(device.TouchUrl, device.Uid);
            //	in this case we know the response should be JSON, so we get the value.
            //	if this Device had a URL action then we would expect the response to be a 302 Redirect.
            Object touchResponse = client.GetResponseContent(request);

            Console.WriteLine("------------------------------------Touch--------------------------------------");
            Console.WriteLine(touchResponse);

            //  finally, delete the device
            client.DeleteDevice(device.Id);

            //	now test our utility ConvertVCardToContact function
            Console.WriteLine("-----------------------------------Contact--------------------------------------");
            Contact contact = client.ConvertVCardToContact("BEGIN:VCARD\r\nVERSION:2.1\r\nFN:Mr John P Smith Sr\r\nN:Smith;John;P;Mr;Sr\r\nORG:Gototags;Engineering\r\nTITLE:Chief\r\nROLE:Architect\r\nBDAY:19840204\r\nEMAIL;WORK:[email protected]\r\nEMAIL;HOME:[email protected]\r\nTEL;CELL:222-222-2222\r\nTEL;WORK:333-333-3333\r\nTEL;HOME:111-111-1111\r\nTEL;FAX;WORK:555-555-5555\r\nTEL;FAX;HOME:444-444-4444\r\nURL;WORK:http://gototags.com/john\r\nURL;HOME:http://home.com/john\r\nADR;WORK:;;one gototags way;Seattle;WA;98034;USA\r\nADR;HOME:;;12206 Kinley Ave NE;Duvall;WA;98055;USA\r\nEND:VCARD");

            Console.WriteLine(contact);

            Console.WriteLine("---------------------------------Current User-----------------------------------");
            User user = client.GetCurrentUser();

            Console.WriteLine(user);

            Console.WriteLine("--------------------------------------------------------------------------------");
            Console.WriteLine("All Tests Passed");
        }