Skip to content

Commit a953c3c

Browse files
committed
Rename ResponsiveBreakpoints to ResponsiveBreakpoint and have it extend JSONObject.
1 parent 2e528c7 commit a953c3c

File tree

5 files changed

+109
-143
lines changed

5 files changed

+109
-143
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.cloudinary;
2+
3+
import org.cloudinary.json.JSONObject;
4+
5+
public class ResponsiveBreakpoint extends JSONObject {
6+
public ResponsiveBreakpoint() {
7+
put("create_derived", true);
8+
}
9+
10+
public boolean isCreateDerived() {
11+
return optBoolean("create_derived");
12+
}
13+
14+
public ResponsiveBreakpoint createDerived(boolean createDerived) {
15+
put("create_derived", createDerived);
16+
return this;
17+
}
18+
19+
public Transformation transformation() {
20+
return (Transformation) opt("transformation");
21+
}
22+
23+
public ResponsiveBreakpoint transformation(Transformation transformation) {
24+
put("transformation", transformation);
25+
return this;
26+
}
27+
28+
public int maxWidth() {
29+
return optInt("max_width");
30+
}
31+
32+
public ResponsiveBreakpoint maxWidth(int maxWidth) {
33+
put("max_width", maxWidth);
34+
return this;
35+
}
36+
37+
public int minWidth() {
38+
return optInt("min_width");
39+
}
40+
41+
public ResponsiveBreakpoint minWidth(Integer minWidth) {
42+
put("min_width", minWidth);
43+
return this;
44+
}
45+
46+
public int bytesStep() {
47+
return optInt("bytes_step");
48+
}
49+
50+
public ResponsiveBreakpoint bytesStep(Integer bytesStep) {
51+
put("bytes_step", bytesStep);
52+
return this;
53+
}
54+
55+
public int maxImages() {
56+
return optInt("max_images");
57+
}
58+
59+
public ResponsiveBreakpoint maxImages(Integer maxImages) {
60+
put("max_images", maxImages);
61+
return this;
62+
}
63+
64+
}

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

Lines changed: 0 additions & 95 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public Map explicit(String publicId, Map options) throws IOException {
192192
params.put("context", ObjectUtils.encodeMap(options.get("context")));
193193
}
194194
if (options.get("responsive_breakpoints") != null) {
195-
params.put("responsive_breakpoints", ResponsiveBreakpoints.toJsonString(options.get("responsive_breakpoints")));
195+
params.put("responsive_breakpoints", JSONObject.wrap(options.get("responsive_breakpoints")));
196196
}
197197
params.put("invalidate", ObjectUtils.asBoolean(options.get("invalidate"), false).toString());
198198
return callApi("explicit", params, options, null);

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
import java.util.List;
77
import java.util.Map;
88

9-
import org.cloudinary.json.JSONArray;
10-
119
import com.cloudinary.utils.ObjectUtils;
1210
import com.cloudinary.utils.StringUtils;
11+
import org.cloudinary.json.JSONObject;
1312

