Skip to content

Commit 42b821d

Browse files
author
rathnapandi
committed
- Fix issue #499
1 parent 9800748 commit 42b821d

File tree

2 files changed

+211
-16
lines changed

2 files changed

+211
-16
lines changed

modules/apis/src/main/java/com/axway/apim/apiimport/actions/APIQuotaManager.java

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void updateRestrictions(List<QuotaRestriction> actualRestrictions, List<Q
5858
LOG.info("{} quota for API: {} is UN-CHANGED. Nothing to do.", type.getFriendlyName(), createdAPI.getName());
5959
return;
6060
}
61-
if(desiredRestrictions != null && desiredRestrictions.isEmpty()) {
61+
if (desiredRestrictions != null && desiredRestrictions.isEmpty()) {
6262
LOG.info("{} quota for API: {} Nothing to do.", type.getFriendlyName(), createdAPI.getName());
6363
return;
6464
}
@@ -68,7 +68,7 @@ public void updateRestrictions(List<QuotaRestriction> actualRestrictions, List<Q
6868
// Load the entire current default quota
6969
APIQuota currentDefaultQuota = quotaManager.getDefaultQuota(type);
7070
LOG.debug("Current Default Quota : {}", currentDefaultQuota);
71-
List<QuotaRestriction> mergedRestrictions = addOrMergeRestriction(actualRestrictions, desiredRestrictions);
71+
List<QuotaRestriction> mergedRestrictions = mergeRestriction(actualRestrictions, desiredRestrictions);
7272
LOG.debug("Merged Quota : {}", mergedRestrictions);
7373
populateMethodId(createdAPI, mergedRestrictions);
7474
// If there is an actual API, remove the restrictions for the current actual API
@@ -80,22 +80,22 @@ public void updateRestrictions(List<QuotaRestriction> actualRestrictions, List<Q
8080
quotaManager.saveQuota(currentDefaultQuota, currentDefaultQuota.getId());
8181
}
8282

83-
public List<QuotaRestriction> addOrMergeRestriction(List<QuotaRestriction> existingRestrictions, List<QuotaRestriction> desiredRestrictions) {
83+
public List<QuotaRestriction> mergeRestriction(List<QuotaRestriction> existingRestrictions, List<QuotaRestriction> desiredRestrictions) {
8484
List<QuotaRestriction> mergedRestrictions = new ArrayList<>();
8585
if (existingRestrictions == null) existingRestrictions = new ArrayList<>();
86-
boolean existingRestrictionFound = false;
8786
if (CoreParameters.getInstance().getQuotaMode().equals(CoreParameters.Mode.replace)) {
8887
LOG.info("Removing existing Quotas for API: {} as quotaMode is set to replace.", this.actualState.getName());
8988
} else {
9089
// Otherwise initially take over all existing restrictions for that API.
9190
mergedRestrictions.addAll(existingRestrictions);
9291
}
92+
9393
if (desiredRestrictions != null) {
94-
// Iterate over the given desired restrictions
94+
// Iterate over the given desired restrictions and copy quota
9595
for (QuotaRestriction desiredRestriction : desiredRestrictions) {
9696
desiredRestriction.setApiId(null);
9797
// And compare each desired restriction, if it is already included in the existing restrictions
98-
for (QuotaRestriction existingRestriction : mergedRestrictions) {
98+
for (QuotaRestriction existingRestriction : existingRestrictions) {
9999
// It's considered as the same restriction when type, method, period & per are equal
100100
if (desiredRestriction.isSameRestriction(existingRestriction, true)) {
101101
// If it is the same restriction, we need to update the restriction configuration
@@ -104,17 +104,37 @@ public List<QuotaRestriction> addOrMergeRestriction(List<QuotaRestriction> exist
104104
} else {
105105
existingRestriction.getConfig().put("mb", desiredRestriction.getConfig().get("mb"));
106106
}
107-
existingRestrictionFound = true;
108107
break;
109108
}
110109
}
111-
// If we haven't found any existing restriction add a new desired restriction
112-
if (!existingRestrictionFound) mergedRestrictions.add(desiredRestriction);
110+
}
111+
// Add missing desired restrictions to actual restriction
112+
for (QuotaRestriction desiredRestriction : desiredRestrictions) {
113+
if (!quotaApiMethodExists(existingRestrictions, desiredRestriction)) {
114+
mergedRestrictions.add(desiredRestriction);
115+
}
116+
}
117+
118+
// Remove actual restrictions are not found in desired restriction.
119+
for (QuotaRestriction existingRestriction : existingRestrictions) {
120+
if (!quotaApiMethodExists(desiredRestrictions, existingRestriction)) {
121+
mergedRestrictions.remove(existingRestriction);
122+
}
113123
}
114124
}
115125
return mergedRestrictions;
116126
}
117127

128+
public boolean quotaApiMethodExists(List<QuotaRestriction> desiredRestrictions, QuotaRestriction existingRestriction) {
129+
for (QuotaRestriction desiredRestriction : desiredRestrictions) {
130+
if (desiredRestriction.getMethod().equals(existingRestriction.getMethod())) {
131+
return true;
132+
}
133+
}
134+
return false;
135+
}
136+
137+
118138
public void populateMethodId(API createdAPI, List<QuotaRestriction> mergedRestrictions) throws AppException {
119139
APIManagerAPIMethodAdapter methodAdapter = APIManagerAdapter.getInstance().getMethodAdapter();
120140
for (QuotaRestriction restriction : mergedRestrictions) {
@@ -133,7 +153,7 @@ public void populateMethodId(API createdAPI, List<QuotaRestriction> mergedRestri
133153
} else {
134154
LOG.warn("API Method Name : {} not found in specification", restriction.getMethod());
135155
}
136-
}else {
156+
} else {
137157
// For new api creation
138158
APIMethod newMethod = methodAdapter.getMethodForName(createdAPI.getId(), restriction.getMethod());
139159
restriction.setMethod(newMethod.getId());

modules/apis/src/test/java/com/axway/apim/apiimport/actions/APIQuotaManagerTest.java

Lines changed: 181 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
public class APIQuotaManagerTest {
1313

1414
@Test
15-
public void addOrMergeRestriction() throws IOException {
15+
public void mergeRestriction() throws IOException {
1616
ObjectMapper objectMapper = new ObjectMapper();
1717
String actualQuota = "[\n" +
1818
" {\n" +
@@ -69,11 +69,186 @@ public void addOrMergeRestriction() throws IOException {
6969
" }\n" +
7070
" }\n" +
7171
" ]";
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);
7576
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());
78253
}
79254
}

0 commit comments

Comments
 (0)