Skip to content

Commit 17833f8

Browse files
authored
[CI-3701] Add Support for deleting facet configurations (#153)
* Delete Facet Configuration * lint
1 parent c0cf1bc commit 17833f8

File tree

2 files changed

+145
-13
lines changed

2 files changed

+145
-13
lines changed

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2893,4 +2893,49 @@ public String createFacetConfiguration(FacetConfigurationRequest facetConfigurat
28932893
throw new ConstructorException(exception);
28942894
}
28952895
}
2896+
2897+
/**
2898+
* Delete a facet configuration
2899+
*
2900+
* @param facetName the facet name
2901+
* @param section the section to which the facet belongs
2902+
* @return returns the deleted facet
2903+
* @throws ConstructorException if the request is invalid.
2904+
*/
2905+
public String deleteFacetConfiguration(String facetName, String section)
2906+
throws ConstructorException {
2907+
try {
2908+
HttpUrl url =
2909+
this.makeUrl(Arrays.asList("v1", "facets", facetName))
2910+
.newBuilder()
2911+
.addQueryParameter("section", section)
2912+
.build();
2913+
2914+
Request request = this.makeAuthorizedRequestBuilder().url(url).delete().build();
2915+
2916+
Response response = client.newCall(request).execute();
2917+
2918+
return getResponseBody(response);
2919+
} catch (Exception exception) {
2920+
throw new ConstructorException(exception);
2921+
}
2922+
}
2923+
2924+
public String deleteFacetConfiguration(String facetName) throws ConstructorException {
2925+
return deleteFacetConfiguration(facetName, "Products");
2926+
}
2927+
2928+
/**
2929+
* Delete a facet configuration
2930+
*
2931+
* @param facetConfigurationRequest the facetConfiguration request
2932+
* @return returns the deleted facet
2933+
* @throws ConstructorException if the request is invalid.
2934+
*/
2935+
public String deleteFacetConfiguration(FacetConfigurationRequest facetConfigurationRequest)
2936+
throws ConstructorException {
2937+
return deleteFacetConfiguration(
2938+
facetConfigurationRequest.geFacetConfiguration().getName(),
2939+
facetConfigurationRequest.getSection());
2940+
}
28962941
}

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

Lines changed: 100 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,49 @@
44

55
import com.google.gson.Gson;
66
import io.constructor.client.models.FacetConfiguration;
7+
import java.util.ArrayList;
78
import org.json.JSONObject;
8-
import org.junit.After;
9-
import org.junit.Before;
9+
import org.junit.AfterClass;
1010
import org.junit.Rule;
1111
import org.junit.Test;
1212
import org.junit.rules.ExpectedException;
1313

