Skip to content

Commit

Permalink
Merge pull request #69 from josdem/feature/54
Browse files Browse the repository at this point in the history
[small]Feature/54
  • Loading branch information
josdem authored Jan 17, 2025
2 parents f6320c1 + f9767aa commit 7ce500d
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group = 'com.josdem.jmetadata'
version = '1.2.4'
version = '1.2.5'

java {
toolchain {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public synchronized ActionResult completeAlbumMetadata(List<Metadata> metadatas)
log.info("MusicBrainz Response: {}", musicBrainzResponse);
Album album = musicBrainzService.getAlbumByName(albumName);
log.info("MusicBrainz Album: {}", album);
musicBrainzService.completeAlbum(metadatas, album);
if (album.getCoverArtArchive().isFront()) {
log.info("Getting cover art");
var coverArtResponse = coverArtRestService.getRelease(album.getId());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2024 Jose Morales [email protected]
Copyright 2025 Jose Morales [email protected]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -17,8 +17,12 @@
package com.josdem.jmetadata.service;

import com.josdem.jmetadata.model.Album;
import com.josdem.jmetadata.model.Metadata;
import java.util.List;

public interface MusicBrainzService {

Album getAlbumByName(String name);

List<Metadata> completeAlbum(List<Metadata> metadataList, Album album);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,23 @@
import com.josdem.jmetadata.exception.BusinessException;
import com.josdem.jmetadata.helper.RetrofitHelper;
import com.josdem.jmetadata.model.Album;
import com.josdem.jmetadata.model.Metadata;
import com.josdem.jmetadata.service.MusicBrainzService;
import com.josdem.jmetadata.service.RestService;
import com.josdem.jmetadata.util.AlbumUtils;
import com.josdem.jmetadata.util.ApplicationState;
import java.io.IOException;
import java.util.List;
import javax.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import retrofit2.Response;

@Slf4j
@Service
@RequiredArgsConstructor
public class MusicBrainzServiceImpl implements MusicBrainzService {

private RestService restService;
Expand All @@ -51,4 +57,19 @@ public Album getAlbumByName(String name) {
throw new BusinessException(e.getMessage());
}
}

@Override
public List<Metadata> completeAlbum(List<Metadata> metadataList, Album album) {
log.info("Trying to complete year from album");
if (StringUtils.isEmpty(album.getDate()) || StringUtils.isBlank(album.getDate())) {
throw new BusinessException("Album date is not valid");
}
metadataList.forEach(
metadata -> {
if (StringUtils.isEmpty(metadata.getYear()) || StringUtils.isBlank(metadata.getYear())) {
metadata.setYear(AlbumUtils.formatYear(album.getDate()));
}
});
return metadataList;
}
}
28 changes: 28 additions & 0 deletions src/main/java/com/josdem/jmetadata/util/AlbumUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright 2025 Jose Morales [email protected]
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.util;

public class AlbumUtils {

private AlbumUtils() {
throw new IllegalStateException("Utility class");
}

public static String formatYear(String date) {
return date.substring(0, 4);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2024 Jose Morales [email protected]
Copyright 2025 Jose Morales [email protected]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -22,17 +22,22 @@

import com.josdem.jmetadata.exception.BusinessException;
import com.josdem.jmetadata.model.Album;
import com.josdem.jmetadata.model.Metadata;
import com.josdem.jmetadata.model.MusicBrainzResponse;
import com.josdem.jmetadata.model.Release;
import com.josdem.jmetadata.service.impl.MusicBrainzServiceImpl;
import com.josdem.jmetadata.util.ApplicationState;
import java.io.IOException;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
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.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
Expand All @@ -51,6 +56,9 @@ public class MusicBrainzServiceTest {

@Mock private Call<Album> call;

private final Metadata metadata = new Metadata();
private final Album album = new Album();

@BeforeEach
void setup() {
MockitoAnnotations.openMocks(this);
Expand Down Expand Up @@ -89,4 +97,39 @@ private MusicBrainzResponse getExpectedResponse() {
musicBrainzResponse.setReleases(releases);
return musicBrainzResponse;
}

@DisplayName("completing year from album")
@ParameterizedTest
@NullSource
@ValueSource(strings = {"", " "})
void shouldCompleteYearFromAlbum(String metadataYear) {
setMetadataExpectations();
metadata.setYear(metadataYear);
var metadataList = List.of(metadata);
album.setDate("1999-03-29");

var result = musicBrainzService.completeAlbum(metadataList, album);

assertEquals("1999", result.getFirst().getYear());
assertEquals(1, result.size());
}

@ParameterizedTest
@NullSource
@ValueSource(strings = {"", " "})
@DisplayName("not completing year if not valid format")
void shouldNotCompleteYearIfNotValidFormat(String date) {
setMetadataExpectations();
metadata.setYear(StringUtils.EMPTY);
var metadataList = List.of(metadata);
album.setDate(date);

assertThrows(
BusinessException.class, () -> musicBrainzService.completeAlbum(metadataList, album));
}

private void setMetadataExpectations() {
metadata.setAlbum("Nightlife");
metadata.setArtist("Pet shop boys");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2014 Jose Morales [email protected]
Copyright 2025 Jose Morales [email protected]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down

0 comments on commit 7ce500d

Please sign in to comment.