|
| 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.never; |
| 26 | +import static org.mockito.Mockito.verify; |
| 27 | +import static org.mockito.Mockito.withSettings; |
| 28 | + |
| 29 | +import java.util.HashMap; |
| 30 | +import org.junit.jupiter.api.BeforeAll; |
| 31 | +import org.junit.jupiter.api.Disabled; |
| 32 | +import org.junit.jupiter.api.Test; |
| 33 | +import org.junit.jupiter.params.ParameterizedTest; |
| 34 | +import org.junit.jupiter.params.provider.EmptySource; |
| 35 | +import org.junit.jupiter.params.provider.NullSource; |
| 36 | +import org.mockito.quality.Strictness; |
| 37 | +import org.parosproxy.paros.Constant; |
| 38 | +import org.parosproxy.paros.core.scanner.Alert; |
| 39 | +import org.zaproxy.addon.llm.communication.Confidence; |
| 40 | +import org.zaproxy.zap.testutils.TestUtils; |
| 41 | +import org.zaproxy.zap.utils.I18N; |
| 42 | + |
| 43 | +/** Unit test for {@link LlmCommunicationServiceUnitTest}. */ |
| 44 | +class LlmCommunicationServiceUnitTest extends TestUtils { |
| 45 | + |
| 46 | + @BeforeAll |
| 47 | + static void beforeAll() { |
| 48 | + Constant.messages = mock(I18N.class); |
| 49 | + } |
| 50 | + |
| 51 | + @Disabled |
| 52 | + @ParameterizedTest |
| 53 | + @NullSource |
| 54 | + @EmptySource |
| 55 | + void shouldUseTwoParamReviewMethodWhenNoOtherInfo(String otherInfo) { |
| 56 | + // Given |
| 57 | + LlmAssistant assistant = |
| 58 | + mock(LlmAssistant.class, withSettings().strictness(Strictness.LENIENT)); |
| 59 | + LlmCommunicationService service = new LlmCommunicationService(); |
| 60 | + service.setAssistant(assistant); |
| 61 | + Alert alert = new Alert(-1); |
| 62 | + alert.setDescription("desc"); |
| 63 | + alert.setEvidence("evidence"); |
| 64 | + alert.setConfidence(Alert.CONFIDENCE_MEDIUM); |
| 65 | + alert.setOtherInfo(otherInfo); |
| 66 | + alert.setTags(new HashMap<String, String>()); |
| 67 | + given(assistant.review(anyString(), anyString())) |
| 68 | + .willReturn(new Confidence(Alert.CONFIDENCE_MEDIUM, "explanation")); |
| 69 | + given(assistant.review(anyString(), anyString(), anyString())) |
| 70 | + .willReturn(new Confidence(Alert.CONFIDENCE_MEDIUM, "explanation")); |
| 71 | + |
| 72 | + // When |
| 73 | + service.reviewAlert(alert); |
| 74 | + // Then |
| 75 | + verify(assistant).review(anyString(), anyString()); |
| 76 | + verify(assistant, never()).review(anyString(), anyString(), anyString()); |
| 77 | + } |
| 78 | + |
| 79 | + @Disabled |
| 80 | + @Test |
| 81 | + void shouldUseThreeParamReviewMethodWhenNoOtherInfo() { |
| 82 | + // Given |
| 83 | + LlmAssistant assistant = |
| 84 | + mock(LlmAssistant.class, withSettings().strictness(Strictness.LENIENT)); |
| 85 | + LlmCommunicationService service = new LlmCommunicationService(); |
| 86 | + service.setAssistant(assistant); |
| 87 | + Alert alert = new Alert(-1); |
| 88 | + alert.setDescription("desc"); |
| 89 | + alert.setEvidence("evidence"); |
| 90 | + alert.setConfidence(Alert.CONFIDENCE_MEDIUM); |
| 91 | + alert.setOtherInfo("other info"); |
| 92 | + alert.setTags(new HashMap<String, String>()); |
| 93 | + given(assistant.review(anyString(), anyString())) |
| 94 | + .willReturn(new Confidence(Alert.CONFIDENCE_MEDIUM, "explanation")); |
| 95 | + given(assistant.review(anyString(), anyString(), anyString())) |
| 96 | + .willReturn(new Confidence(Alert.CONFIDENCE_MEDIUM, "explanation")); |
| 97 | + |
| 98 | + // When |
| 99 | + service.reviewAlert(alert); |
| 100 | + // Then |
| 101 | + verify(assistant).review(anyString(), anyString(), anyString()); |
| 102 | + verify(assistant, never()).review(anyString(), anyString()); |
| 103 | + } |
| 104 | +} |
0 commit comments