-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathPineconeBuilderTest.java
More file actions
145 lines (114 loc) · 6.07 KB
/
PineconeBuilderTest.java
File metadata and controls
145 lines (114 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package io.pinecone;
import io.pinecone.exceptions.PineconeConfigurationException;
import io.pinecone.clients.Pinecone;
import org.mockito.ArgumentCaptor;
import org.openapitools.db_control.client.model.*;
import okhttp3.*;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.AbstractMap;
import static io.pinecone.commons.Constants.pineconeClientVersion;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
public class PineconeBuilderTest {
private static AbstractMap.SimpleEntry<Call, OkHttpClient> buildMockCallAndClient(ResponseBody response) throws IOException {
Response mockResponse = new Response.Builder()
.request(new Request.Builder().url("http://localhost").build())
.protocol(Protocol.HTTP_1_1)
.code(200)
.message("OK")
.body(response)
.build();
Call mockCall = mock(Call.class);
when(mockCall.execute()).thenReturn(mockResponse);
OkHttpClient mockClient = mock(OkHttpClient.class);
when(mockClient.newCall(any(Request.class))).thenReturn(mockCall);
return new AbstractMap.SimpleEntry<>(mockCall, mockClient);
}
@Test
public void PineconeWithNullApiKey() {
PineconeConfigurationException exception = assertThrows(PineconeConfigurationException.class, () -> {
new Pinecone.Builder(null).build();
});
assertTrue(exception.getLocalizedMessage().contains("The API key is required and must not be empty or null"));
}
@Test
public void PineconeWithEmptyApiKey() {
PineconeConfigurationException exception = assertThrows(PineconeConfigurationException.class, () -> {
new Pinecone.Builder("").build();
});
assertTrue(exception.getLocalizedMessage().contains("The API key is required and must not be empty or null"));
}
@Test
public void PineconeWithOkHttpClientAndUserAgent() throws IOException {
String filePath = "src/test/resources/serverlessIndexJsonString.json";
String indexJsonStringServerless = new String(Files.readAllBytes(Paths.get(filePath)));
AbstractMap.SimpleEntry<Call, OkHttpClient> mockCallAndClient =
buildMockCallAndClient(ResponseBody.create(
indexJsonStringServerless,
MediaType.parse("application/json")));
OkHttpClient mockClient = mockCallAndClient.getValue();
Pinecone client = new Pinecone.Builder("testAPiKey")
.withOkHttpClient(mockClient)
.build();
// Create expected index after client creation to ensure JSON class is initialized
IndexModel expectedIndex = IndexModel.fromJson(indexJsonStringServerless);
IndexModel index = client.describeIndex("testIndex");
ArgumentCaptor<Request> requestCaptor = ArgumentCaptor.forClass(Request.class);
assertEquals(expectedIndex, index);
verify(mockClient, times(1)).newCall(requestCaptor.capture());
assertEquals("lang=java; pineconeClientVersion=" + pineconeClientVersion, requestCaptor.getValue().header("User-Agent"));
}
@Test
public void PineconeWithSourceTag() throws IOException {
String filePath = "src/test/resources/serverlessIndexJsonString.json";
String indexJsonStringServerless = new String(Files.readAllBytes(Paths.get(filePath)));
AbstractMap.SimpleEntry<Call, OkHttpClient> mockCallAndClient =
buildMockCallAndClient(ResponseBody.create(
indexJsonStringServerless,
MediaType.parse("application/json")));
OkHttpClient mockClient = mockCallAndClient.getValue();
Pinecone client = new Pinecone.Builder("testAPiKey")
.withSourceTag("testSourceTag")
.withOkHttpClient(mockClient)
.build();
// Create expected index after client creation to ensure JSON class is initialized
IndexModel expectedIndex = IndexModel.fromJson(indexJsonStringServerless);
IndexModel index = client.describeIndex("testIndex");
ArgumentCaptor<Request> requestCaptor = ArgumentCaptor.forClass(Request.class);
assertEquals(expectedIndex, index);
verify(mockClient, times(1)).newCall(requestCaptor.capture());
assertEquals("lang=java; pineconeClientVersion=" + pineconeClientVersion + "; source_tag=testSourceTag", requestCaptor.getValue().header("User-Agent"));
}
@Test
public void PineconeWithHostAndCustomOkHttpClient() {
String customHost = "http://localhost:5080";
OkHttpClient customClient = new OkHttpClient.Builder().build();
// Verify that the builder doesn't throw an exception when both host and custom client are set
Pinecone client = new Pinecone.Builder("testApiKey")
.withHost(customHost)
.withOkHttpClient(customClient)
.build();
assertNotNull(client, "Pinecone client should be created successfully with both host and custom OkHttpClient");
}
@Test
public void PineconeWithHostButNoCustomOkHttpClient() {
String customHost = "http://localhost:5080";
// Verify that the builder doesn't throw an exception when host is set without custom client
Pinecone client = new Pinecone.Builder("testApiKey")
.withHost(customHost)
.build();
assertNotNull(client, "Pinecone client should be created successfully with host but no custom OkHttpClient");
}
@Test
public void PineconeWithCustomOkHttpClientButNoHost() {
OkHttpClient customClient = new OkHttpClient.Builder().build();
// Verify that the builder doesn't throw an exception when custom client is set without host
Pinecone client = new Pinecone.Builder("testApiKey")
.withOkHttpClient(customClient)
.build();
assertNotNull(client, "Pinecone client should be created successfully with custom OkHttpClient but no host");
}
}