-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(kotlin): migration of the ExtractorSelector to Kotlin
- Loading branch information
1 parent
a59dc23
commit 6f2fcc9
Showing
7 changed files
with
128 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 0 additions & 40 deletions
40
backend/src/main/java/lan/dk/podcastserver/manager/selector/ExtractorSelector.java
This file was deleted.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
...src/main/kotlin/com/github/davinkevin/podcastserver/manager/selector/ExtractorSelector.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.github.davinkevin.podcastserver.manager.selector | ||
|
||
import lan.dk.podcastserver.manager.worker.Extractor | ||
import lan.dk.podcastserver.manager.worker.noop.NoOpExtractor | ||
import org.springframework.aop.TargetClassAware | ||
import org.springframework.context.ApplicationContext | ||
import org.springframework.stereotype.Service | ||
|
||
/** | ||
* Created by kevin on 03/12/2017 | ||
*/ | ||
@Service | ||
class ExtractorSelector(val context: ApplicationContext, val extractors: Set<Extractor>) { | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
fun of(url: String?) = | ||
if (url.isNullOrEmpty()) { | ||
NO_OP_EXTRACTOR | ||
} else { | ||
val e = extractors.minBy { it.compatibility(url) }!! | ||
val eClass = (if (e is TargetClassAware) e.targetClass else e.javaClass) as Class<Extractor> | ||
context.getBean(eClass) | ||
} | ||
|
||
companion object { | ||
val NO_OP_EXTRACTOR: Extractor = NoOpExtractor() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 0 additions & 64 deletions
64
backend/src/test/java/lan/dk/podcastserver/manager/selector/ExtractorSelectorTest.java
This file was deleted.
Oops, something went wrong.
96 changes: 96 additions & 0 deletions
96
...test/kotlin/com/github/davinkevin/podcastserver/manager/selector/ExtractorSelectorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.github.davinkevin.podcastserver.manager.selector | ||
|
||
import com.github.davinkevin.podcastserver.manager.worker.francetv.FranceTvExtractor | ||
import com.github.davinkevin.podcastserver.manager.worker.sixplay.SixPlayExtractor | ||
import com.nhaarman.mockitokotlin2.any | ||
import com.nhaarman.mockitokotlin2.whenever | ||
import lan.dk.podcastserver.manager.worker.gulli.GulliExtractor | ||
import lan.dk.podcastserver.manager.worker.mycanal.MyCanalExtractor | ||
import lan.dk.podcastserver.manager.worker.noop.PassThroughExtractor | ||
import lan.dk.podcastserver.manager.worker.tf1replay.TF1ReplayExtractor | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.DisplayName | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
import org.junit.jupiter.params.ParameterizedTest | ||
import org.junit.jupiter.params.provider.Arguments | ||
import org.junit.jupiter.params.provider.MethodSource | ||
import org.mockito.junit.jupiter.MockitoSettings | ||
import org.mockito.quality.Strictness | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.test.mock.mockito.MockBean | ||
import org.springframework.context.ApplicationContext | ||
import org.springframework.test.context.junit.jupiter.SpringExtension | ||
import java.util.stream.Stream | ||
import kotlin.reflect.KClass | ||
|
||
/** | ||
* Created by kevin on 03/12/2017 | ||
*/ | ||
@ExtendWith(SpringExtension::class) | ||
@MockitoSettings(strictness = Strictness.LENIENT) | ||
class ExtractorSelectorTest { | ||
|
||
@MockBean lateinit var franceTvExtractor: FranceTvExtractor | ||
@MockBean lateinit var gulliExtractor: GulliExtractor | ||
@MockBean lateinit var myCanalExtractor: MyCanalExtractor | ||
@MockBean lateinit var passThroughExtractor: PassThroughExtractor | ||
@MockBean lateinit var sixPlayExtractor: SixPlayExtractor | ||
@MockBean lateinit var tf1ReplayExtractor: TF1ReplayExtractor | ||
|
||
@Autowired lateinit var applicationContext: ApplicationContext | ||
lateinit var extractor: ExtractorSelector | ||
|
||
@BeforeEach | ||
fun beforeEach() { | ||
val extractors = setOf( | ||
franceTvExtractor, | ||
gulliExtractor, | ||
myCanalExtractor, | ||
passThroughExtractor, | ||
sixPlayExtractor, | ||
tf1ReplayExtractor) | ||
|
||
extractors.forEach { whenever(it.compatibility(any())).thenCallRealMethod() } | ||
|
||
extractor = ExtractorSelector(applicationContext, extractors | ||
) | ||
} | ||
|
||
@Test | ||
fun `should return no op if empty string`() { | ||
/* When */ | ||
val extractorClass = extractor.of("") | ||
/* Then */ | ||
assertThat(extractorClass).isEqualTo(ExtractorSelector.NO_OP_EXTRACTOR) | ||
} | ||
|
||
@MethodSource("urlToExtractor") | ||
@DisplayName("should return") | ||
@ParameterizedTest(name = "{1} finder for {0}") | ||
fun `should return matching updater`(url: String, type: KClass<*>) { | ||
/* When */ | ||
val finderClass = extractor.of(url) | ||
/* Then */ | ||
assertThat(finderClass).isInstanceOf(type.java) | ||
} | ||
|
||
companion object { | ||
@JvmStatic | ||
fun urlToExtractor() = | ||
Stream.of( | ||
Arguments.of("http://www.beinsports.com/france/replay/lexpresso", PassThroughExtractor::class), | ||
Arguments.of("http://www.dailymotion.com/foo/bar", PassThroughExtractor::class), | ||
Arguments.of("http://www.france.tv/videos/comment_ca_va_bien.html", FranceTvExtractor::class), | ||
Arguments.of("http://replay.gulli.fr/videos/foo/bar", GulliExtractor::class), | ||
Arguments.of("http://www.jeuxvideo.com/chroniques-video.htm", PassThroughExtractor::class), | ||
Arguments.of("http://www.mycanal.fr/c-divertissement/c-le-grand-journal/pid5411-le-grand-journal.html", MyCanalExtractor::class), | ||
Arguments.of("http://foo.bar.com/to/rss/file.xml", PassThroughExtractor::class), | ||
Arguments.of("http://www.6play.fr/videos/foo/bar", SixPlayExtractor::class), | ||
Arguments.of("http://www.tf1.fr/videos/foo/bar", TF1ReplayExtractor::class), | ||
Arguments.of("http://www.youtube.com/channel/UC_ioajefokjFAOI", PassThroughExtractor::class) | ||
) | ||
} | ||
|
||
} |