|
6 | 6 |
|
7 | 7 | import com.fasterxml.jackson.core.JsonProcessingException; |
8 | 8 | import io.securecodebox.persistence.defectdojo.config.Config; |
9 | | -import org.junit.jupiter.api.BeforeEach; |
10 | 9 | import org.junit.jupiter.api.Test; |
11 | 10 | import org.springframework.http.MediaType; |
12 | 11 | import org.springframework.test.web.client.MockRestServiceServer; |
|
21 | 20 |
|
22 | 21 | // This test is sufficient for all services (except user profile) as all the code is generic |
23 | 22 | class FindingServiceTest { |
| 23 | + private static final String FINDING_RESPONSE = """ |
| 24 | + { |
| 25 | + "count": 1, |
| 26 | + "next": null, |
| 27 | + "previous": null, |
| 28 | + "results": |
| 29 | + [ |
| 30 | + { |
| 31 | + "id": 42, |
| 32 | + "tags": [], |
| 33 | + "request_response": { |
| 34 | + "req_resp": [] |
| 35 | + }, |
| 36 | + "accepted_risks": [], |
| 37 | + "push_to_jira": false, |
| 38 | + "age": 145, |
| 39 | + "sla_days_remaining": null, |
| 40 | + "finding_meta": [], |
| 41 | + "related_fields": null, |
| 42 | + "jira_creation": null, |
| 43 | + "jira_change": null, |
| 44 | + "display_status": "Active, Verified", |
| 45 | + "finding_groups": [], |
| 46 | + "title": "Open Port: 9929/TCP", |
| 47 | + "date": "2021-03-18", |
| 48 | + "sla_start_date": null, |
| 49 | + "cwe": 0, |
| 50 | + "cve": null, |
| 51 | + "cvssv3": null, |
| 52 | + "cvssv3_score": null, |
| 53 | + "url": null, |
| 54 | + "severity": "Info", |
| 55 | + "description": "### Host\\n\\n**IP Address:** 198.51.100.0\\n**FQDN:** scanme.nmap.org\\n\\n\\n**Port/Protocol:** 9929/tcp\\n\\n\\n\\n\\n", |
| 56 | + "mitigation": "N/A", |
| 57 | + "impact": "No impact provided", |
| 58 | + "steps_to_reproduce": null, |
| 59 | + "severity_justification": null, |
| 60 | + "references": null, |
| 61 | + "is_template": false, |
| 62 | + "active": true, |
| 63 | + "verified": true, |
| 64 | + "false_p": false, |
| 65 | + "duplicate": false, |
| 66 | + "out_of_scope": false, |
| 67 | + "risk_accepted": false, |
| 68 | + "under_review": false, |
| 69 | + "last_status_update": "2021-07-21T12:43:36.628994Z", |
| 70 | + "under_defect_review": false, |
| 71 | + "is_mitigated": false, |
| 72 | + "thread_id": 0, |
| 73 | + "mitigated": null, |
| 74 | + "numerical_severity": "S4", |
| 75 | + "last_reviewed": "2021-07-21T12:43:36.545348Z", |
| 76 | + "line_number": null, |
| 77 | + "sourcefilepath": null, |
| 78 | + "sourcefile": null, |
| 79 | + "param": null, |
| 80 | + "payload": null, |
| 81 | + "hash_code": "8dbaad23d4056f0a97bb8f487795fe392b4124f28d4049d16430e43415f1c219", |
| 82 | + "line": null, |
| 83 | + "file_path": null, |
| 84 | + "component_name": null, |
| 85 | + "component_version": null, |
| 86 | + "static_finding": false, |
| 87 | + "dynamic_finding": true, |
| 88 | + "created": "2021-07-21T12:43:36.549669Z", |
| 89 | + "scanner_confidence": null, |
| 90 | + "unique_id_from_tool": null, |
| 91 | + "vuln_id_from_tool": null, |
| 92 | + "sast_source_object": null, |
| 93 | + "sast_sink_object": null, |
| 94 | + "sast_source_line": null, |
| 95 | + "sast_source_file_path": null, |
| 96 | + "nb_occurences": null, |
| 97 | + "publish_date": null, |
| 98 | + "test": 222, |
| 99 | + "duplicate_finding": null, |
| 100 | + "review_requested_by": null, |
| 101 | + "defect_review_requested_by": null, |
| 102 | + "mitigated_by": null, |
| 103 | + "reporter": 5, |
| 104 | + "last_reviewed_by": 5, |
| 105 | + "sonarqube_issue": null, |
| 106 | + "endpoints": [ |
| 107 | + 875 |
| 108 | + ], |
| 109 | + "endpoint_status": [ |
| 110 | + 8640 |
| 111 | + ], |
| 112 | + "reviewers": [], |
| 113 | + "notes": [], |
| 114 | + "files": [], |
| 115 | + "found_by": [ |
| 116 | + 132 |
| 117 | + ] |
| 118 | + } |
| 119 | + ], |
| 120 | + "prefetch": {} |
| 121 | + } |
| 122 | + """; |
24 | 123 |
|
25 | | - Config config; |
26 | | - FindingService underTest; |
27 | | - MockRestServiceServer mockServer; |
| 124 | + private final Config config = new Config("https://defectdojo.example.com", "abc", 42); |
| 125 | + private final FindingService sut = new FindingService(config); |
| 126 | + private final MockRestServiceServer server = MockRestServiceServer.createServer(sut.getRestTemplate()); |
28 | 127 |
|
29 | | - String findingResponse = """ |
30 | | - { |
31 | | - "count": 1, |
32 | | - "next": null, |
33 | | - "previous": null, |
34 | | - "results": |
35 | | - [ |
36 | | - { |
37 | | - "id": 42, |
38 | | - "tags": [], |
39 | | - "request_response": { |
40 | | - "req_resp": [] |
41 | | - }, |
42 | | - "accepted_risks": [], |
43 | | - "push_to_jira": false, |
44 | | - "age": 145, |
45 | | - "sla_days_remaining": null, |
46 | | - "finding_meta": [], |
47 | | - "related_fields": null, |
48 | | - "jira_creation": null, |
49 | | - "jira_change": null, |
50 | | - "display_status": "Active, Verified", |
51 | | - "finding_groups": [], |
52 | | - "title": "Open Port: 9929/TCP", |
53 | | - "date": "2021-03-18", |
54 | | - "sla_start_date": null, |
55 | | - "cwe": 0, |
56 | | - "cve": null, |
57 | | - "cvssv3": null, |
58 | | - "cvssv3_score": null, |
59 | | - "url": null, |
60 | | - "severity": "Info", |
61 | | - "description": "### Host\\n\\n**IP Address:** 198.51.100.0\\n**FQDN:** scanme.nmap.org\\n\\n\\n**Port/Protocol:** 9929/tcp\\n\\n\\n\\n\\n", |
62 | | - "mitigation": "N/A", |
63 | | - "impact": "No impact provided", |
64 | | - "steps_to_reproduce": null, |
65 | | - "severity_justification": null, |
66 | | - "references": null, |
67 | | - "is_template": false, |
68 | | - "active": true, |
69 | | - "verified": true, |
70 | | - "false_p": false, |
71 | | - "duplicate": false, |
72 | | - "out_of_scope": false, |
73 | | - "risk_accepted": false, |
74 | | - "under_review": false, |
75 | | - "last_status_update": "2021-07-21T12:43:36.628994Z", |
76 | | - "under_defect_review": false, |
77 | | - "is_mitigated": false, |
78 | | - "thread_id": 0, |
79 | | - "mitigated": null, |
80 | | - "numerical_severity": "S4", |
81 | | - "last_reviewed": "2021-07-21T12:43:36.545348Z", |
82 | | - "line_number": null, |
83 | | - "sourcefilepath": null, |
84 | | - "sourcefile": null, |
85 | | - "param": null, |
86 | | - "payload": null, |
87 | | - "hash_code": "8dbaad23d4056f0a97bb8f487795fe392b4124f28d4049d16430e43415f1c219", |
88 | | - "line": null, |
89 | | - "file_path": null, |
90 | | - "component_name": null, |
91 | | - "component_version": null, |
92 | | - "static_finding": false, |
93 | | - "dynamic_finding": true, |
94 | | - "created": "2021-07-21T12:43:36.549669Z", |
95 | | - "scanner_confidence": null, |
96 | | - "unique_id_from_tool": null, |
97 | | - "vuln_id_from_tool": null, |
98 | | - "sast_source_object": null, |
99 | | - "sast_sink_object": null, |
100 | | - "sast_source_line": null, |
101 | | - "sast_source_file_path": null, |
102 | | - "nb_occurences": null, |
103 | | - "publish_date": null, |
104 | | - "test": 222, |
105 | | - "duplicate_finding": null, |
106 | | - "review_requested_by": null, |
107 | | - "defect_review_requested_by": null, |
108 | | - "mitigated_by": null, |
109 | | - "reporter": 5, |
110 | | - "last_reviewed_by": 5, |
111 | | - "sonarqube_issue": null, |
112 | | - "endpoints": [ |
113 | | - 875 |
114 | | - ], |
115 | | - "endpoint_status": [ |
116 | | - 8640 |
117 | | - ], |
118 | | - "reviewers": [], |
119 | | - "notes": [], |
120 | | - "files": [], |
121 | | - "found_by": [ |
122 | | - 132 |
123 | | - ] |
124 | | - } |
125 | | - ], |
126 | | - "prefetch": {} |
127 | | - } |
128 | | - """; |
129 | | - |
130 | | - @BeforeEach |
131 | | - void setup() { |
132 | | - config = new Config("https://defectdojo.example.com", "abc", 42); |
133 | | - underTest = new FindingService(config); |
134 | | - mockServer = MockRestServiceServer.createServer(underTest.getRestTemplate()); |
135 | | - } |
136 | 128 |
|
137 | 129 | @Test |
138 | | - void deserializeList() throws JsonProcessingException { |
139 | | - var foo = underTest.deserializeList(findingResponse); |
| 130 | + void deserializeList_shouldResultExactlyOneResult() throws JsonProcessingException { |
| 131 | + var result = sut.deserializeList(FINDING_RESPONSE); |
140 | 132 |
|
141 | | - assertEquals(1, foo.getCount()); |
| 133 | + assertEquals(1, result.getCount()); |
142 | 134 | } |
143 | 135 |
|
144 | 136 | @Test |
145 | | - void testSearch() throws JsonProcessingException, URISyntaxException { |
146 | | - var url = config.getUrl() + "/api/v2/" + underTest.getUrlPath() + "/?offset=0&limit=100"; |
147 | | - mockServer.expect(requestTo(url)).andRespond(withSuccess(findingResponse, MediaType.APPLICATION_JSON)); |
148 | | - |
149 | | - var expected = underTest.deserializeList(findingResponse).getResults(); |
150 | | - var actual = underTest.search(); |
| 137 | + void search() throws JsonProcessingException, URISyntaxException { |
| 138 | + final var url = String.format("%s/api/v2/%s/?offset=0&limit=100", config.getUrl(), sut.getUrlPath()); |
| 139 | + server.expect(requestTo(url)) |
| 140 | + .andRespond(withSuccess(FINDING_RESPONSE, MediaType.APPLICATION_JSON)); |
151 | 141 |
|
152 | | - mockServer.verify(); |
153 | | - assertIterableEquals(expected, actual); |
| 142 | + assertIterableEquals(sut.deserializeList(FINDING_RESPONSE).getResults(), sut.search()); |
| 143 | + server.verify(); |
154 | 144 | } |
155 | 145 | } |
0 commit comments