11package de .asedem .service ;
22
3+ import de .asedem .HttpTestServer ;
34import de .asedem .Ollama ;
45import de .asedem .exception .OllamaConnectionException ;
56import de .asedem .model .ChatRequest ;
67import de .asedem .model .ChatResponse ;
78import de .asedem .model .Message ;
8- import de .asedem .rest .HttpMethode ;
9- import de .asedem .rest .Rest ;
10- import de .asedem .rest .RestResponse ;
119import org .junit .jupiter .api .Test ;
12- import org .mockito .MockedStatic ;
13- import org .mockito .Mockito ;
1410
15- import java .io .IOException ;
1611import java .util .List ;
1712
1813import static org .junit .jupiter .api .Assertions .*;
@@ -25,31 +20,27 @@ class ChatServiceTest {
2520 );
2621
2722 @ Test
28- void testMethodCall () {
29-
30- final Ollama ollama = Ollama .initDefault ();
31-
32- try (MockedStatic <Rest > utilities = Mockito .mockStatic (Rest .class )) {
33- utilities .when (() -> Rest .requestSync (ollama .buildUrl ("/api/chat" ),
34- HttpMethode .POST , request , 10000 , 30000 ))
35- .thenReturn (new RestResponse (200 , """
36- {
37- "model": "llama3.2",
38- "created_at": "2023-12-12T14:13:43.416799Z",
39- "message": {
40- "role": "assistant",
41- "content": "Hello! How are you today?"
42- },
43- "done": true,
44- "total_duration": 5191566416,
45- "load_duration": 2154458,
46- "prompt_eval_count": 26,
47- "prompt_eval_duration": 383809000,
48- "eval_count": 298,
49- "eval_duration": 4799921000
50- }
51- """ ));
23+ void testMethodCall () throws Exception {
24+ try (HttpTestServer server = new HttpTestServer ()) {
25+ server .setResponse (200 , """
26+ {
27+ "model": "llama3.2",
28+ "created_at": "2023-12-12T14:13:43.416799Z",
29+ "message": {
30+ "role": "assistant",
31+ "content": "Hello! How are you today?"
32+ },
33+ "done": true,
34+ "total_duration": 5191566416,
35+ "load_duration": 2154458,
36+ "prompt_eval_count": 26,
37+ "prompt_eval_duration": 383809000,
38+ "eval_count": 298,
39+ "eval_duration": 4799921000
40+ }
41+ """ );
5242
43+ final Ollama ollama = Ollama .init ("http://127.0.0.1" , server .getPort ());
5344 final ChatResponse response = ollama .chat (request );
5445
5546 assertEquals ("llama3.2" , response .model ());
@@ -58,18 +49,21 @@ void testMethodCall() {
5849 assertTrue (response .done ());
5950 assertEquals (5191566416L , response .totalDuration ());
6051 assertEquals (4799921000L , response .evalDuration ());
52+
53+ assertEquals ("POST" , server .getLastMethod ());
54+ assertEquals ("/api/chat" , server .getLastPath ());
55+ assertTrue (server .getLastBody ().contains ("\" model\" :\" llama3.2\" " ));
56+ assertTrue (server .getLastBody ().contains ("\" stream\" :false" ));
6157 }
6258 }
6359
6460 @ Test
65- void testException () {
66-
67- final Ollama ollama = Ollama .initDefault ();
61+ void testExceptionOnConnectionFailure () throws Exception {
62+ try (HttpTestServer server = new HttpTestServer ()) {
63+ final int port = server .getPort ();
64+ server .close ();
6865
69- try (MockedStatic <Rest > utilities = Mockito .mockStatic (Rest .class )) {
70- utilities .when (() -> Rest .requestSync (ollama .buildUrl ("/api/chat" ),
71- HttpMethode .POST , request , 10000 , 30000 ))
72- .thenThrow (new IOException ());
66+ final Ollama ollama = Ollama .init ("http://127.0.0.1" , port );
7367
7468 assertThrows (OllamaConnectionException .class , () -> ollama .chat (request ));
7569 }
0 commit comments