|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +using System; |
| 17 | +using System.Collections.Generic; |
| 18 | +using System.IO; |
| 19 | +using System.Text; |
| 20 | +using Amazon.Extensions.Configuration.SystemsManager.AppConfig; |
| 21 | +using Xunit; |
| 22 | + |
| 23 | +namespace Amazon.Extensions.Configuration.SystemsManager.Tests |
| 24 | +{ |
| 25 | + public class AppConfigProcessorTests |
| 26 | + { |
| 27 | + [Fact] |
| 28 | + public void ParseConfig_ApplicationJson_ParsesSuccessfully() |
| 29 | + { |
| 30 | + var jsonContent = "{\"key1\":\"value1\",\"key2\":\"value2\"}"; |
| 31 | + using var stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonContent)); |
| 32 | + |
| 33 | + var result = AppConfigProcessor.ParseConfig("application/json", stream); |
| 34 | + |
| 35 | + Assert.Equal("value1", result["key1"]); |
| 36 | + Assert.Equal("value2", result["key2"]); |
| 37 | + } |
| 38 | + |
| 39 | + [Fact] |
| 40 | + public void ParseConfig_ApplicationOctetStream_ParsesJsonSuccessfully() |
| 41 | + { |
| 42 | + var jsonContent = "{\"key1\":\"value1\",\"key2\":\"value2\"}"; |
| 43 | + using var stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonContent)); |
| 44 | + |
| 45 | + var result = AppConfigProcessor.ParseConfig("application/octet-stream", stream); |
| 46 | + |
| 47 | + Assert.Equal("value1", result["key1"]); |
| 48 | + Assert.Equal("value2", result["key2"]); |
| 49 | + } |
| 50 | + |
| 51 | + [Fact] |
| 52 | + public void ParseConfig_ApplicationJsonWithCharset_ParsesSuccessfully() |
| 53 | + { |
| 54 | + var jsonContent = "{\"key1\":\"value1\",\"key2\":\"value2\"}"; |
| 55 | + using var stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonContent)); |
| 56 | + |
| 57 | + var result = AppConfigProcessor.ParseConfig("application/json; charset=utf-8", stream); |
| 58 | + |
| 59 | + Assert.Equal("value1", result["key1"]); |
| 60 | + Assert.Equal("value2", result["key2"]); |
| 61 | + } |
| 62 | + |
| 63 | + [Fact] |
| 64 | + public void ParseConfig_UnknownContentType_ParsesJsonSuccessfully() |
| 65 | + { |
| 66 | + var jsonContent = "{\"key1\":\"value1\",\"key2\":\"value2\"}"; |
| 67 | + using var stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonContent)); |
| 68 | + |
| 69 | + var result = AppConfigProcessor.ParseConfig("application/unknown", stream); |
| 70 | + |
| 71 | + Assert.Equal("value1", result["key1"]); |
| 72 | + Assert.Equal("value2", result["key2"]); |
| 73 | + } |
| 74 | + |
| 75 | + [Fact] |
| 76 | + public void ParseConfig_NullContentType_ParsesJsonSuccessfully() |
| 77 | + { |
| 78 | + var jsonContent = "{\"key1\":\"value1\",\"key2\":\"value2\"}"; |
| 79 | + using var stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonContent)); |
| 80 | + |
| 81 | + var result = AppConfigProcessor.ParseConfig(null, stream); |
| 82 | + |
| 83 | + Assert.Equal("value1", result["key1"]); |
| 84 | + Assert.Equal("value2", result["key2"]); |
| 85 | + } |
| 86 | + |
| 87 | + [Fact] |
| 88 | + public void ParseConfig_NestedJson_ParsesSuccessfully() |
| 89 | + { |
| 90 | + var jsonContent = "{\"section1\":{\"key1\":\"value1\",\"key2\":\"value2\"},\"section2\":{\"key3\":\"value3\"}}"; |
| 91 | + using var stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonContent)); |
| 92 | + |
| 93 | + var result = AppConfigProcessor.ParseConfig("application/octet-stream", stream); |
| 94 | + |
| 95 | + Assert.Equal("value1", result["section1:key1"]); |
| 96 | + Assert.Equal("value2", result["section1:key2"]); |
| 97 | + Assert.Equal("value3", result["section2:key3"]); |
| 98 | + } |
| 99 | + |
| 100 | + [Fact] |
| 101 | + public void ParseConfig_InvalidJson_ThrowsInvalidOperationException() |
| 102 | + { |
| 103 | + var invalidJsonContent = "{ invalid json content }"; |
| 104 | + using var stream = new MemoryStream(Encoding.UTF8.GetBytes(invalidJsonContent)); |
| 105 | + |
| 106 | + var exception = Assert.Throws<InvalidOperationException>(() => |
| 107 | + AppConfigProcessor.ParseConfig("application/json", stream)); |
| 108 | + |
| 109 | + Assert.Contains("Failed to parse AppConfig content as JSON", exception.Message); |
| 110 | + Assert.Contains("Content-Type was 'application/json'", exception.Message); |
| 111 | + } |
| 112 | + |
| 113 | + [Fact] |
| 114 | + public void ParseConfig_InvalidJsonWithOctetStream_ThrowsInvalidOperationException() |
| 115 | + { |
| 116 | + var invalidJsonContent = "not json at all"; |
| 117 | + using var stream = new MemoryStream(Encoding.UTF8.GetBytes(invalidJsonContent)); |
| 118 | + |
| 119 | + var exception = Assert.Throws<InvalidOperationException>(() => |
| 120 | + AppConfigProcessor.ParseConfig("application/octet-stream", stream)); |
| 121 | + |
| 122 | + Assert.Contains("Failed to parse AppConfig content as JSON", exception.Message); |
| 123 | + Assert.Contains("Content-Type was 'application/octet-stream'", exception.Message); |
| 124 | + } |
| 125 | + |
| 126 | + [Fact] |
| 127 | + public void ParseConfig_EmptyJson_ReturnsEmptyDictionary() |
| 128 | + { |
| 129 | + var jsonContent = "{}"; |
| 130 | + using var stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonContent)); |
| 131 | + |
| 132 | + var result = AppConfigProcessor.ParseConfig("application/json", stream); |
| 133 | + |
| 134 | + Assert.Empty(result); |
| 135 | + } |
| 136 | + |
| 137 | + [Fact] |
| 138 | + public void ParseConfig_EmptyStream_ThrowsInvalidOperationException() |
| 139 | + { |
| 140 | + using var stream = new MemoryStream(); |
| 141 | + |
| 142 | + var exception = Assert.Throws<InvalidOperationException>(() => |
| 143 | + AppConfigProcessor.ParseConfig("application/json", stream)); |
| 144 | + |
| 145 | + Assert.Contains("Failed to parse AppConfig content as JSON", exception.Message); |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments