diff --git a/build.gradle b/build.gradle index 137cdd8d..98ec7122 100644 --- a/build.gradle +++ b/build.gradle @@ -3,13 +3,14 @@ plugins { id 'application' id 'jacoco' id 'org.sonarqube' version '6.0.0.5145' - id 'com.diffplug.spotless' version '6.25.0' + id 'com.diffplug.spotless' version '7.0.2' id 'org.springframework.boot' version '3.4.1' id 'io.spring.dependency-management' version '1.1.7' + id 'org.jetbrains.kotlin.jvm' version '2.1.20' } group = 'com.josdem.jmetadata' -version = '1.4.2' +version = '1.4.3' java { toolchain { @@ -17,6 +18,10 @@ java { } } +kotlin { + jvmToolchain(21) +} + application { mainClass = 'com.josdem.jmetadata.Launcher' } @@ -34,6 +39,7 @@ ext { mockitoVersion = '3.12.4' mockitoInlineVersion = '5.2.0' targetDir = "src/main/java/com/josdem/jmetadata" + mockitoKotlinVersion = "5.4.0" } configurations { @@ -56,6 +62,15 @@ spotless { removeUnusedImports() endWithNewline() } + kotlin { + target fileTree('.') { + include 'src/**/*.kt' + exclude '/build/**' + } + ktlint() + trimTrailingWhitespace() + endWithNewline() + } } dependencies { @@ -79,6 +94,7 @@ dependencies { testCompileOnly 'org.projectlombok:lombok' testAnnotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' + testImplementation "org.mockito.kotlin:mockito-kotlin:$mockitoKotlinVersion" implementation files('lib/last.fm-bindings-0.1.0.jar') } diff --git a/src/main/java/com/josdem/jmetadata/helper/ExporterHelper.java b/src/main/java/com/josdem/jmetadata/helper/ExporterHelper.java index 4fe22a0a..dcc82219 100644 --- a/src/main/java/com/josdem/jmetadata/helper/ExporterHelper.java +++ b/src/main/java/com/josdem/jmetadata/helper/ExporterHelper.java @@ -23,17 +23,18 @@ import java.io.IOException; import java.util.Collections; import java.util.List; +import lombok.RequiredArgsConstructor; import org.jaudiotagger.audio.exceptions.CannotReadException; import org.jaudiotagger.audio.exceptions.ReadOnlyFileException; import org.jaudiotagger.tag.TagException; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service +@RequiredArgsConstructor public class ExporterHelper { - @Autowired private ImageExporter imageExporter; - @Autowired private MetadataExporter metadataExporter; + private final ImageExporter imageExporter; + private final MetadataExporter metadataExporter; public ActionResult export(ExportPackage exportPackage) throws IOException, diff --git a/src/main/java/com/josdem/jmetadata/helper/LastFMAlbumHelper.java b/src/main/java/com/josdem/jmetadata/helper/LastFMAlbumHelper.java index 1138c275..d9eb2a2d 100644 --- a/src/main/java/com/josdem/jmetadata/helper/LastFMAlbumHelper.java +++ b/src/main/java/com/josdem/jmetadata/helper/LastFMAlbumHelper.java @@ -19,9 +19,8 @@ import com.josdem.jmetadata.Auth; import com.josdem.jmetadata.model.GenreTypes; import de.umass.lastfm.Album; -import java.text.SimpleDateFormat; +import java.time.LocalDate; import java.util.Collection; -import java.util.Date; import java.util.Iterator; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; @@ -35,14 +34,13 @@ public Album getAlbum(String artist, String album) { return Album.getInfo(artist, album, Auth.KEY); } - public String getYear(Date releaseDate) { + public String getYear(LocalDate releaseDate) { if (releaseDate == null) { return StringUtils.EMPTY; } - SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy"); - String year = simpleDateFormat.format(releaseDate); + var year = releaseDate.getYear(); log.info("Year: {}", year); - return year; + return String.valueOf(year); } public String getGenre(Album album) { diff --git a/src/main/java/com/josdem/jmetadata/helper/LastFMAuthenticator.java b/src/main/java/com/josdem/jmetadata/helper/LastFMAuthenticator.java index 6d6570e3..8408127e 100644 --- a/src/main/java/com/josdem/jmetadata/helper/LastFMAuthenticator.java +++ b/src/main/java/com/josdem/jmetadata/helper/LastFMAuthenticator.java @@ -18,14 +18,15 @@ import de.umass.lastfm.Session; import java.io.IOException; +import lombok.RequiredArgsConstructor; import org.apache.commons.lang3.StringUtils; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component +@RequiredArgsConstructor public class LastFMAuthenticator { - @Autowired private AuthenticatorHelper authenticatorHelper; + private final AuthenticatorHelper authenticatorHelper; public Session login(String username, String password) throws IOException { if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) { diff --git a/src/main/java/com/josdem/jmetadata/helper/LastFMTrackHelper.java b/src/main/java/com/josdem/jmetadata/helper/LastFMTrackHelper.java index f19e183c..5bf38c49 100644 --- a/src/main/java/com/josdem/jmetadata/helper/LastFMTrackHelper.java +++ b/src/main/java/com/josdem/jmetadata/helper/LastFMTrackHelper.java @@ -19,7 +19,9 @@ import de.umass.lastfm.Session; import de.umass.lastfm.Track; import de.umass.lastfm.scrobble.ScrobbleResult; +import org.springframework.stereotype.Component; +@Component public class LastFMTrackHelper { public ScrobbleResult scrobble(String artist, String title, int time, Session session) { diff --git a/src/main/java/com/josdem/jmetadata/helper/MetadataExporter.java b/src/main/java/com/josdem/jmetadata/helper/MetadataExporter.java index 0f3f7053..84502358 100644 --- a/src/main/java/com/josdem/jmetadata/helper/MetadataExporter.java +++ b/src/main/java/com/josdem/jmetadata/helper/MetadataExporter.java @@ -27,16 +27,17 @@ import java.io.IOException; import java.io.OutputStream; import java.util.List; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.jaudiotagger.audio.exceptions.CannotReadException; import org.jaudiotagger.audio.exceptions.ReadOnlyFileException; import org.jaudiotagger.tag.TagException; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Slf4j @Service +@RequiredArgsConstructor public class MetadataExporter { private static final String NEW_LINE = "\n"; private static final String DASH = " - "; @@ -44,11 +45,11 @@ public class MetadataExporter { private static final String PAR_OPEN = " ("; private static final String PAR_CLOSE = ")"; private static final String BY = " by "; - private FileUtils fileUtils = new FileUtils(); - private OutStreamWriter outputStreamWriter = new OutStreamWriter(); - @Autowired private MetadataService metadataService; - @Autowired private FormatterService formatter; + private final FileUtils fileUtils; + private final OutStreamWriter outputStreamWriter; + private final MetadataService metadataService; + private final FormatterService formatter; public void export(ExportPackage exportPackage) throws IOException, @@ -59,18 +60,18 @@ public void export(ExportPackage exportPackage) File file = fileUtils.createFile( exportPackage.getRoot(), StringUtils.EMPTY, ApplicationConstants.FILE_EXT); - log.info("Exporting metadata to: " + file.getAbsolutePath()); + log.info("Exporting metadata to: {}", file.getAbsolutePath()); OutputStream writer = outputStreamWriter.getWriter(file); int counter = 1; - List metadatas = exportPackage.getMetadataList(); - if (metadataService.isSameAlbum(metadatas)) { - writer.write(metadatas.get(0).getAlbum().getBytes()); + List metadataList = exportPackage.getMetadataList(); + if (metadataService.isSameAlbum(metadataList)) { + writer.write(metadataList.getFirst().getAlbum().getBytes()); writer.write(BY.getBytes()); - writer.write(metadatas.get(0).getArtist().getBytes()); + writer.write(metadataList.getFirst().getArtist().getBytes()); writer.write(NEW_LINE.getBytes()); writer.write(NEW_LINE.getBytes()); } - for (Metadata metadata : metadatas) { + for (Metadata metadata : metadataList) { writer.write(Integer.toString(counter++).getBytes()); writer.write(DOT.getBytes()); writer.write(metadata.getArtist().getBytes()); diff --git a/src/main/java/com/josdem/jmetadata/helper/OutStreamWriter.java b/src/main/java/com/josdem/jmetadata/helper/OutStreamWriter.java index 6b9e140b..682c3fd4 100644 --- a/src/main/java/com/josdem/jmetadata/helper/OutStreamWriter.java +++ b/src/main/java/com/josdem/jmetadata/helper/OutStreamWriter.java @@ -20,7 +20,9 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.OutputStream; +import org.springframework.stereotype.Component; +@Component public class OutStreamWriter { public OutputStream getWriter(File file) throws FileNotFoundException { diff --git a/src/main/java/com/josdem/jmetadata/helper/ScrobblerHelper.java b/src/main/java/com/josdem/jmetadata/helper/ScrobblerHelper.java index 0f98fb8d..dde02315 100644 --- a/src/main/java/com/josdem/jmetadata/helper/ScrobblerHelper.java +++ b/src/main/java/com/josdem/jmetadata/helper/ScrobblerHelper.java @@ -31,29 +31,30 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; +import lombok.RequiredArgsConstructor; +import lombok.Setter; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.asmatron.messengine.ControlEngine; import org.springframework.stereotype.Service; @Slf4j - -/** - * @understands A class who knows how to send scrobblings - */ @Service +@RequiredArgsConstructor public class ScrobblerHelper { private static final int ONE_THOUSAND = 1000; - private static final int MIN_LENGHT = 240; + private static final int MIN_LENGTH = 240; private static final int REQUEST_PERIOD = 250; - private Map metadataMap = new HashMap(); - private LastFMTrackHelper lastFMTrackHelper = new LastFMTrackHelper(); - private ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); + private final Map metadataMap = new HashMap(); private static final int DELTA = 120; - private ControlEngine controlEngine; + private final LastFMTrackHelper lastFMTrackHelper; + + @Setter private ControlEngine controlEngine; + + private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); - private ActionResult scrobbling(Metadata metadata) throws IOException, InterruptedException { + private ActionResult scrobbling(Metadata metadata) throws InterruptedException { User currentUser = controlEngine.get(Model.CURRENT_USER); if (StringUtils.isEmpty(currentUser.getUsername())) { return ActionResult.NOT_LOGGED; @@ -84,18 +85,14 @@ public ActionResult send(Metadata metadata) throws IOException, InterruptedExcep // According to submission rules http://www.last.fm/api/submissions if (StringUtils.isNotEmpty(metadata.getArtist()) && StringUtils.isNotEmpty(metadata.getTitle()) - && metadata.getLength() > MIN_LENGHT) { - long startTime = time - (metadataMap.size() * DELTA); + && metadata.getLength() > MIN_LENGTH) { + long startTime = time - ((long) metadataMap.size() * DELTA); metadataMap.put(metadata, startTime); return scrobbling(metadata); } return ActionResult.NOT_RECORDED; } - public void setControlEngine(ControlEngine controlEngine) { - this.controlEngine = controlEngine; - } - private class ScrobbleTask implements Callable { private final Metadata metadata; private final Session session; @@ -114,14 +111,13 @@ public ActionResult call() throws Exception { metadataMap.get(metadata).intValue(), session); if (result.isSuccessful() && !result.isIgnored()) { - log.debug( - metadata.getArtist() - + " - " - + metadata.getTitle() - + " scrobbling to Last.fm was Successful"); + log.info( + "{} - {} scrobbling to Last.fm was Successful", + metadata.getArtist(), + metadata.getTitle()); return ActionResult.SENT; } else { - log.error("Submitting track " + metadata.getTitle() + " to Last.fm failed: " + result); + log.error("Submitting track {} to Last.fm failed: {}", metadata.getTitle(), result); return ActionResult.ERROR; } } diff --git a/src/main/java/com/josdem/jmetadata/service/impl/LastFMCompleteServiceImpl.java b/src/main/java/com/josdem/jmetadata/service/impl/LastFMCompleteServiceImpl.java index e8f3233d..a62dc223 100644 --- a/src/main/java/com/josdem/jmetadata/service/impl/LastFMCompleteServiceImpl.java +++ b/src/main/java/com/josdem/jmetadata/service/impl/LastFMCompleteServiceImpl.java @@ -29,6 +29,8 @@ import de.umass.lastfm.ImageSize; import java.awt.Image; import java.io.IOException; +import java.time.LocalDate; +import java.time.ZoneId; import java.util.Date; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -117,7 +119,8 @@ private void setYear(Metadata metadata, LastfmAlbum lastfmAlbum) { } if (release != null) { log.info("Year date format: {}", release); - lastfmAlbum.setYear(lastfmHelper.getYear(release)); + var localDate = convertToLocalDateViaInstant(release); + lastfmAlbum.setYear(lastfmHelper.getYear(localDate)); log.info("Year metadata format: {}", lastfmAlbum.getYear()); } else { lastfmAlbum.setYear(StringUtils.EMPTY); @@ -142,4 +145,8 @@ public ActionResult isSomethingNew(LastfmAlbum lastfmAlbum, Metadata metadata) { } return ActionResult.NEW; } + + private LocalDate convertToLocalDateViaInstant(Date dateToConvert) { + return dateToConvert.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); + } } diff --git a/src/test/java/com/josdem/jmetadata/helper/ApplicationContextSingletonTest.java b/src/test/java/com/josdem/jmetadata/helper/ApplicationContextSingletonTest.java deleted file mode 100644 index 9aebf282..00000000 --- a/src/test/java/com/josdem/jmetadata/helper/ApplicationContextSingletonTest.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - Copyright 2025 Jose Morales contact@josdem.io - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package com.josdem.jmetadata.helper; - -import static org.junit.jupiter.api.Assertions.assertNotNull; - -import com.josdem.jmetadata.gui.LoginWindow; -import com.josdem.jmetadata.gui.MainWindow; -import org.asmatron.messengine.engines.DefaultEngine; -import org.junit.jupiter.api.Test; -import org.springframework.context.ConfigurableApplicationContext; - -class ApplicationContextSingletonTest { - - @Test - public void shouldCreateAnApplicationContext() throws Exception { - ConfigurableApplicationContext applicationContext = - ApplicationContextSingleton.getApplicationContext(); - DefaultEngine defaultEngine = applicationContext.getBean(DefaultEngine.class); - MainWindow mainWindow = applicationContext.getBean(MainWindow.class); - LoginWindow loginWindow = applicationContext.getBean(LoginWindow.class); - assertNotNull(defaultEngine); - assertNotNull(mainWindow); - assertNotNull(loginWindow); - } -} diff --git a/src/test/java/com/josdem/jmetadata/helper/ApplicationContextSingletonTest.kt b/src/test/java/com/josdem/jmetadata/helper/ApplicationContextSingletonTest.kt new file mode 100644 index 00000000..ba362cb2 --- /dev/null +++ b/src/test/java/com/josdem/jmetadata/helper/ApplicationContextSingletonTest.kt @@ -0,0 +1,41 @@ +/* + Copyright 2025 Jose Morales contact@josdem.io + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package com.josdem.jmetadata.helper + +import com.josdem.jmetadata.gui.LoginWindow +import com.josdem.jmetadata.gui.MainWindow +import org.asmatron.messengine.engines.DefaultEngine +import org.junit.jupiter.api.Assertions.assertNotNull +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.TestInfo +import org.slf4j.LoggerFactory + +internal class ApplicationContextSingletonTest { + private val log = LoggerFactory.getLogger(this::class.java) + + @Test + fun `should create an application context`(testInfo: TestInfo) { + log.info(testInfo.displayName) + val applicationContext = ApplicationContextSingleton.getApplicationContext() + val defaultEngine = applicationContext.getBean(DefaultEngine::class.java) + val mainWindow = applicationContext.getBean(MainWindow::class.java) + val loginWindow = applicationContext.getBean(LoginWindow::class.java) + assertNotNull(defaultEngine) + assertNotNull(mainWindow) + assertNotNull(loginWindow) + } +} diff --git a/src/test/java/com/josdem/jmetadata/helper/ArtworkHelperTest.java b/src/test/java/com/josdem/jmetadata/helper/ArtworkHelperTest.java deleted file mode 100644 index 04d769c4..00000000 --- a/src/test/java/com/josdem/jmetadata/helper/ArtworkHelperTest.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - Copyright 2025 Jose Morales contact@josdem.io - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package com.josdem.jmetadata.helper; - -import static org.junit.jupiter.api.Assertions.assertNotNull; - -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.ContextConfiguration; - -@Slf4j -@SpringBootTest -@ContextConfiguration(classes = {ApplicationContextSingleton.class, ArtworkHelper.class}) -@RequiredArgsConstructor(onConstructor_ = @Autowired) -class ArtworkHelperTest { - - private final ArtworkHelper artworkHelper; - - @Test - @DisplayName("creating an artwork") - public void shouldCreateAnArtWork(TestInfo testInfo) { - log.info(testInfo.getDisplayName()); - assertNotNull(artworkHelper.createArtwork(), "should create an artwork"); - } -} diff --git a/src/test/java/com/josdem/jmetadata/helper/ArtworkHelperTest.kt b/src/test/java/com/josdem/jmetadata/helper/ArtworkHelperTest.kt new file mode 100644 index 00000000..08cc4b8f --- /dev/null +++ b/src/test/java/com/josdem/jmetadata/helper/ArtworkHelperTest.kt @@ -0,0 +1,40 @@ +/* + Copyright 2025 Jose Morales contact@josdem.io + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package com.josdem.jmetadata.helper + +import org.junit.jupiter.api.Assertions.assertNotNull +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.TestInfo +import org.slf4j.LoggerFactory +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.test.context.ContextConfiguration + +@SpringBootTest +@ContextConfiguration(classes = [ApplicationContextSingleton::class, ArtworkHelper::class]) +internal class ArtworkHelperTest { + @Autowired + private lateinit var artworkHelper: ArtworkHelper + + private val log = LoggerFactory.getLogger(this::class.java) + + @Test + fun `should create an artwork helper`(testInfo: TestInfo) { + log.info(testInfo.displayName) + assertNotNull(artworkHelper.createArtwork()) { "should create an artwork" } + } +} diff --git a/src/test/java/com/josdem/jmetadata/helper/AudioFileHelperTest.java b/src/test/java/com/josdem/jmetadata/helper/AudioFileHelperTest.java deleted file mode 100644 index bd5abfe3..00000000 --- a/src/test/java/com/josdem/jmetadata/helper/AudioFileHelperTest.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - Copyright 2025 Jose Morales contact@josdem.io - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package com.josdem.jmetadata.helper; - -import static org.junit.jupiter.api.Assertions.assertNotNull; - -import java.io.File; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.ContextConfiguration; - -@Slf4j -@SpringBootTest -@ContextConfiguration(classes = {ApplicationContextSingleton.class, AudioFileHelper.class}) -@RequiredArgsConstructor(onConstructor_ = @Autowired) -class AudioFileHelperTest { - - private final AudioFileHelper audioFileHelper; - - private final File pepeGarden = - new File("src/test/resources/audio/Jaytech - Pepe Garden (Original Mix).mp3"); - - @Test - @DisplayName("reading audio file") - public void shouldRead(TestInfo testInfo) throws Exception { - log.info(testInfo.getDisplayName()); - assertNotNull(audioFileHelper.read(pepeGarden), "should read audio file"); - } -} diff --git a/src/test/java/com/josdem/jmetadata/helper/AudioFileHelperTest.kt b/src/test/java/com/josdem/jmetadata/helper/AudioFileHelperTest.kt new file mode 100644 index 00000000..0f15d7da --- /dev/null +++ b/src/test/java/com/josdem/jmetadata/helper/AudioFileHelperTest.kt @@ -0,0 +1,43 @@ +/* + Copyright 2025 Jose Morales contact@josdem.io + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package com.josdem.jmetadata.helper + +import org.junit.jupiter.api.Assertions.assertNotNull +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.TestInfo +import org.slf4j.LoggerFactory +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.test.context.ContextConfiguration +import java.io.File + +@SpringBootTest +@ContextConfiguration(classes = [ApplicationContextSingleton::class, AudioFileHelper::class]) +internal class AudioFileHelperTest { + @Autowired + private lateinit var audioFileHelper: AudioFileHelper + + private val pepeGardenFile = File("src/test/resources/audio/Jaytech - Pepe Garden (Original Mix).mp3") + + private val log = LoggerFactory.getLogger(this::class.java) + + @Test + fun `should read an audio file`(testInfo: TestInfo) { + log.info(testInfo.displayName) + assertNotNull(audioFileHelper.read(pepeGardenFile)) { "should read an audio file" } + } +} diff --git a/src/test/java/com/josdem/jmetadata/helper/ExporterHelperTest.java b/src/test/java/com/josdem/jmetadata/helper/ExporterHelperTest.java deleted file mode 100644 index d3fa8670..00000000 --- a/src/test/java/com/josdem/jmetadata/helper/ExporterHelperTest.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - Copyright 2025 Jose Morales contact@josdem.io - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package com.josdem.jmetadata.helper; - -import static org.mockito.Mockito.verify; - -import com.josdem.jmetadata.model.ExportPackage; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -class ExporterHelperTest { - @InjectMocks private ExporterHelper exporterHelper = new ExporterHelper(); - - @Mock private ImageExporter imageExporter; - @Mock private MetadataExporter metadataExporter; - @Mock private ExportPackage exportPackage; - - @BeforeEach - public void setup() throws Exception { - MockitoAnnotations.initMocks(this); - } - - @Test - public void shouldExportImage() throws Exception { - exporterHelper.export(exportPackage); - verify(imageExporter).export(exportPackage); - verify(metadataExporter).export(exportPackage); - } -} diff --git a/src/test/java/com/josdem/jmetadata/helper/ExporterHelperTest.kt b/src/test/java/com/josdem/jmetadata/helper/ExporterHelperTest.kt new file mode 100644 index 00000000..ad6a1a30 --- /dev/null +++ b/src/test/java/com/josdem/jmetadata/helper/ExporterHelperTest.kt @@ -0,0 +1,55 @@ +/* + Copyright 2025 Jose Morales contact@josdem.io + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package com.josdem.jmetadata.helper + +import com.josdem.jmetadata.model.ExportPackage +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.TestInfo +import org.mockito.Mock +import org.mockito.MockitoAnnotations +import org.mockito.kotlin.verify +import org.slf4j.LoggerFactory + +internal class ExporterHelperTest { + private lateinit var exporterHelper: ExporterHelper + + @Mock + private lateinit var imageExporter: ImageExporter + + @Mock + private lateinit var metadataExporter: MetadataExporter + + @Mock + private lateinit var exportPackage: ExportPackage + + private val log = LoggerFactory.getLogger(this::class.java) + + @BeforeEach + fun setUp() { + MockitoAnnotations.openMocks(this) + exporterHelper = ExporterHelper(imageExporter, metadataExporter) + } + + @Test + fun `should export image`(testInfo: TestInfo) { + log.info(testInfo.displayName) + exporterHelper.export(exportPackage) + verify(imageExporter).export(exportPackage) + verify(metadataExporter).export(exportPackage) + } +} diff --git a/src/test/java/com/josdem/jmetadata/helper/FormatterHelperTest.java b/src/test/java/com/josdem/jmetadata/helper/FormatterHelperTest.java deleted file mode 100644 index 738d9a2a..00000000 --- a/src/test/java/com/josdem/jmetadata/helper/FormatterHelperTest.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - Copyright 2025 Jose Morales contact@josdem.io - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package com.josdem.jmetadata.helper; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.ContextConfiguration; - -@Slf4j -@SpringBootTest -@ContextConfiguration(classes = {ApplicationContextSingleton.class, FormatterHelper.class}) -@RequiredArgsConstructor(onConstructor_ = @Autowired) -class FormatterHelperTest { - - private final FormatterHelper helper; - - @Test - @DisplayName("should format for comparison") - public void shouldFormatForComparison(TestInfo testInfo) { - log.info(testInfo.getDisplayName()); - var word = "de - pazz"; - var expectedWord = "depazz"; - assertEquals(expectedWord, helper.getBasicFormat(word)); - } -} diff --git a/src/test/java/com/josdem/jmetadata/helper/FormatterHelperTest.kt b/src/test/java/com/josdem/jmetadata/helper/FormatterHelperTest.kt new file mode 100644 index 00000000..97a60418 --- /dev/null +++ b/src/test/java/com/josdem/jmetadata/helper/FormatterHelperTest.kt @@ -0,0 +1,37 @@ +/* + Copyright 2025 Jose Morales contact@josdem.io + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package com.josdem.jmetadata.helper + +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.TestInfo +import org.slf4j.LoggerFactory + +internal class FormatterHelperTest { + private val formatterHelper: FormatterHelper = FormatterHelper() + + private val log = LoggerFactory.getLogger(this::class.java) + + @Test + fun `should format keyword deleting special characters`(testInfo: TestInfo) { + log.info(testInfo.displayName) + val word = "de - pazz" + val expectedWord = "depazz" + + assertEquals(expectedWord, formatterHelper.getBasicFormat(word)) + } +} diff --git a/src/test/java/com/josdem/jmetadata/helper/ImageExporterTest.java b/src/test/java/com/josdem/jmetadata/helper/ImageExporterTest.java deleted file mode 100644 index 876a2e8e..00000000 --- a/src/test/java/com/josdem/jmetadata/helper/ImageExporterTest.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - Copyright 2025 Jose Morales contact@josdem.io - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package com.josdem.jmetadata.helper; - -import static org.mockito.Mockito.*; - -import com.josdem.jmetadata.model.ExportPackage; -import com.josdem.jmetadata.model.Metadata; -import com.josdem.jmetadata.service.MetadataService; -import com.josdem.jmetadata.util.ImageUtils; -import java.awt.*; -import java.io.File; -import java.util.ArrayList; -import java.util.List; -import org.apache.commons.lang3.StringUtils; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -class ImageExporterTest { - private ImageExporter imageExporter; - - @Mock private ImageUtils imageUtils; - @Mock private Metadata metadata; - @Mock private Image coverArt; - @Mock private MetadataService metadataService; - - private final List metadataList = new ArrayList<>(); - private ExportPackage exportPackage; - @Mock private File root; - - @BeforeEach - void setup() throws Exception { - var title = "Bliksem"; - var album = "Bliksem Album"; - var artist = "Sander van Doorn"; - MockitoAnnotations.openMocks(this); - when(metadata.getAlbum()).thenReturn(album); - when(metadata.getArtist()).thenReturn(artist); - when(metadata.getTitle()).thenReturn(title); - metadataList.add(metadata); - exportPackage = new ExportPackage(root, metadataList); - imageExporter = new ImageExporter(imageUtils, metadataService); - } - - @Test - public void shouldExportASingleImage() throws Exception { - when(metadata.getCoverArt()).thenReturn(coverArt); - when(metadataService.isSameAlbum(metadataList)).thenReturn(true); - imageExporter.export(exportPackage); - verify(imageUtils) - .saveCoverArtToFile(metadataList.get(0).getCoverArt(), root, StringUtils.EMPTY); - } - - @Test - public void shouldExportASingleImageWhenSameAlbum() throws Exception { - when(metadata.getCoverArt()).thenReturn(coverArt); - when(metadataService.isSameAlbum(metadataList)).thenReturn(true); - metadataList.add(metadata); - imageExporter.export(exportPackage); - verify(imageUtils) - .saveCoverArtToFile(metadataList.get(0).getCoverArt(), root, StringUtils.EMPTY); - } - - @Test - public void shouldExportTwoImagesWhenDifAlbum() throws Exception { - when(metadata.getCoverArt()).thenReturn(coverArt); - Metadata metadata = setSecondMetadataExpectations(); - metadataList.add(metadata); - imageExporter.export(exportPackage); - verify(imageUtils).saveCoverArtToFile(coverArt, root, "Sander van Doorn" + "-" + "Bliksem"); - verify(imageUtils) - .saveCoverArtToFile(coverArt, root, "ATA" + "-" + "Blue Skies (Andy Tau Remix)"); - } - - private Metadata setSecondMetadataExpectations() { - Metadata metadata = mock(Metadata.class); - when(metadata.getAlbum()).thenReturn("Blue Skies"); - when(metadata.getArtist()).thenReturn("ATA"); - when(metadata.getTitle()).thenReturn("Blue Skies (Andy Tau Remix)"); - when(metadata.getCoverArt()).thenReturn(coverArt); - return metadata; - } - - @Test - public void shouldNotExportIfNoImage() throws Exception { - imageExporter.export(exportPackage); - verify(imageUtils, never()) - .saveCoverArtToFile(metadataList.get(0).getCoverArt(), root, StringUtils.EMPTY); - } -} diff --git a/src/test/java/com/josdem/jmetadata/helper/ImageExporterTest.kt b/src/test/java/com/josdem/jmetadata/helper/ImageExporterTest.kt new file mode 100644 index 00000000..2f5fa4c8 --- /dev/null +++ b/src/test/java/com/josdem/jmetadata/helper/ImageExporterTest.kt @@ -0,0 +1,105 @@ +/* + Copyright 2025 Jose Morales contact@josdem.io + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package com.josdem.jmetadata.helper + +import com.josdem.jmetadata.model.ExportPackage +import com.josdem.jmetadata.model.Metadata +import com.josdem.jmetadata.service.MetadataService +import com.josdem.jmetadata.util.ImageUtils +import org.apache.commons.lang3.StringUtils +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.TestInfo +import org.mockito.Mock +import org.mockito.Mockito.mock +import org.mockito.Mockito.`when` +import org.mockito.MockitoAnnotations +import org.mockito.kotlin.never +import org.mockito.kotlin.verify +import org.slf4j.LoggerFactory +import java.awt.Image +import java.io.File + +internal class ImageExporterTest { + private lateinit var imageExporter: ImageExporter + private lateinit var exportPackage: ExportPackage + + @Mock private lateinit var imageUtils: ImageUtils + + @Mock private lateinit var metadata: Metadata + + @Mock private lateinit var coverArt: Image + + @Mock private lateinit var metadataService: MetadataService + + @Mock private lateinit var root: File + + private val metadataList = mutableListOf() + + private val log = LoggerFactory.getLogger(this::class.java) + + @BeforeEach + @Throws(Exception::class) + fun setup() { + val title = "Bliksem" + val album = "Bliksem Album" + val artist = "Sander van Doorn" + MockitoAnnotations.openMocks(this) + `when`(metadata.album).thenReturn(album) + `when`(metadata.artist).thenReturn(artist) + `when`(metadata.title).thenReturn(title) + metadataList.add(metadata) + exportPackage = ExportPackage(root, metadataList) + imageExporter = ImageExporter(imageUtils, metadataService) + } + + @Test + fun `should export image`(testInfo: TestInfo) { + log.info(testInfo.displayName) + `when`(metadata.coverArt).thenReturn(coverArt) + `when`(metadataService.isSameAlbum(metadataList)).thenReturn(true) + imageExporter.export(exportPackage) + verify(imageUtils).saveCoverArtToFile(metadataList.first().coverArt, root, StringUtils.EMPTY) + } + + @Test + fun `should export different images`(testInfo: TestInfo) { + log.info(testInfo.displayName) + `when`(metadata.coverArt).thenReturn(coverArt) + val secondMetadata = setSecondMetadataExpectations() + metadataList.add(secondMetadata) + imageExporter.export(exportPackage) + verify(imageUtils).saveCoverArtToFile(coverArt, root, "Sander van Doorn-Bliksem") + verify(imageUtils).saveCoverArtToFile(coverArt, root, "ATA-Blue Skies (Andy Tau Remix)") + } + + @Test + fun `should not export if no image`(testInfo: TestInfo) { + log.info(testInfo.displayName) + imageExporter.export(exportPackage) + verify(imageUtils, never()).saveCoverArtToFile(metadataList.first().coverArt, root, StringUtils.EMPTY) + } + + private fun setSecondMetadataExpectations(): Metadata { + val metadata: Metadata = mock(Metadata::class.java) + `when`(metadata.album).thenReturn("Blue Skies") + `when`(metadata.artist).thenReturn("ATA") + `when`(metadata.title).thenReturn("Blue Skies (Andy Tau Remix)") + `when`(metadata.coverArt).thenReturn(coverArt) + return metadata + } +} diff --git a/src/test/java/com/josdem/jmetadata/helper/LastFMAlbumHelperTest.java b/src/test/java/com/josdem/jmetadata/helper/LastFMAlbumHelperTest.java deleted file mode 100644 index 91777fbe..00000000 --- a/src/test/java/com/josdem/jmetadata/helper/LastFMAlbumHelperTest.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - Copyright 2025 Jose Morales contact@josdem.io - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package com.josdem.jmetadata.helper; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.Mockito.when; - -import de.umass.lastfm.Album; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Date; -import org.apache.commons.lang3.StringUtils; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -class LastFMAlbumHelperTest { - private final LastFMAlbumHelper lastFMAlbumHelper = new LastFMAlbumHelper(); - - private final Date releaseDate = new Date(); - - @Mock private Album album; - - @BeforeEach - public void setup() throws Exception { - MockitoAnnotations.initMocks(this); - } - - @Test - public void shouldGetYear() throws Exception { - SimpleDateFormat simpleDateformat = new SimpleDateFormat("yyyy"); - String currentYear = simpleDateformat.format(releaseDate); - String year = lastFMAlbumHelper.getYear(releaseDate); - assertEquals(currentYear, year); - } - - @Test - public void shouldGetEmptyYear() throws Exception { - assertEquals(StringUtils.EMPTY, lastFMAlbumHelper.getYear(null)); - } - - @Test - public void shouldMatchAGenre() throws Exception { - Collection tags = new ArrayList(); - String tag = "House"; - tags.add(tag); - - when(album.getTags()).thenReturn(tags); - - String result = lastFMAlbumHelper.getGenre(album); - assertEquals(tag, result); - } - - @Test - public void shouldNotMatchAGenre() throws Exception { - Collection tags = new ArrayList(); - String tag = "usa"; - tags.add(tag); - - when(album.getTags()).thenReturn(tags); - - String result = lastFMAlbumHelper.getGenre(album); - assertEquals(StringUtils.EMPTY, result); - } -} diff --git a/src/test/java/com/josdem/jmetadata/helper/LastFMAlbumHelperTest.kt b/src/test/java/com/josdem/jmetadata/helper/LastFMAlbumHelperTest.kt new file mode 100644 index 00000000..93f76fc0 --- /dev/null +++ b/src/test/java/com/josdem/jmetadata/helper/LastFMAlbumHelperTest.kt @@ -0,0 +1,76 @@ +/* + Copyright 2025 Jose Morales contact@josdem.io + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package com.josdem.jmetadata.helper + +import de.umass.lastfm.Album +import org.apache.commons.lang3.StringUtils +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.TestInfo +import org.mockito.Mock +import org.mockito.Mockito.`when` +import org.mockito.MockitoAnnotations +import org.slf4j.LoggerFactory +import java.time.LocalDate + +internal class LastFMAlbumHelperTest { + private val lastFMAlbumHelper: LastFMAlbumHelper = LastFMAlbumHelper() + + private val releaseDate = LocalDate.now() + + @Mock private lateinit var album: Album + + private val log = LoggerFactory.getLogger(this::class.java) + + @BeforeEach + fun setup() { + MockitoAnnotations.openMocks(this) + } + + @Test + fun `should get year from release date`(testInfo: TestInfo) { + log.info(testInfo.displayName) + val year = lastFMAlbumHelper.getYear(releaseDate) + assertEquals(year, "${releaseDate.year}") { "should get year from release date" } + } + + @Test + fun `should get empty year when no info`(testInfo: TestInfo) { + log.info(testInfo.displayName) + val year = lastFMAlbumHelper.getYear(null) + assertEquals(year, "") { "should get empty year when no info" } + } + + @Test + fun `should get genres from album`(testInfo: TestInfo) { + log.info(testInfo.displayName) + val tags = listOf("House") + `when`(album.tags).thenReturn(tags) + val result = lastFMAlbumHelper.getGenre(album) + assertEquals("House", result) { "should get genre from album" } + } + + @Test + fun `should get empty genre when no info`(testInfo: TestInfo) { + log.info(testInfo.displayName) + val tags = listOf("ThisGenreDoesNotExist") + `when`(album.tags).thenReturn(tags) + val result = lastFMAlbumHelper.getGenre(album) + assertEquals(StringUtils.EMPTY, result) { "should get empty genre when no info" } + } +} diff --git a/src/test/java/com/josdem/jmetadata/helper/LastFMAuthenticatorTest.java b/src/test/java/com/josdem/jmetadata/helper/LastFMAuthenticatorTest.java deleted file mode 100644 index 7b4d4769..00000000 --- a/src/test/java/com/josdem/jmetadata/helper/LastFMAuthenticatorTest.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - Copyright 2025 Jose Morales contact@josdem.io - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package com.josdem.jmetadata.helper; - -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.mockito.Mockito.when; - -import de.umass.lastfm.Session; -import org.apache.commons.lang3.StringUtils; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -class LastFMAuthenticatorTest { - - @InjectMocks private final LastFMAuthenticator lastFMAuthenticator = new LastFMAuthenticator(); - - @Mock private AuthenticatorHelper authenticatorHelper; - @Mock private Session session; - - int result; - - @BeforeEach - public void initialize() { - MockitoAnnotations.initMocks(this); - } - - @Test - public void shouldLogin() throws Exception { - String username = "josdem"; - String password = "validPassword"; - - when(authenticatorHelper.getSession(username, password)).thenReturn(session); - - assertNotNull(lastFMAuthenticator.login(username, password)); - } - - @Test - public void shouldFailAtLoginIfNoUsernameAndPassword() throws Exception { - String username = StringUtils.EMPTY; - String password = StringUtils.EMPTY; - - assertNull(lastFMAuthenticator.login(username, password)); - } - - @Test - public void shouldFailAtLoginIfNoUsername() throws Exception { - String username = StringUtils.EMPTY; - String password = "somePassword"; - - assertNull(lastFMAuthenticator.login(username, password)); - } - - @Test - public void shouldFailAtLoginIfNoPassword() throws Exception { - String username = "someUsername"; - String password = StringUtils.EMPTY; - - assertNull(lastFMAuthenticator.login(username, password)); - } - - @Test - public void shouldFailAtLogin() throws Exception { - String username = "josdem"; - String password = "invalidPassword"; - - assertNull(lastFMAuthenticator.login(username, password)); - } -} diff --git a/src/test/java/com/josdem/jmetadata/helper/LastFMAuthenticatorTest.kt b/src/test/java/com/josdem/jmetadata/helper/LastFMAuthenticatorTest.kt new file mode 100644 index 00000000..cc8a7996 --- /dev/null +++ b/src/test/java/com/josdem/jmetadata/helper/LastFMAuthenticatorTest.kt @@ -0,0 +1,72 @@ +/* + Copyright 2025 Jose Morales contact@josdem.io + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package com.josdem.jmetadata.helper + +import de.umass.lastfm.Session +import org.apache.commons.lang3.StringUtils +import org.junit.jupiter.api.Assertions.assertNotNull +import org.junit.jupiter.api.Assertions.assertNull +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.TestInfo +import org.mockito.Mock +import org.mockito.Mockito.`when` +import org.mockito.MockitoAnnotations +import org.slf4j.LoggerFactory + +private const val USERNAME = "josdem" + +internal class LastFMAuthenticatorTest { + private lateinit var lastFMAuthenticator: LastFMAuthenticator + + @Mock private lateinit var authenticatorHelper: AuthenticatorHelper + + @Mock private lateinit var session: Session + + private val log = LoggerFactory.getLogger(this::class.java) + + @BeforeEach + fun setup() { + MockitoAnnotations.openMocks(this) + lastFMAuthenticator = LastFMAuthenticator(authenticatorHelper) + } + + @Test + fun `should login`(testInfo: TestInfo) { + log.info(testInfo.displayName) + val username = USERNAME + val password = "password" + `when`(authenticatorHelper.getSession(username, password)).thenReturn(session) + assertNotNull(lastFMAuthenticator.login(username, password)) { "should login" } + } + + @Test + fun `should fail to login due to not username nor password`(testInfo: TestInfo) { + log.info(testInfo.displayName) + val username = StringUtils.EMPTY + val password = StringUtils.EMPTY + assertNull(lastFMAuthenticator.login(username, password)) { "should fail to login due to not username nor password" } + } + + @Test + fun `should fail to login`(testInfo: TestInfo) { + log.info(testInfo.displayName) + val username = USERNAME + val password = "invalidPassword" + assertNull(lastFMAuthenticator.login(username, password)) { "should fail to login due to invalid credentials" } + } +} diff --git a/src/test/java/com/josdem/jmetadata/helper/MetadataAdapterTest.java b/src/test/java/com/josdem/jmetadata/helper/MetadataAdapterTest.java deleted file mode 100644 index 8e7442d8..00000000 --- a/src/test/java/com/josdem/jmetadata/helper/MetadataAdapterTest.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - Copyright 2025 Jose Morales contact@josdem.io - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package com.josdem.jmetadata.helper; - -import static org.mockito.Mockito.verify; - -import com.josdem.jmetadata.ApplicationConstants; -import com.josdem.jmetadata.model.Metadata; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -class MetadataAdapterTest { - - private final MetadataAdapter adapter = new MetadataAdapter(); - - @Mock private Metadata metadata; - private static final String ARTIST = "Daniel Kandi"; - private static final String TITLE = "Make Me Believe"; - private static final String ALBUM = "Anjunabeats 5"; - private static final String TRACK_NUMBER = "5"; - private static final String TOTAL_TRACKS = "13"; - private static final String GENRE_COLUMN = "Minimal Techno"; - private static final String YEAR_COLUMN = "2001"; - private static final String CD_NUMBER = "1"; - - private static final String TOTAL_CDS_NUMBER = null; - - @BeforeEach - public void setup() throws Exception { - MockitoAnnotations.initMocks(this); - } - - @Test - public void shouldUpdateArtist() throws Exception { - adapter.update(metadata, ApplicationConstants.ARTIST_COLUMN, ARTIST); - - verify(metadata).setArtist(ARTIST); - } - - @Test - public void shouldUpdateTitle() throws Exception { - adapter.update(metadata, ApplicationConstants.TITLE_COLUMN, TITLE); - - verify(metadata).setTitle(TITLE); - } - - @Test - public void shouldUpdateAlbum() throws Exception { - adapter.update(metadata, ApplicationConstants.ALBUM_COLUMN, ALBUM); - - verify(metadata).setAlbum(ALBUM); - } - - @Test - public void shouldUpdateTrackNumber() throws Exception { - adapter.update(metadata, ApplicationConstants.TRACK_NUMBER_COLUMN, TRACK_NUMBER); - - verify(metadata).setTrackNumber(TRACK_NUMBER); - } - - @Test - public void shouldUpdateTotalTracksNumber() throws Exception { - adapter.update(metadata, ApplicationConstants.TOTAL_TRACKS_NUMBER_COLUMN, TOTAL_TRACKS); - - verify(metadata).setTotalTracks(TOTAL_TRACKS); - } - - @Test - public void shouldUpdateGenre() throws Exception { - adapter.update(metadata, ApplicationConstants.GENRE_COLUMN, GENRE_COLUMN); - - verify(metadata).setGenre(GENRE_COLUMN); - } - - @Test - public void shouldUpdateYear() throws Exception { - adapter.update(metadata, ApplicationConstants.YEAR_COLUMN, YEAR_COLUMN); - - verify(metadata).setYear(YEAR_COLUMN); - } - - @Test - public void shouldUpdateCdNumber() throws Exception { - adapter.update(metadata, ApplicationConstants.CD_NUMBER_COLUMN, CD_NUMBER); - - verify(metadata).setCdNumber(CD_NUMBER); - } - - @Test - public void shouldUpdateTotalCds() throws Exception { - adapter.update(metadata, ApplicationConstants.TOTAL_CDS_NUMBER_COLUMN, TOTAL_CDS_NUMBER); - - verify(metadata).setTotalCds(TOTAL_CDS_NUMBER); - } -} diff --git a/src/test/java/com/josdem/jmetadata/helper/MetadataAdapterTest.kt b/src/test/java/com/josdem/jmetadata/helper/MetadataAdapterTest.kt new file mode 100644 index 00000000..82be08d6 --- /dev/null +++ b/src/test/java/com/josdem/jmetadata/helper/MetadataAdapterTest.kt @@ -0,0 +1,113 @@ +/* + Copyright 2025 Jose Morales contact@josdem.io + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package com.josdem.jmetadata.helper + +import com.josdem.jmetadata.ApplicationConstants +import com.josdem.jmetadata.model.Metadata +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.TestInfo +import org.mockito.Mock +import org.mockito.MockitoAnnotations +import org.mockito.kotlin.verify +import org.slf4j.LoggerFactory + +private const val ARTIST = "Daniel Kandi" +private const val TITLE = "Make Me Believe" +private const val ALBUM = "Anjunabeats 5" +private const val TRACK_NUMBER = "5" +private const val TOTAL_TRACKS = "13" +private const val GENRE = "Minimal Techno" +private const val YEAR = "2001" +private const val CD_NUMBER = "1" +private const val TOTAL_CDS_NUMBER = "2" + +internal class MetadataAdapterTest { + private val metadataAdapter = MetadataAdapter() + + @Mock private lateinit var metadata: Metadata + + private val log = LoggerFactory.getLogger(this::class.java) + + @BeforeEach + fun setUp() { + MockitoAnnotations.openMocks(this) + } + + @Test + fun `should update artist`(testInfo: TestInfo) { + log.info(testInfo.displayName) + metadataAdapter.update(metadata, ApplicationConstants.ARTIST_COLUMN, ARTIST) + verify(metadata).artist = ARTIST + } + + @Test + fun `should update title`(testInfo: TestInfo) { + log.info(testInfo.displayName) + metadataAdapter.update(metadata, ApplicationConstants.TITLE_COLUMN, TITLE) + verify(metadata).title = TITLE + } + + @Test + fun `should update album`(testInfo: TestInfo) { + log.info(testInfo.displayName) + metadataAdapter.update(metadata, ApplicationConstants.ALBUM_COLUMN, ALBUM) + verify(metadata).album = ALBUM + } + + @Test + fun `should update track number`(testInfo: TestInfo) { + log.info(testInfo.displayName) + metadataAdapter.update(metadata, ApplicationConstants.TRACK_NUMBER_COLUMN, TRACK_NUMBER) + verify(metadata).trackNumber = TRACK_NUMBER + } + + @Test + fun `should update total tracks number`(testInfo: TestInfo) { + log.info(testInfo.displayName) + metadataAdapter.update(metadata, ApplicationConstants.TOTAL_TRACKS_NUMBER_COLUMN, TOTAL_TRACKS) + verify(metadata).totalTracks = TOTAL_TRACKS + } + + @Test + fun `should update genre`(testInfo: TestInfo) { + log.info(testInfo.displayName) + metadataAdapter.update(metadata, ApplicationConstants.GENRE_COLUMN, GENRE) + verify(metadata).genre = GENRE + } + + @Test + fun `should update year`(testInfo: TestInfo) { + log.info(testInfo.displayName) + metadataAdapter.update(metadata, ApplicationConstants.YEAR_COLUMN, YEAR) + verify(metadata).year = YEAR + } + + @Test + fun `should update cd number`(testInfo: TestInfo) { + log.info(testInfo.displayName) + metadataAdapter.update(metadata, ApplicationConstants.CD_NUMBER_COLUMN, CD_NUMBER) + verify(metadata).cdNumber = CD_NUMBER + } + + @Test + fun `should update total cds`(testInfo: TestInfo) { + log.info(testInfo.displayName) + metadataAdapter.update(metadata, ApplicationConstants.TOTAL_CDS_NUMBER_COLUMN, TOTAL_CDS_NUMBER) + verify(metadata).totalCds = TOTAL_CDS_NUMBER + } +} diff --git a/src/test/java/com/josdem/jmetadata/helper/MetadataExporterTest.java b/src/test/java/com/josdem/jmetadata/helper/MetadataExporterTest.java deleted file mode 100644 index 85bb60cc..00000000 --- a/src/test/java/com/josdem/jmetadata/helper/MetadataExporterTest.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - Copyright 2025 Jose Morales contact@josdem.io - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package com.josdem.jmetadata.helper; - -import static org.mockito.Mockito.*; - -import com.josdem.jmetadata.ApplicationConstants; -import com.josdem.jmetadata.model.ExportPackage; -import com.josdem.jmetadata.model.Metadata; -import com.josdem.jmetadata.service.FormatterService; -import com.josdem.jmetadata.service.MetadataService; -import com.josdem.jmetadata.util.FileUtils; -import java.io.File; -import java.io.OutputStream; -import java.util.ArrayList; -import java.util.List; -import org.apache.commons.lang3.StringUtils; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -class MetadataExporterTest { - private static final String NEW_LINE = "\n"; - private static final String DASH = " - "; - private static final String DOT = ". "; - private static final String PAR_OPEN = " ("; - private static final String PAR_CLOSE = ")"; - private static final String BY = " by "; - - @InjectMocks private MetadataExporter metadataExporter = new MetadataExporter(); - - @Mock private FileUtils fileUtils; - @Mock private FormatterService formatter; - @Mock private Metadata metadata; - @Mock private File file; - @Mock private OutStreamWriter outputStreamWriter; - @Mock private OutputStream writer; - @Mock private ExportPackage exportPackage; - @Mock private MetadataService metadataService; - - int length = 397; - - private final List metadatas = new ArrayList<>(); - - @BeforeEach - public void setup() throws Exception { - MockitoAnnotations.initMocks(this); - String album = "Bliksem"; - when(metadata.getAlbum()).thenReturn(album); - String artist = "Sander van Doorn"; - when(metadata.getArtist()).thenReturn(artist); - String title = "Bliksem"; - when(metadata.getTitle()).thenReturn(title); - when(metadata.getLength()).thenReturn(length); - String lenghtFormated = "6:37"; - when(formatter.getDuration(metadata.getLength())).thenReturn(lenghtFormated); - metadatas.add(metadata); - when(exportPackage.getRoot()).thenReturn(file); - when(exportPackage.getMetadataList()).thenReturn(metadatas); - when(fileUtils.createFile(file, StringUtils.EMPTY, ApplicationConstants.FILE_EXT)) - .thenReturn(file); - when(outputStreamWriter.getWriter(file)).thenReturn(writer); - } - - @Test - public void shouldExport() throws Exception { - when(metadataService.isSameAlbum(metadatas)).thenReturn(true); - - metadataExporter.export(exportPackage); - - verify(writer, times(2)).write(metadata.getAlbum().getBytes()); - verify(writer).write(BY.getBytes()); - verify(writer, times(2)).write(metadata.getArtist().getBytes()); - verify(writer, times(3)).write(NEW_LINE.getBytes()); - - verify(metadataService).isSameAlbum(metadatas); - verify(writer).write(Integer.toString(1).getBytes()); - verify(writer).write(DOT.getBytes()); - verify(writer).write(DASH.getBytes()); - verify(writer, times(2)).write(metadata.getTitle().getBytes()); - verify(writer).write(PAR_OPEN.getBytes()); - verify(writer).write(formatter.getDuration(metadata.getLength()).getBytes()); - verify(writer).write(PAR_CLOSE.getBytes()); - } -} diff --git a/src/test/java/com/josdem/jmetadata/helper/MetadataExporterTest.kt b/src/test/java/com/josdem/jmetadata/helper/MetadataExporterTest.kt new file mode 100644 index 00000000..fde77c8f --- /dev/null +++ b/src/test/java/com/josdem/jmetadata/helper/MetadataExporterTest.kt @@ -0,0 +1,105 @@ +/* + Copyright 2025 Jose Morales contact@josdem.io + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package com.josdem.jmetadata.helper + +import com.josdem.jmetadata.ApplicationConstants +import com.josdem.jmetadata.model.ExportPackage +import com.josdem.jmetadata.model.Metadata +import com.josdem.jmetadata.service.FormatterService +import com.josdem.jmetadata.service.MetadataService +import com.josdem.jmetadata.util.FileUtils +import org.apache.commons.lang3.StringUtils +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.TestInfo +import org.mockito.Mock +import org.mockito.Mockito.times +import org.mockito.Mockito.`when` +import org.mockito.MockitoAnnotations +import org.mockito.kotlin.verify +import org.slf4j.LoggerFactory +import java.io.File +import java.io.OutputStream + +private const val DURATION = 397 +private const val NEW_LINE = "\n" +private const val DASH = " - " +private const val DOT = ". " +private const val PAR_OPEN = " (" +private const val PAR_CLOSE = ")" +private const val BY = " by " + +internal class MetadataExporterTest { + private lateinit var metadataExporter: MetadataExporter + + @Mock private lateinit var fileUtils: FileUtils + + @Mock private lateinit var outStreamWriter: OutStreamWriter + + @Mock private lateinit var metadataService: MetadataService + + @Mock private lateinit var formatterService: FormatterService + + @Mock private lateinit var exportPackage: ExportPackage + + @Mock private lateinit var file: File + + @Mock private lateinit var outputStream: OutputStream + + @Mock private lateinit var metadata: Metadata + + private val metadataList = mutableListOf() + + private val log = LoggerFactory.getLogger(this::class.java) + + @BeforeEach + fun setup() { + MockitoAnnotations.openMocks(this) + metadataExporter = MetadataExporter(fileUtils, outStreamWriter, metadataService, formatterService) + metadataList.add(metadata) + `when`(metadata.album).thenReturn("Bliksem") + `when`(metadata.artist).thenReturn("Sander Van Doorn") + `when`(metadata.title).thenReturn("Bliksem track") + `when`(metadata.length).thenReturn(DURATION) + `when`(formatterService.getDuration(DURATION)).thenReturn("6:37") + `when`(exportPackage.root).thenReturn(file) + `when`(exportPackage.metadataList).thenReturn(metadataList) + `when`(fileUtils.createFile(file, StringUtils.EMPTY, ApplicationConstants.FILE_EXT)).thenReturn(file) + `when`(outStreamWriter.getWriter(file)).thenReturn(outputStream) + } + + @Test + fun `should export metadata to file`(testInfo: TestInfo) { + log.info(testInfo.displayName) + `when`(metadataService.isSameAlbum(metadataList)).thenReturn(true) + + metadataExporter.export(exportPackage) + + verify(outputStream).write(metadata.album.toByteArray()) + verify(outputStream).write(BY.toByteArray()) + verify(outputStream, times(2)).write(metadata.artist.toByteArray()) + verify(outputStream, times(3)).write(NEW_LINE.toByteArray()) + verify(metadataService).isSameAlbum(metadataList) + verify(outputStream).write("1".toByteArray()) + verify(outputStream).write(DOT.toByteArray()) + verify(outputStream).write(DASH.toByteArray()) + verify(outputStream).write(metadata.title.toByteArray()) + verify(outputStream).write(PAR_OPEN.toByteArray()) + verify(outputStream).write(formatterService.getDuration(metadata.length).toByteArray()) + verify(outputStream).write(PAR_CLOSE.toByteArray()) + } +} diff --git a/src/test/java/com/josdem/jmetadata/helper/MetadataHelperTest.java b/src/test/java/com/josdem/jmetadata/helper/MetadataHelperTest.java deleted file mode 100644 index 3fd9b7bc..00000000 --- a/src/test/java/com/josdem/jmetadata/helper/MetadataHelperTest.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - Copyright 2025 Jose Morales contact@josdem.io - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package com.josdem.jmetadata.helper; - -import static org.junit.jupiter.api.Assertions.assertNotNull; - -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.ContextConfiguration; - -@Slf4j -@SpringBootTest -@ContextConfiguration(classes = {ApplicationContextSingleton.class, MetadataHelper.class}) -@RequiredArgsConstructor(onConstructor_ = @Autowired) -class MetadataHelperTest { - - private final MetadataHelper metadataHelper; - - @Test - @DisplayName("creating a hashset") - public void shouldCreateAHashset(TestInfo testInfo) { - log.info(testInfo.getDisplayName()); - assertNotNull(metadataHelper.createHashSet(), "should create a hashset"); - } -} diff --git a/src/test/java/com/josdem/jmetadata/helper/MetadataHelperTest.kt b/src/test/java/com/josdem/jmetadata/helper/MetadataHelperTest.kt new file mode 100644 index 00000000..9db31140 --- /dev/null +++ b/src/test/java/com/josdem/jmetadata/helper/MetadataHelperTest.kt @@ -0,0 +1,34 @@ +/* + Copyright 2025 Jose Morales contact@josdem.io + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package com.josdem.jmetadata.helper + +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.TestInfo +import org.slf4j.LoggerFactory + +internal class MetadataHelperTest { + private val metadataHelper = MetadataHelper() + + private val log = LoggerFactory.getLogger(this::class.java) + + @Test + fun `should create a hashset from the helper`(testInfo: TestInfo) { + log.info(testInfo.displayName) + val hashSet = metadataHelper.createHashSet() + assert(hashSet.isEmpty()) { "should create a hashset from the helper" } + } +} diff --git a/src/test/java/com/josdem/jmetadata/helper/ReaderHelperTest.java b/src/test/java/com/josdem/jmetadata/helper/ReaderHelperTest.java deleted file mode 100644 index cf010175..00000000 --- a/src/test/java/com/josdem/jmetadata/helper/ReaderHelperTest.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - Copyright 2025 Jose Morales contact@josdem.io - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package com.josdem.jmetadata.helper; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.Mockito.when; - -import lombok.extern.slf4j.Slf4j; -import org.jaudiotagger.tag.FieldKey; -import org.jaudiotagger.tag.Tag; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -@Slf4j -class ReaderHelperTest { - - @InjectMocks private ReaderHelper readerHelper = new ReaderHelper(); - - @Mock private Tag tag; - - @BeforeEach - public void setup() throws Exception { - MockitoAnnotations.openMocks(this); - } - - @Test - @DisplayName("getting genre") - public void shouldGetGenre(TestInfo testInfo) { - log.info(testInfo.getDisplayName()); - var genre = "Minimal Techno"; - when(tag.getFirst(FieldKey.GENRE)).thenReturn(genre); - assertEquals(genre, readerHelper.getGenre(tag, genre)); - } - - @Test - @DisplayName("getting genre by code") - public void shouldGetGenreByCodeWithParentheses(TestInfo testInfo) { - log.info(testInfo.getDisplayName()); - var genreAsCode = "(18)"; - var genre = "Techno"; - when(tag.getFirst(FieldKey.GENRE)).thenReturn(genreAsCode); - assertEquals(genre, readerHelper.getGenre(tag, genreAsCode)); - } - - @Test - @DisplayName("getting genre when not valid number") - public void shouldKnowWhenMp3IsNotANumberInsideParenthesis(TestInfo testInfo) { - log.info(testInfo.getDisplayName()); - var genre = "(None)"; - when(tag.getFirst(FieldKey.GENRE)).thenReturn(genre); - assertEquals(genre, readerHelper.getGenre(tag, genre)); - } -} diff --git a/src/test/java/com/josdem/jmetadata/helper/ReaderHelperTest.kt b/src/test/java/com/josdem/jmetadata/helper/ReaderHelperTest.kt new file mode 100644 index 00000000..822ffd8b --- /dev/null +++ b/src/test/java/com/josdem/jmetadata/helper/ReaderHelperTest.kt @@ -0,0 +1,66 @@ +/* + Copyright 2025 Jose Morales contact@josdem.io + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package com.josdem.jmetadata.helper + +import org.jaudiotagger.tag.FieldKey +import org.jaudiotagger.tag.Tag +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.TestInfo +import org.mockito.Mock +import org.mockito.Mockito.`when` +import org.mockito.MockitoAnnotations +import org.slf4j.LoggerFactory + +internal class ReaderHelperTest { + private val readerHelper = ReaderHelper() + + @Mock private lateinit var tag: Tag + + private val log = LoggerFactory.getLogger(this::class.java) + + @BeforeEach + fun setup() { + MockitoAnnotations.openMocks(this) + } + + @Test + fun `should get genre`(testInfo: TestInfo) { + log.info(testInfo.displayName) + val genre = "Minimal Techno" + `when`(tag.getFirst(FieldKey.GENRE)).thenReturn(genre) + assertEquals(genre, readerHelper.getGenre(tag, genre)) { "should get genre" } + } + + @Test + fun `should get genre by code`(testInfo: TestInfo) { + log.info(testInfo.displayName) + val genreAsCode = "(18)" + val genre = "Techno" + `when`(tag.getFirst(FieldKey.GENRE)).thenReturn(genreAsCode) + assertEquals(genre, readerHelper.getGenre(tag, genreAsCode)) { "should get genre by code" } + } + + @Test + fun `should return same value when no valid genre`(testInfo: TestInfo) { + log.info(testInfo.displayName) + val genre = "(None)" + `when`(tag.getFirst(FieldKey.GENRE)).thenReturn(genre) + assertEquals(genre, readerHelper.getGenre(tag, genre)) { "should return same value when no valid genre" } + } +} diff --git a/src/test/java/com/josdem/jmetadata/helper/ScrobblerHelperTest.java b/src/test/java/com/josdem/jmetadata/helper/ScrobblerHelperTest.java deleted file mode 100644 index 7a75b30c..00000000 --- a/src/test/java/com/josdem/jmetadata/helper/ScrobblerHelperTest.java +++ /dev/null @@ -1,155 +0,0 @@ -/* - Copyright 2025 Jose Morales contact@josdem.io - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package com.josdem.jmetadata.helper; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.Matchers.isA; -import static org.mockito.Mockito.*; - -import com.josdem.jmetadata.action.ActionResult; -import com.josdem.jmetadata.model.Metadata; -import com.josdem.jmetadata.model.Model; -import com.josdem.jmetadata.model.User; -import de.umass.lastfm.Session; -import de.umass.lastfm.scrobble.ScrobbleResult; -import java.util.Map; -import org.apache.commons.lang3.StringUtils; -import org.asmatron.messengine.ControlEngine; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -class ScrobblerHelperTest { - private static final String TRACK_NUMBER = "1"; - - @InjectMocks private ScrobblerHelper helperScrobbler = new ScrobblerHelper(); - - @Mock private Metadata metadata; - @Mock private Map metadataMap; - @Mock private ControlEngine controlEngine; - @Mock private User currentUser; - @Mock private LastFMTrackHelper lastFMTrackHelper; - @Mock private Session session; - - private ActionResult result; - private String username = "josdem"; - private String password = "password"; - - @BeforeEach - public void setup() { - MockitoAnnotations.initMocks(this); - when(currentUser.getUsername()).thenReturn(username); - when(currentUser.getPassword()).thenReturn(password); - when(controlEngine.get(Model.CURRENT_USER)).thenReturn(currentUser); - helperScrobbler.setControlEngine(controlEngine); - } - - @Test - public void shouldNotAddAScrobblingifTrackSmallerThan240() throws Exception { - setExpectations(); - when(metadata.getArtist()).thenReturn("Above & Beyond"); - when(metadata.getTitle()).thenReturn("Anjunabeach"); - - result = helperScrobbler.send(metadata); - - notSendToScrobblingMapAssertion(); - } - - @Test - public void shouldNotAddAScrobblingIfNoArtist() throws Exception { - setExpectations(); - when(metadata.getArtist()).thenReturn(StringUtils.EMPTY); - when(metadata.getTitle()).thenReturn("Anjunabeach"); - - result = helperScrobbler.send(metadata); - - notSendToScrobblingMapAssertion(); - } - - private void notSendToScrobblingMapAssertion() { - verify(metadataMap, never()).size(); - verify(metadataMap, never()).put(isA(Metadata.class), isA(Long.class)); - assertEquals(ActionResult.NOT_RECORDED, result); - } - - @Test - public void shouldNotAddAScrobblingIfNoTitle() throws Exception { - setExpectations(); - when(metadata.getArtist()).thenReturn("Above & Beyond"); - when(metadata.getTitle()).thenReturn(StringUtils.EMPTY); - - result = helperScrobbler.send(metadata); - notSendToScrobblingMapAssertion(); - } - - @Test - public void shouldFailWhenSubmitScrobbler() throws Exception { - ScrobbleResult result = mock(ScrobbleResult.class); - when(metadataMap.get(metadata)).thenReturn(100L); - setExpectations(); - setMetadataTrackExpectations(); - when(result.isSuccessful()).thenReturn(false); - when(lastFMTrackHelper.scrobble( - metadata.getArtist(), - metadata.getTitle(), - metadataMap.get(metadata).intValue(), - currentUser.getSession())) - .thenReturn(result); - - assertEquals(ActionResult.SESSION_LESS, helperScrobbler.send(metadata)); - } - - private void setMetadataTrackExpectations() { - when(metadata.getLength()).thenReturn(300); - when(metadata.getArtist()).thenReturn("Above & Beyond"); - when(metadata.getTitle()).thenReturn("Anjunabeach"); - } - - @Test - public void shouldSendAnScrobbler() throws Exception { - ScrobbleResult result = mock(ScrobbleResult.class); - when(metadataMap.get(metadata)).thenReturn(100L); - when(currentUser.getSession()).thenReturn(session); - setExpectations(); - setMetadataTrackExpectations(); - when(result.isSuccessful()).thenReturn(true); - when(lastFMTrackHelper.scrobble( - metadata.getArtist(), - metadata.getTitle(), - metadataMap.get(metadata).intValue(), - currentUser.getSession())) - .thenReturn(result); - - assertEquals(ActionResult.SENT, helperScrobbler.send(metadata)); - } - - private void setExpectations() { - when(metadata.getAlbum()).thenReturn(StringUtils.EMPTY); - when(metadata.getLength()).thenReturn(1); - when(metadata.getTrackNumber()).thenReturn(TRACK_NUMBER); - } - - @Test - public void shouldReturnIfNoLogin() throws Exception { - setMetadataTrackExpectations(); - when(currentUser.getUsername()).thenReturn(StringUtils.EMPTY); - - assertEquals(ActionResult.NOT_LOGGED, helperScrobbler.send(metadata)); - } -} diff --git a/src/test/java/com/josdem/jmetadata/helper/ScrobblerHelperTest.kt b/src/test/java/com/josdem/jmetadata/helper/ScrobblerHelperTest.kt new file mode 100644 index 00000000..9397a744 --- /dev/null +++ b/src/test/java/com/josdem/jmetadata/helper/ScrobblerHelperTest.kt @@ -0,0 +1,151 @@ +/* + Copyright 2025 Jose Morales contact@josdem.io + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package com.josdem.jmetadata.helper + +import com.josdem.jmetadata.action.ActionResult +import com.josdem.jmetadata.model.Metadata +import com.josdem.jmetadata.model.Model +import com.josdem.jmetadata.model.User +import de.umass.lastfm.scrobble.ScrobbleResult +import org.apache.commons.lang3.StringUtils +import org.asmatron.messengine.ControlEngine +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.TestInfo +import org.mockito.ArgumentMatchers.isA +import org.mockito.Mock +import org.mockito.Mockito.never +import org.mockito.Mockito.`when` +import org.mockito.MockitoAnnotations +import org.mockito.kotlin.verify +import org.slf4j.LoggerFactory + +private const val USERNAME = "josdem" +private const val PASSWORD = "password" +private const val TRACK_NUMBER = "1" + +internal class ScrobblerHelperTest { + private lateinit var scrobblerHelper: ScrobblerHelper + private lateinit var result: ActionResult + + @Mock private lateinit var lastFMTrackHelper: LastFMTrackHelper + + @Mock private lateinit var metadata: Metadata + + @Mock private lateinit var currentUser: User + + @Mock private lateinit var controlEngine: ControlEngine + + @Mock private lateinit var scrobbleResult: ScrobbleResult + + @Mock private lateinit var metadataMap: HashMap + + private val log = LoggerFactory.getLogger(this::class.java) + + @BeforeEach + fun setup() { + MockitoAnnotations.openMocks(this) + scrobblerHelper = ScrobblerHelper(lastFMTrackHelper) + `when`(currentUser.username).thenReturn(USERNAME) + `when`(currentUser.password).thenReturn(PASSWORD) + `when`(controlEngine.get(Model.CURRENT_USER)).thenReturn(currentUser) + scrobblerHelper.setControlEngine(controlEngine) + } + + @Test + fun `should not add scrobbling if track less than 240`(testInfo: TestInfo) { + log.info(testInfo.displayName) + setExpectations() + `when`(metadata.artist).thenReturn("Above & Beyond") + `when`(metadata.title).thenReturn("Anjunabeach") + + result = scrobblerHelper.send(metadata) + + notSendToScrobblingMapAssertion() + } + + @Test + fun `should not add scrobbling if no artist`(testInfo: TestInfo) { + log.info(testInfo.displayName) + setExpectations() + `when`(metadata.artist).thenReturn(StringUtils.EMPTY) + `when`(metadata.title).thenReturn("Anjunabeach") + + result = scrobblerHelper.send(metadata) + + notSendToScrobblingMapAssertion() + } + + @Test + fun `should not add scrobbling if no title`(testInfo: TestInfo) { + log.info(testInfo.displayName) + setExpectations() + `when`(metadata.artist).thenReturn("Above & Beyond") + `when`(metadata.title).thenReturn(StringUtils.EMPTY) + + result = scrobblerHelper.send(metadata) + + notSendToScrobblingMapAssertion() + } + + @Test + fun `should fail when submit a scrobbler`(testInfo: TestInfo) { + log.info(testInfo.displayName) + `when`(metadataMap.get(metadata)).thenReturn(100L) + setExpectations() + setMetadataTrackExpectations() + `when`(scrobbleResult.isSuccessful).thenReturn(false) + `when`( + lastFMTrackHelper.scrobble( + metadata.artist, + metadata.title, + 100, + currentUser.session, + ), + ).thenReturn(scrobbleResult) + + assertEquals(ActionResult.SESSION_LESS, scrobblerHelper.send(metadata)) + } + + @Test + fun `should return if no login`(testInfo: TestInfo) { + log.info(testInfo.displayName) + setMetadataTrackExpectations() + `when`(currentUser.username).thenReturn(StringUtils.EMPTY) + + assertEquals(ActionResult.NOT_LOGGED, scrobblerHelper.send(metadata)) + } + + private fun setExpectations() { + `when`(metadata.album).thenReturn(StringUtils.EMPTY) + `when`(metadata.length).thenReturn(1) + `when`(metadata.trackNumber).thenReturn(TRACK_NUMBER) + } + + private fun notSendToScrobblingMapAssertion() { + verify(metadataMap, never()).size + verify(metadataMap, never()).put(isA(Metadata::class.java), isA(Long::class.java)) + assertEquals(ActionResult.NOT_RECORDED, result) + } + + private fun setMetadataTrackExpectations() { + `when`(metadata.length).thenReturn(300) + `when`(metadata.artist).thenReturn("Above & Beyond") + `when`(metadata.title).thenReturn("Anjunabeach") + } +} diff --git a/src/test/java/com/josdem/jmetadata/helper/ImageHelperTest.java b/src/test/java/com/josdem/jmetadata/service/ImageServiceTest.java similarity index 89% rename from src/test/java/com/josdem/jmetadata/helper/ImageHelperTest.java rename to src/test/java/com/josdem/jmetadata/service/ImageServiceTest.java index ce2d7345..ef17965d 100644 --- a/src/test/java/com/josdem/jmetadata/helper/ImageHelperTest.java +++ b/src/test/java/com/josdem/jmetadata/service/ImageServiceTest.java @@ -14,12 +14,12 @@ limitations under the License. */ -package com.josdem.jmetadata.helper; +package com.josdem.jmetadata.service; import static org.junit.jupiter.api.Assertions.assertTrue; import com.josdem.jmetadata.ApplicationConstants; -import com.josdem.jmetadata.service.ImageService; +import com.josdem.jmetadata.helper.ApplicationContextSingleton; import com.josdem.jmetadata.service.impl.ImageServiceImpl; import java.io.File; import lombok.RequiredArgsConstructor; @@ -35,13 +35,13 @@ @SpringBootTest @ContextConfiguration(classes = {ApplicationContextSingleton.class, ImageServiceImpl.class}) @RequiredArgsConstructor(onConstructor_ = @Autowired) -class ImageHelperTest { +class ImageServiceTest { private final ImageService imageService; @Test @DisplayName("creating a temp file for cover art") - public void shouldCreateTempFile(TestInfo testInfo) throws Exception { + void shouldCreateTempFile(TestInfo testInfo) throws Exception { log.info(testInfo.getDisplayName()); File tempFile = imageService.createTempFile(); assertTrue(tempFile.getName().contains(ApplicationConstants.PREFIX)); diff --git a/src/test/java/com/josdem/jmetadata/service/LastFMCompleteServiceTest.java b/src/test/java/com/josdem/jmetadata/service/LastFMCompleteServiceTest.java index 1e796f73..4cc9d1d1 100644 --- a/src/test/java/com/josdem/jmetadata/service/LastFMCompleteServiceTest.java +++ b/src/test/java/com/josdem/jmetadata/service/LastFMCompleteServiceTest.java @@ -20,6 +20,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.isA; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -34,6 +35,7 @@ import de.umass.lastfm.ImageSize; import java.awt.Image; import java.io.IOException; +import java.time.LocalDate; import java.util.Date; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; @@ -171,7 +173,7 @@ void shouldGetLastfm(TestInfo testInfo) throws Exception { private void setYearAndGenreExpectations() { var date = new Date(); when(albumFromLastFM.getReleaseDate()).thenReturn(date); - when(lastfmHelper.getYear(date)).thenReturn(year); + when(lastfmHelper.getYear(isA(LocalDate.class))).thenReturn(year); when(lastfmHelper.getGenre(albumFromLastFM)).thenReturn(genre); }