Exemple #1
0
        public void GetServices_NotPass_EmptyDatabase()
        {   //Arrange
            var expected = false;
            var actual   = false;
            ICollection <ServiceDisplayResp> registeredServices;
            var option = new DbContextOptionsBuilder <ApiGatewayContext>()
                         .UseInMemoryDatabase(databaseName: "ServiceDiscoveryManager.GetServices_Empty_database")
                         .Options;

            //Act
            using (var context = new ApiGatewayContext(option))
            {
                var serviceDiscoveryService = new ServiceDiscoveryService(context);
                var serviceDiscoveryManager = new ServiceDiscoveryManager(serviceDiscoveryService);
                registeredServices = serviceDiscoveryManager.GetAvailableServices(GenerateRandomKey(Int32.Parse(Environment.GetEnvironmentVariable("APIKeyInputLength", EnvironmentVariableTarget.User))));
            }

            //Since input is invalid, the registeredServices should be null
            if (registeredServices != null)
            {
                actual = true;
            }

            //Assert
            Assert.AreEqual(expected, actual);
        }
Exemple #2
0
        public void GetServices_NotPass_NullInput()
        {   //Arrange
            var expected = false;
            var actual   = false;
            ICollection <ServiceDisplayResp> registeredServices;

            //Act
            using (var context = new ApiGatewayContext())
            {
                var serviceDiscoveryService = new ServiceDiscoveryService(context);
                var serviceDiscoveryManager = new ServiceDiscoveryManager(serviceDiscoveryService);
                registeredServices = serviceDiscoveryManager.GetAvailableServices(null);
            }



            //Since input is invalid, the registeredServices should be null
            if (registeredServices != null)
            {
                actual = true;
            }

            //Assert
            Assert.AreEqual(expected, actual);
        }
Exemple #3
0
        //Fail condition for ServiceDiscoveryManager.GetAvailableServices(), when input is too short, return no data
        public void GetServices_NotPass_InputTooShort()
        {   //Arrange
            var expected = false;
            var actual   = false;
            ICollection <ServiceDisplayResp> registeredServices;

            //Act
            using (var context = new ApiGatewayContext())
            {
                var serviceDiscoveryService = new ServiceDiscoveryService(context);
                var serviceDiscoveryManager = new ServiceDiscoveryManager(serviceDiscoveryService);
                //Generate a key that is shorter than requirement
                var randomId = GenerateRandomKey(Constants.clientIdLength - 1);
                registeredServices = serviceDiscoveryManager.GetAvailableServices(randomId);
            }

            //Since input is invalid, the registeredServices should be null
            if (registeredServices != null)
            {
                actual = true;
            }

            //Assert
            Assert.AreEqual(expected, actual);
        }
Exemple #4
0
        public void IfClientExist_InMemory_Pass()
        {   //Arrange
            var expected = true;
            var actual   = false;
            var option   = new DbContextOptionsBuilder <ApiGatewayContext>()
                           .UseInMemoryDatabase(databaseName: "ServiceDiscoveryService.IfClientExist_Pass_database")
                           .Options;

            //Act
            using (var context = new ApiGatewayContext(option))
            {
                var teamForTesting = new Team();
                var randomKey      = GenerateRandomKey(Int32.Parse(Environment.GetEnvironmentVariable("APIKeyInputLength", EnvironmentVariableTarget.User)));
                teamForTesting.ClientId    = randomKey;
                teamForTesting.WebsiteUrl  = "testingWebSiteUrl";
                teamForTesting.Secret      = "testingSecret";
                teamForTesting.CallbackUrl = "testingCallBackUrl";
                teamForTesting.Digest      = "testingDigest";
                teamForTesting.Username    = "******";
                context.Team.Add(teamForTesting);
                context.SaveChanges();

                var serviceDiscoverService = new ServiceDiscoveryService(context);

                //Different from GetServices, check if a team exist need the same random key
                actual = serviceDiscoverService.IfClientExist(randomKey);
            }

            //Assert
            Assert.AreEqual(expected, actual);
        }
Exemple #5
0
        public void IfClientExist_WhiteSpaceInput()
        {   //Arrange
            var expected = false;
            var actual   = false;

            //Act
            using (var context = new ApiGatewayContext())
            {
                var serviceDiscoverService = new ServiceDiscoveryService(context);
                actual = serviceDiscoverService.IfClientExist("");
            }

            //Assert
            Assert.AreEqual(expected, actual);
        }
