1+ package com .cloudinary ;
2+
3+ import com .cloudinary .utils .ObjectUtils ;
4+ import org .cloudinary .json .JSONArray ;
5+ import org .junit .After ;
6+ import org .junit .Before ;
7+ import org .junit .Test ;
8+
9+ import java .util .ArrayList ;
10+ import java .util .List ;
11+ import java .util .Map ;
12+
13+ import static org .junit .Assert .*;
14+
15+ /**
16+ *
17+ */
18+ @ SuppressWarnings ("unchecked" )
19+ public class TransformationTest {
20+
21+ @ Before
22+ public void setUp () throws Exception {
23+
24+ }
25+
26+ @ After
27+ public void tearDown () throws Exception {
28+
29+ }
30+
31+ @ Test
32+ public void withLiteral () throws Exception {
33+ Transformation transformation = new Transformation ().ifCondition ("w_lt_200" ).crop ("fill" ).height (120 ).width (80 );
34+ assertEquals ("should include the if parameter as the first component in the transformation string" , "if_w_lt_200,c_fill,h_120,w_80" , transformation .toString ());
35+
36+ transformation = new Transformation ().crop ("fill" ).height (120 ).ifCondition ("w_lt_200" ).width (80 );
37+ assertEquals ("should include the if parameter as the first component in the transformation string" , "if_w_lt_200,c_fill,h_120,w_80" , transformation .toString ());
38+
39+ String chained = "[{if: \" w_lt_200\" ,crop: \" fill\" ,height: 120, width: 80}, {if: \" w_gt_400\" ,crop: \" fit\" ,width: 150,height: 150},{effect: \" sepia\" }]" ;
40+ List transformations = ObjectUtils .toList (new JSONArray (chained ));
41+
42+ transformation = new Transformation (transformations );
43+ assertEquals ("should allow multiple conditions when chaining transformations" , "if_w_lt_200,c_fill,h_120,w_80/if_w_gt_400,c_fit,h_150,w_150/e_sepia" , transformation .toString ());
44+ }
45+
46+ @ Test
47+ public void literalWithSpaces () throws Exception {
48+ Map map = ObjectUtils .asMap ("if" , "w < 200" , "crop" , "fill" , "height" , 120 , "width" , 80 );
49+ List <Map > list = new ArrayList <Map >();
50+ list .add (map );
51+ Transformation transformation = new Transformation (list );
52+
53+ assertEquals ("should translate operators" , "if_w_lt_200,c_fill,h_120,w_80" , transformation .toString ());
54+ }
55+
56+ @ Test
57+ public void endIf () throws Exception {
58+ String chained = "[{if: \" w_lt_200\" },\n " +
59+ " {crop: \" fill\" , height: 120, width: 80,effect: \" sharpen\" },\n " +
60+ " {effect: \" brightness:50\" },\n " +
61+ " {effect: \" shadow\" ,color: \" red\" }, {if: \" end\" }]" ;
62+ List transformations = ObjectUtils .toList (new JSONArray (chained ));
63+
64+ Transformation transformation = new Transformation (transformations );
65+ assertEquals ("should include the if_end as the last parameter in its component" , "if_w_lt_200/c_fill,e_sharpen,h_120,w_80/e_brightness:50/co_red,e_shadow/if_end" , transformation .toString ());
66+
67+ }
68+
69+ @ Test
70+ public void ifElse () throws Exception {
71+ String chained = "[{if: \" w_lt_200\" ,crop: \" fill\" ,height: 120,width: 80},\n " +
72+ " {if: \" else\" ,crop: \" fill\" ,height: 90, width: 100}]" ;
73+ List transformations = ObjectUtils .toList (new JSONArray (chained ));
74+
75+ Transformation transformation = new Transformation (transformations );
76+
77+ assertEquals ("should support if_else with transformation parameters" , "if_w_lt_200,c_fill,h_120,w_80/if_else,c_fill,h_90,w_100" , transformation .toString ());
78+
79+ chained = "[{if: \" w_lt_200\" },\n " +
80+ " {crop: \" fill\" ,height: 120,width: 80},\n " +
81+ " {if: \" else\" },\n " +
82+ " {crop: \" fill\" ,height: 90,width: 100}]" ;
83+ transformations = ObjectUtils .toList (new JSONArray (chained ));
84+
85+ transformation = new Transformation (transformations );
86+ assertEquals ("if_else should be without any transformation parameters" , "if_w_lt_200/c_fill,h_120,w_80/if_else/c_fill,h_90,w_100" , transformation .toString ());
87+ }
88+
89+ @ Test
90+ public void chainedConditions () throws Exception {
91+ Transformation transformation = new Transformation ().ifCondition ().aspectRatio ("gt" , "3:4" ).then ().width (100 ).crop ("scale" );
92+ assertEquals ("passing an operator and a value adds a condition" , "if_ar_gt_3:4,c_scale,w_100" , transformation .toString ());
93+ transformation = new Transformation ().ifCondition ().aspectRatio ("gt" , "3:4" ).and ().width ("gt" , 100 ).then ().width (50 ).crop ("scale" );
94+ assertEquals ("should chaining condition with `and`" , "if_ar_gt_3:4_and_w_gt_100,c_scale,w_50" , transformation .toString ());
95+ transformation = new Transformation ().ifCondition ().aspectRatio ("gt" , "3:4" ).and ().width ("gt" , 100 ).or ().width ("gt" , 200 ).then ().width (50 ).crop ("scale" );
96+ assertEquals ("should chain conditions with `or`" , "if_ar_gt_3:4_and_w_gt_100_or_w_gt_200,c_scale,w_50" , transformation .toString ());
97+ transformation = new Transformation ().ifCondition ().aspectRatio (">" , "3:4" ).and ().width ("<=" , 100 ).or ().width ("gt" , 200 ).then ().width (50 ).crop ("scale" );
98+ assertEquals ("should translate operators" , "if_ar_gt_3:4_and_w_lte_100_or_w_gt_200,c_scale,w_50" , transformation .toString ());
99+ transformation = new Transformation ().ifCondition ().aspectRatio (">" , "3:4" ).and ().width ("<=" , 100 ).or ().width (">" , 200 ).then ().width (50 ).crop ("scale" );
100+ assertEquals ("should translate operators" , "if_ar_gt_3:4_and_w_lte_100_or_w_gt_200,c_scale,w_50" , transformation .toString ());
101+ transformation = new Transformation ().ifCondition ().aspectRatio (">=" , "3:4" ).and ().pages (">=" , 100 ).or ().pages ("!=" , 0 ).then ().width (50 ).crop ("scale" );
102+ assertEquals ("should translate operators" , "if_ar_gte_3:4_and_pg_gte_100_or_pg_ne_0,c_scale,w_50" , transformation .toString ());
103+
104+ }
105+
106+ @ Test
107+ public void shouldSupportAndTranslateOperators () {
108+
109+ String allOperators =
110+ "if_" +
111+ "w_eq_0_and" +
112+ "_w_ne_0_or" +
113+ "_w_lt_0_and" +
114+ "_w_gt_0_and" +
115+ "_w_lte_0_and" +
116+ "_w_gte_0" +
117+ ",e_grayscale" ;
118+ assertEquals ("should support and translate operators: '=', '!=', '<', '>', '<=', '>=', '&&', '||'" ,
119+ allOperators , new Transformation ().ifCondition ()
120+ .width ("=" , 0 ).and ()
121+ .width ("!=" , 0 ).or ()
122+ .width ("<" , 0 ).and ()
123+ .width (">" , 0 ).and ()
124+ .width ("<=" , 0 ).and ()
125+ .width (">=" , 0 )
126+ .then ().effect ("grayscale" ).toString ());
127+
128+ assertEquals (allOperators , new Transformation ().ifCondition ("w = 0 && w != 0 || w < 0 and w > 0 and w <= 0 and w >= 0" )
129+ .effect ("grayscale" )
130+ .toString ());
131+ }
132+
133+ @ Test
134+ public void endIf2 () throws Exception {
135+ Transformation transformation = new Transformation ().ifCondition ().width ("gt" , 100 ).and ().width ("lt" , 200 ).then ().width (50 ).crop ("scale" ).endIf ();
136+ assertEquals ("should serialize to 'if_end'" , "if_w_gt_100_and_w_lt_200/c_scale,w_50/if_end" , transformation .toString ());
137+ transformation = new Transformation ().ifCondition ().width ("gt" , 100 ).and ().width ("lt" , 200 ).then ().width (50 ).crop ("scale" ).endIf ();
138+ assertEquals ("force the if clause to be chained" , "if_w_gt_100_and_w_lt_200/c_scale,w_50/if_end" , transformation .toString ());
139+ transformation = new Transformation ().ifCondition ().width ("gt" , 100 ).and ().width ("lt" , 200 ).then ().width (50 ).crop ("scale" ).ifElse ().width (100 ).crop ("crop" ).endIf ();
140+ assertEquals ("force the if_else clause to be chained" , "if_w_gt_100_and_w_lt_200/c_scale,w_50/if_else/c_crop,w_100/if_end" , transformation .toString ());
141+
142+ }
143+
144+ }
0 commit comments