public void CommitOperationThrowsExceptionWhenFindNewlyAddedKeyAfterLoadOperation()
        {
            var json              = @"{
  ""name"": ""test"",
  ""address"": {
    ""street"": ""Something street"",
    ""zipcode"": ""12345""
  }
}";
            var newJson           = @"{
  ""name"": ""test"",
  ""address"": {
    ""street"": ""Something street"",
    ""zipcode"": ""12345""
  },
  ""NewKey"": ""NewValue""
}";
            var jsonConfigSrc     = new JsonConfigurationSource(ArbitraryFilePath);
            var outputCacheStream = new MemoryStream();

            jsonConfigSrc.Load(StringToStream(json));

            var exception = Assert.Throws <InvalidOperationException>(
                () => jsonConfigSrc.Commit(StringToStream(newJson), outputCacheStream));

            Assert.Equal(Resources.FormatError_CommitWhenNewKeyFound("NewKey"), exception.Message);
        }
        public void CommitOperationThrowsExceptionWhenFindInvalidModificationAfterLoadOperation()
        {
            var json              = @"{
  ""name"": ""test"",
  ""address"": {
    ""street"": ""Something street"",
    ""zipcode"": ""12345""
  }
}";
            var modifiedJson      = @"
{
  ""name"": [""first"", ""last""],
  ""address"": {
    ""street"": ""Something street"",
    ""zipcode"": ""12345""
  }
}";
            var jsonConfigSrc     = new JsonConfigurationSource(ArbitraryFilePath);
            var outputCacheStream = new MemoryStream();

            jsonConfigSrc.Load(StringToStream(json));

            var exception = Assert.Throws <FormatException>(
                () => jsonConfigSrc.Commit(StringToStream(modifiedJson), outputCacheStream));

            Assert.Equal(Resources.FormatError_UnsupportedJSONToken("StartArray", "name", 3, 12), exception.Message);
        }
        public void CommitMethodCanHandleEmptyValue()
        {
            var json              = @"{
  ""key1"": """",
  ""key2"": {
    ""key3"": """"
  }
}";
            var expectedJson      = @"{
  ""key1"": ""value1"",
  ""key2"": {
    ""key3"": ""value2""
  }
}";
            var jsonConfigSrc     = new JsonConfigurationSource(ArbitraryFilePath);
            var outputCacheStream = new MemoryStream();

            jsonConfigSrc.Load(StringToStream(json));
            jsonConfigSrc.Set("key1", "value1");
            jsonConfigSrc.Set("key2:key3", "value2");

            jsonConfigSrc.Commit(StringToStream(json), outputCacheStream);

            var newContents = StreamToString(outputCacheStream);

            Assert.Equal(expectedJson, newContents);
        }
        public void CommitOperationThrowsExceptionWhenKeysAreMissingInConfigFile()
        {
            var json              = @"{
  ""name"": ""test"",
  ""address"": {
    ""street"": ""Something street"",
    ""zipcode"": ""12345""
  }
}";
            var jsonConfigSrc     = new JsonConfigurationSource(ArbitraryFilePath);
            var outputCacheStream = new MemoryStream();

            jsonConfigSrc.Load(StringToStream(json));
            json = json.Replace(@"""name"": ""test"",", string.Empty);

            var exception = Assert.Throws <InvalidOperationException>(
                () => jsonConfigSrc.Commit(StringToStream(json), outputCacheStream));

            Assert.Equal(Resources.FormatError_CommitWhenKeyMissing("name"), exception.Message);
        }
        public void CommitMethodPreservesCommments()
        {
            var json              = @"{
  ""name"": ""test"",
  ""address"": {
    ""street"": ""Something street"",
    ""zipcode"": ""12345""
  }
}";
            var jsonConfigSrc     = new JsonConfigurationSource(ArbitraryFilePath);
            var outputCacheStream = new MemoryStream();

            jsonConfigSrc.Load(StringToStream(json));

            jsonConfigSrc.Commit(StringToStream(json), outputCacheStream);

            var newContents = StreamToString(outputCacheStream);

            Assert.Equal(json, newContents);
        }
        public void CommitMethodUpdatesValues()
        {
            var json              = @"{
  ""name"": ""test"",
  ""address"": {
    ""street"": ""Something street"",
    ""zipcode"": ""12345""
  }
}";
            var jsonConfigSrc     = new JsonConfigurationSource(ArbitraryFilePath);
            var outputCacheStream = new MemoryStream();

            jsonConfigSrc.Load(StringToStream(json));
            jsonConfigSrc.Set("name", "new_name");
            jsonConfigSrc.Set("address:zipcode", "67890");

            jsonConfigSrc.Commit(StringToStream(json), outputCacheStream);

            var newContents = StreamToString(outputCacheStream);

            Assert.Equal(json.Replace("test", "new_name").Replace("12345", "67890"), newContents);
        }