Skip to content

Commit 34c677b

Browse files
committed
Cleanup
1 parent f09e4a0 commit 34c677b

File tree

2 files changed

+61
-40
lines changed

2 files changed

+61
-40
lines changed

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,23 @@ public void PatchCatalogWithInvalidExtensionShouldError() throws Exception {
539539
constructor.patchCatalog(req);
540540
}
541541

542+
@Test
543+
public void UpdateCatalogWithInvalidExtensionShouldError() throws Exception {
544+
ConstructorIO constructor = new ConstructorIO(token, apiKey, true, null);
545+
Map<String, File> files = new HashMap<String, File>();
546+
547+
files.put("items", invalidExtensionFile);
548+
549+
CatalogRequest req = new CatalogRequest(files, "Products");
550+
551+
thrown.expect(ConstructorException.class);
552+
thrown.expectMessage("Invalid file type for 'items'");
553+
thrown.expectMessage(".csv");
554+
thrown.expectMessage(".json");
555+
thrown.expectMessage(".jsonl");
556+
constructor.updateCatalog(req);
557+
}
558+
542559
// Edge Case Tests
543560

544561
@Test
@@ -556,6 +573,36 @@ public void ReplaceCatalogWithNullFileShouldError() throws Exception {
556573
constructor.replaceCatalog(req);
557574
}
558575

576+
@Test
577+
public void UpdateCatalogWithNullFileShouldError() throws Exception {
578+
ConstructorIO constructor = new ConstructorIO(token, apiKey, true, null);
579+
Map<String, File> files = new HashMap<String, File>();
580+
581+
files.put("items", null);
582+
583+
CatalogRequest req = new CatalogRequest(files, "Products");
584+
585+
thrown.expect(ConstructorException.class);
586+
thrown.expectMessage("Invalid file for 'items'");
587+
thrown.expectMessage("file cannot be null");
588+
constructor.updateCatalog(req);
589+
}
590+
591+
@Test
592+
public void PatchCatalogWithNullFileShouldError() throws Exception {
593+
ConstructorIO constructor = new ConstructorIO(token, apiKey, true, null);
594+
Map<String, File> files = new HashMap<String, File>();
595+
596+
files.put("items", null);
597+
598+
CatalogRequest req = new CatalogRequest(files, "Products");
599+
600+
thrown.expect(ConstructorException.class);
601+
thrown.expectMessage("Invalid file for 'items'");
602+
thrown.expectMessage("file cannot be null");
603+
constructor.patchCatalog(req);
604+
}
605+
559606
@Test
560607
public void ReplaceCatalogWithMixedFileTypesShouldSucceed() throws Exception {
561608
ConstructorIO constructor = new ConstructorIO(token, apiKey, true, null);

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

Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -121,36 +121,6 @@ public static void enableHTTPLogging() {
121121

122122
private static final Gson gson = new Gson();
123123

124-
/**
125-
* Creates a JSON string for an item group.
126-
*
127-
* @param id the group ID
128-
* @param name the group display name
129-
* @param parentId the parent group ID
130-
* @return JSON string representation
131-
*/
132-
private static String itemGroupToJson(String id, String name, String parentId) {
133-
Map<String, Object> dataMap = new HashMap<String, Object>();
134-
dataMap.put("parent_id", parentId);
135-
136-
Map<String, Object> group = new HashMap<String, Object>();
137-
group.put("id", id);
138-
group.put("name", name);
139-
group.put("data", dataMap);
140-
141-
return gson.toJson(group);
142-
}
143-
144-
/**
145-
* Generates a unique ID for test data.
146-
*
147-
* @param prefix the prefix for the ID
148-
* @return a unique ID string
149-
*/
150-
private static String generateId(String prefix) {
151-
return prefix + UUID.randomUUID().toString().substring(0, 8);
152-
}
153-
154124
/**
155125
* Creates a temporary JSON file containing an array of items.
156126
* Uses createProductItem() to generate realistic test data.
@@ -262,10 +232,15 @@ public static File createItemGroupsJsonlFile(int count) throws IOException {
262232

263233
StringBuilder sb = new StringBuilder();
264234
for (int i = 0; i < count; i++) {
265-
String id = generateId("group");
266-
String name = "Group " + (i + 1);
267-
String parentId = "root";
268-
sb.append(itemGroupToJson(id, name, parentId)).append("\n");
235+
Map<String, Object> dataMap = new HashMap<String, Object>();
236+
dataMap.put("parent_id", "root");
237+
238+
Map<String, Object> group = new HashMap<String, Object>();
239+
group.put("id", "group" + UUID.randomUUID().toString().substring(0, 8));
240+
group.put("name", "Group " + (i + 1));
241+
group.put("data", dataMap);
242+
243+
sb.append(gson.toJson(group)).append("\n");
269244
}
270245

271246
try (FileWriter writer = new FileWriter(file)) {
@@ -298,14 +273,13 @@ public static File createInvalidExtensionFile() throws IOException {
298273
* @throws IOException if file creation fails
299274
*/
300275
public static File createNoExtensionFile() throws IOException {
301-
File tempFile = File.createTempFile("items", ".tmp");
302-
File noExtFile = new File(tempFile.getParent(), "items_" + UUID.randomUUID().toString().substring(0, 8));
303-
tempFile.renameTo(noExtFile);
304-
noExtFile.deleteOnExit();
276+
String tmpDir = System.getProperty("java.io.tmpdir");
277+
File file = new File(tmpDir, "items_" + UUID.randomUUID().toString().substring(0, 8));
278+
file.deleteOnExit();
305279

306-
try (FileWriter writer = new FileWriter(noExtFile)) {
280+
try (FileWriter writer = new FileWriter(file)) {
307281
writer.write("This file has no extension for testing validation.");
308282
}
309-
return noExtFile;
283+
return file;
310284
}
311285
}

0 commit comments

Comments
 (0)