Esempio n. 1
0
        public async Task ConfigureWriteTest()
        {
            // setup
            Server server = new Server
            {
                Services = { Publisher.BindService(new PluginHive.Plugin.Plugin()) },
                Ports    = { new ServerPort("localhost", 0, ServerCredentials.Insecure) }
            };

            server.Start();

            var port = server.Ports.First().BoundPort;

            var channel = new Channel($"localhost:{port}", ChannelCredentials.Insecure);
            var client  = new Publisher.PublisherClient(channel);

            var connectRequest = GetConnectSettings();

            var request = new ConfigureWriteRequest
            {
                Form = new ConfigurationFormRequest
                {
                    DataJson = JsonConvert.SerializeObject(new ConfigureWriteFormData
                    {
                        StoredProcedure = "\"public\".\"INSERT_ACTOR\""
                    })
                }
            };

            // act
            client.Connect(connectRequest);
            var response = client.ConfigureWrite(request);

            // assert
            Assert.IsType <ConfigureWriteResponse>(response);

            // cleanup
            await channel.ShutdownAsync();

            await server.ShutdownAsync();
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a form and handles form updates for write backs
        /// </summary>
        /// <param name="request"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public override async Task <ConfigureWriteResponse> ConfigureWrite(ConfigureWriteRequest request,
                                                                           ServerCallContext context)
        {
            Logger.Info("Configuring write...");

            var storedProcedures = await Write.GetAllStoredProceduresAsync(_connectionFactory);

            var schemaJson = Write.GetSchemaJson(storedProcedures);
            var uiJson     = Write.GetUIJson();

            // if first call
            if (string.IsNullOrWhiteSpace(request.Form.DataJson) || request.Form.DataJson == "{}")
            {
                return(new ConfigureWriteResponse
                {
                    Form = new ConfigurationFormResponse
                    {
                        DataJson = "",
                        DataErrorsJson = "",
                        Errors = { },
                        SchemaJson = schemaJson,
                        UiJson = uiJson,
                        StateJson = ""
                    },
                    Schema = null
                });
            }

            try
            {
                // get form data
                var formData        = JsonConvert.DeserializeObject <ConfigureWriteFormData>(request.Form.DataJson);
                var storedProcedure = storedProcedures.Find(s => s.GetId() == formData.StoredProcedure);

                // base schema to return
                var schema = await Write.GetSchemaForStoredProcedureAsync(_connectionFactory, storedProcedure);

                return(new ConfigureWriteResponse
                {
                    Form = new ConfigurationFormResponse
                    {
                        DataJson = request.Form.DataJson,
                        Errors = { },
                        SchemaJson = schemaJson,
                        UiJson = uiJson,
                        StateJson = request.Form.StateJson
                    },
                    Schema = schema
                });
            }
            catch (Exception e)
            {
                Logger.Error(e, e.Message, context);
                return(new ConfigureWriteResponse
                {
                    Form = new ConfigurationFormResponse
                    {
                        DataJson = request.Form.DataJson,
                        Errors = { e.Message },
                        SchemaJson = schemaJson,
                        UiJson = uiJson,
                        StateJson = request.Form.StateJson
                    },
                    Schema = null
                });
            }
        }
Esempio n. 3
0
        public async Task WriteTest()
        {
            // setup
            Server server = new Server
            {
                Services = { Publisher.BindService(new PluginMariaDB.Plugin.Plugin()) },
                Ports    = { new ServerPort("localhost", 0, ServerCredentials.Insecure) }
            };

            server.Start();

            var port = server.Ports.First().BoundPort;

            var channel = new Channel($"localhost:{port}", ChannelCredentials.Insecure);
            var client  = new Publisher.PublisherClient(channel);

            var connectRequest = GetConnectSettings();

            var configureRequest = new ConfigureWriteRequest
            {
                Form = new ConfigurationFormRequest
                {
                    DataJson = JsonConvert.SerializeObject(new ConfigureWriteFormData
                    {
                        StoredProcedure = "`test`.`UpsertIntoTestTable`"
                    })
                }
            };

            var records = new List <Record>()
            {
                {
                    new Record
                    {
                        Action        = Record.Types.Action.Upsert,
                        CorrelationId = "test",
                        RecordId      = "record1",
                        DataJson      = "{\"id\":\"1\",\"name\":\"Test First\"}",
                    }
                }
            };

            var recordAcks = new List <RecordAck>();

            // act
            client.Connect(connectRequest);

            var configureResponse = client.ConfigureWrite(configureRequest);

            var prepareWriteRequest = new PrepareWriteRequest()
            {
                Schema           = configureResponse.Schema,
                CommitSlaSeconds = 1000,
                DataVersions     = new DataVersions
                {
                    JobId            = "jobUnitTest",
                    ShapeId          = "shapeUnitTest",
                    JobDataVersion   = 1,
                    ShapeDataVersion = 1
                }
            };

            client.PrepareWrite(prepareWriteRequest);

            using (var call = client.WriteStream())
            {
                var responseReaderTask = Task.Run(async() =>
                {
                    while (await call.ResponseStream.MoveNext())
                    {
                        var ack = call.ResponseStream.Current;
                        recordAcks.Add(ack);
                    }
                });

                foreach (Record record in records)
                {
                    await call.RequestStream.WriteAsync(record);
                }

                await call.RequestStream.CompleteAsync();

                await responseReaderTask;
            }

            // assert
            Assert.Single(recordAcks);
            Assert.Equal("", recordAcks[0].Error);
            Assert.Equal("test", recordAcks[0].CorrelationId);

            // cleanup
            await channel.ShutdownAsync();

            await server.ShutdownAsync();
        }
