Project

General

Profile

Download (14.3 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2020 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.api.service.media;
10

    
11
import static org.junit.Assert.assertEquals;
12
import static org.junit.Assert.assertTrue;
13

    
14
import java.net.URI;
15
import java.net.URISyntaxException;
16
import java.util.List;
17

    
18
import org.junit.Test;
19

    
20
import eu.etaxonomy.cdm.model.media.ImageFile;
21
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
22

    
23
/**
24
 * @author a.kohlbecker
25
 * @since Jul 7, 2020
26
 */
27
public class MediaUriTransformationProcessorTest {
28

    
29
    private static final String URI_STRING_1 = "https://pictures.bgbm.org/digilib/Scaler/?fn=Cyprus/Salvia_aethiopis_A1.jpg&mo=file";
30

    
31
    private static final String PATTERN_1 = "digilib/Scaler/\\?fn=([^\\/]+)/([^\\&]+)(.*)";
32

    
33
    private MediaRepresentation makeImageMediaRepresentation(int width, int height) throws URISyntaxException {
34
        MediaRepresentation repr = MediaRepresentation.NewInstance("image/jpeg", null);
35
        ImageFile part = ImageFile.NewInstance(new URI(URI_STRING_1), null, height, width);
36
        repr.addRepresentationPart(part);
37
        return repr;
38
    }
39

    
40
    @Test
41
    public void testUriTranformation() throws URISyntaxException {
42
        URI uri = new URI(URI_STRING_1);
43

    
44
        MediaUriTransformation transformation1 = new MediaUriTransformation();
45
        transformation1.setPathQueryFragment(new SearchReplace(PATTERN_1, "digilib/Scaler/IIIF/$1!$2/full/full/0/default.jpg"));
46

    
47
        MediaUriTransformation transformation2 = new MediaUriTransformation();
48
        transformation2.setPathQueryFragment(new SearchReplace(PATTERN_1, "digilib/Scaler/IIIF/$1!$2/!400,400/full/0/default.jpg"));
49
        transformation2.setHost(new SearchReplace("pictures.bgbm.org", "images.bgbm.org")); // host part wont match => no transformation!
50

    
51
        MediaUriTransformationProcessor processor = new MediaUriTransformationProcessor();
52
        processor.add(transformation1);
53
        processor.add(transformation2);
54

    
55
        List<URI> transformed =  processor.applyTo(uri);
56
        assertEquals("https://pictures.bgbm.org/digilib/Scaler/IIIF/Cyprus!Salvia_aethiopis_A1.jpg/full/full/0/default.jpg", transformed.get(0).toString());
57
        assertEquals("https://images.bgbm.org/digilib/Scaler/IIIF/Cyprus!Salvia_aethiopis_A1.jpg/!400,400/full/0/default.jpg", transformed.get(1).toString());
58
    }
59

    
60
    @Test
61
    public void testUriTranformationWithNotMatchCase() throws URISyntaxException {
62
        URI uri = new URI(URI_STRING_1);
63

    
64
        MediaUriTransformation transformation2 = new MediaUriTransformation();
65
        transformation2.setPathQueryFragment(new SearchReplace(PATTERN_1, "digilib/Scaler/IIIF/$1!$2/!400,400/full/0/default.jpg"));
66
        transformation2.setHost(new SearchReplace("pictures.bgbm.org", "pictures.bgbm.org")); // host part only used for matching, no replace!
67

    
68
        MediaUriTransformationProcessor processor = new MediaUriTransformationProcessor();
69
        processor.add(transformation2);
70

    
71
        List<URI> transformed =  processor.applyTo(uri);
72
        assertEquals("https://pictures.bgbm.org/digilib/Scaler/IIIF/Cyprus!Salvia_aethiopis_A1.jpg/!400,400/full/0/default.jpg", transformed.get(0).toString());
73
    }
74

    
75
    @Test
76
    public void testUriTranformationWithMatchCase() throws URISyntaxException {
77
        URI uri = new URI(URI_STRING_1);
78

    
79
        MediaUriTransformation transformation2 = new MediaUriTransformation();
80
        transformation2.setPathQueryFragment(new SearchReplace(PATTERN_1, "digilib/Scaler/IIIF/$1!$2/!400,400/full/0/default.jpg"));
81
        transformation2.setHost(new SearchReplace("not.matching.host", "images.bgbm.org"));
82

    
83
        MediaUriTransformationProcessor processor = new MediaUriTransformationProcessor();
84
        processor.add(transformation2);
85

    
86
        List<URI> transformed =  processor.applyTo(uri);
87
        assertEquals(URI_STRING_1, transformed.get(0).toString());
88
    }
89

    
90
    @Test
91
    public void testMakeMedia() throws URISyntaxException {
92

    
93
        URI uri = new URI(URI_STRING_1);
94

    
95
        MediaUriTransformation transformation1 = new MediaUriTransformation();
96
        transformation1.setPathQueryFragment(new SearchReplace(PATTERN_1, "digilib/Scaler/IIIF/$1!$2/400,200/full/0/default.jpg"));
97
        transformation1.setMimeType("image/jpeg");
98
        transformation1.setWidth(400);
99
        transformation1.setHeight(200);
100
        MediaUriTransformationProcessor processor = new MediaUriTransformationProcessor();
101
        processor.add(transformation1);
102

    
103
        List<MediaRepresentation> representations = processor.makeNewMediaRepresentationsFor(uri);
104

    
105
        assertEquals("https://pictures.bgbm.org/digilib/Scaler/IIIF/Cyprus!Salvia_aethiopis_A1.jpg/400,200/full/0/default.jpg", representations.get(0).getParts().get(0).getUri().toString());
106
        assertEquals(ImageFile.class, representations.get(0).getParts().get(0).getClass());
107
        ImageFile image = (ImageFile)representations.get(0).getParts().get(0);
108
        assertEquals(transformation1.getWidth(), image.getWidth());
109
        assertEquals(transformation1.getHeight(), image.getHeight());
110
    }
111

    
112
    @Test
113
    public void testMakeMediaWithNotMatchCase() throws URISyntaxException {
114

    
115
        MediaUriTransformation transformation2 = new MediaUriTransformation();
116
        transformation2.setPathQueryFragment(new SearchReplace(PATTERN_1, "digilib/Scaler/IIIF/$1!$2/!400,400/full/0/default.jpg"));
117
        transformation2.setHost(new SearchReplace("not.matching.host", "pictures.bgbm.org")); // host part only used for matching, no replace!
118
        transformation2.setMimeType("image/jpeg");
119
        transformation2.setWidth(400);
120
        transformation2.setHeight(200);
121
        MediaUriTransformationProcessor processor = new MediaUriTransformationProcessor();
122
        processor.add(transformation2);
123

    
124

    
125
        MediaRepresentation repr = makeImageMediaRepresentation(2000, 1500);
126

    
127
        List<MediaRepresentation> representations = processor.makeNewMediaRepresentationsFor(repr.getParts().get(0));
128
        assertTrue(representations.isEmpty());
129
    }
130

    
131
    @Test
132
    public void testMakeMediaCalculateWidth() throws URISyntaxException {
133

    
134
        MediaUriTransformation transformation1 = new MediaUriTransformation();
135
        transformation1.setPathQueryFragment(new SearchReplace(PATTERN_1, "digilib/Scaler/IIIF/$1!$2/400,/full/0/default.jpg"));
136
        transformation1.setMimeType("image/jpeg");
137
        transformation1.setWidth(400);
138
        transformation1.setHeight(null);
139
        MediaUriTransformationProcessor processor = new MediaUriTransformationProcessor();
140
        processor.add(transformation1);
141

    
142
        MediaRepresentation repr = makeImageMediaRepresentation(2000, 1500); // aspect ratio = 4/3
143

    
144
        List<MediaRepresentation> representations = processor.makeNewMediaRepresentationsFor(repr.getParts().get(0));
145

    
146
        assertEquals("https://pictures.bgbm.org/digilib/Scaler/IIIF/Cyprus!Salvia_aethiopis_A1.jpg/400,/full/0/default.jpg", representations.get(0).getParts().get(0).getUri().toString());
147
        assertEquals(ImageFile.class, representations.get(0).getParts().get(0).getClass());
148
        ImageFile image = (ImageFile)representations.get(0).getParts().get(0);
149
        assertEquals(Integer.valueOf(400), image.getWidth());
150
        assertEquals(Integer.valueOf(300), image.getHeight());
151
    }
152

    
153
    @Test
154
    public void testMakeMediaCalculateHeight() throws URISyntaxException {
155

    
156

    
157
        MediaUriTransformation transformation1 = new MediaUriTransformation();
158
        transformation1.setPathQueryFragment(new SearchReplace(PATTERN_1, "digilib/Scaler/IIIF/$1!$2/,300/full/0/default.jpg"));
159
        transformation1.setMimeType("image/jpeg");
160
        transformation1.setWidth(null);
161
        transformation1.setHeight(300);
162
        MediaUriTransformationProcessor processor = new MediaUriTransformationProcessor();
163
        processor.add(transformation1);
164

    
165
        MediaRepresentation repr = makeImageMediaRepresentation(2000, 1500); // aspect ratio = 4/3
166

    
167
        List<MediaRepresentation> representations = processor.makeNewMediaRepresentationsFor(repr.getParts().get(0));
168

    
169
        assertEquals("https://pictures.bgbm.org/digilib/Scaler/IIIF/Cyprus!Salvia_aethiopis_A1.jpg/,300/full/0/default.jpg", representations.get(0).getParts().get(0).getUri().toString());
170
        assertEquals(ImageFile.class, representations.get(0).getParts().get(0).getClass());
171
        ImageFile image = (ImageFile)representations.get(0).getParts().get(0);
172
        assertEquals(Integer.valueOf(400), image.getWidth());
173
        assertEquals(Integer.valueOf(300), image.getHeight());
174
    }
175

    
176
    @Test
177
    public void testMakeMediaCropLandcape() throws URISyntaxException {
178

    
179
        MediaUriTransformation transformation1 = new MediaUriTransformation();
180
        transformation1.setPathQueryFragment(new SearchReplace(PATTERN_1, "digilib/Scaler/?fn=$1/$2&mo=crop&dw=200&dh=147&uvfix=1"));
181
        transformation1.setMimeType("image/jpeg");
182
        transformation1.setWidth(200);
183
        transformation1.setHeight(147);
184
        MediaUriTransformationProcessor processor = new MediaUriTransformationProcessor();
185
        processor.add(transformation1);
186

    
187
        MediaRepresentation repr = makeImageMediaRepresentation(2000, 1500); // aspect ratio = 4/3
188

    
189
        List<MediaRepresentation> representations = processor.makeNewMediaRepresentationsFor(repr.getParts().get(0));
190

    
191
        assertEquals("https://pictures.bgbm.org/digilib/Scaler/?fn=Cyprus/Salvia_aethiopis_A1.jpg&mo=crop&dw=200&dh=147&uvfix=1", representations.get(0).getParts().get(0).getUri().toString());
192
        assertEquals(ImageFile.class, representations.get(0).getParts().get(0).getClass());
193
        ImageFile image = (ImageFile)representations.get(0).getParts().get(0);
194
        assertEquals(Integer.valueOf(200), image.getWidth());
195
        assertEquals(Integer.valueOf(147), image.getHeight());
196

    
197
        // aspect ratio = 3/4
198
        MediaRepresentation repr2 = makeImageMediaRepresentation(1500, 2000);
199

    
200
        representations = processor.makeNewMediaRepresentationsFor(repr2.getParts().get(0));
201

    
202
        assertEquals("https://pictures.bgbm.org/digilib/Scaler/?fn=Cyprus/Salvia_aethiopis_A1.jpg&mo=crop&dw=200&dh=147&uvfix=1", representations.get(0).getParts().get(0).getUri().toString());
203
        assertEquals(ImageFile.class, representations.get(0).getParts().get(0).getClass());
204
        image = (ImageFile)representations.get(0).getParts().get(0);
205
        assertEquals(Integer.valueOf(200), image.getWidth());
206
        assertEquals(Integer.valueOf(147), image.getHeight());
207
    }
208

    
209
    @Test
210
    public void testMakeMediaCropPortrait() throws URISyntaxException {
211

    
212
        MediaUriTransformation transformation1 = new MediaUriTransformation();
213
        transformation1.setPathQueryFragment(new SearchReplace(PATTERN_1, "digilib/Scaler/?fn=$1/$2&mo=crop&dw=147&dh=200&uvfix=1"));
214
        transformation1.setMimeType("image/jpeg");
215
        transformation1.setWidth(147);
216
        transformation1.setHeight(200);
217
        MediaUriTransformationProcessor processor = new MediaUriTransformationProcessor();
218
        processor.add(transformation1);
219

    
220
        MediaRepresentation repr = makeImageMediaRepresentation(2000, 1500); // aspect ratio = 4/3
221

    
222
        List<MediaRepresentation> representations = processor.makeNewMediaRepresentationsFor(repr.getParts().get(0));
223

    
224
        assertEquals("https://pictures.bgbm.org/digilib/Scaler/?fn=Cyprus/Salvia_aethiopis_A1.jpg&mo=crop&dw=147&dh=200&uvfix=1", representations.get(0).getParts().get(0).getUri().toString());
225
        assertEquals(ImageFile.class, representations.get(0).getParts().get(0).getClass());
226
        ImageFile image = (ImageFile)representations.get(0).getParts().get(0);
227
        assertEquals(Integer.valueOf(147), image.getWidth());
228
        assertEquals(Integer.valueOf(200), image.getHeight());
229

    
230
        // aspect ratio = 3/4
231
        MediaRepresentation repr2 = makeImageMediaRepresentation(1500, 2000);
232

    
233
        representations = processor.makeNewMediaRepresentationsFor(repr2.getParts().get(0));
234

    
235
        assertEquals("https://pictures.bgbm.org/digilib/Scaler/?fn=Cyprus/Salvia_aethiopis_A1.jpg&mo=crop&dw=147&dh=200&uvfix=1", representations.get(0).getParts().get(0).getUri().toString());
236
        assertEquals(ImageFile.class, representations.get(0).getParts().get(0).getClass());
237
        image = (ImageFile)representations.get(0).getParts().get(0);
238
        assertEquals(Integer.valueOf(147), image.getWidth());
239
        assertEquals(Integer.valueOf(200), image.getHeight());
240
    }
241

    
242
    @Test
243
    public void testMakeMediaCalculateExtend() throws URISyntaxException {
244

    
245
        MediaUriTransformation transformation1 = new MediaUriTransformation();
246
        transformation1.setPathQueryFragment(new SearchReplace(PATTERN_1, "digilib/Scaler/IIIF/$1!$2/!400,400/full/0/default.jpg"));
247
        transformation1.setMimeType("image/jpeg");
248
        transformation1.setWidth(400);
249
        transformation1.setHeight(400);
250
        MediaUriTransformationProcessor processor = new MediaUriTransformationProcessor();
251
        processor.add(transformation1);
252

    
253
        // aspect ratio = 4/3
254
        MediaRepresentation repr1 = makeImageMediaRepresentation(2000, 1500);
255

    
256
        List<MediaRepresentation> representations = processor.makeNewMediaRepresentationsFor(repr1.getParts().get(0));
257

    
258
        assertEquals("https://pictures.bgbm.org/digilib/Scaler/IIIF/Cyprus!Salvia_aethiopis_A1.jpg/!400,400/full/0/default.jpg", representations.get(0).getParts().get(0).getUri().toString());
259
        assertEquals(ImageFile.class, representations.get(0).getParts().get(0).getClass());
260
        ImageFile image = (ImageFile)representations.get(0).getParts().get(0);
261
        assertEquals(Integer.valueOf(400), image.getWidth());
262
        assertEquals(Integer.valueOf(300), image.getHeight());
263

    
264
        // aspect ratio = 3/4
265
        MediaRepresentation repr2 = makeImageMediaRepresentation(1500, 2000);
266

    
267
        representations = processor.makeNewMediaRepresentationsFor(repr2.getParts().get(0));
268

    
269
        assertEquals("https://pictures.bgbm.org/digilib/Scaler/IIIF/Cyprus!Salvia_aethiopis_A1.jpg/!400,400/full/0/default.jpg", representations.get(0).getParts().get(0).getUri().toString());
270
        assertEquals(ImageFile.class, representations.get(0).getParts().get(0).getClass());
271
        image = (ImageFile)representations.get(0).getParts().get(0);
272
        assertEquals(Integer.valueOf(300), image.getWidth());
273
        assertEquals(Integer.valueOf(400), image.getHeight());
274
    }
275
}
    (1-1/1)