|
| 1 | +package com.developer.nefarious.zjoule.test.login.api; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.mockito.Mockito.mock; |
| 5 | +import static org.mockito.Mockito.mockStatic; |
| 6 | +import static org.mockito.Mockito.spy; |
| 7 | +import static org.mockito.Mockito.verify; |
| 8 | +import static org.mockito.Mockito.when; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | +import java.net.URI; |
| 12 | +import java.net.http.HttpClient; |
| 13 | +import java.net.http.HttpRequest; |
| 14 | +import java.net.http.HttpRequest.Builder; |
| 15 | +import java.net.http.HttpResponse; |
| 16 | + |
| 17 | +import org.junit.jupiter.api.AfterEach; |
| 18 | +import org.junit.jupiter.api.BeforeEach; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.mockito.Mock; |
| 21 | +import org.mockito.MockedStatic; |
| 22 | +import org.mockito.MockitoAnnotations; |
| 23 | + |
| 24 | +import com.developer.nefarious.zjoule.plugin.login.api.GetOllamaModelsResponse; |
| 25 | +import com.developer.nefarious.zjoule.plugin.login.api.IOllamaLoginClient; |
| 26 | +import com.developer.nefarious.zjoule.plugin.login.api.IOllamaLoginClientHelper; |
| 27 | +import com.developer.nefarious.zjoule.plugin.login.api.OllamaLoginClient; |
| 28 | + |
| 29 | +public class OllamaLoginClientTest { |
| 30 | + |
| 31 | + private IOllamaLoginClient cut; |
| 32 | + |
| 33 | + MockedStatic<HttpClient> mockedStaticHttpClient; |
| 34 | + |
| 35 | + MockedStatic<HttpRequest> mockedStaticHttpRequest; |
| 36 | + |
| 37 | + @Mock |
| 38 | + private HttpClient mockHttpClient; |
| 39 | + |
| 40 | + @Mock |
| 41 | + private IOllamaLoginClientHelper mockOllamaLoginClientHelper; |
| 42 | + |
| 43 | + @Mock |
| 44 | + private Builder mockBuilder; |
| 45 | + |
| 46 | + @Mock |
| 47 | + private HttpRequest mockHttpRequest; |
| 48 | + |
| 49 | + @Mock |
| 50 | + private HttpResponse mockHttpResponse; |
| 51 | + |
| 52 | + @BeforeEach |
| 53 | + public void setUp() { |
| 54 | + MockitoAnnotations.openMocks(this); |
| 55 | + |
| 56 | + mockedStaticHttpClient = mockStatic(HttpClient.class); |
| 57 | + mockedStaticHttpClient.when(HttpClient::newHttpClient).thenReturn(mockHttpClient); |
| 58 | + |
| 59 | + mockedStaticHttpRequest = mockStatic(HttpRequest.class); |
| 60 | + mockedStaticHttpRequest.when(HttpRequest::newBuilder).thenReturn(mockBuilder); |
| 61 | + |
| 62 | + cut = spy(new OllamaLoginClient(mockOllamaLoginClientHelper)); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void shouldPlumbModels() throws IOException, InterruptedException { |
| 67 | + // Arrange |
| 68 | + GetOllamaModelsResponse expectedValue = mock(GetOllamaModelsResponse.class); |
| 69 | + |
| 70 | + String mockOllamaUrl = "https://some-url.com"; |
| 71 | + |
| 72 | + String mockEndpointInStringFormat = mockOllamaUrl + "/api/tags"; |
| 73 | + URI mockEndpointInURIFormat = mock(URI.class); |
| 74 | + when(mockOllamaLoginClientHelper.createUri(mockEndpointInStringFormat)).thenReturn(mockEndpointInURIFormat); |
| 75 | + when(mockBuilder.uri(mockEndpointInURIFormat)).thenReturn(mockBuilder); |
| 76 | + |
| 77 | + when(mockBuilder.GET()).thenReturn(mockBuilder); |
| 78 | + when(mockBuilder.build()).thenReturn(mockHttpRequest); |
| 79 | + when(mockHttpClient.send(mockHttpRequest, HttpResponse.BodyHandlers.ofString())).thenReturn(mockHttpResponse); |
| 80 | + |
| 81 | + String mockResponseBody = "response-content"; |
| 82 | + when(mockHttpResponse.body()).thenReturn(mockResponseBody); |
| 83 | + when(mockOllamaLoginClientHelper.parseOllamaModelsResponseToObject(mockResponseBody)).thenReturn(expectedValue); |
| 84 | + |
| 85 | + // Act |
| 86 | + GetOllamaModelsResponse returnValue = cut.getModels(mockOllamaUrl); |
| 87 | + |
| 88 | + // Assert |
| 89 | + assertEquals(expectedValue, returnValue); |
| 90 | + verify(mockBuilder).uri(mockEndpointInURIFormat); |
| 91 | + verify(mockBuilder).GET(); |
| 92 | + } |
| 93 | + |
| 94 | + @AfterEach |
| 95 | + public void tearDown() { |
| 96 | + if (mockedStaticHttpClient != null) { |
| 97 | + mockedStaticHttpClient.close(); |
| 98 | + } |
| 99 | + if (mockedStaticHttpRequest != null) { |
| 100 | + mockedStaticHttpRequest.close(); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments