|
| 1 | +/* |
| 2 | + * Zed Attack Proxy (ZAP) and its related class files. |
| 3 | + * |
| 4 | + * ZAP is an HTTP/HTTPS proxy for assessing web application security. |
| 5 | + * |
| 6 | + * Copyright 2025 The ZAP Development Team |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * you may not use this file except in compliance with the License. |
| 10 | + * You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + */ |
| 20 | +package org.zaproxy.addon.llm.services; |
| 21 | + |
| 22 | +import static org.mockito.ArgumentMatchers.anyString; |
| 23 | +import static org.mockito.BDDMockito.given; |
| 24 | +import static org.mockito.Mockito.mock; |
| 25 | +import static org.mockito.Mockito.verify; |
| 26 | + |
| 27 | +import org.junit.jupiter.api.BeforeAll; |
| 28 | +import org.junit.jupiter.api.Disabled; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | +import org.junit.jupiter.params.ParameterizedTest; |
| 31 | +import org.junit.jupiter.params.provider.EmptySource; |
| 32 | +import org.junit.jupiter.params.provider.ValueSource; |
| 33 | +import org.parosproxy.paros.Constant; |
| 34 | +import org.parosproxy.paros.core.scanner.Alert; |
| 35 | +import org.zaproxy.addon.llm.communication.Confidence; |
| 36 | +import org.zaproxy.zap.testutils.TestUtils; |
| 37 | +import org.zaproxy.zap.utils.I18N; |
| 38 | + |
| 39 | +/** Unit test for {@link LlmCommunicationService}. */ |
| 40 | +class LlmCommunicationServiceUnitTest extends TestUtils { |
| 41 | + |
| 42 | + private static final Confidence CONFIDENCE = |
| 43 | + new Confidence(Alert.CONFIDENCE_MEDIUM, "explanation"); |
| 44 | + |
| 45 | + @BeforeAll |
| 46 | + static void beforeAll() { |
| 47 | + Constant.messages = mock(I18N.class); |
| 48 | + } |
| 49 | + |
| 50 | + @Disabled("Pending zaproxy/zap-extensions#6651") |
| 51 | + @ParameterizedTest |
| 52 | + @EmptySource |
| 53 | + @ValueSource(strings = {" ", "\t", "\r", "\n"}) |
| 54 | + void shouldUseTwoParamReviewMethodWhenNoOtherInfo(String otherInfo) { |
| 55 | + // Given |
| 56 | + LlmAssistant assistant = mock(); |
| 57 | + LlmCommunicationService service = new LlmCommunicationService(assistant); |
| 58 | + |
| 59 | + Alert alert = createBaseAlert(); |
| 60 | + alert.setOtherInfo(otherInfo); |
| 61 | + |
| 62 | + given(assistant.review(anyString(), anyString())).willReturn(CONFIDENCE); |
| 63 | + // When |
| 64 | + service.reviewAlert(alert); |
| 65 | + // Then |
| 66 | + verify(assistant).review(anyString(), anyString()); |
| 67 | + } |
| 68 | + |
| 69 | + @Disabled("Pending zaproxy/zap-extensions#6651") |
| 70 | + @Test |
| 71 | + void shouldUseThreeParamReviewMethodWhenHasOtherInfo() { |
| 72 | + // Given |
| 73 | + LlmAssistant assistant = mock(); |
| 74 | + LlmCommunicationService service = new LlmCommunicationService(assistant); |
| 75 | + |
| 76 | + Alert alert = createBaseAlert(); |
| 77 | + alert.setOtherInfo("other info"); |
| 78 | + |
| 79 | + given(assistant.review(anyString(), anyString(), anyString())).willReturn(CONFIDENCE); |
| 80 | + // When |
| 81 | + service.reviewAlert(alert); |
| 82 | + // Then |
| 83 | + verify(assistant).review(anyString(), anyString(), anyString()); |
| 84 | + } |
| 85 | + |
| 86 | + private static Alert createBaseAlert() { |
| 87 | + return Alert.builder() |
| 88 | + .setDescription("desc") |
| 89 | + .setEvidence("evidence") |
| 90 | + .setConfidence(Alert.CONFIDENCE_MEDIUM) |
| 91 | + .build(); |
| 92 | + } |
| 93 | +} |
0 commit comments