public async Task SaveState_BatchMultipleCalls()
        {
            var inputs = new Dictionary <string, int>()
            {
                { "key1", 1 },
                { "key2", 2 },
                { "key3", 3 },
            };

            await this.CallFunctionAsync(nameof(Functions.SaveState_BatchMultipleCalls), "inputs", inputs);

            SavedHttpRequest req = this.GetSingleSaveStateRequest();

            Assert.Equal($"/v1.0/state/store1", req.Path);

            // Sort the elements since the order may change.
            // This is preferred over DeepEquals because we get more debug info from xunit when comparing collections
            JToken[] expected = JArray.Parse(@"[{""key"":""key1"",""value"":1},{""key"":""key2"",""value"":2},{""key"":""key3"",""value"":3}]").ToArray();
            JToken[] actual   = JArray.Parse(req.ContentAsString).ToArray();
            var      comparer = new Comparison <JToken>((t1, t2) => t1.ToString(Formatting.None).CompareTo(t2.ToString(Formatting.None)));

            Array.Sort(expected, comparer);
            Array.Sort(actual, comparer);

            Assert.Equal(expected, actual);
        }
Ejemplo n.º 2
0
        public async Task SendMessage_JObjectAsyncCollector(object message)
        {
            JObject input = JObject.Parse(
                $@"{{
                        ""data"": {JsonConvert.SerializeObject(message)},
                        ""operation"": ""create"",
                        ""metadata"": {{
                            ""key"": ""myKey""
                        }},
                        ""bindingName"": ""myBinding"",
                   }}");

            JObject expectedPayload = JObject.Parse(
                $@"{{
                        ""data"": {JsonConvert.SerializeObject(message)},
                        ""operation"": ""create"",
                        ""metadata"": {{
                            ""key"": ""myKey""
                        }}
                   }}");

            await this.CallFunctionAsync(nameof(Functions.JObjectAsyncCollector), "input", input);

            SavedHttpRequest req = this.GetSingleSendMessgaeRequest();

            Assert.Equal("/v1.0/bindings/myBinding", req.Path);
            Assert.Equal(JsonConvert.SerializeObject(expectedPayload), req.ContentAsString);
        }
        SavedHttpRequest GetSingleGetSecretRequest()
        {
            SavedHttpRequest[] requests = this.GetDaprRequests();
            SavedHttpRequest   req      = Assert.Single(requests);

            Assert.Equal("GET", req.Method);
            return(req);
        }
        public async Task SaveState_BindToKeyName(string keyName)
        {
            await this.CallFunctionAsync(nameof(Functions.SaveState_BindToKeyName), "key", keyName);

            SavedHttpRequest req = this.GetSingleSaveStateRequest();

            Assert.Equal("/v1.0/state/store1", req.Path);
            Assert.Equal(@$ "[{{" "key" ":" "{keyName}" "," "value" ":42}}]", req.ContentAsString);
        }
        SavedHttpRequest GetSingleSaveStateRequest()
        {
            SavedHttpRequest[] requests = this.GetDaprRequests();
            SavedHttpRequest   req      = Assert.Single(requests);

            Assert.StartsWith("application/json", req.ContentType);
            Assert.Equal("POST", req.Method);
            return(req);
        }
