Skip to content

Commit

Permalink
refactor(kotlin): migration of the ExtractorSelector to Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
davinkevin committed Dec 30, 2018
1 parent a59dc23 commit 6f2fcc9
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 107 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Podcast-Server

Back-end : [![Codacy Badge](https://api.codacy.com/project/badge/Grade/2030290b1c2145f6878e9ad7811c542e)](https://www.codacy.com/app/davin-kevin/Podcast-Server?utm_source=github.com&utm_medium=referral&utm_content=davinkevin/Podcast-Server&utm_campaign=Badge_Grade) [![Coverage Status](https://coveralls.io/repos/davinkevin/Podcast-Server/badge.svg?branch=master)](https://coveralls.io/r/davinkevin/Podcast-Server?branch=master)

Migration : ![Java stage](https://badgen.net/badge/Java/65%25/orange) ![Kotlin stage](https://badgen.net/badge/Kotlin/35%25/purple)
Migration : ![Java stage](https://badgen.net/badge/Java/64%25/orange) ![Kotlin stage](https://badgen.net/badge/Kotlin/36%25/purple)

Front-end : [![Code Climate](https://codeclimate.com/github/davinkevin/Podcast-Server/badges/gpa.svg)](https://codeclimate.com/github/davinkevin/Podcast-Server)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import lan.dk.podcastserver.manager.downloader.Downloader;
import lan.dk.podcastserver.manager.downloader.DownloadingItem;
import lan.dk.podcastserver.manager.selector.DownloaderSelector;
import lan.dk.podcastserver.manager.selector.ExtractorSelector;
import com.github.davinkevin.podcastserver.manager.selector.ExtractorSelector;
import lan.dk.podcastserver.repository.ItemRepository;
import lan.dk.podcastserver.service.properties.PodcastServerParameters;
import lombok.extern.slf4j.Slf4j;
Expand Down

This file was deleted.

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()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import lan.dk.podcastserver.manager.downloader.Downloader;
import lan.dk.podcastserver.manager.downloader.DownloadingItem;
import lan.dk.podcastserver.manager.selector.DownloaderSelector;
import lan.dk.podcastserver.manager.selector.ExtractorSelector;
import com.github.davinkevin.podcastserver.manager.selector.ExtractorSelector;
import lan.dk.podcastserver.manager.worker.noop.NoOpExtractor;
import lan.dk.podcastserver.repository.ItemRepository;
import lan.dk.podcastserver.service.properties.PodcastServerParameters;
Expand Down

This file was deleted.

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)
)
}

}

0 comments on commit 6f2fcc9

Please sign in to comment.