Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring service image #25

Merged
merged 10 commits into from
Nov 28, 2023
4 changes: 2 additions & 2 deletions buildSrc/src/main/kotlin/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ object Config {
const val minSdk = 21

const val major = 0
const val minor = 6
const val patch = 3
const val minor = 7
const val patch = 2
StaehliJ marked this conversation as resolved.
Show resolved Hide resolved
const val versionName = "$major.$minor.$patch"

const val maven_group = "ch.srg.data.provider"
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,25 @@ data class ImageUrl(
*
* @return the undecorated url
*/
val rawUrl: String
internal val rawUrl: String
) : Serializable {

@JvmOverloads
fun getIlImage(): IlImage {
return IlImage(rawUrl)
/**
* Url
*
* @param decorator The [ImageUrlDecorator] used to decorate the [rawUrl].
* @param widthPixels The width of the image.
* @return The decorated [rawUrl].
*/
fun url(decorator: ImageUrlDecorator, widthPixels: Int): String {
Loic-Dumas marked this conversation as resolved.
Show resolved Hide resolved
StaehliJ marked this conversation as resolved.
Show resolved Hide resolved
return decorator.decorate(rawUrl, widthPixels)
}

override fun toString(): String {
return rawUrl
}
}

interface ImageUrlDecorator {
fun decorate(source: String, widthPixels: Int): String
StaehliJ marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package ch.srg.dataProvider.integrationlayer

import android.net.Uri
import ch.srg.dataProvider.integrationlayer.data.ImageUrl
import ch.srg.dataProvider.integrationlayer.request.IlHost
import ch.srg.dataProvider.integrationlayer.request.image.DefaultImageUrlDecorator
import org.junit.Assert
import org.junit.Test

class TestDefaultImageUrlDecorator {
private val decorator = DefaultImageUrlDecorator(ilHost = IlHost.PROD)

@Test
fun testNonRtsUrl() {
val input = ImageUrl("https://ws.srf.ch/asset/image/audio/123")
val encodedInput = Uri.encode("https://ws.srf.ch/asset/image/audio/123")
val expected = "https://il.srgssr.ch/images/?imageUrl=${encodedInput}&format=webp&width=480"
Assert.assertEquals(expected, input.url(decorator, 480))
}

@Test
fun testRtsUrlWithoutImage() {
val input = ImageUrl("https://ws.rts.ch/asset/image/audio/123")
val encodedInput = Uri.encode("https://ws.rts.ch/asset/image/audio/123")
val expected = "https://il.srgssr.ch/images/?imageUrl=${encodedInput}&format=webp&width=480"
Assert.assertEquals(expected, input.url(decorator, 480))
}

@Test
fun testUrlWithImageOnly() {
val input = ImageUrl("https://ws.srf.ch/asset/image/audio/123.image")
val encodedInput = Uri.encode("https://ws.srf.ch/asset/image/audio/123.image")
val expected = "https://il.srgssr.ch/images/?imageUrl=${encodedInput}&format=webp&width=480"
Assert.assertEquals(expected, input.url(decorator, 480))
}

@Test
fun testRtsUrlWithImage() {
val input = ImageUrl("https://ws.rts.ch/asset/image/audio/123.image")
val expected = "https://ws.rts.ch/asset/image/audio/123.image/scale/width/460"
Assert.assertEquals(expected, input.url(decorator, 460))
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package ch.srg.dataProvider.integrationlayer

import android.net.Uri
import ch.srg.dataProvider.integrationlayer.data.ImageUrl
import ch.srg.dataProvider.integrationlayer.request.IlHost
import ch.srg.dataProvider.integrationlayer.request.image.IlHostImageUrlDecorator
import ch.srg.dataProvider.integrationlayer.request.image.ImageSize
import ch.srg.dataProvider.integrationlayer.request.image.ImageWidth
import ch.srg.dataProvider.integrationlayer.request.image.url
import org.junit.Assert.assertEquals
import org.junit.Test

class TestIlHostImageUrlDecorator {

private val decorator = IlHostImageUrlDecorator(ilHost = IlHost.PROD)

@Test
fun testPixelValid() {
val input = ImageUrl("https://ws.srf.ch/asset/image/audio/123")
val encodedInput = Uri.encode("https://ws.srf.ch/asset/image/audio/123")
val expected = "https://il.srgssr.ch/images/?imageUrl=${encodedInput}&format=webp&width=480"
assertEquals(expected, input.url(decorator, 480))
}

@Test
fun testPixelWidthInvalid() {
val input = ImageUrl("https://ws.srf.ch/asset/image/audio/123")
val encodedInput = Uri.encode("https://ws.srf.ch/asset/image/audio/123")
val expected = "https://il.srgssr.ch/images/?imageUrl=${encodedInput}&format=webp&width=480"
assertEquals(expected, input.url(decorator, 460))
}

@Test
fun testImageSize() {
val input = ImageUrl("https://ws.srf.ch/asset/image/audio/123")
val encodedInput = Uri.encode("https://ws.srf.ch/asset/image/audio/123")
val expected = "https://il.srgssr.ch/images/?imageUrl=${encodedInput}&format=webp&width=480"
assertEquals(expected, input.url(decorator, ImageSize.MEDIUM))
}

@Test
fun testImageWidth() {
val input = ImageUrl("https://ws.srf.ch/asset/image/audio/123")
val encodedInput = Uri.encode("https://ws.srf.ch/asset/image/audio/123")
val expected = "https://il.srgssr.ch/images/?imageUrl=${encodedInput}&format=webp&width=1920"
assertEquals(expected, input.url(decorator, ImageWidth.W1920))
}

@Test
fun testOtherIlHost() {
val input = ImageUrl("https://ws.srf.ch/asset/image/audio/123")
val encodedInput = Uri.encode("https://ws.srf.ch/asset/image/audio/123")
val expected = "https://il-stage.srgssr.ch/images/?imageUrl=${encodedInput}&format=webp&width=1920"
assertEquals(expected, input.url(decorator = IlHostImageUrlDecorator(IlHost.STAGE), width = ImageWidth.W1920))
}

@Test
fun testExtensionImageWidthWithIlHost() {
val input = ImageUrl("https://ws.srf.ch/asset/image/audio/123")
val encodedInput = Uri.encode("https://ws.srf.ch/asset/image/audio/123")
val expected = "https://il-stage.srgssr.ch/images/?imageUrl=${encodedInput}&format=webp&width=1920"
assertEquals(expected, input.url(ilHost = IlHost.STAGE, width = ImageWidth.W1920))
}

@Test
fun testExtensionImageSizeWithIlHost() {
val input = ImageUrl("https://ws.srf.ch/asset/image/audio/123")
val encodedInput = Uri.encode("https://ws.srf.ch/asset/image/audio/123")
val expected = "https://il-test.srgssr.ch/images/?imageUrl=${encodedInput}&format=webp&width=480"
assertEquals(expected, input.url(ilHost = IlHost.TEST, imageSize = ImageSize.MEDIUM))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void createFromValidUrn() {
}

@Test
public void testIsAudioValideUrn() {
public void testIsAudioValidUrn() {
Assert.assertFalse(IlUrn.isAudio("urn:rts:video:123456"));
Assert.assertTrue(IlUrn.isAudio("urn:rts:audio:123456"));
Assert.assertFalse(IlUrn.isAudio("urn:a:b:12345"));
Expand All @@ -58,7 +58,7 @@ public void testIsAudioNullUrn() {


@Test
public void testIsVideoValideUrn() {
public void testIsVideoValidUrn() {
Assert.assertTrue(IlUrn.isVideo("urn:rts:video:123456"));
Assert.assertFalse(IlUrn.isVideo("urn:rts:audio:123456"));
Assert.assertFalse(IlUrn.isVideo("urn:a:b:12345"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ch.srg.dataProvider.integrationlayer

import ch.srg.dataProvider.integrationlayer.data.ImageUrl
import ch.srg.dataProvider.integrationlayer.request.image.ImageSize
import ch.srg.dataProvider.integrationlayer.request.image.ImageWidth
import ch.srg.dataProvider.integrationlayer.request.image.ScaleWidthImageUrlDecorator
import ch.srg.dataProvider.integrationlayer.request.image.url
import org.junit.Assert.assertEquals
import org.junit.Test

class TestScaleWidthImageUrlDecorator {

private val decorator = ScaleWidthImageUrlDecorator

@Test
fun testScaleWidth() {
val input = ImageUrl("https://www.data.com/images/images.png")
val width = 458
val expected = "https://www.data.com/images/images.png/scale/width/458"
assertEquals(expected, input.url(decorator, width))
}

@Test
fun testScaleWidthImageSize() {
val input = ImageUrl("https://www.data.com/images/images.png")
val expected = "https://www.data.com/images/images.png/scale/width/480"
assertEquals(expected, input.url(decorator, ImageSize.MEDIUM))
}

@Test
fun testScaleWidthImageWidth() {
val input = ImageUrl("https://www.data.com/images/images.png")
val expected = "https://www.data.com/images/images.png/scale/width/1920"
assertEquals(expected, input.url(decorator, ImageWidth.W1920))
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ch.srg.dataProvider.integrationlayer.request.image

import ch.srg.dataProvider.integrationlayer.data.ImageUrlDecorator
import ch.srg.dataProvider.integrationlayer.request.IlHost

/**
* Copyright (c) SRG SSR. All rights reserved.
*
*
* License information is available from the LICENSE file.
*/

/**
* Default image url decorator
*
* For specific RTS image url the old [ScaleWidthImageUrlDecorator] is used, but it should be fixed soon or later.
StaehliJ marked this conversation as resolved.
Show resolved Hide resolved
* *
* @param ilHost The [IlHost] to use with [ilHostImageUrlDecorator].
*/
class DefaultImageUrlDecorator(ilHost: IlHost = IlHost.PROD) : ImageUrlDecorator {
private val ilHostImageUrlDecorator = IlHostImageUrlDecorator(ilHost)

override fun decorate(imageUrl: String, widthPixels: Int): String {
// FIXME https://github.com/SRGSSR/srgdataprovider-apple/issues/47 once RTS image service is well connected to Il Play image service.
return if (imageUrl.contains("rts.ch") && imageUrl.contains(".image")) {
ScaleWidthImageUrlDecorator.decorate(imageUrl, widthPixels)
} else {
ilHostImageUrlDecorator.decorate(imageUrl, widthPixels)
}
}
}
Loading