public void PatchUpdatesFlag()
        {
            var flag       = new FeatureFlagBuilder().Version(1).Build();
            var patchEvent = PatchEvent(flag.ToJsonString("flag1"));

            WithDataSourceAndServer(StreamWithEmptyInitialDataAndThen(patchEvent), (dataSource, s, t) =>
            {
                _updateSink.ExpectInit(BasicUser);

                var receivedItem = _updateSink.ExpectUpsert(BasicUser, "flag1");
                AssertHelpers.DataItemsEqual(flag.ToItemDescriptor(), receivedItem);
            });
        }
Ejemplo n.º 2
0
        public void ParsePatchData()
        {
            var flag        = new FeatureFlagBuilder("flagkey").Version(2).On(true).Build();
            var segment     = new SegmentBuilder("segmentkey").Version(3).Included("x").Build();
            var flagJson    = LdJsonSerialization.SerializeObject(flag);
            var segmentJson = LdJsonSerialization.SerializeObject(segment);

            var validFlagInput  = @"{""path"": ""/flags/flagkey"", ""data"": " + flagJson + "}";
            var validFlagResult = StreamProcessorEvents.ParsePatchData(Utf8Bytes(validFlagInput));

            Assert.Equal(DataModel.Features, validFlagResult.Kind);
            Assert.Equal("flagkey", validFlagResult.Key);
            AssertHelpers.DataItemsEqual(DataModel.Features, new ItemDescriptor(flag.Version, flag),
                                         validFlagResult.Item);

            var validSegmentInput  = @"{""path"": ""/segments/segmentkey"", ""data"": " + segmentJson + "}";
            var validSegmentResult = StreamProcessorEvents.ParsePatchData(Utf8Bytes(validSegmentInput));

            Assert.Equal(DataModel.Segments, validSegmentResult.Kind);
            Assert.Equal("segmentkey", validSegmentResult.Key);
            AssertHelpers.DataItemsEqual(DataModel.Segments, new ItemDescriptor(segment.Version, segment),
                                         validSegmentResult.Item);

            var validFlagInputWithDataBeforePath  = @"{""data"": " + flagJson + @", ""path"": ""/flags/flagkey""}";
            var validFlagResultWithDataBeforePath = StreamProcessorEvents.ParsePatchData(Utf8Bytes(validFlagInputWithDataBeforePath));

            Assert.Equal(DataModel.Features, validFlagResultWithDataBeforePath.Kind);
            Assert.Equal("flagkey", validFlagResultWithDataBeforePath.Key);
            AssertHelpers.DataItemsEqual(DataModel.Features, new ItemDescriptor(flag.Version, flag),
                                         validFlagResultWithDataBeforePath.Item);

            var inputWithUnrecognizedPath  = @"{""path"": ""/cats/lucy"", ""data"": " + flagJson + "}";
            var resultWithUnrecognizedPath = StreamProcessorEvents.ParsePatchData(Utf8Bytes(inputWithUnrecognizedPath));

            Assert.Null(resultWithUnrecognizedPath.Kind);
            Assert.Null(resultWithUnrecognizedPath.Key);

            var inputWithMissingPath = @"{""data"": " + flagJson + "}";

            Assert.ThrowsAny <JsonReadException>(() => StreamProcessorEvents.ParsePatchData(Utf8Bytes(inputWithMissingPath)));

            var inputWithMissingData = @"{""path"": ""/flags/flagkey""}";

            Assert.ThrowsAny <JsonReadException>(() => StreamProcessorEvents.ParsePatchData(Utf8Bytes(inputWithMissingData)));

            var malformedJsonInput = @"{no";

            Assert.ThrowsAny <JsonReadException>(() => StreamProcessorEvents.ParsePatchData(Utf8Bytes(malformedJsonInput)));
        }