diff --git a/Main/TestData.cs b/Main/TestData.cs index 4392f3e36aad2ef33f1e3ddb5c4f44f58313aa76..bb8de0a235fa8245955c854ff058e07503581079 100644 --- a/Main/TestData.cs +++ b/Main/TestData.cs @@ -295,5 +295,40 @@ namespace Main return sensorDataContracts; } + + public static ShiftContract GetShift() + { + return new ShiftContract + { + Id = 1, + Title = "shift1", + BranchId = 1, + StartTime = 1599644652178, + StopTime = 1599644652178 + }; + } + + public static ShiftContract[] GetShifts() + { + return new[] + { + new ShiftContract + { + Id = 1, + Title = "shift1", + BranchId = 1, + StartTime = 1599644652178, + StopTime = 1599644652178 + }, + new ShiftContract + { + Id = 2, + Title = "shift2", + BranchId = 1, + StartTime = 1599644652178, + StopTime = 1599644652178 + } + }; + } } } diff --git a/README.md b/README.md index d2f830f385becdff9a0095eb1b021e610abac747..006fb0da1d246b419b376bd526ec9bb75c4ffd31 100644 --- a/README.md +++ b/README.md @@ -234,6 +234,9 @@ await devkitConnector.AddSensorData(sensorContracts); * **Shifts** * `GetShifts()` - Get all shifts * `GetShift(id)` - Get shift by ID + * `AddShift(shiftContract)` - Add a shift with specified properties + * `UpdateShift(shiftContract)` - Update an existing shift with new properties + * `DeleteShift(id)` - Delete an existing shift by ID * **Users** * `GetUserInfo()` - Get information about the current user (in order to do this you need to be **authenticated** as said user) * **Utils** diff --git a/SDK/Connection/DevkitConnector.cs b/SDK/Connection/DevkitConnector.cs index 044459824295ad63b781fa723343e9b230933e7a..d069e373ae3005e17ed2b09bc6b37fc8d2d065e9 100644 --- a/SDK/Connection/DevkitConnector.cs +++ b/SDK/Connection/DevkitConnector.cs @@ -54,12 +54,11 @@ namespace SDK return await JsonResponse<Type>(response); } - protected async Task<Type> PatchRequest<Type>(string subUrl, object body) + protected async Task PatchRequest<Type>(string subUrl, object body) { string bodyContent = JsonSerializer.Serialize(body); HttpContent httpContent = new StringContent(bodyContent, Encoding.UTF8, "application/json"); - var response = await httpClient.PatchAsync(subUrl, httpContent); - return await JsonResponse<Type>(response); + await httpClient.PatchAsync(subUrl, httpContent); } protected async Task PatchRequest(string subUrl, object body) diff --git a/SDK/Connection/V3/Shifts.cs b/SDK/Connection/V3/Shifts.cs index 963fa24eadcd38c157e5e5f4388d60350d4a07a3..8ed4a310122383cec2bac150a744ed73e252a96f 100644 --- a/SDK/Connection/V3/Shifts.cs +++ b/SDK/Connection/V3/Shifts.cs @@ -1,4 +1,6 @@ -using SDK.Contracts.Data; +using SDK.Contracts.Communication; +using SDK.Contracts.Data; +using SDK.Exceptions; using SDK.Models; using System; using System.Threading.Tasks; @@ -15,6 +17,7 @@ namespace SDK return response; } + public async Task<ShiftContract> GetShift(int id, string queryString = "") { string subUrl = Address.UrlCombine(Address.Shifts, Convert.ToString(id), queryString); @@ -22,5 +25,34 @@ namespace SDK return response; } + + public async Task<ShiftContract> AddShift(ShiftContract shiftContract) + { + string subUrl = Address.UrlCombine(Address.Shifts); + var response = await PostRequest<AddShiftResponseContract>(subUrl, shiftContract); + + if (response.ErrorMessage != null) + { + throw new ServerResponseException(ServerResponseException.message + " " + response.ErrorMessage); + } + + return (ShiftContract)response; + } + + public async Task UpdateShift(ShiftContract shiftContract) + { + if (shiftContract.Id == 0) + { + throw new BadRequestException(NotFoundException.message + " Shift object has no Id."); + } + string subUrl = Address.UrlCombine(Address.Shifts, Convert.ToString(shiftContract.Id)); + await PatchRequest<string>(subUrl, shiftContract); + } + + public async Task DeleteShift(int id) + { + string subUrl = Address.UrlCombine(Address.Shifts, Convert.ToString(id)); + await DeleteRequest(subUrl); + } } } diff --git a/SDK/Contracts/Communication/AddShiftResponseContract.cs b/SDK/Contracts/Communication/AddShiftResponseContract.cs new file mode 100644 index 0000000000000000000000000000000000000000..00e9b9a220fc87f849e1212af479fec6cd6ce93a --- /dev/null +++ b/SDK/Contracts/Communication/AddShiftResponseContract.cs @@ -0,0 +1,30 @@ +using SDK.Communication; +using SDK.Contracts.Data; + +namespace SDK.Contracts.Communication +{ + public class AddShiftResponseContract : PostResponseContract + { + public int Id { get; set; } + + public string Title { get; set; } + + public int BranchId { get; set; } + + public long StartTime { get; set; } + + public long StopTime { get; set; } + + public static explicit operator ShiftContract(AddShiftResponseContract addShiftResponseContract) + { + return new ShiftContract + { + Id = addShiftResponseContract.Id, + Title = addShiftResponseContract.Title, + BranchId = addShiftResponseContract.BranchId, + StartTime = addShiftResponseContract.StartTime, + StopTime = addShiftResponseContract.StopTime + }; + } + } +} \ No newline at end of file diff --git a/Test/Test/TestData.cs b/Test/Test/TestData.cs index 101a46e99782327dbe0d547bc0c965f52d7aa096..be59b14d497b60a5f6c2e73dfe0f793a1cbca74f 100644 --- a/Test/Test/TestData.cs +++ b/Test/Test/TestData.cs @@ -7,6 +7,7 @@ namespace Main { public static class TestData { + #region Beacons public static BeaconContract GetBeacon() { return new BeaconContract @@ -48,10 +49,9 @@ namespace Main UseGps = false, }; } - public static DeviceContract GetDevice() { - DeviceContract device = new DeviceContract + return new DeviceContract { Mac = "00:00:00:00:00:00", BranchId = 1, @@ -60,27 +60,23 @@ namespace Main Title = "sdk-device", X = 10.0, Y = 10.0, - //AppVersion = "1.0", IsMoving = false, FallStatus = FallType.OK, - //Battery = 46f, DeviceTypeId = 8, Position = false, Geofence = false }; - return device; } - public static DeviceLocationContract[] GetLocalizationDataBatch() { - DistanceContract[] distanceContract1 = new [] + DistanceContract[] distanceContract1 = new[] { new DistanceContract { BeaconId = 34, RSSI = -56 }, new DistanceContract { BeaconId = 34, RSSI = -56 }, new DistanceContract { BeaconId = 34, RSSI = -56 }, }; - LocationContract[] locationContract = new[] + return new[] { new DeviceLocationContract { Login = "rtu-sdk", Locations = new[] { new LocationContract {SectorId = 1, Battery = 100, IsMoving = true, Timestamp = 1599644652178, X = 0, Y = 0, Z = 0, Interval = 300, Distances = distanceContract1 }, @@ -90,9 +86,8 @@ namespace Main X = 0, Y = 0, Z = 0, Interval = 300, Distances = distanceContract1 }, new LocationContract {SectorId = 4, Battery = 100, IsMoving = true, Timestamp = 1599644652178, X = 0, Y = 0, Z = 0, Interval = 300, Distances = distanceContract1 }, - }; - - return new[] { new DeviceLocationContract { Login = "rtu-sdk", Locations = locationContract } }; + } + }}; } public static LocationContract[] GetLocalizationData() { @@ -115,7 +110,9 @@ namespace Main X = 0, Y = 0, Z = 0, Interval = 300, Distances = distanceContract1 }, }; } + #endregion + #region Sectors public static SectorContract GetSector() { return new() @@ -128,41 +125,41 @@ namespace Main SectorHeight = 10000 }; } - public static SensorContract GetSensor() { - SensorDataContract data1 = new SensorDataContract - { - Quantity = "Temperature", - Value = "16", - Unit = "°C", - DataType = SensorDataType.Decimal - }; - SensorDataContract data2 = new SensorDataContract - { - Quantity = "Humidity", - Value = "31", - Unit = "%", - DataType = SensorDataType.Decimal + return new SensorContract + { + Login = "sdk-sensor1", + SensorData = new[] + { + new SensorDataContract + { + Quantity = "Temperature", + Value = "16", + Unit = "°C", + DataType = SensorDataType.Decimal + }, + new SensorDataContract + { + Quantity = "Humidity", + Value = "31", + Unit = "%", + DataType = SensorDataType.Decimal + }, + new SensorDataContract + { + Quantity = "CO2", + Timestamp = 1614599484673, + Value = "800", + DataType = SensorDataType.Int32, + Unit = "unit" + } + } }; - SensorDataContract[] sensorDataContracts = new [] { data1, data2 }; - - SensorContract sensor = new SensorContract - { - //Id = 7351, - Login = "sdk-sensor", - //Password = "sdk", - Title = "sdk-sensor", - SectorId = 2, - SensorData = sensorDataContracts, - //AreaId = 19 - }; - - return sensor; } public static SensorContract GetSensorUpdate() { - SensorContract sensor = new SensorContract + return new SensorContract { Id = 7351, Login = "sdk-sensor2", @@ -170,74 +167,84 @@ namespace Main SectorId = 2, AreaId = 19 }; - - return sensor; } public static SensorContract[] GetSensorDataBatch() { - SensorDataContract data1 = new SensorDataContract - { - Quantity = "Temperature", - Value = "16", - Unit = "°C", - DataType = SensorDataType.Decimal - }; - SensorDataContract data2 = new SensorDataContract - { - Quantity = "Humidity", - Value = "31", - Unit = "%", - DataType = SensorDataType.Decimal - }; - SensorDataContract data3 = new SensorDataContract - { - Quantity = "CO2", - Timestamp = 1614599484673, - Value = "800", - DataType = SensorDataType.Int32, - Unit = "unit" - }; - var sensorDataContracts = new[] { data1, data2, data3 }; - - SensorContract sensor = new SensorContract + return new[] { - Login = "enviro-sdk", - //SectorId = 2, - SensorData = sensorDataContracts, - //AreaId = 24 + new SensorContract + { + Login = "sdk-sensor1", + SensorData = GetSensorData() + }, + new SensorContract + { + Login = "sdk-sensor2", + SensorData = GetSensorData() + } }; - SensorContract[] sensorContracts = new SensorContract[] { sensor }; - - return sensorContracts; } public static SensorDataContract[] GetSensorData() { - SensorDataContract data1 = new SensorDataContract + return new[] { - Quantity = "Temperature", - Value = "16", - Unit = "°C", - DataType = SensorDataType.Decimal + new SensorDataContract + { + Quantity = "Temperature", + Value = "16", + Unit = "°C", + DataType = SensorDataType.Decimal + }, + new SensorDataContract + { + Quantity = "Humidity", + Value = "31", + Unit = "%", + DataType = SensorDataType.Decimal + }, + new SensorDataContract + { + Quantity = "CO2", + Timestamp = 1614599484673, + Value = "800", + DataType = SensorDataType.Int32, + Unit = "unit" + } }; - SensorDataContract data2 = new SensorDataContract + } + #endregion + + #region Shifts + public static ShiftContract GetShift() + { + return new ShiftContract { - Quantity = "Humidity", - Value = "31", - Unit = "%", - DataType = SensorDataType.Decimal + BranchId = 1, + Title = "shift1", + StartTime = 1599644652178, + StopTime = 1599644652178 }; - SensorDataContract data3 = new SensorDataContract + } + public static ShiftContract[] GetShifts() + { + return new[] { - Quantity = "CO2", - Timestamp = 1614599484673, - Value = "800", - DataType = SensorDataType.Int32, - Unit = "unit" + new ShiftContract + { + BranchId = 1, + Title = "shift1", + StartTime = 1599644652178, + StopTime = 1599644652178 + }, + new ShiftContract + { + BranchId = 1, + Title = "shift2", + StartTime = 1599644652178, + StopTime = 1599644652178 + } }; - - SensorDataContract[] sensorDataContracts = new[] { data1, data2, data3 }; - - return sensorDataContracts; } + #endregion } } diff --git a/Test/Test/V3/Requests.cs b/Test/Test/V3/Requests.cs index 546a7329c5c4bee3bdfaf797a02a9cafcd601a33..7479dc601603c7259231cbf246430f454cbe3b90 100644 --- a/Test/Test/V3/Requests.cs +++ b/Test/Test/V3/Requests.cs @@ -28,6 +28,7 @@ namespace Test.V3 const string LAYERS = "layers"; const string LOG = "logs"; const string SECTORS = "sectors"; + const string SHIFTS = "shifts"; static WireMockServer server; static DevkitConnectorV3 devkitConnector; @@ -62,6 +63,8 @@ namespace Test.V3 server.Dispose(); } + #region Device + [TestMethod] public async Task GetDevices_ErrorHandling_ShouldThrowsException() { @@ -82,7 +85,7 @@ namespace Test.V3 public async Task GetDevice_GetAllDevices_ShouldReturn200() { var bodyContent = new DeviceContract[] { - new DeviceContract(){} + new DeviceContract(){ } }; server.Reset(); @@ -224,6 +227,9 @@ namespace Test.V3 Assert.IsInstanceOfType(response, typeof(ManDownResponseContract[])); } + #endregion + + #region Layer [TestCategory("Layer")] [TestMethod] public async Task GetLocalizationLayers_ShouldReturnLayers() @@ -248,6 +254,9 @@ namespace Test.V3 Assert.IsInstanceOfType(response, typeof(LayerContract[])); } + #endregion + + #region Log [TestCategory("Log")] [TestMethod] @@ -273,6 +282,9 @@ namespace Test.V3 Assert.IsInstanceOfType(response, typeof(LogContract)); } + #endregion + + #region Sector [TestCategory("Sector")] [TestMethod] public async Task GetSector_GetDeviceByLogin_ShouldReturn200() @@ -290,7 +302,117 @@ namespace Test.V3 Assert.IsInstanceOfType(response, typeof(SectorContract[])); } + #endregion + + #region Shifts + [TestCategory("Shifts")] + [TestMethod] + public async Task GetShift_ShouldReturnShiftContract() + { + const int Id = 1; + var bodyContent = new ShiftContract() + { + Id = 1, + BranchId = 1, + StartTime = 1599644652178, + StopTime = 1599644652178, + Title = "shift1" + }; + + server.Given(Request.Create().WithPath(PATH_BASE + SHIFTS + "/" + Id).UsingGet()) + .RespondWith(Response.Create().WithStatusCode(200).WithBodyAsJson(bodyContent)); + + ShiftContract response = await devkitConnector.GetShift(1); + Assert.IsInstanceOfType(response, typeof(ShiftContract)); + } + + [TestCategory("Shifts")] + [TestMethod] + public async Task GetShifts_ShouldReturnShiftContracts() + { + var bodyContent = new ShiftContract[] + { + new ShiftContract + { + Id = 1, + BranchId = 1, + StartTime = 1599644652178, + StopTime = 1599644652178, + Title = "shift1" + }, + new ShiftContract + { + Id = 2, + BranchId = 1, + StartTime = 1599644652178, + StopTime = 1599644652178, + Title = "shift2" + } + }; + + server.Given(Request.Create().WithPath(PATH_BASE + SHIFTS).UsingGet()) + .RespondWith(Response.Create().WithStatusCode(200).WithBodyAsJson(bodyContent)); + + ShiftContract[] response = await devkitConnector.GetShifts(); + + Assert.IsInstanceOfType(response, typeof(ShiftContract[])); + } + + [TestCategory("Shifts")] + [TestMethod] + public async Task AddShift_ShouldReturnShiftContract() + { + var bodyContent = new ShiftContract + { + Id = 1, + BranchId = 1, + StartTime = 1599644652178, + StopTime = 1599644652178, + Title = "shift1" + }; + + server.Given(Request.Create().WithPath(PATH_BASE + SHIFTS).UsingPost()) + .RespondWith(Response.Create().WithStatusCode(200).WithBodyAsJson(bodyContent)); + + ShiftContract response = await devkitConnector.AddShift(bodyContent); + + Assert.IsInstanceOfType(response, typeof(ShiftContract)); + } + + [TestCategory("Shifts")] + [TestMethod] + public async Task UpdateShift() + { + var bodyContent = new ShiftContract + { + Id = 1, + BranchId = 1, + StartTime = 1599644652178, + StopTime = 1599644652178, + Title = "shift1" + }; + + server.Given(Request.Create().WithPath(PATH_BASE + SHIFTS + "/" + bodyContent.Id).UsingPatch()) + .RespondWith(Response.Create().WithStatusCode(200)); + + await devkitConnector.UpdateShift(bodyContent); + Assert.IsTrue(true); + } + + [TestCategory("Shifts")] + [TestMethod] + public async Task DeleteShift() + { + const int Id = 1; + server.Given(Request.Create().WithPath(PATH_BASE + SHIFTS + "/" + Id).UsingDelete()) + .RespondWith(Response.Create().WithStatusCode(200)); + + await devkitConnector.DeleteShift(Id); + Assert.IsTrue(true); + } + #endregion + #region GetAccountConfiguration [TestCategory("GetAccountConfiguration")] [TestMethod] @@ -306,5 +428,6 @@ namespace Test.V3 Assert.IsInstanceOfType(response, typeof(JsonDocument)); } + #endregion } }