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