1414
public class ConstructorIOFacetConfigurationTest {
1515

16-
private String token = System.getenv("TEST_API_TOKEN");
17-
private String apiKey = System.getenv("TEST_CATALOG_API_KEY");
16+
private static String token = System.getenv("TEST_API_TOKEN");
17+
private static String apiKey = System.getenv("TEST_CATALOG_API_KEY");
18+
private static ArrayList<String> facetsToCleanup = new ArrayList<>();
1819

19-
@Rule public ExpectedException thrown = ExpectedException.none();
20+
private void addFacetToCleanupArray(String facetName, String section) {
21+
if (section == null) {
22+
section = "Products";
23+
}
24+
facetsToCleanup.add(facetName + "|" + section);
25+
}
2026

21-
@Before
22-
public void init() throws Exception {
23-
// TODO: Add facet configuration cleanup after deleteFacetConfiguration function
24-
// has been implemented
27+
private void addFacetToCleanupArray(String facetName) {
28+
addFacetToCleanupArray(facetName, "Products");
2529
}
2630

27-
@After
28-
public void teardown() throws Exception {
29-
// TODO: Add facet configuration cleanup after deleteFacetConfiguration function
30-
// has been implemented
31+
@AfterClass
32+
public static void cleanupFacets() throws ConstructorException {
33+
ConstructorIO constructor = new ConstructorIO(token, apiKey, true, null);
34+
35+
for (String facet : facetsToCleanup) {
36+
String[] parts = facet.split("\\|");
37+
String facetName = parts[0];
38+
String section = parts[1];
39+
40+
try {
41+
constructor.deleteFacetConfiguration(facetName, section);
42+
} catch (ConstructorException e) {
43+
System.err.println("Warning: Failed to clean up facet: " + facetName);
44+
}
45+
}
3146
}
3247

48+
@Rule public ExpectedException thrown = ExpectedException.none();
49+
3350
@Test
3451
public void CreateFacetConfigurationShouldReturn() throws Exception {
3552
ConstructorIO constructor = new ConstructorIO(token, apiKey, true, null);
@@ -56,6 +73,7 @@ public void CreateFacetConfigurationShouldReturn() throws Exception {
5673
assertEquals(jsonObj.get("protected"), loadedJsonObj.get("protected"));
5774
assertEquals(jsonObj.get("countable"), loadedJsonObj.get("countable"));
5875
assertEquals(jsonObj.get("options_limit"), loadedJsonObj.get("options_limit"));
76+
addFacetToCleanupArray("testFacet");
5977
}
6078

6179
@Test(expected = ConstructorException.class)
@@ -73,6 +91,7 @@ public void testCreateFacetConfigurationWithEmptySection() throws Exception {
7391
FacetConfigurationRequest request = new FacetConfigurationRequest(config, "");
7492

7593
constructor.createFacetConfiguration(request);
94+
addFacetToCleanupArray("emptySection");
7695
}
7796

7897
@Test(expected = IllegalArgumentException.class)
@@ -98,5 +117,73 @@ public void testCreateFacetConfigurationWithDifferentSection() throws Exception
98117
JSONObject jsonObj = new JSONObject(response);
99118

100119
assertEquals("brand", jsonObj.getString("name"));
120+
addFacetToCleanupArray("brand", "Search Suggestions");
121+
}
122+
123+
@Test
124+
public void testDeleteFacetConfigurationWithFacetNameAndSection() throws Exception {
125+
ConstructorIO constructor = new ConstructorIO(token, apiKey, true, null);
126+
127+
// Create a facet first
128+
String string = Utils.getTestResource("facet.configuration.json");
129+
FacetConfiguration facetConfig = new Gson().fromJson(string, FacetConfiguration.class);
130+
facetConfig.setName("testDeleteFacet");
131+
132+
constructor.createFacetConfiguration(
133+
new FacetConfigurationRequest(facetConfig, "Products"));
134+
135+
// Delete the facet
136+
String deleteResponse = constructor.deleteFacetConfiguration("testDeleteFacet", "Products");
137+
JSONObject jsonObj = new JSONObject(deleteResponse);
138+
139+
assertTrue("Deleted facet name matches", jsonObj.get("name").equals("testDeleteFacet"));
140+
}
141+
142+
@Test
143+
public void testDeleteFacetConfigurationWithDefaultSection() throws Exception {
144+
ConstructorIO constructor = new ConstructorIO(token, apiKey, true, null);
145+
146+
// Create a facet first
147+
String string = Utils.getTestResource("facet.configuration.json");
148+
FacetConfiguration facetConfig = new Gson().fromJson(string, FacetConfiguration.class);
149+
facetConfig.setName("testDefaultSectionFacet");
150+
151+
constructor.createFacetConfiguration(
152+
new FacetConfigurationRequest(facetConfig, "Products"));
153+
154+
// Delete the facet
155+
String deleteResponse = constructor.deleteFacetConfiguration("testDefaultSectionFacet");
156+
JSONObject jsonObj = new JSONObject(deleteResponse);
157+
158+
assertTrue(
159+
"Deleted facet name matches",
160+
jsonObj.get("name").equals("testDefaultSectionFacet"));
161+
}
162+
163+
@Test
164+
public void testDeleteFacetConfigurationWithFacetConfiguration() throws Exception {
165+
ConstructorIO constructor = new ConstructorIO(token, apiKey, true, null);
166+
167+
// Create a facet first
168+
String string = Utils.getTestResource("facet.configuration.json");
169+
FacetConfiguration facetConfig = new Gson().fromJson(string, FacetConfiguration.class);
170+
facetConfig.setName("testDeleteWithFacetConfiguration");
171+
172+
FacetConfigurationRequest request = new FacetConfigurationRequest(facetConfig, "Products");
173+
constructor.createFacetConfiguration(request);
174+
175+
// Delete the facet
176+
String deleteResponse = constructor.deleteFacetConfiguration(request);
177+
JSONObject jsonObj = new JSONObject(deleteResponse);
178+
179+
assertTrue(
180+
"Deleted facet name matches",
181+
jsonObj.get("name").equals("testDeleteWithFacetConfiguration"));
182+
}
183+
184+
@Test(expected = ConstructorException.class)
185+
public void testDeleteNonExistentFacetShouldThrowException() throws Exception {
186+
ConstructorIO constructor = new ConstructorIO(token, apiKey, true, null);
187+
constructor.deleteFacetConfiguration("nonExistentFacet", "Products");
101188
}
102189
}

0 commit comments

Comments
 (0)