Skip to content

Commit 5608ce8

Browse files
authored
Merge pull request #1031 from mneeta/prepare-command-upgraded-ssc
Resolved build failure issue
2 parents 02d0f53 + a950ade commit 5608ce8

2 files changed

Lines changed: 133 additions & 132 deletions

File tree

fcli-core/fcli-aviator/src/main/java/com/fortify/cli/aviator/ssc/helper/AviatorSSCCustomTagHelper.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,9 @@ private JsonNode findInInternalCustomTags() {
170170
return JsonHelper.stream((ArrayNode) data)
171171
.filter(tag -> tagDef.getGuid().equalsIgnoreCase(tag.path("guid").asText()))
172172
.findFirst().orElse(null);
173-
} catch (UnexpectedHttpResponseException e) {
174-
// Endpoint may not exist in older SSC versions - this is expected
173+
} catch (Exception e) {
174+
// Endpoint may not exist in older SSC versions - this is expected.
175+
// Various exceptions can occur: HTTP 404, JSON parse errors, etc.
175176
LOG.debug("Could not query /internalCustomTags (may not exist in this SSC version): {}", e.getMessage());
176177
return null;
177178
}

fcli-core/fcli-aviator/src/test/java/com/fortify/cli/aviator/ssc/helper/AviatorSSCCustomTagHelperTest.java

Lines changed: 130 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,30 @@
1818
import static org.junit.jupiter.api.Assertions.assertNull;
1919
import static org.junit.jupiter.api.Assertions.assertTrue;
2020

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+
2128
import org.junit.jupiter.api.DisplayName;
2229
import org.junit.jupiter.api.Nested;
2330
import org.junit.jupiter.api.Test;
2431

2532
import com.fasterxml.jackson.databind.JsonNode;
2633
import com.fasterxml.jackson.databind.ObjectMapper;
34+
import com.fasterxml.jackson.databind.node.ArrayNode;
35+
import com.fasterxml.jackson.databind.node.ObjectNode;
2736
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;
2845

2946
/**
3047
* Unit tests for {@link AviatorSSCCustomTagHelper} capability detection
@@ -166,155 +183,138 @@ void dastCorrelationTag_isTextType() {
166183
assertTrue(tag.getValues().isEmpty(), "TEXT tags should have empty value list");
167184
}
168185
}
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;
191186

192-
import kong.unirest.UnirestInstance;
187+
@Nested
188+
@DisplayName("Integration tests with mock SSC server")
189+
class IntegrationTests {
193190

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+
}
217215
}
218216
}
219-
}
220217

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+
}
249248
}
250249
}
251-
}
252250

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+
}
272272
}
273273
}
274-
}
275274

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+
}
282281

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+
}
289288

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+
}
293292

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;
301302
}
302-
return result;
303-
}
304303

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+
}
310309

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+
}
315315
}
316+
return false;
316317
}
317-
return false;
318318
}
319319

320320
private static final class TestSscServer implements AutoCloseable {

0 commit comments

Comments
 (0)