Skip to content

Commit 923594a

Browse files
authored
Add restrictions field to metadata (#312)
1 parent c1abb91 commit 923594a

File tree

3 files changed

+69
-4
lines changed

3 files changed

+69
-4
lines changed

cloudinary-core/src/main/java/com/cloudinary/metadata/MetadataField.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class MetadataField<T> extends JSONObject {
1616
public static final String MANDATORY = "mandatory";
1717
public static final String TYPE = "type";
1818
public static final String VALIDATION = "validation";
19+
public static final String RESTRICTIONS = "restrictions";
1920

2021
public MetadataField(MetadataFieldType type) {
2122
put(TYPE, type.toString());
@@ -130,4 +131,12 @@ public MetadataDataSource getDataSource() {
130131
public void setDataSource(MetadataDataSource dataSource) {
131132
put("datasource", dataSource);
132133
}
134+
135+
/**
136+
* Set the restrictions rules of this field.
137+
* @param restrictions The rules to set.
138+
*/
139+
public void setRestrictions(Restrictions restrictions) {
140+
put(RESTRICTIONS, restrictions.toHash());
141+
}
133142
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.cloudinary.metadata;
2+
3+
import java.util.HashMap;
4+
5+
/**
6+
* Represents the restrictions metadata field.
7+
*/
8+
public class Restrictions {
9+
10+
private final HashMap restrictions = new HashMap();
11+
12+
/**
13+
* Set the custom field into restrictions.
14+
* @param key The key of the field.
15+
* @param value The value of the field.
16+
*/
17+
public Restrictions setRestriction(String key, Object value) {
18+
restrictions.put(key, value);
19+
return this;
20+
}
21+
22+
/**
23+
* Set the read only ui field.
24+
* @param value The read only ui value.
25+
*/
26+
public Restrictions setReadOnlyUI(Boolean value) {
27+
return setRestriction("readonly_ui", value);
28+
}
29+
30+
/**
31+
* Set the read only ui field to true.
32+
*/
33+
public Restrictions setReadOnlyUI() {
34+
return this.setReadOnlyUI(true);
35+
}
36+
37+
public HashMap toHash() {
38+
return restrictions;
39+
}
40+
}

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,18 @@ public void testCreateMetadata() throws Exception {
7272
assertEquals(setField.getLabel(), result.get("label"));
7373
}
7474

75+
@Test
76+
public void testFieldRestrictions() throws Exception {
77+
StringMetadataField stringField = newFieldInstance("testCreateMetadata_3");
78+
stringField.setRestrictions(new Restrictions().setReadOnlyUI());
79+
80+
ApiResponse result = api.addMetadataField(stringField);
81+
assertNotNull(result);
82+
Map<String, Object> restrictions = (Map<String, Object>) result.get("restrictions");
83+
assertNotNull(restrictions);
84+
assertTrue((Boolean) restrictions.get("readonly_ui"));
85+
}
86+
7587
@Test
7688
public void testDateFieldDefaultValueValidation() throws Exception {
7789
// now minus 3 days hours.
@@ -130,13 +142,17 @@ public void testGetMetadata() throws Exception {
130142

131143
@Test
132144
public void testUpdateField() throws Exception {
133-
ApiResponse fieldResult = addFieldToAccount(newFieldInstance("testUpdateField"));
145+
StringMetadataField metadataField = newFieldInstance("testUpdateField");
146+
ApiResponse fieldResult = addFieldToAccount(metadataField);
134147
assertNotEquals("new_def", fieldResult.get("default_value"));
135-
StringMetadataField field = new StringMetadataField();
136-
field.setDefaultValue("new_def");
137-
ApiResponse result = api.updateMetadataField(fieldResult.get("external_id").toString(), field);
148+
metadataField.setDefaultValue("new_def");
149+
metadataField.setRestrictions(new Restrictions().setReadOnlyUI());
150+
ApiResponse result = api.updateMetadataField(fieldResult.get("external_id").toString(), metadataField);
138151
assertNotNull(result);
139152
assertEquals("new_def", result.get("default_value"));
153+
Map<String, Object> restrictions = (Map<String, Object>) result.get("restrictions");
154+
assertNotNull(restrictions);
155+
assertTrue((Boolean)restrictions.get("readonly_ui"));
140156
}
141157

142158
@Test

0 commit comments

Comments
 (0)