|
12 | 12 | public class APIQuotaManagerTest { |
13 | 13 |
|
14 | 14 | @Test |
15 | | - public void addOrMergeRestriction() throws IOException { |
| 15 | + public void mergeRestriction() throws IOException { |
16 | 16 | ObjectMapper objectMapper = new ObjectMapper(); |
17 | 17 | String actualQuota = "[\n" + |
18 | 18 | " {\n" + |
@@ -69,11 +69,186 @@ public void addOrMergeRestriction() throws IOException { |
69 | 69 | " }\n" + |
70 | 70 | " }\n" + |
71 | 71 | " ]"; |
72 | | - TypeReference<List<QuotaRestriction>> quotaRestrictionTypeRef = new TypeReference<List<QuotaRestriction>>() {}; |
73 | | - List<QuotaRestriction> actualQuotaRestriction = objectMapper.readValue(actualQuota, quotaRestrictionTypeRef); |
74 | | - List<QuotaRestriction> desiredQuotaRestriction = objectMapper.readValue(desiredQuota, quotaRestrictionTypeRef); |
| 72 | + TypeReference<List<QuotaRestriction>> quotaRestrictionTypeRef = new TypeReference<List<QuotaRestriction>>() { |
| 73 | + }; |
| 74 | + List<QuotaRestriction> actualQuotaRestriction = objectMapper.readValue(actualQuota, quotaRestrictionTypeRef); |
| 75 | + List<QuotaRestriction> desiredQuotaRestriction = objectMapper.readValue(desiredQuota, quotaRestrictionTypeRef); |
75 | 76 | APIQuotaManager apiQuotaManager = new APIQuotaManager(null, null); |
76 | | - List<QuotaRestriction> result = apiQuotaManager.addOrMergeRestriction(actualQuotaRestriction, desiredQuotaRestriction); |
77 | | - Assert.assertEquals(5, result.size()); |
| 77 | + List<QuotaRestriction> result = apiQuotaManager.mergeRestriction(actualQuotaRestriction, desiredQuotaRestriction); |
| 78 | + Assert.assertEquals(3, result.size()); |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + @Test |
| 83 | + public void mergeRestrictionActual() throws IOException { |
| 84 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 85 | + String actualQuota = "[\n" + |
| 86 | + " {\n" + |
| 87 | + " \"api\": \"4f52e8a2-cba1-4645-8088-2ed98f5ea57e\",\n" + |
| 88 | + " \"method\": \"method1\",\n" + |
| 89 | + " \"type\": \"throttle\",\n" + |
| 90 | + " \"config\": {\n" + |
| 91 | + " \"period\": \"second\",\n" + |
| 92 | + " \"per\": \"8\",\n" + |
| 93 | + " \"messages\": \"1000\"\n" + |
| 94 | + " }\n" + |
| 95 | + " },\n" + |
| 96 | + " {\n" + |
| 97 | + " \"api\": \"*\",\n" + |
| 98 | + " \"method\": \"method2\",\n" + |
| 99 | + " \"type\": \"throttle\",\n" + |
| 100 | + " \"config\": {\n" + |
| 101 | + " \"period\": \"second\",\n" + |
| 102 | + " \"per\": \"5\",\n" + |
| 103 | + " \"messages\": \"1000\"\n" + |
| 104 | + " }\n" + |
| 105 | + " },\n" + |
| 106 | + " {\n" + |
| 107 | + " \"api\": \"4f52e8a2-cba1-4645-8088-2ed98f5ea57e\",\n" + |
| 108 | + " \"method\": \"*\",\n" + |
| 109 | + " \"type\": \"throttle\",\n" + |
| 110 | + " \"config\": {\n" + |
| 111 | + " \"messages\": \"1000\",\n" + |
| 112 | + " \"period\": \"second\",\n" + |
| 113 | + " \"per\": \"2\"\n" + |
| 114 | + " }\n" + |
| 115 | + " }\n" + |
| 116 | + "]"; |
| 117 | + |
| 118 | + String desiredQuota = "[\n" + |
| 119 | + " {\n" + |
| 120 | + " \"api\": \"4f52e8a2-cba1-4645-8088-2ed98f5ea57e\",\n" + |
| 121 | + " \"method\": \"method1\",\n" + |
| 122 | + " \"type\": \"throttle\",\n" + |
| 123 | + " \"config\": {\n" + |
| 124 | + " \"period\": \"second\",\n" + |
| 125 | + " \"per\": \"8\",\n" + |
| 126 | + " \"messages\": \"1000\"\n" + |
| 127 | + " }\n" + |
| 128 | + " },\n" + |
| 129 | + " {\n" + |
| 130 | + " \"api\": \"method2\",\n" + |
| 131 | + " \"method\": \"*\",\n" + |
| 132 | + " \"type\": \"throttle\",\n" + |
| 133 | + " \"config\": {\n" + |
| 134 | + " \"period\": \"second\",\n" + |
| 135 | + " \"per\": \"5\",\n" + |
| 136 | + " \"messages\": \"1000\"\n" + |
| 137 | + " }\n" + |
| 138 | + " }\n" + |
| 139 | + " ]"; |
| 140 | + TypeReference<List<QuotaRestriction>> quotaRestrictionTypeRef = new TypeReference<List<QuotaRestriction>>() { |
| 141 | + }; |
| 142 | + List<QuotaRestriction> actualQuotaRestriction = objectMapper.readValue(actualQuota, quotaRestrictionTypeRef); |
| 143 | + List<QuotaRestriction> desiredQuotaRestriction = objectMapper.readValue(desiredQuota, quotaRestrictionTypeRef); |
| 144 | + APIQuotaManager apiQuotaManager = new APIQuotaManager(null, null); |
| 145 | + List<QuotaRestriction> result = apiQuotaManager.mergeRestriction(actualQuotaRestriction, desiredQuotaRestriction); |
| 146 | + Assert.assertEquals(2, result.size()); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + public void mergeRestrictionDesired() throws IOException { |
| 151 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 152 | + String actualQuota = "[\n" + |
| 153 | + " {\n" + |
| 154 | + " \"method\": \"PATCH /{txid}\",\n" + |
| 155 | + " \"type\": \"throttle\",\n" + |
| 156 | + " \"config\": {\n" + |
| 157 | + " \"period\": \"second\",\n" + |
| 158 | + " \"per\": \"1\",\n" + |
| 159 | + " \"messages\": \"656\"\n" + |
| 160 | + " }\n" + |
| 161 | + " },\n" + |
| 162 | + " {\n" + |
| 163 | + " \"method\": \"OPTIONS /{txid}\",\n" + |
| 164 | + " \"type\": \"throttle\",\n" + |
| 165 | + " \"config\": {\n" + |
| 166 | + " \"period\": \"second\",\n" + |
| 167 | + " \"per\": \"1\",\n" + |
| 168 | + " \"messages\": \"750\"\n" + |
| 169 | + " }\n" + |
| 170 | + " }\n" + |
| 171 | + " ]"; |
| 172 | + |
| 173 | + String desiredQuota = "[\n" + |
| 174 | + " {\n" + |
| 175 | + " \"method\": \"PATCH /{txid}\",\n" + |
| 176 | + " \"type\": \"throttle\",\n" + |
| 177 | + " \"config\": {\n" + |
| 178 | + " \"period\": \"second\",\n" + |
| 179 | + " \"per\": \"1\",\n" + |
| 180 | + " \"messages\": \"656\"\n" + |
| 181 | + " }\n" + |
| 182 | + " },\n" + |
| 183 | + " {\n" + |
| 184 | + " \"method\": \"OPTIONS /{txid}\",\n" + |
| 185 | + " \"type\": \"throttle\",\n" + |
| 186 | + " \"config\": {\n" + |
| 187 | + " \"period\": \"second\",\n" + |
| 188 | + " \"per\": \"1\",\n" + |
| 189 | + " \"messages\": \"750\"\n" + |
| 190 | + " }\n" + |
| 191 | + " },\n" + |
| 192 | + " {\n" + |
| 193 | + " \"method\": \"POST /{txid}\",\n" + |
| 194 | + " \"type\": \"throttle\",\n" + |
| 195 | + " \"config\": {\n" + |
| 196 | + " \"period\": \"second\",\n" + |
| 197 | + " \"per\": \"1\",\n" + |
| 198 | + " \"messages\": \"887\"\n" + |
| 199 | + " }\n" + |
| 200 | + " }\n" + |
| 201 | + " ]"; |
| 202 | + TypeReference<List<QuotaRestriction>> quotaRestrictionTypeRef = new TypeReference<List<QuotaRestriction>>() { |
| 203 | + }; |
| 204 | + List<QuotaRestriction> actualQuotaRestriction = objectMapper.readValue(actualQuota, quotaRestrictionTypeRef); |
| 205 | + List<QuotaRestriction> desiredQuotaRestriction = objectMapper.readValue(desiredQuota, quotaRestrictionTypeRef); |
| 206 | + APIQuotaManager apiQuotaManager = new APIQuotaManager(null, null); |
| 207 | + List<QuotaRestriction> result = apiQuotaManager.mergeRestriction(actualQuotaRestriction, desiredQuotaRestriction); |
| 208 | + Assert.assertEquals(3, result.size()); |
| 209 | + } |
| 210 | + |
| 211 | + |
| 212 | + @Test |
| 213 | + public void mergeRestrictionDesiredWithNoActual() throws IOException { |
| 214 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 215 | + String actualQuota = "[]"; |
| 216 | + |
| 217 | + String desiredQuota = "[\n" + |
| 218 | + " {\n" + |
| 219 | + " \"method\": \"PATCH /{txid}\",\n" + |
| 220 | + " \"type\": \"throttle\",\n" + |
| 221 | + " \"config\": {\n" + |
| 222 | + " \"period\": \"second\",\n" + |
| 223 | + " \"per\": \"1\",\n" + |
| 224 | + " \"messages\": \"656\"\n" + |
| 225 | + " }\n" + |
| 226 | + " },\n" + |
| 227 | + " {\n" + |
| 228 | + " \"method\": \"OPTIONS /{txid}\",\n" + |
| 229 | + " \"type\": \"throttle\",\n" + |
| 230 | + " \"config\": {\n" + |
| 231 | + " \"period\": \"second\",\n" + |
| 232 | + " \"per\": \"1\",\n" + |
| 233 | + " \"messages\": \"750\"\n" + |
| 234 | + " }\n" + |
| 235 | + " },\n" + |
| 236 | + " {\n" + |
| 237 | + " \"method\": \"POST /{txid}\",\n" + |
| 238 | + " \"type\": \"throttle\",\n" + |
| 239 | + " \"config\": {\n" + |
| 240 | + " \"period\": \"second\",\n" + |
| 241 | + " \"per\": \"1\",\n" + |
| 242 | + " \"messages\": \"887\"\n" + |
| 243 | + " }\n" + |
| 244 | + " }\n" + |
| 245 | + " ]"; |
| 246 | + TypeReference<List<QuotaRestriction>> quotaRestrictionTypeRef = new TypeReference<List<QuotaRestriction>>() { |
| 247 | + }; |
| 248 | + List<QuotaRestriction> actualQuotaRestriction = objectMapper.readValue(actualQuota, quotaRestrictionTypeRef); |
| 249 | + List<QuotaRestriction> desiredQuotaRestriction = objectMapper.readValue(desiredQuota, quotaRestrictionTypeRef); |
| 250 | + APIQuotaManager apiQuotaManager = new APIQuotaManager(null, null); |
| 251 | + List<QuotaRestriction> result = apiQuotaManager.mergeRestriction(actualQuotaRestriction, desiredQuotaRestriction); |
| 252 | + Assert.assertEquals(3, result.size()); |
78 | 253 | } |
79 | 254 | } |
0 commit comments