public void AllAttributesPrivateMakesAttributesPrivate()
        {
            var f    = new EventOutputFormatter(new SimpleConfiguration());
            var user = User.Builder("userkey")
                       .Anonymous(true)
                       .Avatar("http://avatar")
                       .Country("US")
                       .Custom("custom1", "value1")
                       .Custom("custom2", "value2")
                       .Email("*****@*****.**")
                       .FirstName("first")
                       .IPAddress("1.2.3.4")
                       .LastName("last")
                       .Name("me")
                       .Secondary("s")
                       .Build();
            var userJson = LdValue.Parse(@"{
                ""key"":""userkey"",
                ""anonymous"":true,
                ""privateAttrs"":[
                    ""avatar"", ""country"", ""custom1"", ""custom2"", ""email"",
                    ""firstName"", ""ip"", ""lastName"", ""name"", ""secondary""
                ]
                }");
            var config   = new SimpleConfiguration()
            {
                AllAttributesPrivate = true
            };

            TestInlineUserSerialization(user, userJson, config);
        }
        private void TestEventSerialization(Event e, LdValue expectedJsonValue)
        {
            var f            = new EventOutputFormatter(new SimpleConfiguration());
            var emptySummary = new EventSummary();

            var outputEvent = LdValue.Parse(f.SerializeOutputEvents(new Event[] { e }, emptySummary, out var count)).Get(0);

            Assert.Equal(1, count);
            Assert.Equal(expectedJsonValue, outputEvent);
        }
        public void SummaryEventIsSerialized()
        {
            var summary = new EventSummary();

            summary.NoteTimestamp(1001);

            summary.IncrementCounter("first", 1, 11, LdValue.Of("value1a"), LdValue.Of("default1"));

            summary.IncrementCounter("second", 1, 21, LdValue.Of("value2a"), LdValue.Of("default2"));

            summary.IncrementCounter("first", 1, 11, LdValue.Of("value1a"), LdValue.Of("default1"));
            summary.IncrementCounter("first", 1, 12, LdValue.Of("value1a"), LdValue.Of("default1"));

            summary.IncrementCounter("second", 2, 21, LdValue.Of("value2b"), LdValue.Of("default2"));
            summary.IncrementCounter("second", null, 21, LdValue.Of("default2"), LdValue.Of("default2"));  // flag exists (has version), but eval failed (no variation)

            summary.IncrementCounter("third", null, null, LdValue.Of("default3"), LdValue.Of("default3")); // flag doesn't exist (no version)

            summary.NoteTimestamp(1000);
            summary.NoteTimestamp(1002);

            var f           = new EventOutputFormatter(new SimpleConfiguration());
            var outputEvent = LdValue.Parse(f.SerializeOutputEvents(new Event[0], summary, out var count)).Get(0);

            Assert.Equal(1, count);

            Assert.Equal("summary", outputEvent.Get("kind").AsString);
            Assert.Equal(1000, outputEvent.Get("startDate").AsInt);
            Assert.Equal(1002, outputEvent.Get("endDate").AsInt);

            var featuresJson = outputEvent.Get("features");

            Assert.Equal(3, featuresJson.Count);

            var firstJson = featuresJson.Get("first");

            Assert.Equal("default1", firstJson.Get("default").AsString);
            TestUtil.AssertContainsInAnyOrder(firstJson.Get("counters").AsList(LdValue.Convert.Json),
                                              LdValue.Parse(@"{""value"":""value1a"",""variation"":1,""version"":11,""count"":2}"),
                                              LdValue.Parse(@"{""value"":""value1a"",""variation"":1,""version"":12,""count"":1}"));

            var secondJson = featuresJson.Get("second");

            Assert.Equal("default2", secondJson.Get("default").AsString);
            TestUtil.AssertContainsInAnyOrder(secondJson.Get("counters").AsList(LdValue.Convert.Json),
                                              LdValue.Parse(@"{""value"":""value2a"",""variation"":1,""version"":21,""count"":1}"),
                                              LdValue.Parse(@"{""value"":""value2b"",""variation"":2,""version"":21,""count"":1}"),
                                              LdValue.Parse(@"{""value"":""default2"",""version"":21,""count"":1}"));

            var thirdJson = featuresJson.Get("third");

            Assert.Equal("default3", thirdJson.Get("default").AsString);
            TestUtil.AssertContainsInAnyOrder(thirdJson.Get("counters").AsList(LdValue.Convert.Json),
                                              LdValue.Parse(@"{""unknown"":true,""value"":""default3"",""count"":1}"));
        }
        public void UnsetUserAttributesAreNotSerialized()
        {
            var f    = new EventOutputFormatter(new SimpleConfiguration());
            var user = User.Builder("userkey")
                       .Build();
            var userJson = LdValue.Parse(@"{
                ""key"":""userkey""
                }");

            TestInlineUserSerialization(user, userJson, new SimpleConfiguration());
        }
        public void UserKeyIsSetInsteadOfUserWhenNotInlined()
        {
            var user = User.Builder("userkey")
                       .Name("me")
                       .Build();
            var userJson     = LdValue.Parse(@"{""key"":""userkey"",""name"":""me""}");
            var f            = new EventOutputFormatter(new SimpleConfiguration());
            var emptySummary = new EventSummary();

            var featureEvent = EventFactory.Default.NewFeatureRequestEvent(
                new FlagEventPropertiesBuilder("flag").Build(),
                user,
                new EvaluationDetail <LdValue>(LdValue.Null, null, EvaluationReason.OffReason),
                LdValue.Null);
            var outputEvent = LdValue.Parse(f.SerializeOutputEvents(new Event[] { featureEvent }, emptySummary, out var count)).Get(0);

            Assert.Equal(1, count);
            Assert.Equal(LdValue.Null, outputEvent.Get("user"));
            Assert.Equal(user.Key, outputEvent.Get("userKey").AsString);

            var identifyEvent = EventFactory.Default.NewIdentifyEvent(user);

            outputEvent = LdValue.Parse(f.SerializeOutputEvents(new Event[] { featureEvent }, emptySummary, out count)).Get(0);
            Assert.Equal(1, count);
            Assert.Equal(LdValue.Null, outputEvent.Get("user"));
            Assert.Equal(user.Key, outputEvent.Get("userKey").AsString);

            var customEvent = EventFactory.Default.NewCustomEvent("custom", user, LdValue.Null);

            outputEvent = LdValue.Parse(f.SerializeOutputEvents(new Event[] { featureEvent }, emptySummary, out count)).Get(0);
            Assert.Equal(1, count);
            Assert.Equal(LdValue.Null, outputEvent.Get("user"));
            Assert.Equal(user.Key, outputEvent.Get("userKey").AsString);

            // user is always inlined in index event
            var indexEvent = new IndexEvent(0, user);

            outputEvent = LdValue.Parse(f.SerializeOutputEvents(new Event[] { indexEvent }, emptySummary, out count)).Get(0);
            Assert.Equal(1, count);
            Assert.Equal(LdValue.Null, outputEvent.Get("userKey"));
            Assert.Equal(userJson, outputEvent.Get("user"));
        }
        private void TestInlineUserSerialization(User user, LdValue expectedJsonValue, SimpleConfiguration config)
        {
            config.InlineUsersInEvents = true;
            var f            = new EventOutputFormatter(config);
            var emptySummary = new EventSummary();

            var featureEvent = EventFactory.Default.NewFeatureRequestEvent(
                new FlagEventPropertiesBuilder("flag").Build(),
                user,
                new EvaluationDetail <LdValue>(LdValue.Null, null, EvaluationReason.OffReason),
                LdValue.Null);
            var outputEvent = LdValue.Parse(f.SerializeOutputEvents(new Event[] { featureEvent }, emptySummary, out var count)).Get(0);

            Assert.Equal(1, count);
            Assert.Equal(LdValue.Null, outputEvent.Get("userKey"));
            Assert.Equal(expectedJsonValue, outputEvent.Get("user"));

            var identifyEvent = EventFactory.Default.NewIdentifyEvent(user);

            outputEvent = LdValue.Parse(f.SerializeOutputEvents(new Event[] { featureEvent }, emptySummary, out count)).Get(0);
            Assert.Equal(1, count);
            Assert.Equal(LdValue.Null, outputEvent.Get("userKey"));
            Assert.Equal(expectedJsonValue, outputEvent.Get("user"));

            var customEvent = EventFactory.Default.NewCustomEvent("custom", user, LdValue.Null);

            outputEvent = LdValue.Parse(f.SerializeOutputEvents(new Event[] { featureEvent }, emptySummary, out count)).Get(0);
            Assert.Equal(1, count);
            Assert.Equal(LdValue.Null, outputEvent.Get("userKey"));
            Assert.Equal(expectedJsonValue, outputEvent.Get("user"));

            var indexEvent = new IndexEvent(0, user);

            outputEvent = LdValue.Parse(f.SerializeOutputEvents(new Event[] { indexEvent }, emptySummary, out count)).Get(0);
            Assert.Equal(1, count);
            Assert.Equal(LdValue.Null, outputEvent.Get("userKey"));
            Assert.Equal(expectedJsonValue, outputEvent.Get("user"));
        }