Ejemplo n.º 6
0
        public async Task Publish_OutputParameter(object input)
        {
            await this.CallFunctionAsync(nameof(Functions.ObjectOutputParameter), "input", input);

            SavedHttpRequest req = this.GetSinglePublishRequest();

            string expectedValue = JsonConvert.SerializeObject(input);

            Assert.Equal("/v1.0/publish/MyPubSub/TopicA", req.Path);
            Assert.Equal(expectedValue, req.ContentAsString);
        }
        public async Task SaveState_ObjectAsyncCollector(object input)
        {
            await this.CallFunctionAsync(nameof(Functions.SaveState_ObjectAsyncCollector), "input", input);

            SavedHttpRequest req = this.GetSingleSaveStateRequest();

            string expectedValue = JsonConvert.SerializeObject(input);

            Assert.Equal("/v1.0/state/store1", req.Path);
            Assert.Equal(@$ "[{{" "key" ":" "key1" "," "value" ":{expectedValue}}}]", req.ContentAsString);
        }
        public async Task SaveState_BindToStateStoreName(string storeName)
        {
            await this.CallFunctionAsync(nameof(Functions.SaveState_BindToStoreName), "storeName", storeName);

            SavedHttpRequest req = this.GetSingleSaveStateRequest();

            Assert.Equal($"/v1.0/state/{Uri.EscapeDataString(storeName)}", req.Path);
            Assert.Equal(@$ "[{{" "key" ":" "key1" "," "value" ":42}}]", req.ContentAsString);

            this.ValidatePersistedState(42, storeName, "key1");
        }
        public async Task SaveState_BindToStateStoreName(string storeName)
        {
            await this.CallFunctionAsync(nameof(Functions.SaveState_BindToStoreName), "storeName", storeName);

            SavedHttpRequest req = this.GetSingleSaveStateRequest();

            string expectedKeyName = Uri.EscapeUriString(storeName);

            Assert.Equal($"/v1.0/state/{expectedKeyName}", req.Path);
            Assert.Equal(@$ "[{{" "key" ":" "key1" "," "value" ":42}}]", req.ContentAsString);
        }
        public async Task GetSecret_ExplicitSettings_NoMetadata()
        {
            await this.CallFunctionAsync(nameof(Functions.GetSecret_ExplicitSettings_NoMetadata));

            SavedHttpRequest req = this.GetSingleGetSecretRequest();

            Assert.Equal("/v1.0/secrets/store1/key", req.Path);
            Assert.Equal(QueryString.Empty, req.Query);

            IEnumerable <string> functionLogs = this.GetFunctionLogs(nameof(Functions.GetSecret_ExplicitSettings_NoMetadata));

            Assert.Contains(ExpectedSecret, functionLogs);
        }
        public async Task GetSecret_BindToDictionary()
        {
            await this.CallFunctionAsync(nameof(Functions.GetSecret_BindToDictionary));

            SavedHttpRequest req = this.GetSingleGetSecretRequest();

            Assert.Equal("/v1.0/secrets/store1/key", req.Path);
            Assert.Equal(QueryString.Empty, req.Query);

            IEnumerable <string> functionLogs = this.GetFunctionLogs(nameof(Functions.GetSecret_BindToDictionary));

            Assert.Contains(@"key1: secret!, key2: another secret!", functionLogs);
        }
Ejemplo n.º 12
0
        public async Task Publish_ReturnValue_Bound()
        {
            var input = new DaprPubSubEvent("Hello, world!");

            await this.CallFunctionAsync(nameof(Functions.DaprPubSubEventReturnValueBound), "input", input);

            SavedHttpRequest req = this.GetSinglePublishRequest();

            string expectedValue = JsonConvert.SerializeObject(input.Payload);

            Assert.Equal($"/v1.0/publish/MyBoundPubSub/MyBoundTopic", req.Path);
            Assert.Equal(expectedValue, req.ContentAsString);
        }
        public async Task SaveState_BindToJObject_AllFields()
        {
            var parameters = new { stateStore = "store1", key = "key1", value = "value1" };

            await this.CallFunctionAsync(
                nameof(Functions.SaveState_BindToJObject_AllFields),
                "saveStateParameters",
                JObject.FromObject(parameters));

            SavedHttpRequest req = this.GetSingleSaveStateRequest();

            Assert.Equal($"/v1.0/state/{parameters.stateStore}", req.Path);
            Assert.Equal(@$ "[{{" "key" ":" "{parameters.key}" "," "value" ":" "{parameters.value}" "}}]", req.ContentAsString);
        }
        public async Task GetSecret_BindToKeyName()
        {
            string keyName = "key";

            await this.CallFunctionAsync(nameof(Functions.GetSecret_BindToKeyName), "key", keyName);

            SavedHttpRequest req = this.GetSingleGetSecretRequest();

            Assert.Equal($"/v1.0/secrets/store1/{keyName}", req.Path);
            Assert.Equal(QueryString.Empty, req.Query);

            IEnumerable <string> functionLogs = this.GetFunctionLogs(nameof(Functions.GetSecret_BindToKeyName));

            Assert.Contains(ExpectedSecret, functionLogs);
        }