1413
public class Util {
1514
static final String[] BOOLEAN_UPLOAD_OPTIONS = new String[] { "backup", "exif", "faces", "colors", "image_metadata", "use_filename", "unique_filename",
@@ -37,7 +36,9 @@ public static final Map<String, Object> buildUploadParams(Map options) {
3736
params.put("folder", (String) options.get("folder"));
3837
params.put("allowed_formats", StringUtils.join(ObjectUtils.asArray(options.get("allowed_formats")), ","));
3938
params.put("moderation", options.get("moderation"));
40-
params.put("responsive_breakpoints", ResponsiveBreakpoints.toJsonString(options.get("responsive_breakpoints")));
39+
Object responsive_breakpoints = options.get("responsive_breakpoints");
40+
if (responsive_breakpoints != null){
41+
params.put("responsive_breakpoints", JSONObject.wrap(responsive_breakpoints));}
4142
params.put("upload_preset", options.get("upload_preset"));
4243

4344
if (options.get("signature") == null) {

cloudinary-core/src/test/java/com/cloudinary/test/CloudinaryTest.java

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
package com.cloudinary.test;
22

3-
import static org.junit.Assert.assertEquals;
4-
import static org.junit.Assert.assertNull;
5-
import static org.junit.Assert.assertTrue;
6-
73
import java.io.UnsupportedEncodingException;
84
import java.net.URI;
95
import java.net.URLDecoder;
6+
import java.util.Arrays;
107
import java.util.HashMap;
118
import java.util.Map;
12-
import java.util.Set;
139
import java.util.regex.Matcher;
1410
import java.util.regex.Pattern;
1511

12+
import com.cloudinary.ResponsiveBreakpoint;
13+
import org.cloudinary.json.JSONArray;
14+
import org.cloudinary.json.JSONObject;
1615
import org.junit.Before;
1716
import org.junit.Rule;
1817
import org.junit.Test;
1918
import org.junit.rules.TestName;
2019

2120
import com.cloudinary.Cloudinary;
22-
import com.cloudinary.ResponsiveBreakpoints;
2321
import com.cloudinary.Transformation;
2422
import com.cloudinary.transformation.*;
2523
import com.cloudinary.utils.ObjectUtils;
2624

25+
import static org.junit.Assert.*;
26+
2727
public class CloudinaryTest {
2828
private static final String DEFAULT_ROOT_PATH = "http://res.cloudinary.com/test123/";
2929
private static final String DEFAULT_UPLOAD_PATH = DEFAULT_ROOT_PATH + "image/upload/";
@@ -269,7 +269,7 @@ public void testOverlay() {
269269
assertNull(transformation.getHtmlWidth());
270270
assertEquals(DEFAULT_UPLOAD_PATH + "h_100,l_text:hello,w_100/test", result);
271271

272-
transformation = new Transformation().overlay(new TextLayerBuilder().text("goodbye"));
272+
transformation = new Transformation().overlay(new TextLayer().text("goodbye"));
273273
result = cloudinary.url().transformation(transformation).generate("test");
274274
assertEquals(DEFAULT_UPLOAD_PATH + "l_text:goodbye/test", result);
275275
}
@@ -906,23 +906,23 @@ public void testAspectRatio() {
906906
@Test
907907
public void testOverlayOptions() {
908908
Object tests[] = {
909-
new LayerBuilder().publicId("logo"),
909+
new Layer().publicId("logo"),
910910
"logo",
911-
new LayerBuilder().publicId("folder/logo"),
911+
new Layer().publicId("folder/logo"),
912912
"folder:logo",
913-
new LayerBuilder().publicId("logo").type("private"),
913+
new Layer().publicId("logo").type("private"),
914914
"private:logo",
915-
new LayerBuilder().publicId("logo").format("png"),
915+
new Layer().publicId("logo").format("png"),
916916
"logo.png",
917-
new LayerBuilder().resourceType("video").publicId("cat"),
917+
new Layer().resourceType("video").publicId("cat"),
918918
"video:cat",
919-
new TextLayerBuilder().text("Hello World, Nice to meet you?").fontFamily("Arial").fontSize(18),
919+
new TextLayer().text("Hello World, Nice to meet you?").fontFamily("Arial").fontSize(18),
920920
"text:Arial_18:Hello%20World%E2%80%9A%20Nice%20to%20meet%20you%3F",
921-
new TextLayerBuilder().text("Hello World, Nice to meet you?").fontFamily("Arial").fontSize(18)
921+
new TextLayer().text("Hello World, Nice to meet you?").fontFamily("Arial").fontSize(18)
922922
.fontWeight("bold").fontStyle("italic").letterSpacing("4").lineSpacing(3),
923923
"text:Arial_18_bold_italic_letter_spacing_4_line_spacing_3:Hello%20World%E2%80%9A%20Nice%20to%20meet%20you%3F",
924-
new SubtitlesLayerBuilder().publicId("sample_sub_en.srt"), "subtitles:sample_sub_en.srt",
925-
new SubtitlesLayerBuilder().publicId("sample_sub_he.srt").fontFamily("Arial").fontSize(40),
924+
new SubtitlesLayer().publicId("sample_sub_en.srt"), "subtitles:sample_sub_en.srt",
925+
new SubtitlesLayer().publicId("sample_sub_he.srt").fontFamily("Arial").fontSize(40),
926926
"subtitles:Arial_40:sample_sub_he.srt" };
927927

928928
for (int i = 0; i < tests.length; i += 2) {
@@ -935,44 +935,40 @@ public void testOverlayOptions() {
935935
@Test(expected = IllegalArgumentException.class)
936936
public void testOverlayError1() {
937937
// Must supply font_family for text in overlay
938-
cloudinary.url().transformation(new Transformation().overlay(new TextLayerBuilder().fontStyle("italic"))).generate("test");
938+
cloudinary.url().transformation(new Transformation().overlay(new TextLayer().fontStyle("italic"))).generate("test");
939939
}
940940

941941
@Test(expected = IllegalArgumentException.class)
942942
public void testOverlayError2() {
943943
// Must supply public_id for for non-text underlay
944-
cloudinary.url().transformation(new Transformation().underlay(new LayerBuilder().resourceType("video"))).generate("test");
944+
cloudinary.url().transformation(new Transformation().underlay(new Layer().resourceType("video"))).generate("test");
945945
}
946946

947947
@Test
948948
public void testResponsiveBreakpointsToJson() {
949-
assertEquals(
949+
assertEquals("an empty ResponsiveBreakpoint should have create_derived=true",
950950
"[{\"create_derived\":true}]",
951-
ResponsiveBreakpoints.toJsonString(
952-
new ResponsiveBreakpoints()
953-
));
954-
955-
assertEquals(
956-
"[{\"create_derived\":false,\"max_width\":500,\"min_width\":100,\"max_images\":5,\"transformation\":\"a_45\"}]",
957-
ResponsiveBreakpoints.toJsonString(
958-
new ResponsiveBreakpoints().createDerived(false)
959-
.transformation(new Transformation().angle(45))
960-
.maxWidth(500)
961-
.minWidth(100)
962-
.maxImages(5)
963-
));
964-
assertEquals(
965-
"[{\"create_derived\":false,\"max_width\":500,\"min_width\":100,\"max_images\":5,\"transformation\":\"a_45\"},{\"create_derived\":true}]",
966-
ResponsiveBreakpoints.toJsonString(
967-
new ResponsiveBreakpoints[]{
968-
new ResponsiveBreakpoints().createDerived(false)
969-
.transformation(new Transformation().angle(45))
970-
.maxWidth(500)
971-
.minWidth(100)
972-
.maxImages(5),
973-
new ResponsiveBreakpoints()
974-
}
975-
));
951+
new ResponsiveBreakpoint().toString()
952+
);
953+
JSONObject expected = new JSONObject("{\"create_derived\":false,\"max_width\":500,\"min_width\":100,\"max_images\":5,\"transformation\":\"a_45\"}");
954+
JSONObject actual = new ResponsiveBreakpoint().createDerived(false)
955+
.transformation(new Transformation().angle(45))
956+
.maxWidth(500)
957+
.minWidth(100)
958+
.maxImages(5)
959+
;
960+
assertTrue(actual.similar(expected));
961+
962+
JSONArray actualArray = new JSONArray(Arrays.asList(
963+
new ResponsiveBreakpoint().createDerived(false)
964+
.transformation(new Transformation().angle(45))
965+
.maxWidth(500)
966+
.minWidth(100)
967+
.maxImages(5),
968+
new ResponsiveBreakpoint()
969+
));;
970+
JSONArray expectedArray = new JSONArray("[{\"create_derived\":false,\"max_width\":500,\"min_width\":100,\"max_images\":5,\"transformation\":\"a_45\"},{\"create_derived\":true}]");
971+
assertTrue(actualArray.similar(expectedArray));
976972
}
977973

978974
public static Map<String, String> getUrlParameters(URI uri) throws UnsupportedEncodingException {

0 commit comments

Comments
 (0)