Skip to content

Commit 6666d0f

Browse files
authored
[CH10267] Add ability to set apiKey after instance has been defined (setter) (#40)
* Provide setter for apiKey. * Add test for multiple instances of client. * Wrap in try/catch to allow exception. * Add set key test for autocomplete, browse, recommendations + code review feedback. * No return for key setter.
1 parent e003a98 commit 6666d0f

File tree

6 files changed

+77
-0
lines changed

6 files changed

+77
-0
lines changed

constructorio-client/src/main/java/io/constructor/client/ConstructorIO.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ public ConstructorIO(String apiToken, String apiKey, boolean isHTTPS, String hos
109109
this(apiToken, apiKey, isHTTPS, host, null);
110110
}
111111

112+
/**
113+
* Sets apiKey
114+
*/
115+
public void setApiKey(String apiKey) {
116+
this.apiKey = apiKey;
117+
}
118+
112119
/**
113120
* Verifies that an autocomplete service is working.
114121
*

constructorio-client/src/test/java/io/constructor/client/ConstructorIOAutocompleteTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,16 @@ public void autocompleteShouldReturnAResultWithSearchSuggestionsOnly() throws Ex
8282
assertEquals("autocomplete search suggestions exist", response.getSections().get("Search Suggestions").size(), 1);
8383
assertTrue("autocomplete result id exists", response.getResultId() != null);
8484
}
85+
86+
@Test
87+
public void autocompleteShouldReturnAResultWithNewApiKeySet() throws Exception {
88+
ConstructorIO constructor = new ConstructorIO("", "thiskeydoesnotexist", true, null);
89+
constructor.setApiKey("ZqXaOfXuBWD4s3XzCI1q");
90+
AutocompleteRequest request = new AutocompleteRequest("Stanley");
91+
request.getResultsPerSection().put("Search Suggestions", 10);
92+
AutocompleteResponse response = constructor.autocomplete(request, null);
93+
assertEquals("autocomplete product suggestions exist", response.getSections().get("Products").size(), 0);
94+
assertEquals("autocomplete search suggestions exist", response.getSections().get("Search Suggestions").size(), 1);
95+
assertTrue("autocomplete result id exists", response.getResultId() != null);
96+
}
8597
}

constructorio-client/src/test/java/io/constructor/client/ConstructorIOBasicTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,28 @@ public void newShouldSetHostnameDefault() throws Exception {
4949
assertEquals("host should be set to default", constructor.host, "ac.cnstrc.com");
5050
}
5151

52+
@Test
53+
public void shouldSetNewApiKey() throws Exception {
54+
ConstructorIO constructor = new ConstructorIO("boinkaToken", "doinkaKey", false, null);
55+
assertEquals("key should be set to default", constructor.apiKey, "doinkaKey");
56+
constructor.setApiKey("foobar");
57+
assertEquals("key should be set to new value", constructor.apiKey, "foobar");
58+
}
59+
60+
@Test
61+
public void shouldSetNewApiKeyMultipleInstances() throws Exception {
62+
ConstructorIO constructorA = new ConstructorIO("boinkaToken", "foo", false, null);
63+
ConstructorIO constructorB = new ConstructorIO("boinkaToken", "bar", false, null);
64+
assertEquals("instance 1 key should be set to default", constructorA.apiKey, "foo");
65+
assertEquals("instance 2 key should be set to default", constructorB.apiKey, "bar");
66+
constructorA.setApiKey("newfoo");
67+
assertEquals("instance 1 key should be set to new value", constructorA.apiKey, "newfoo");
68+
assertEquals("instance 2 key should be set to default", constructorB.apiKey, "bar");
69+
constructorB.setApiKey("newbar");
70+
assertEquals("instance 1 key should be set to new value", constructorA.apiKey, "newfoo");
71+
assertEquals("instance 2 key should be set to new value", constructorB.apiKey, "newbar");
72+
}
73+
5274
@Test
5375
public void makeAuthorizedRequestBuilderShouldSetAuthorizationHeader() throws Exception {
5476
ConstructorIO constructor = new ConstructorIO("boinkaToken", "doinkaKey", true, null);

constructorio-client/src/test/java/io/constructor/client/ConstructorIOBrowseTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,15 @@ public void BrowseShouldReturnAResultWithCollectionId() throws Exception {
111111
assertEquals("browse results count as expected", (int)response.getResponse().getTotalNumberOfResults(), 999);
112112
assertTrue("browse result id exists", response.getResultId() != null);
113113
}
114+
115+
@Test
116+
public void BrowseShouldReturnAResultWithNewApiKeySet() throws Exception {
117+
ConstructorIO constructor = new ConstructorIO("", "thiskeydoesnotexist", true, "betaac.cnstrc.com");
118+
constructor.setApiKey("key_aXLmVpkVp4BX21Sw");
119+
BrowseRequest request = new BrowseRequest("collection_id", "fresh-deals");
120+
BrowseResponse response = constructor.browse(request, null);
121+
assertEquals("browse results exist", response.getResponse().getResults().size(), 30);
122+
assertEquals("browse results count as expected", (int)response.getResponse().getTotalNumberOfResults(), 999);
123+
assertTrue("browse result id exists", response.getResultId() != null);
124+
}
114125
}

constructorio-client/src/test/java/io/constructor/client/ConstructorIORecommendationsTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,17 @@ public void getRecommendationsShouldReturnAResultWithNumResults() throws Excepti
6161
assertEquals("recommendation results exist", response.getResponse().getResults().size(), 5);
6262
assertTrue("recommendation result id exists", response.getResultId() != null);
6363
}
64+
65+
@Test
66+
public void getRecommendationsShouldReturnAResultWithNewApiKeySet() throws Exception {
67+
ConstructorIO constructor = new ConstructorIO("", "thiskeydoesnotexist", true, null);
68+
constructor.setApiKey("ZqXaOfXuBWD4s3XzCI1q");
69+
UserInfo userInfo = new UserInfo(3, "c62a-2a09-faie");
70+
RecommendationsRequest request = new RecommendationsRequest("item_page_1");
71+
request.setItemIds(Arrays.asList("power_drill", "drill"));
72+
request.setNumResults(5);
73+
RecommendationsResponse response = constructor.recommendations(request, userInfo);
74+
assertEquals("recommendation results exist", response.getResponse().getResults().size(), 5);
75+
assertTrue("recommendation result id exists", response.getResultId() != null);
76+
}
6477
}

constructorio-client/src/test/java/io/constructor/client/ConstructorIOSearchTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,16 @@ public void SearchShouldReturnAResultWithCollectionId() throws Exception {
125125
assertEquals("search results count as expected", (int)response.getResponse().getTotalNumberOfResults(), 33);
126126
assertTrue("search result id exists", response.getResultId() != null);
127127
}
128+
129+
@Test
130+
public void SearchShouldReturnAResultWithNewApiKeySet() throws Exception {
131+
ConstructorIO constructor = new ConstructorIO("", "thiskeydoesnotexist", true, "betaac.cnstrc.com");
132+
constructor.setApiKey("key_aXLmVpkVp4BX21Sw");
133+
SearchRequest request = new SearchRequest("bananas");
134+
request.setCollectionId("fresh-deals");
135+
SearchResponse response = constructor.search(request, null);
136+
assertEquals("search results exist", response.getResponse().getResults().size(), 30);
137+
assertEquals("search results count as expected", (int)response.getResponse().getTotalNumberOfResults(), 33);
138+
assertTrue("search result id exists", response.getResultId() != null);
139+
}
128140
}

0 commit comments

Comments
 (0)