Skip to content

Commit

Permalink
Add tests for Service classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Migwel authored and Migwel committed Jan 1, 2024
1 parent d686a15 commit 4c4fc30
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.List;

@Component
@ParametersAreNonnullByDefault
public class FromServiceFactory {

private final List<FromService<?>> fromServices;
Expand All @@ -16,9 +19,11 @@ public FromServiceFactory(List<FromService<?>> fromServices) {
this.fromServices = List.copyOf(fromServices);
}

public <T extends FromRequest> FromService<T> getFromService(T fromRequest) {
@Nonnull
public <T extends FromRequest> FromService<T> getFromService(
Class<? extends FromRequest> requestClass) {
for (FromService<?> fromService : fromServices) {
if (fromService.isRelevant(fromRequest.getClass())) {
if (fromService.isRelevant(requestClass)) {
return (FromService<T>) fromService;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/dev/migwel/sts/domain/service/SaveService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ public SaveService(FromServiceFactory fromServiceFactory, ToServiceFactory toSer
}

public <T extends FromRequest, U extends ToRequest> void save(T from, U to) {
FromService<T> fromService = fromServiceFactory.getFromService(from);
FromService<T> fromService = fromServiceFactory.getFromService(from.getClass());
Optional<Song> optionalSong = fromService.search(from);
if (optionalSong.isEmpty()) {
logger.info("Could not find what's playing for " + from);
return;
}
ToService<U> toService = toServiceFactory.getToService(to);
ToService<U> toService = toServiceFactory.getToService(to.getClass());
toService.save(optionalSong.get(), to);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

import java.util.List;

import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;

@Component
@ParametersAreNonnullByDefault
public class ToServiceFactory {

private final List<ToService<?>> toServices;
Expand All @@ -17,9 +21,11 @@ public ToServiceFactory(List<ToService<?>> toServices) {
this.toServices = List.copyOf(toServices);
}

public <T extends ToRequest> ToService<T> getToService(T saveRequest) {
@Nonnull
public <T extends ToRequest> ToService<T> getToService(
Class<? extends ToRequest> requestClass) {
for (ToService<?> toService : toServices) {
if (toService.isRelevant(saveRequest.getClass())) {
if (toService.isRelevant(requestClass)) {
return (ToService<T>) toService;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package dev.migwel.sts.domain.service;

import static org.junit.jupiter.api.Assertions.*;

import dev.migwel.sts.Application;
import dev.migwel.sts.domain.model.FromRequest;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

@SpringJUnitConfig(Application.class)
class FromServiceFactoryTest {

private final FromServiceFactory serviceFactory;

@Autowired
FromServiceFactoryTest(FromServiceFactory serviceFactory) {
this.serviceFactory = serviceFactory;
}

@Test
void getFromService() {
Class<?>[] permittedSearchRequests = FromRequest.class.getPermittedSubclasses();
for (Class<?> permittedSearchRequest : permittedSearchRequests) {
try {
serviceFactory.getFromService(
(Class<? extends FromRequest>) permittedSearchRequest);
} catch (IllegalArgumentException e) {
fail(
"Exception occurred while trying to get from service for request: "
+ permittedSearchRequest
+ ". Reason: "
+ e.getMessage());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.migwel.sts.service;
package dev.migwel.sts.domain.service;

import static org.junit.jupiter.api.Assertions.*;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class SaveServiceTest {
@InjectMocks private SaveService saveService;

@BeforeEach
private void before() {
public void before() {
doReturn(fromService).when(fromServiceFactory).getFromService(any());
lenient().doReturn(toService).when(toServiceFactory).getToService(any());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package dev.migwel.sts.domain.service;

import static org.junit.jupiter.api.Assertions.*;

import dev.migwel.sts.Application;
import dev.migwel.sts.domain.model.FromRequest;

import dev.migwel.sts.domain.model.ToRequest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

@SpringJUnitConfig(Application.class)
class ToServiceFactoryTest {

private final ToServiceFactory serviceFactory;

@Autowired
ToServiceFactoryTest(ToServiceFactory serviceFactory) {
this.serviceFactory = serviceFactory;
}

@Test
void getFromService() {
Class<?>[] permittedSearchRequests = ToRequest.class.getPermittedSubclasses();
for (Class<?> permittedSearchRequest : permittedSearchRequests) {
try {
serviceFactory.getToService((Class<? extends ToRequest>) permittedSearchRequest);
} catch (IllegalArgumentException e) {
fail(
"Exception occurred while trying to get to service for request: "
+ permittedSearchRequest
+ ". Reason: "
+ e.getMessage());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.migwel.sts.service;
package dev.migwel.sts.domain.service;

import static org.junit.jupiter.api.Assertions.*;

Expand Down

0 comments on commit 4c4fc30

Please sign in to comment.