Exemple #6
0
        public void IfClientExist_InMemory_EmptyDatabase()
        {   //Arrange
            var expected = false;
            var actual   = false;
            var option   = new DbContextOptionsBuilder <ApiGatewayContext>()
                           .UseInMemoryDatabase(databaseName: "ServiceDiscoveryService.IfClientExist_NotPass_database")
                           .Options;

            //Act
            using (var context = new ApiGatewayContext(option))
            {
                var serviceDiscoverService = new ServiceDiscoveryService(context);
                var inputClientId          = GenerateRandomKey(Int32.Parse(Environment.GetEnvironmentVariable("APIKeyInputLength", EnvironmentVariableTarget.User)));

                actual = serviceDiscoverService.IfClientExist(inputClientId);
            }

            //Assert
            Assert.AreEqual(expected, actual);
        }
        /// <inheritdoc />
        public async Task OnGameInitialized()
        {
            //We need to connect the hub to the social backend
            ResolveServiceEndpointResponse endpointResponse = await ServiceDiscoveryService.DiscoverService(new ResolveServiceEndpointRequest(ClientRegionLocale.US, "SocialService"))
                                                              .ConfigureAwait(false);

            if (!endpointResponse.isSuccessful)
            {
                throw new InvalidOperationException($"Failed to query for SocialService. Reason: {endpointResponse.ResultCode}");
            }

            string hubConnectionString = $@"{endpointResponse.Endpoint.EndpointAddress}:{endpointResponse.Endpoint.EndpointPort}/realtime/textchat";

            if (Logger.IsInfoEnabled)
            {
                Logger.Info($"Social HubConnection String: {hubConnectionString}");
            }

            //TODO: Handle failed service disc query
            HubConnection connection = new HubConnectionBuilder()
                                       .WithUrl(hubConnectionString, options =>
            {
                options.Headers.Add(SocialNetworkConstants.CharacterIdRequestHeaderName, PlayerDetails.LocalPlayerGuid.EntityId.ToString());
                options.AccessTokenProvider = () => Task.FromResult(AuthTokenProvider.Retrieve());
            })
                                       .AddJsonProtocol()
                                       .Build();

            foreach (var i in InitializableSocialServices)
            {
                i.Connection = connection;
            }

            //Just start the service when the game initializes
            //This will make it so that the signalR clients will start to recieve messages.
            await connection.StartAsync()
            .ConfigureAwait(false);
        }
Exemple #8
0
        public void IfClientExist_InMemory_ClientDoesnotMatch()
        {   //Arrange
            var expected = false;
            var actual   = false;
            var option   = new DbContextOptionsBuilder <ApiGatewayContext>()
                           .UseInMemoryDatabase(databaseName: "ServiceDiscoveryService.IfClientExist_NotPass_database")
                           .Options;

            //Act
            using (var context = new ApiGatewayContext(option))
            {
                // Create a team first
                var teamForTesting = new Team();
                var randomId       = GenerateRandomKey(Int32.Parse(Environment.GetEnvironmentVariable("APIKeyInputLength", EnvironmentVariableTarget.User)));
                teamForTesting.ClientId    = randomId;
                teamForTesting.WebsiteUrl  = "testingWebSiteUrl";
                teamForTesting.Secret      = "testingSecret";
                teamForTesting.CallbackUrl = "testingCallBackUrl";
                teamForTesting.Digest      = "testingDigest";
                teamForTesting.Username    = "******";
                context.Team.Add(teamForTesting);

                var serviceDiscoverService = new ServiceDiscoveryService(context);
                var inputClientId          = GenerateRandomKey(Int32.Parse(Environment.GetEnvironmentVariable("APIKeyInputLength", EnvironmentVariableTarget.User)));

                //Make sure the second random generated clientId is different from the first one
                while (inputClientId == randomId)
                {
                    inputClientId = GenerateRandomKey(Int32.Parse(Environment.GetEnvironmentVariable("APIKeyInputLength", EnvironmentVariableTarget.User)));
                }

                actual = serviceDiscoverService.IfClientExist(inputClientId);
            }

            //Assert
            Assert.AreEqual(expected, actual);
        }
