Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete Facet Configuration #153

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2893,4 +2893,49 @@ public String createFacetConfiguration(FacetConfigurationRequest facetConfigurat
throw new ConstructorException(exception);
}
}

/**
* Delete a facet configuration
*
* @param facetName the facet name
* @param section the section to which the facet belongs
* @return returns the deleted facet
* @throws ConstructorException if the request is invalid.
*/
public String deleteFacetConfiguration(String facetName, String section)
throws ConstructorException {
try {
HttpUrl url =
this.makeUrl(Arrays.asList("v1", "facets", facetName))
.newBuilder()
.addQueryParameter("section", section)
.build();

Request request = this.makeAuthorizedRequestBuilder().url(url).delete().build();

Response response = client.newCall(request).execute();

return getResponseBody(response);
} catch (Exception exception) {
throw new ConstructorException(exception);
}
}

public String deleteFacetConfiguration(String facetName) throws ConstructorException {
return deleteFacetConfiguration(facetName, "Products");
}

/**
* Delete a facet configuration
*
* @param facetConfigurationRequest the facetConfiguration request
* @return returns the deleted facet
* @throws ConstructorException if the request is invalid.
*/
public String deleteFacetConfiguration(FacetConfigurationRequest facetConfigurationRequest)
throws ConstructorException {
return deleteFacetConfiguration(
facetConfigurationRequest.geFacetConfiguration().getName(),
facetConfigurationRequest.getSection());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,49 @@

import com.google.gson.Gson;
import io.constructor.client.models.FacetConfiguration;
import java.util.ArrayList;
import org.json.JSONObject;
import org.junit.After;
import org.junit.Before;
import org.junit.AfterClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class ConstructorIOFacetConfigurationTest {

private String token = System.getenv("TEST_API_TOKEN");
private String apiKey = System.getenv("TEST_CATALOG_API_KEY");
private static String token = System.getenv("TEST_API_TOKEN");
private static String apiKey = System.getenv("TEST_CATALOG_API_KEY");
private static ArrayList<String> facetsToCleanup = new ArrayList<>();

@Rule public ExpectedException thrown = ExpectedException.none();
private void addFacetToCleanupArray(String facetName, String section) {
if (section == null) {
section = "Products";
}
facetsToCleanup.add(facetName + "|" + section);
}

@Before
public void init() throws Exception {
// TODO: Add facet configuration cleanup after deleteFacetConfiguration function
// has been implemented
private void addFacetToCleanupArray(String facetName) {
addFacetToCleanupArray(facetName, "Products");
}

@After
public void teardown() throws Exception {
// TODO: Add facet configuration cleanup after deleteFacetConfiguration function
// has been implemented
@AfterClass
public static void cleanupFacets() throws ConstructorException {
ConstructorIO constructor = new ConstructorIO(token, apiKey, true, null);

for (String facet : facetsToCleanup) {
String[] parts = facet.split("\\|");
String facetName = parts[0];
String section = parts[1];

try {
constructor.deleteFacetConfiguration(facetName, section);
} catch (ConstructorException e) {
System.err.println("Warning: Failed to clean up facet: " + facetName);
}
}
}

@Rule public ExpectedException thrown = ExpectedException.none();

@Test
public void CreateFacetConfigurationShouldReturn() throws Exception {
ConstructorIO constructor = new ConstructorIO(token, apiKey, true, null);
Expand All @@ -56,6 +73,7 @@ public void CreateFacetConfigurationShouldReturn() throws Exception {
assertEquals(jsonObj.get("protected"), loadedJsonObj.get("protected"));
assertEquals(jsonObj.get("countable"), loadedJsonObj.get("countable"));
assertEquals(jsonObj.get("options_limit"), loadedJsonObj.get("options_limit"));
addFacetToCleanupArray("testFacet");
}

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

constructor.createFacetConfiguration(request);
addFacetToCleanupArray("emptySection");
}

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

assertEquals("brand", jsonObj.getString("name"));
addFacetToCleanupArray("brand", "Search Suggestions");
}

@Test
public void testDeleteFacetConfigurationWithFacetNameAndSection() throws Exception {
ConstructorIO constructor = new ConstructorIO(token, apiKey, true, null);

// Create a facet first
String string = Utils.getTestResource("facet.configuration.json");
FacetConfiguration facetConfig = new Gson().fromJson(string, FacetConfiguration.class);
facetConfig.setName("testDeleteFacet");

constructor.createFacetConfiguration(
new FacetConfigurationRequest(facetConfig, "Products"));

// Delete the facet
String deleteResponse = constructor.deleteFacetConfiguration("testDeleteFacet", "Products");
JSONObject jsonObj = new JSONObject(deleteResponse);

assertTrue("Deleted facet name matches", jsonObj.get("name").equals("testDeleteFacet"));
}

@Test
public void testDeleteFacetConfigurationWithDefaultSection() throws Exception {
ConstructorIO constructor = new ConstructorIO(token, apiKey, true, null);

// Create a facet first
String string = Utils.getTestResource("facet.configuration.json");
FacetConfiguration facetConfig = new Gson().fromJson(string, FacetConfiguration.class);
facetConfig.setName("testDefaultSectionFacet");

constructor.createFacetConfiguration(
new FacetConfigurationRequest(facetConfig, "Products"));

// Delete the facet
String deleteResponse = constructor.deleteFacetConfiguration("testDefaultSectionFacet");
JSONObject jsonObj = new JSONObject(deleteResponse);

assertTrue(
"Deleted facet name matches",
jsonObj.get("name").equals("testDefaultSectionFacet"));
}

@Test
public void testDeleteFacetConfigurationWithFacetConfiguration() throws Exception {
ConstructorIO constructor = new ConstructorIO(token, apiKey, true, null);

// Create a facet first
String string = Utils.getTestResource("facet.configuration.json");
FacetConfiguration facetConfig = new Gson().fromJson(string, FacetConfiguration.class);
facetConfig.setName("testDeleteWithFacetConfiguration");

FacetConfigurationRequest request = new FacetConfigurationRequest(facetConfig, "Products");
constructor.createFacetConfiguration(request);

// Delete the facet
String deleteResponse = constructor.deleteFacetConfiguration(request);
JSONObject jsonObj = new JSONObject(deleteResponse);

assertTrue(
"Deleted facet name matches",
jsonObj.get("name").equals("testDeleteWithFacetConfiguration"));
}

@Test(expected = ConstructorException.class)
public void testDeleteNonExistentFacetShouldThrowException() throws Exception {
ConstructorIO constructor = new ConstructorIO(token, apiKey, true, null);
constructor.deleteFacetConfiguration("nonExistentFacet", "Products");
}
}
Loading