Ejemplo n.º 15
0
        public async Task SendMessage_OutputParameter(object inputMessage)
        {
            await this.CallFunctionAsync(nameof(Functions.ObjectOutputParameter), "input", inputMessage);

            SavedHttpRequest req = this.GetSingleSendMessgaeRequest();

            JObject expectedPayload = JObject.Parse(
                $@"{{
                       ""data"": {JsonConvert.SerializeObject(inputMessage)},
                       ""operation"": ""create""
                   }}");

            Assert.Equal("/v1.0/bindings/myBinding", req.Path);
            Assert.Equal(JsonConvert.SerializeObject(expectedPayload), req.ContentAsString);
        }
Ejemplo n.º 16
0
        public async Task SendMessage_ReturnValue()
        {
            var input = new DaprBindingMessage("hello", new Dictionary <string, object> {
                { "key", "myKey" }
            }, "myBinding", "create");

            await this.CallFunctionAsync(nameof(Functions.DaprConnectorReturnValueAnyMessage), "input", input);

            SavedHttpRequest req = this.GetSingleSendMessgaeRequest();

            JObject expectedPayload = JObject.Parse($@"{{""data"": ""hello"", ""operation"": ""create"", ""metadata"": {{""key"": ""myKey""}}}}");

            Assert.Equal("/v1.0/bindings/myBinding", req.Path);
            Assert.Equal(JsonConvert.SerializeObject(expectedPayload), req.ContentAsString);
        }
Ejemplo n.º 17
0
        public async Task Publish_MultipleEvents()
        {
            await this.CallFunctionAsync(nameof(Functions.AsyncCollectorMultipleItems), "input", null);

            // Two events are published, each to a separate topic
            SavedHttpRequest[] requests = this.GetDaprRequests();
            Assert.Equal(2, requests.Length);
            Assert.All(requests, req => Assert.Equal("POST", req.Method));
            Assert.All(requests, req => Assert.StartsWith("application/json", req.ContentType));

            // The order of the requests is not guaranteed
            SavedHttpRequest req1 = Assert.Single(requests, req => req.Path == "/v1.0/publish/MyPubSub/TopicA");

            Assert.Equal("1", req1.ContentAsString);

            SavedHttpRequest req2 = Assert.Single(requests, req => req.Path == "/v1.0/publish/MyPubSub/TopicB");

            Assert.Equal("2", req2.ContentAsString);
        }
        public async Task GetSecret_BindToMetadata()
        {
            JObject metadata = JObject.FromObject(
                new
            {
                version_id    = 3,
                version_stage = 4,
            });

            await this.CallFunctionAsync(nameof(Functions.GetSecret_BindToMetadata), "metadata", metadata);

            SavedHttpRequest req = this.GetSingleGetSecretRequest();

            Assert.Equal("/v1.0/secrets/store1/key", req.Path);
            Assert.Equal("?metadata.version_id=3&metadata.version_stage=4", req.Query.ToString());

            IEnumerable <string> functionLogs = this.GetFunctionLogs(nameof(Functions.GetSecret_BindToMetadata));

            Assert.Contains(ExpectedSecret, functionLogs);
        }
Ejemplo n.º 19
0
        public async Task SendMessage_MultipleEvents()
        {
            await this.CallFunctionAsync(nameof(Functions.AsyncCollectorMultipleItems), "input", null);

            // Two events are published, each to a separate topic
            SavedHttpRequest[] requests = this.GetDaprRequests();
            Assert.Equal(2, requests.Length);
            Assert.All(requests, req => Assert.Equal("POST", req.Method));
            Assert.All(requests, req => Assert.StartsWith("application/json", req.ContentType));

            JObject expectedPayload1 = JObject.Parse($@"{{""data"": 1, ""operation"": ""create"", ""metadata"": {{""key"": ""myKey""}}}}");
            JObject expectedPayload2 = JObject.Parse($@"{{""data"": 2, ""operation"": ""create"", ""metadata"": {{""key"": ""myKey""}}}}");

            // The order of the requests is not guaranteed
            SavedHttpRequest req1 = Assert.Single(requests, req => req.Path == "/v1.0/bindings/myBinding1");

            Assert.Equal(JsonConvert.SerializeObject(expectedPayload1), req1.ContentAsString);

            SavedHttpRequest req2 = Assert.Single(requests, req => req.Path == "/v1.0/bindings/myBinding2");

            Assert.Equal(JsonConvert.SerializeObject(expectedPayload2), req2.ContentAsString);
        }