From 91d0a55d5ef99639c73521d27085356750c5b31e Mon Sep 17 00:00:00 2001 From: Brandon Watson Date: Thu, 4 Mar 2021 12:08:53 -0500 Subject: [PATCH] Re-arranging files to add test project. Adding WIP cursor list --- AuroraSignal.test/AuroraSignal.test.csproj | 26 +++++ AuroraSignal.test/CursorList.test.cs | 70 ++++++++++++++ AuroraSignal/Src/CursorList.cs | 94 +++++++++++++++++++ {Src => AuroraSignal/Src}/Program.cs | 2 +- {Src => AuroraSignal/Src}/Protos/signal.proto | 36 ++++++- AuroraSignal/Src/Services/Signal/Events.cs | 7 ++ AuroraSignal/Src/Services/Signal/Party.cs | 34 +++++++ .../Src/Services/Signal}/SignalService.cs | 7 +- {Src => AuroraSignal/Src}/Startup.cs | 3 +- .../appsettings.Development.json | 0 .../appsettings.json | 0 .../aurora-cradle-sharp.csproj | 0 12 files changed, 272 insertions(+), 7 deletions(-) create mode 100644 AuroraSignal.test/AuroraSignal.test.csproj create mode 100644 AuroraSignal.test/CursorList.test.cs create mode 100644 AuroraSignal/Src/CursorList.cs rename {Src => AuroraSignal/Src}/Program.cs (93%) rename {Src => AuroraSignal/Src}/Protos/signal.proto (97%) create mode 100644 AuroraSignal/Src/Services/Signal/Events.cs create mode 100644 AuroraSignal/Src/Services/Signal/Party.cs rename {Src/Services => AuroraSignal/Src/Services/Signal}/SignalService.cs (63%) rename {Src => AuroraSignal/Src}/Startup.cs (94%) rename appsettings.Development.json => AuroraSignal/appsettings.Development.json (100%) rename appsettings.json => AuroraSignal/appsettings.json (100%) rename aurora-cradle-sharp.csproj => AuroraSignal/aurora-cradle-sharp.csproj (100%) diff --git a/AuroraSignal.test/AuroraSignal.test.csproj b/AuroraSignal.test/AuroraSignal.test.csproj new file mode 100644 index 0000000..e343af3 --- /dev/null +++ b/AuroraSignal.test/AuroraSignal.test.csproj @@ -0,0 +1,26 @@ + + + + net5.0 + + false + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + diff --git a/AuroraSignal.test/CursorList.test.cs b/AuroraSignal.test/CursorList.test.cs new file mode 100644 index 0000000..913b1ec --- /dev/null +++ b/AuroraSignal.test/CursorList.test.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections.Generic; +using Xunit; +using Aurora; +using Aurora.Services.Signal; + + +namespace AuroraSignal.test +{ + public class CursorListTest + { + [Theory()] + [InlineData(SortDirection.Asc)] + [InlineData(SortDirection.Desc)] + public void CursorListSortOnStringValue(SortDirection direction) + { + CursorList cursor = new CursorList(); + cursor.Add(new Party(){Name = "asdf", PartyId = "1111"}); + cursor.Add(new Party(){Name = "bsdf", PartyId = "2222"}); + cursor.Add(new Party(){Name = "csdf", PartyId = "3333"}); + + List result = cursor.WithSort("Name", direction).Get(); + + if(direction == SortDirection.Desc) + { + Assert.Collection(cursor, + item => item.Name.Equals("asdf"), + item => item.Name.Equals("bsdf"), + item => item.Name.Equals("csdf")); + } + else + { + Assert.Collection(cursor, + item => item.Name.Equals("csdf"), + item => item.Name.Equals("bsdf"), + item => item.Name.Equals("asdf")); + + } + } + + [Theory()] + [InlineData(SortDirection.Asc)] + [InlineData(SortDirection.Desc)] + public void CursorListSortOnIntValue(SortDirection direction) + { + CursorList cursor = new CursorList(); + cursor.Add(new {Name = "asdf", PartyId = 1111}); + cursor.Add(new {Name = "bsdf", PartyId = 2222}); + cursor.Add(new {Name = "csdf", PartyId = 3333}); + + List result = cursor.WithSort("PartyId", direction).Get(); + + if(direction == SortDirection.Desc) + { + Assert.Collection(cursor, + item => item.Equals(new {Name = "asdf", PartyId = 1111}), + item => item.Equals(new {Name = "bsdf", PartyId = 2222}), + item => item.Equals(new {Name = "csdf", PartyId = 3333})); + } + else + { + Assert.Collection(cursor, + item => item.Equals(new {Name = "csdf", PartyId = 3333}), + item => item.Equals(new {Name = "bsdf", PartyId = 2222}), + item => item.Equals(new {Name = "asdf", PartyId = 1111})); + + } + } + } +} diff --git a/AuroraSignal/Src/CursorList.cs b/AuroraSignal/Src/CursorList.cs new file mode 100644 index 0000000..3a29106 --- /dev/null +++ b/AuroraSignal/Src/CursorList.cs @@ -0,0 +1,94 @@ +using System.Collections.Generic; + +namespace Aurora +{ + public enum SortDirection { + Asc, + Desc, + } + + public class CursorList : List + { + private string _orderBy; + private SortDirection _direction; + private string _previousPageToken; + private string _nextPageToken; + private int _pageSize; + + public CursorList(){ + this._direction = SortDirection.Desc; + this._orderBy = string.Empty; + this._previousPageToken = string.Empty; + this._nextPageToken = string.Empty; + this._pageSize = 10; + } + + public CursorList WithNextPage(string nextPageToken){ + this._nextPageToken = nextPageToken; + return this; + } + + public CursorList WithPreviousPage(string prevPageToken){ + this._previousPageToken = prevPageToken; + return this; + } + + public CursorList WithSort(string orderBy, SortDirection direction ) { + this._orderBy = orderBy; + this._direction = direction; + return this; + } + + public CursorList WithSize(int size){ + this._pageSize = size; + return this; + } + + public List Get(){ + if(this._nextPageToken != string.Empty && this._previousPageToken != string.Empty){ + throw new System.InvalidOperationException("Cannot specify both next and previous page tokens"); + } + + List tmpList = new List(this); + + + tmpList.Sort(delegate(T first, T second){ + object firstVal = first.GetType().GetProperty(this._orderBy).GetValue(first, null); + object secondVal = first.GetType().GetProperty(this._orderBy).GetValue(second, null); + int compare = 0; + + if(firstVal == null && secondVal == null) + { + compare = 0; + } + else if(firstVal == null) + { + compare = 1; + } + else if(secondVal == null) + { + compare = -1; + } + else + { + + // Determine number or string types + if(firstVal is string) + { + string firstStr = firstVal as string; + string secondStr = secondVal as string; + compare = firstStr.CompareTo(secondStr); + } else if(firstVal is int) + { + int? firstInt = firstVal as int?; + int? secondInt = secondVal as int?; + compare = firstInt > secondInt ? 1 : -1; + } + } + return this._direction == SortDirection.Asc ? compare : compare * -1; + }); + + return tmpList.GetRange(0, this._pageSize > tmpList.Count ? tmpList.Count : this._pageSize); + } + } +} \ No newline at end of file diff --git a/Src/Program.cs b/AuroraSignal/Src/Program.cs similarity index 93% rename from Src/Program.cs rename to AuroraSignal/Src/Program.cs index e35ec24..6ffeaba 100644 --- a/Src/Program.cs +++ b/AuroraSignal/Src/Program.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Hosting; -namespace aurora_cradle_sharp +namespace Aurora { public class Program { diff --git a/Src/Protos/signal.proto b/AuroraSignal/Src/Protos/signal.proto similarity index 97% rename from Src/Protos/signal.proto rename to AuroraSignal/Src/Protos/signal.proto index 8ba2d23..66c12ee 100644 --- a/Src/Protos/signal.proto +++ b/AuroraSignal/Src/Protos/signal.proto @@ -1,6 +1,6 @@ syntax = "proto3"; -option csharp_namespace = "aurora_cradle_sharp"; +option csharp_namespace = "Aurora.Services.Signal"; package signal; @@ -9,28 +9,38 @@ import "google/protobuf/timestamp.proto"; import "google/protobuf/field_mask.proto"; import "google/protobuf/empty.proto"; -service Signal{ + +service Signal { //************** //Party Resource //************** //Get Party rpc ListParties(ListPartiesRequest) returns (ListPartiesResponse); + rpc GetParty(GetPartyRequest) returns (Party); + rpc UpdateParty(UpdatePartyRequest) returns (Party); + rpc CreateParty(CreatePartyRequest) returns (Party); + rpc DeleteParty(DeletePartyRequest) returns (google.protobuf.Empty); + + //*************************** //EventSubscriptions Resource //*************************** //List rpc ListEventSubscriptions(ListEventSubscriptionsRequest) returns (ListEventSubscriptionsResponse); + //Create rpc SubscribeToEvent(EventSubscriptionRequest) returns (EventSubscription); //Delete rpc DeleteEventSubscription(DeleteEventSubscriptionRequest) returns (google.protobuf.Empty); + //CUSTOM: Create EventSubscription List rpc SubscribeToEvents(EventSubscriptionListRequest) returns (EventSubscriptionListResponse); + //CUSTOM: Delete all rpc DeleteAllEventSubscriptions(DeleteAllEventSubscriptionsRequest) returns (google.protobuf.Empty); @@ -40,6 +50,7 @@ service Signal{ //Get rpc GetEventStream(GetEventsRequest) returns (stream BaseEvent) {}; } + message Party { //The resource name of the party string name = 1; @@ -49,38 +60,48 @@ message Party { string host_ip = 5; google.protobuf.Timestamp created_on = 6; } + message PartyListItem { string name = 1; string party_id = 2; } + message ListPartiesRequest { int32 page_size = 1; string page_token = 2; } + message ListPartiesResponse { repeated PartyListItem parties = 1; string next_page_token = 2; } + message GetPartyRequest { string party_id = 1; } + message CreatePartyRequest { string party_id = 1; Party party = 2; } + message DeletePartyRequest { string party_id = 1; } + message UpdatePartyRequest { Party party = 1; google.protobuf.FieldMask update_mask = 2; } + /* Event Types */ enum EventType { NewPartiesAvailable = 0; } + message NewPartiesAvailableEvent { } + message BaseEvent { //Resource name of the event ? string name = 1; @@ -89,42 +110,51 @@ message BaseEvent { NewPartiesAvailableEvent new_parties_available_event = 3; } } + message EventSubscription { EventType type = 2; } + message ListEventSubscriptionsRequest { //Resource name of parent to the subscription list (The member) string parent = 1; int32 page_size = 2; string page_token = 3; } + message ListEventSubscriptionsResponse { repeated EventSubscription subscriptions = 1; string next_page_token = 2; } + message EventSubscriptionRequest { //Resource name of the parent to the subscription list (The member) string parent = 1; EventSubscription event_subscription = 2; } + message DeleteEventSubscriptionRequest { //Resource name of the subscription to delete string parent = 1; EventType type = 2; } + message EventSubscriptionListRequest { //Resource name of the parent to the subscription list (The member) string parent = 1; repeated EventSubscription event_subscriptions = 2; } + message EventSubscriptionListResponse { repeated EventSubscription event_subscriptions = 1; } + message DeleteAllEventSubscriptionsRequest { //Resource name of the parent to the subscription list (the member) string parent = 1; } + message GetEventsRequest { //Resource name of the parent to the event stream (the member) string parent = 1; -} \ No newline at end of file +} diff --git a/AuroraSignal/Src/Services/Signal/Events.cs b/AuroraSignal/Src/Services/Signal/Events.cs new file mode 100644 index 0000000..41e7034 --- /dev/null +++ b/AuroraSignal/Src/Services/Signal/Events.cs @@ -0,0 +1,7 @@ +namespace Aurora.Services.Signal +{ + public partial class SignalService : Signal.SignalBase + { + + } +} \ No newline at end of file diff --git a/AuroraSignal/Src/Services/Signal/Party.cs b/AuroraSignal/Src/Services/Signal/Party.cs new file mode 100644 index 0000000..cccf8f8 --- /dev/null +++ b/AuroraSignal/Src/Services/Signal/Party.cs @@ -0,0 +1,34 @@ +using System.Threading.Tasks; +using Google.Protobuf.WellKnownTypes; +using Grpc.Core; + +namespace Aurora.Services.Signal +{ + public partial class SignalService : Signal.SignalBase + { + public override Task CreateParty(CreatePartyRequest request, ServerCallContext context) + { + return base.CreateParty(request, context); + } + + public override Task DeleteParty(DeletePartyRequest request, ServerCallContext context) + { + return base.DeleteParty(request, context); + } + + public override Task ListParties(ListPartiesRequest request, ServerCallContext context) + { + return base.ListParties(request, context); + } + + public override Task GetParty(GetPartyRequest request, ServerCallContext context) + { + return base.GetParty(request, context); + } + + public override Task UpdateParty(UpdatePartyRequest request, ServerCallContext context) + { + return base.UpdateParty(request, context); + } + } +} \ No newline at end of file diff --git a/Src/Services/SignalService.cs b/AuroraSignal/Src/Services/Signal/SignalService.cs similarity index 63% rename from Src/Services/SignalService.cs rename to AuroraSignal/Src/Services/Signal/SignalService.cs index c7cab5f..6e68a67 100644 --- a/Src/Services/SignalService.cs +++ b/AuroraSignal/Src/Services/Signal/SignalService.cs @@ -5,14 +5,17 @@ using System.Threading.Tasks; using Grpc.Core; using Microsoft.Extensions.Logging; -namespace aurora_cradle_sharp +namespace Aurora.Services.Signal { - public class SignalService : Signal.SignalBase + public partial class SignalService : Signal.SignalBase { private readonly ILogger _logger; + + private CursorList _partyList; public SignalService(ILogger logger) { _logger = logger; + this._partyList = new CursorList(); } } diff --git a/Src/Startup.cs b/AuroraSignal/Src/Startup.cs similarity index 94% rename from Src/Startup.cs rename to AuroraSignal/Src/Startup.cs index 5607904..2ffb350 100644 --- a/Src/Startup.cs +++ b/AuroraSignal/Src/Startup.cs @@ -7,8 +7,9 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using Aurora.Services.Signal; -namespace aurora_cradle_sharp +namespace Aurora { public class Startup { diff --git a/appsettings.Development.json b/AuroraSignal/appsettings.Development.json similarity index 100% rename from appsettings.Development.json rename to AuroraSignal/appsettings.Development.json diff --git a/appsettings.json b/AuroraSignal/appsettings.json similarity index 100% rename from appsettings.json rename to AuroraSignal/appsettings.json diff --git a/aurora-cradle-sharp.csproj b/AuroraSignal/aurora-cradle-sharp.csproj similarity index 100% rename from aurora-cradle-sharp.csproj rename to AuroraSignal/aurora-cradle-sharp.csproj