Esempio n. 4
0
        /// <summary>
        /// Configures writebacks to File
        /// </summary>
        /// <param name="request"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public override Task <ConfigureWriteResponse> ConfigureWrite(ConfigureWriteRequest request,
                                                                     ServerCallContext context)
        {
            Logger.SetLogPrefix("configure_write");
            Logger.Info("Configuring write...");
            Logger.Debug(JsonConvert.SerializeObject(request, Formatting.Indented));

            var schemaJson = Write.GetSchemaJson();
            var uiJson     = Write.GetUIJson();

            // if first call
            if (string.IsNullOrWhiteSpace(request.Form.DataJson) || request.Form.DataJson == "{}")
            {
                Logger.Info("first call...");
                return(Task.FromResult(new ConfigureWriteResponse
                {
                    Form = new ConfigurationFormResponse
                    {
                        DataJson = "",
                        DataErrorsJson = "",
                        Errors = { },
                        SchemaJson = schemaJson,
                        UiJson = uiJson,
                        StateJson = ""
                    },
                    Schema = null
                }));
            }

            try
            {
                Logger.Info("second call...");
                // get form data
                var formData = JsonConvert.DeserializeObject <ConfigureWriteFormData>(request.Form.DataJson);
                formData.ConvertLegacyConfiguration();
                var errors = formData.ValidateWriteFormData(_server.Settings);

                // base schema to return
                var schema = Write.GetSchemaFromForm(formData);

                return(Task.FromResult(new ConfigureWriteResponse
                {
                    Form = new ConfigurationFormResponse
                    {
                        DataJson = request.Form.DataJson,
                        Errors = { errors },
                        SchemaJson = schemaJson,
                        UiJson = uiJson,
                        StateJson = request.Form.StateJson
                    },
                    Schema = schema
                }));
            }
            catch (Exception e)
            {
                Logger.Error(e, e.Message, context);
                return(Task.FromResult(new ConfigureWriteResponse
                {
                    Form = new ConfigurationFormResponse
                    {
                        DataJson = request.Form.DataJson,
                        Errors = { e.Message },
                        SchemaJson = schemaJson,
                        UiJson = uiJson,
                        StateJson = request.Form.StateJson
                    },
                    Schema = null
                }));
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Creates a form and handles form updates for write backs
        /// </summary>
        /// <param name="request"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public override async Task <ConfigureWriteResponse> ConfigureWrite(ConfigureWriteRequest request,
                                                                           ServerCallContext context)
        {
            Logger.Info("Configuring write...");

            var storedProcedures = await Write.GetAllStoredProceduresAsync(_connectionFactory);

            var schemaJson = Write.GetSchemaJson(storedProcedures);
            var uiJson     = Write.GetUIJson();

            // if first call
            if (string.IsNullOrWhiteSpace(request.Form.DataJson) || request.Form.DataJson == "{}")
            {
                return(new ConfigureWriteResponse
                {
                    Form = new ConfigurationFormResponse
                    {
                        DataJson = "",
                        DataErrorsJson = "",
                        Errors = { },
                        SchemaJson = schemaJson,
                        UiJson = uiJson,
                        StateJson = ""
                    },
                    Schema = null
                });
            }

            try
            {
                // get form data
                var formData        = JsonConvert.DeserializeObject <ConfigureWriteFormData>(request.Form.DataJson);
                var storedProcedure = storedProcedures.Find(s => s.GetId() == formData.StoredProcedure);

                // base schema to return
                var schema = await Write.GetSchemaForStoredProcedureAsync(_connectionFactory, storedProcedure, formData.GoldenRecordIdParam);

                if (!string.IsNullOrWhiteSpace(formData.GoldenRecordIdParam))
                {
                    if (schema.Properties.All(p => p.Id != formData.GoldenRecordIdParam))
                    {
                        return(new ConfigureWriteResponse
                        {
                            Form = new ConfigurationFormResponse
                            {
                                DataJson = request.Form.DataJson,
                                Errors = { $"{formData.GoldenRecordIdParam} was specified as the Golden Record Id Parameter but was not a parameter on the stored procedure {formData.StoredProcedure} (available parameters: {string.Join(",", schema.Properties.Select(p => p.Id))})" },
                                SchemaJson = schemaJson,
                                UiJson = uiJson,
                                StateJson = request.Form.StateJson
                            },
                            Schema = schema
                        });
                    }
                }

                return(new ConfigureWriteResponse
                {
                    Form = new ConfigurationFormResponse
                    {
                        DataJson = request.Form.DataJson,
                        Errors = { },
                        SchemaJson = schemaJson,
                        UiJson = uiJson,
                        StateJson = request.Form.StateJson
                    },
                    Schema = schema
                });
            }
            catch (Exception e)
            {
                Logger.Error(e, e.Message, context);
                return(new ConfigureWriteResponse
                {
                    Form = new ConfigurationFormResponse
                    {
                        DataJson = request.Form.DataJson,
                        Errors = { e.Message },
                        SchemaJson = schemaJson,
                        UiJson = uiJson,
                        StateJson = request.Form.StateJson
                    },
                    Schema = null
                });
            }
        }