Skip to content

Commit d5d21a8

Browse files
authored
Support metadata fields reordering
1 parent 8f22204 commit d5d21a8

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

cloudinary-core/src/main/java/com/cloudinary/Api.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,29 @@ public ApiResponse deleteMetadataField(String fieldExternalId) throws Exception
710710
return callApi(HttpMethod.DELETE, uri, Collections.<String, Object>emptyMap(), Collections.emptyMap());
711711
}
712712

713+
/**
714+
* Reorders metadata fields.
715+
*
716+
* @param orderBy Criteria for the order (one of the fields 'label', 'external_id', 'created_at')
717+
* @param direction Optional (gets either asc or desc)
718+
* @param options Additional options
719+
* @return List of metadata fields in their new order
720+
* @throws Exception
721+
*/
722+
public ApiResponse reorderMetadataFields(String orderBy, String direction, Map options) throws Exception {
723+
if (orderBy == null) {
724+
throw new IllegalArgumentException("Must supply orderBy");
725+
}
726+
727+
List<String> uri = Arrays.asList("metadata_fields", "order");
728+
Map<String, Object> map = ObjectUtils.asMap("order_by", orderBy);
729+
if (direction != null) {
730+
map.put("direction", direction);
731+
}
732+
733+
return callApi(HttpMethod.PUT, uri, map, options);
734+
}
735+
713736
private Map<String, ?> extractParams(Map options, List<String> keys) {
714737
Map<String, Object> result = new HashMap<String, Object>();
715738
for (String key : keys) {

cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractStructuredMetadataTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import com.cloudinary.api.ApiResponse;
66
import com.cloudinary.api.exceptions.BadRequest;
77
import com.cloudinary.metadata.*;
8+
9+
import org.hamcrest.Matchers;
810
import org.junit.*;
911
import org.junit.rules.TestName;
1012

@@ -172,6 +174,38 @@ public void testRestoreDatasourceEntries() throws Exception {
172174
assertNotNull(result);
173175
}
174176

177+
@Test
178+
public void testReorderMetadataFieldsByLabel() throws Exception {
179+
AddStringField("some_value");
180+
AddStringField("aaa");
181+
AddStringField("zzz");
182+
183+
ApiResponse result = api.reorderMetadataFields("label", null, Collections.EMPTY_MAP);
184+
assertThat(getField(result, 0), Matchers.containsString("aaa"));
185+
186+
result = api.reorderMetadataFields("label", "desc", Collections.EMPTY_MAP);
187+
assertThat(getField(result, 0), Matchers.containsString("zzz"));
188+
189+
result = api.reorderMetadataFields("label", "asc", Collections.EMPTY_MAP);
190+
assertThat(getField(result, 0), Matchers.containsString("aaa"));
191+
}
192+
193+
@Test(expected = IllegalArgumentException.class)
194+
public void testReorderMetadataFieldsOrderByIsRequired() throws Exception {
195+
api.reorderMetadataFields(null, null, Collections.EMPTY_MAP);
196+
}
197+
198+
private String getField(ApiResponse result, int index) {
199+
String actual = ((Map)((ArrayList)result.get("metadata_fields")).get(index)).get("label").toString();
200+
return actual;
201+
}
202+
203+
private void AddStringField(String labelPrefix) throws Exception {
204+
StringMetadataField field = newFieldInstance(labelPrefix);
205+
ApiResponse fieldResult = addFieldToAccount(field);
206+
String fieldId = fieldResult.get("external_id").toString();
207+
}
208+
175209
@Test
176210
public void testUploadWithMetadata() throws Exception {
177211
StringMetadataField field = newFieldInstance("testUploadWithMetadata");

0 commit comments

Comments
 (0)