Exemple #9
0
        protected override void OnLocalPlayerSpawned(LocalPlayerSpawnedEventArgs args)
        {
            UnityAsyncHelper.UnityMainThreadContext.PostAsync(async() =>
            {
                try
                {
                    //We need to connect the hub to the social backend
                    ResolveServiceEndpointResponse endpointResponse = await ServiceDiscoveryService.DiscoverService(new ResolveServiceEndpointRequest(ClientRegionLocale.US, GladMMONetworkConstants.SOCIAL_SERVICE_NAME))
                                                                      .ConfigureAwaitFalse();

                    if (!endpointResponse.isSuccessful)
                    {
                        throw new InvalidOperationException($"Failed to query for SocialService. Reason: {endpointResponse.ResultCode}");
                    }

                    string hubConnectionString = $@"{endpointResponse.Endpoint.EndpointAddress}:{endpointResponse.Endpoint.EndpointPort}/realtime/social";

                    if (Logger.IsInfoEnabled)
                    {
                        Logger.Info($"Social HubConnection String: {hubConnectionString}");
                    }

                    //TODO: Handle failed service disc query
                    HubConnection connection = new HubConnectionBuilder()
                                               .WithUrl(hubConnectionString, options =>
                    {
                        options.Headers.Add(SocialNetworkConstants.CharacterIdRequestHeaderName, PlayerDetails.LocalPlayerGuid.EntityId.ToString());
                        options.AccessTokenProvider = () => Task.FromResult(AuthTokenProvider.Retrieve());
                    })
                                               .AddJsonProtocol()
                                               .Build();

                    //Register the reciever interface instance for the Connection Hub
                    connection.RegisterClientInterface <IRemoteSocialHubClient>(RemoteSocialClient);

                    foreach (var initable in ConnectionHubInitializable)
                    {
                        initable.Connection = connection;
                    }

                    //Just start the service when the game initializes
                    //This will make it so that the signalR clients will start to recieve messages.
                    await connection.StartAsync()
                    .ConfigureAwaitFalseVoid();

                    if (Logger.IsInfoEnabled)
                    {
                        Logger.Info($"Connected to realtime Social Service.");
                    }

                    OnRealtimeSocialServiceConnected?.Invoke(this, EventArgs.Empty);
                }
                catch (Exception e)
                {
                    if (Logger.IsErrorEnabled)
                    {
                        Logger.Error($"Failed to connect to Social Service: {e.ToString()}");
                    }

                    throw;
                }
            });
        }
Exemple #10
0
 public ServiceDiscoveryManager(ServiceDiscoveryService serviceDiscoveryService)
 {
     _serviceDiscoveryService = serviceDiscoveryService;
 }
Exemple #11
0
        public void GetAvailableServices_InMemory_Pass()
        {   //Arrange
            var expected = true;
            var actual   = false;
            var option   = new DbContextOptionsBuilder <ApiGatewayContext>()
                           .UseInMemoryDatabase(databaseName: "ServiceDiscoveryManager.GetAvailableServices_Pass_Database")
                           .Options;
            ICollection <ServiceDisplayResp> registeredServices;

            //Act
            using (var context = new ApiGatewayContext(option))
            {
                // Create a team first
                var teamForTesting = new Team();
                var randomId       = GenerateRandomKey(Int32.Parse(Environment.GetEnvironmentVariable("APIKeyInputLength", EnvironmentVariableTarget.User)));
                teamForTesting.ClientId    = randomId;
                teamForTesting.WebsiteUrl  = "testingWebSiteUrl";
                teamForTesting.Secret      = "testingSecret";
                teamForTesting.CallbackUrl = "testingCallBackUrl";
                teamForTesting.Digest      = "testingDigest";
                teamForTesting.Username    = "******";
                context.Team.Add(teamForTesting);

                //Service

                var serviceForTesting = new Service();
                serviceForTesting.Endpoint    = "testingEndPoint";
                serviceForTesting.Owner       = randomId;//need to be the same as team's ClineId
                serviceForTesting.Id          = 12345678;
                serviceForTesting.Input       = "int";
                serviceForTesting.Output      = "int";
                serviceForTesting.Dataformat  = "xml";
                serviceForTesting.Description = "Some description for testing";
                context.Service.Add(serviceForTesting);

                //Configuration

                var configForTesting = new Configuration();
                configForTesting.EndPoint = "testingEndPoint";//need to be the same as service
                configForTesting.OpenTo   = randomId;
                configForTesting.Steps    = "some steps for testing";
                context.Configuration.Add(configForTesting);
                context.SaveChanges();

                var serviceDiscoveryService = new ServiceDiscoveryService(context);
                var serviceDiscoveryManager = new ServiceDiscoveryManager(serviceDiscoveryService);

                registeredServices = serviceDiscoveryManager.GetAvailableServices(randomId);
                if (registeredServices.Count > 0)
                {
                    actual = true;
                }
            }

            foreach (var service in registeredServices)
            {
                Trace.WriteLine(service.Endpoint + " " + service.Username + " " + service.Input + " " + service.Output + " " + service.Dataformat + " " + service.Description + Environment.NewLine);
            }

            //Assert
            Assert.AreEqual(expected, actual);
        }
 public ServicesController(ServiceDiscoveryService serviceDiscoveryService)
 {
     this.serviceDiscoveryService = serviceDiscoveryService;
 }