|
18 | 18 | import static org.junit.jupiter.api.Assertions.assertNull; |
19 | 19 | import static org.junit.jupiter.api.Assertions.assertTrue; |
20 | 20 |
|
| 21 | +import java.io.IOException; |
| 22 | +import java.io.OutputStream; |
| 23 | +import java.net.InetSocketAddress; |
| 24 | +import java.nio.charset.StandardCharsets; |
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.Map; |
| 27 | + |
21 | 28 | import org.junit.jupiter.api.DisplayName; |
22 | 29 | import org.junit.jupiter.api.Nested; |
23 | 30 | import org.junit.jupiter.api.Test; |
24 | 31 |
|
25 | 32 | import com.fasterxml.jackson.databind.JsonNode; |
26 | 33 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 34 | +import com.fasterxml.jackson.databind.node.ArrayNode; |
| 35 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
27 | 36 | import com.fortify.cli.aviator.ssc.helper.AviatorSSCCustomTagHelper.SynchronizationResult; |
| 37 | +import com.fortify.cli.aviator.ssc.helper.AviatorSSCTagDefs.TagDefinition; |
| 38 | +import com.fortify.cli.common.json.JsonHelper; |
| 39 | +import com.fortify.cli.common.rest.unirest.UnirestHelper; |
| 40 | +import com.fortify.cli.common.rest.unirest.config.UnirestJsonHeaderConfigurer; |
| 41 | +import com.sun.net.httpserver.HttpExchange; |
| 42 | +import com.sun.net.httpserver.HttpServer; |
| 43 | + |
| 44 | +import kong.unirest.UnirestInstance; |
28 | 45 |
|
29 | 46 | /** |
30 | 47 | * Unit tests for {@link AviatorSSCCustomTagHelper} capability detection |
@@ -166,155 +183,138 @@ void dastCorrelationTag_isTextType() { |
166 | 183 | assertTrue(tag.getValues().isEmpty(), "TEXT tags should have empty value list"); |
167 | 184 | } |
168 | 185 | } |
169 | | -} |
170 | | -import static org.junit.jupiter.api.Assertions.assertNotNull; |
171 | | -import static org.junit.jupiter.api.Assertions.assertTrue; |
172 | | - |
173 | | -import java.io.IOException; |
174 | | -import java.io.OutputStream; |
175 | | -import java.net.InetSocketAddress; |
176 | | -import java.nio.charset.StandardCharsets; |
177 | | -import java.util.HashMap; |
178 | | -import java.util.Map; |
179 | | - |
180 | | -import org.junit.jupiter.api.Test; |
181 | | - |
182 | | -import com.fasterxml.jackson.databind.JsonNode; |
183 | | -import com.fasterxml.jackson.databind.node.ArrayNode; |
184 | | -import com.fasterxml.jackson.databind.node.ObjectNode; |
185 | | -import com.fortify.cli.aviator.ssc.helper.AviatorSSCTagDefs.TagDefinition; |
186 | | -import com.fortify.cli.common.json.JsonHelper; |
187 | | -import com.fortify.cli.common.rest.unirest.UnirestHelper; |
188 | | -import com.fortify.cli.common.rest.unirest.config.UnirestJsonHeaderConfigurer; |
189 | | -import com.sun.net.httpserver.HttpExchange; |
190 | | -import com.sun.net.httpserver.HttpServer; |
191 | 186 |
|
192 | | -import kong.unirest.UnirestInstance; |
| 187 | + @Nested |
| 188 | + @DisplayName("Integration tests with mock SSC server") |
| 189 | + class IntegrationTests { |
193 | 190 |
|
194 | | -class AviatorSSCCustomTagHelperTest { |
195 | | - @Test |
196 | | - void testSynchronizeUsesExistingCustomTag() throws Exception { |
197 | | - try (var server = new TestSscServer()) { |
198 | | - String tagId = "1001"; |
199 | | - server.withCustomTags(tagSummary(tagId, AviatorSSCTagDefs.AVIATOR_STATUS_TAG)); |
200 | | - server.withCustomTagDetails(tagId, tagDetails(tagId, AviatorSSCTagDefs.AVIATOR_STATUS_TAG)); |
201 | | - |
202 | | - try (var unirest = newUnirest(server)) { |
203 | | - var result = new AviatorSSCPrepareHelper.PrepareResult(); |
204 | | - JsonNode syncResult = new AviatorSSCCustomTagHelper(unirest, AviatorSSCTagDefs.AVIATOR_STATUS_TAG) |
205 | | - .synchronize(result); |
206 | | - |
207 | | - assertNotNull(syncResult); |
208 | | - assertEquals(tagId, syncResult.get("id").asText()); |
209 | | - assertEquals(1, syncResult.withArray("valueList").size()); |
210 | | - assertEquals(1, server.getCustomTagsGetCount()); |
211 | | - assertEquals(1, server.getCustomTagDetailsGetCount()); |
212 | | - assertEquals(0, server.getCustomTagCreateCount()); |
213 | | - assertEquals(0, server.getCustomTagUpdateCount()); |
214 | | - assertEquals("VERIFIED", result.toJsonNode().get(0).get("status").asText()); |
215 | | - assertEquals("'Aviator status' is already configured correctly.", |
216 | | - result.toJsonNode().get(0).get("details").asText()); |
| 191 | + @Test |
| 192 | + void testSynchronizeUsesExistingCustomTag() throws Exception { |
| 193 | + try (var server = new TestSscServer()) { |
| 194 | + String tagId = "1001"; |
| 195 | + server.withCustomTags(tagSummary(tagId, AviatorSSCTagDefs.AVIATOR_STATUS_TAG)); |
| 196 | + server.withCustomTagDetails(tagId, tagDetails(tagId, AviatorSSCTagDefs.AVIATOR_STATUS_TAG)); |
| 197 | + |
| 198 | + try (var unirest = newUnirest(server)) { |
| 199 | + var result = new AviatorSSCPrepareHelper.PrepareResult(); |
| 200 | + SynchronizationResult syncResult = new AviatorSSCCustomTagHelper(unirest, AviatorSSCTagDefs.AVIATOR_STATUS_TAG) |
| 201 | + .synchronize(result); |
| 202 | + |
| 203 | + assertNotNull(syncResult); |
| 204 | + assertNotNull(syncResult.getTag()); |
| 205 | + assertEquals(tagId, syncResult.getTag().get("id").asText()); |
| 206 | + assertEquals(1, syncResult.getTag().withArray("valueList").size()); |
| 207 | + assertEquals(1, server.getCustomTagsGetCount()); |
| 208 | + assertEquals(1, server.getCustomTagDetailsGetCount()); |
| 209 | + assertEquals(0, server.getCustomTagCreateCount()); |
| 210 | + assertEquals(0, server.getCustomTagUpdateCount()); |
| 211 | + assertEquals("VERIFIED", result.toJsonNode().get(0).get("status").asText()); |
| 212 | + assertEquals("'Aviator status' is already configured correctly.", |
| 213 | + result.toJsonNode().get(0).get("details").asText()); |
| 214 | + } |
217 | 215 | } |
218 | 216 | } |
219 | | - } |
220 | 217 |
|
221 | | - @Test |
222 | | - void testSynchronizeUpdatesExistingCustomTagWhenValuesAreMissing() throws Exception { |
223 | | - try (var server = new TestSscServer()) { |
224 | | - String tagId = "1002"; |
225 | | - TagDefinition tagDefinition = AviatorSSCTagDefs.AVIATOR_PREDICTION_TAG; |
226 | | - String existingValue = tagDefinition.getValues().get(0); |
227 | | - server.withCustomTags(tagSummary(tagId, tagDefinition)); |
228 | | - server.withCustomTagDetails(tagId, tagDetails(tagId, tagDefinition, existingValue)); |
229 | | - |
230 | | - try (var unirest = newUnirest(server)) { |
231 | | - var result = new AviatorSSCPrepareHelper.PrepareResult(); |
232 | | - JsonNode syncResult = new AviatorSSCCustomTagHelper(unirest, tagDefinition) |
233 | | - .synchronize(result); |
234 | | - |
235 | | - assertNotNull(syncResult); |
236 | | - assertEquals(tagId, syncResult.get("id").asText()); |
237 | | - assertEquals(tagDefinition.getValues().size(), syncResult.withArray("valueList").size()); |
238 | | - assertTrue(hasLookupValue(syncResult.get("valueList"), "AVIATOR:Unsure")); |
239 | | - assertEquals(1, server.getCustomTagsGetCount()); |
240 | | - assertEquals(1, server.getCustomTagDetailsGetCount()); |
241 | | - assertEquals(0, server.getCustomTagCreateCount()); |
242 | | - assertEquals(1, server.getCustomTagUpdateCount()); |
243 | | - assertNotNull(server.getLastUpdatedTag()); |
244 | | - assertEquals(tagDefinition.getValues().size(), server.getLastUpdatedTag().withArray("valueList").size()); |
245 | | - assertEquals("UPDATED", result.toJsonNode().get(0).get("status").asText()); |
246 | | - assertEquals( |
247 | | - "Added 5 missing values to tag 'Aviator prediction'.", |
248 | | - result.toJsonNode().get(0).get("details").asText()); |
| 218 | + @Test |
| 219 | + void testSynchronizeUpdatesExistingCustomTagWhenValuesAreMissing() throws Exception { |
| 220 | + try (var server = new TestSscServer()) { |
| 221 | + String tagId = "1002"; |
| 222 | + TagDefinition tagDefinition = AviatorSSCTagDefs.AVIATOR_PREDICTION_TAG; |
| 223 | + String existingValue = tagDefinition.getValues().get(0); |
| 224 | + server.withCustomTags(tagSummary(tagId, tagDefinition)); |
| 225 | + server.withCustomTagDetails(tagId, tagDetails(tagId, tagDefinition, existingValue)); |
| 226 | + |
| 227 | + try (var unirest = newUnirest(server)) { |
| 228 | + var result = new AviatorSSCPrepareHelper.PrepareResult(); |
| 229 | + SynchronizationResult syncResult = new AviatorSSCCustomTagHelper(unirest, tagDefinition) |
| 230 | + .synchronize(result); |
| 231 | + |
| 232 | + assertNotNull(syncResult); |
| 233 | + assertNotNull(syncResult.getTag()); |
| 234 | + assertEquals(tagId, syncResult.getTag().get("id").asText()); |
| 235 | + assertEquals(tagDefinition.getValues().size(), syncResult.getTag().withArray("valueList").size()); |
| 236 | + assertTrue(hasLookupValue(syncResult.getTag().get("valueList"), "AVIATOR:Unsure")); |
| 237 | + assertEquals(1, server.getCustomTagsGetCount()); |
| 238 | + assertEquals(1, server.getCustomTagDetailsGetCount()); |
| 239 | + assertEquals(0, server.getCustomTagCreateCount()); |
| 240 | + assertEquals(1, server.getCustomTagUpdateCount()); |
| 241 | + assertNotNull(server.getLastUpdatedTag()); |
| 242 | + assertEquals(tagDefinition.getValues().size(), server.getLastUpdatedTag().withArray("valueList").size()); |
| 243 | + assertEquals("UPDATED", result.toJsonNode().get(0).get("status").asText()); |
| 244 | + assertEquals( |
| 245 | + "Added 5 missing values to tag 'Aviator prediction'.", |
| 246 | + result.toJsonNode().get(0).get("details").asText()); |
| 247 | + } |
249 | 248 | } |
250 | 249 | } |
251 | | - } |
252 | 250 |
|
253 | | - @Test |
254 | | - void testSynchronizeCreatesTagWhenAbsentFromBothEndpoints() throws Exception { |
255 | | - try (var server = new TestSscServer()) { |
256 | | - server.withCreateResponse(createdTag("2002", AviatorSSCTagDefs.AVIATOR_STATUS_TAG)); |
257 | | - |
258 | | - try (var unirest = newUnirest(server)) { |
259 | | - var result = new AviatorSSCPrepareHelper.PrepareResult(); |
260 | | - JsonNode syncResult = new AviatorSSCCustomTagHelper(unirest, AviatorSSCTagDefs.AVIATOR_STATUS_TAG) |
261 | | - .synchronize(result); |
262 | | - |
263 | | - assertNotNull(syncResult); |
264 | | - assertEquals("2002", syncResult.get("id").asText()); |
265 | | - assertEquals(1, server.getCustomTagsGetCount()); |
266 | | - assertEquals(0, server.getCustomTagDetailsGetCount()); |
267 | | - assertEquals(1, server.getCustomTagCreateCount()); |
268 | | - assertEquals(0, server.getCustomTagUpdateCount()); |
269 | | - assertEquals("CREATED", result.toJsonNode().get(0).get("status").asText()); |
270 | | - assertEquals("Tag 'Aviator status' created successfully.", |
271 | | - result.toJsonNode().get(0).get("details").asText()); |
| 251 | + @Test |
| 252 | + void testSynchronizeCreatesTagWhenAbsentFromBothEndpoints() throws Exception { |
| 253 | + try (var server = new TestSscServer()) { |
| 254 | + server.withCreateResponse(createdTag("2002", AviatorSSCTagDefs.AVIATOR_STATUS_TAG)); |
| 255 | + |
| 256 | + try (var unirest = newUnirest(server)) { |
| 257 | + var result = new AviatorSSCPrepareHelper.PrepareResult(); |
| 258 | + SynchronizationResult syncResult = new AviatorSSCCustomTagHelper(unirest, AviatorSSCTagDefs.AVIATOR_STATUS_TAG) |
| 259 | + .synchronize(result); |
| 260 | + |
| 261 | + assertNotNull(syncResult); |
| 262 | + assertNotNull(syncResult.getTag()); |
| 263 | + assertEquals("2002", syncResult.getTag().get("id").asText()); |
| 264 | + assertEquals(1, server.getCustomTagsGetCount()); |
| 265 | + assertEquals(0, server.getCustomTagDetailsGetCount()); |
| 266 | + assertEquals(1, server.getCustomTagCreateCount()); |
| 267 | + assertEquals(0, server.getCustomTagUpdateCount()); |
| 268 | + assertEquals("CREATED", result.toJsonNode().get(0).get("status").asText()); |
| 269 | + assertEquals("Tag 'Aviator status' created successfully.", |
| 270 | + result.toJsonNode().get(0).get("details").asText()); |
| 271 | + } |
272 | 272 | } |
273 | 273 | } |
274 | | - } |
275 | 274 |
|
276 | | - private UnirestInstance newUnirest(TestSscServer server) { |
277 | | - return UnirestHelper.createUnirestInstance(unirest -> { |
278 | | - UnirestJsonHeaderConfigurer.configure(unirest); |
279 | | - unirest.config().defaultBaseUrl(server.getBaseUrl()); |
280 | | - }); |
281 | | - } |
| 275 | + private UnirestInstance newUnirest(TestSscServer server) { |
| 276 | + return UnirestHelper.createUnirestInstance(unirest -> { |
| 277 | + UnirestJsonHeaderConfigurer.configure(unirest); |
| 278 | + unirest.config().defaultBaseUrl(server.getBaseUrl()); |
| 279 | + }); |
| 280 | + } |
282 | 281 |
|
283 | | - private static ObjectNode tagSummary(String id, TagDefinition tagDefinition) { |
284 | | - return JsonHelper.getObjectMapper().createObjectNode() |
285 | | - .put("id", id) |
286 | | - .put("guid", tagDefinition.getGuid()) |
287 | | - .put("name", tagDefinition.getName()); |
288 | | - } |
| 282 | + private static ObjectNode tagSummary(String id, TagDefinition tagDefinition) { |
| 283 | + return JsonHelper.getObjectMapper().createObjectNode() |
| 284 | + .put("id", id) |
| 285 | + .put("guid", tagDefinition.getGuid()) |
| 286 | + .put("name", tagDefinition.getName()); |
| 287 | + } |
289 | 288 |
|
290 | | - private static ObjectNode tagDetails(String id, TagDefinition tagDefinition) { |
291 | | - return tagDetails(id, tagDefinition, tagDefinition.getValues().toArray(String[]::new)); |
292 | | - } |
| 289 | + private static ObjectNode tagDetails(String id, TagDefinition tagDefinition) { |
| 290 | + return tagDetails(id, tagDefinition, tagDefinition.getValues().toArray(String[]::new)); |
| 291 | + } |
293 | 292 |
|
294 | | - private static ObjectNode tagDetails(String id, TagDefinition tagDefinition, String... values) { |
295 | | - ObjectNode result = tagSummary(id, tagDefinition) |
296 | | - .put("valueType", "LIST") |
297 | | - .put("customTagType", "CUSTOM"); |
298 | | - ArrayNode valueList = result.putArray("valueList"); |
299 | | - for (String value : values) { |
300 | | - valueList.add(JsonHelper.getObjectMapper().createObjectNode().put("lookupValue", value)); |
| 293 | + private static ObjectNode tagDetails(String id, TagDefinition tagDefinition, String... values) { |
| 294 | + ObjectNode result = tagSummary(id, tagDefinition) |
| 295 | + .put("valueType", "LIST") |
| 296 | + .put("customTagType", "CUSTOM"); |
| 297 | + ArrayNode valueList = result.putArray("valueList"); |
| 298 | + for (String value : values) { |
| 299 | + valueList.add(JsonHelper.getObjectMapper().createObjectNode().put("lookupValue", value)); |
| 300 | + } |
| 301 | + return result; |
301 | 302 | } |
302 | | - return result; |
303 | | - } |
304 | 303 |
|
305 | | - private static ObjectNode createdTag(String id, TagDefinition tagDefinition) { |
306 | | - return tagSummary(id, tagDefinition) |
307 | | - .put("valueType", "LIST") |
308 | | - .put("customTagType", "CUSTOM"); |
309 | | - } |
| 304 | + private static ObjectNode createdTag(String id, TagDefinition tagDefinition) { |
| 305 | + return tagSummary(id, tagDefinition) |
| 306 | + .put("valueType", "LIST") |
| 307 | + .put("customTagType", "CUSTOM"); |
| 308 | + } |
310 | 309 |
|
311 | | - private static boolean hasLookupValue(JsonNode valueList, String lookupValue) { |
312 | | - for (JsonNode value : valueList) { |
313 | | - if (lookupValue.equals(value.path("lookupValue").asText())) { |
314 | | - return true; |
| 310 | + private static boolean hasLookupValue(JsonNode valueList, String lookupValue) { |
| 311 | + for (JsonNode value : valueList) { |
| 312 | + if (lookupValue.equals(value.path("lookupValue").asText())) { |
| 313 | + return true; |
| 314 | + } |
315 | 315 | } |
| 316 | + return false; |
316 | 317 | } |
317 | | - return false; |
318 | 318 | } |
319 | 319 |
|
320 | 320 | private static final class TestSscServer implements AutoCloseable { |
|
0 commit comments