Skip to content

Commit

Permalink
[Issue comixed#225] Return a default image when the publisher does no…
Browse files Browse the repository at this point in the history
…t exist.
  • Loading branch information
mcpierce committed Apr 3, 2020
1 parent 3a37408 commit fc38976
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

package org.comixed.controller.comic;

import java.io.IOException;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.io.IOUtils;
import org.comixed.model.comic.Publisher;
import org.comixed.service.comic.PublisherException;
import org.comixed.service.comic.PublisherService;
Expand All @@ -36,28 +38,40 @@ public class PublisherController {
@Autowired private PublisherService publisherService;

@GetMapping(value = "/api/publishers/{name}", produces = MediaType.APPLICATION_JSON_VALUE)
public Publisher getByName(@PathVariable("name") String name) throws PublisherException {
public Publisher getByName(@PathVariable("name") String name) {
this.log.info("Getting publisher: name={}", name);
return this.publisherService.getByName(name);
}

@GetMapping(
value = "/api/publishers/{name}/thumbnail",
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<byte[]> getThumbnail(@PathVariable("name") String name)
throws PublisherException {
public ResponseEntity<byte[]> getThumbnail(@PathVariable("name") String name) throws IOException {
this.log.debug("Getting thumbnail image for publisher: {}", name);
Publisher publisher = this.publisherService.getByName(name);
byte[] content = null;
if (publisher != null) {
content = publisher.getThumbnail();
} else {
content =
IOUtils.toByteArray(this.getClass().getResourceAsStream("/images/missing-publisher.png"));
}

return new ResponseEntity<>(publisher.getThumbnail(), HttpStatus.OK);
return new ResponseEntity<>(content, HttpStatus.OK);
}

@GetMapping(value = "/api/publishers/{name}/logo", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<byte[]> getLogo(@PathVariable("name") String name)
throws PublisherException {
throws PublisherException, IOException {
this.log.debug("Getting logo image for publisher: {}", name);
Publisher publisher = this.publisherService.getByName(name);
byte[] content = null;
if (publisher != null) {
content = publisher.getLogo();
} else {
content = IOUtils.toByteArray(this.getClass().getResourceAsStream("/images/missing.png"));
}

return new ResponseEntity<>(publisher.getLogo(), HttpStatus.OK);
return new ResponseEntity<>(content, HttpStatus.OK);
}
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

package org.comixed.controller.comic;

import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertSame;
import static junit.framework.TestCase.*;

import java.io.IOException;
import org.comixed.model.comic.Publisher;
import org.comixed.service.comic.PublisherException;
import org.comixed.service.comic.PublisherService;
Expand All @@ -41,20 +41,19 @@ public class PublisherControllerTest {
@Mock private PublisherService publisherService;
@Mock private Publisher publisher;

@Test(expected = PublisherException.class)
public void testGetByNameNoSuchPublisher() throws PublisherException {
Mockito.when(publisherService.getByName(Mockito.anyString()))
.thenThrow(PublisherException.class);
@Test
public void testGetByNameNoSuchPublisher() {
Mockito.when(publisherService.getByName(Mockito.anyString())).thenReturn(null);

Publisher result = publisherController.getByName(TEST_PUBLISHER_NAME);

try {
publisherController.getByName(TEST_PUBLISHER_NAME);
} finally {
Mockito.verify(publisherService, Mockito.times(1)).getByName(TEST_PUBLISHER_NAME);
}
assertNull(result);

Mockito.verify(publisherService, Mockito.times(1)).getByName(TEST_PUBLISHER_NAME);
}

@Test
public void testGetByName() throws PublisherException {
public void testGetByName() {
Mockito.when(publisherService.getByName(Mockito.anyString())).thenReturn(publisher);

Publisher result = publisherController.getByName(TEST_PUBLISHER_NAME);
Expand All @@ -65,20 +64,20 @@ public void testGetByName() throws PublisherException {
Mockito.verify(publisherService, Mockito.times(1)).getByName(TEST_PUBLISHER_NAME);
}

@Test(expected = PublisherException.class)
public void testGetThumnailNoSuchPublisher() throws PublisherException {
Mockito.when(publisherService.getByName(Mockito.anyString()))
.thenThrow(PublisherException.class);
@Test
public void testGetThumnailNoSuchPublisher() throws IOException {
Mockito.when(publisherService.getByName(Mockito.anyString())).thenReturn(null);

ResponseEntity<byte[]> result = publisherController.getThumbnail(TEST_PUBLISHER_NAME);

try {
publisherController.getThumbnail(TEST_PUBLISHER_NAME);
} finally {
Mockito.verify(publisherService, Mockito.times(1)).getByName(TEST_PUBLISHER_NAME);
}
assertNotNull(result);
assertNotNull(result.getBody());

Mockito.verify(publisherService, Mockito.times(1)).getByName(TEST_PUBLISHER_NAME);
}

@Test
public void testGetThumbnail() throws PublisherException {
public void testGetThumbnail() throws IOException {
Mockito.when(publisherService.getByName(Mockito.anyString())).thenReturn(publisher);
Mockito.when(publisher.getThumbnail()).thenReturn(TEST_IMAGE_DATA);

Expand All @@ -91,20 +90,20 @@ public void testGetThumbnail() throws PublisherException {
Mockito.verify(publisher, Mockito.times(1)).getThumbnail();
}

@Test(expected = PublisherException.class)
public void testGetLogoNoSuchPublisher() throws PublisherException {
Mockito.when(publisherService.getByName(Mockito.anyString()))
.thenThrow(PublisherException.class);
@Test
public void testGetLogoNoSuchPublisher() throws PublisherException, IOException {
Mockito.when(publisherService.getByName(Mockito.anyString())).thenReturn(null);

ResponseEntity<byte[]> result = publisherController.getThumbnail(TEST_PUBLISHER_NAME);

assertNotNull(result);
assertNotNull(result.getBody());

try {
publisherController.getThumbnail(TEST_PUBLISHER_NAME);
} finally {
Mockito.verify(publisherService, Mockito.times(1)).getByName(TEST_PUBLISHER_NAME);
}
Mockito.verify(publisherService, Mockito.times(1)).getByName(TEST_PUBLISHER_NAME);
}

@Test
public void testGetLogo() throws PublisherException {
public void testGetLogo() throws PublisherException, IOException {
Mockito.when(publisherService.getByName(Mockito.anyString())).thenReturn(publisher);
Mockito.when(publisher.getLogo()).thenReturn(TEST_IMAGE_DATA);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,8 @@
public class PublisherService {
@Autowired private PublisherRepository publisherRepository;

public Publisher getByName(String name) throws PublisherException {
public Publisher getByName(String name) {
this.log.debug("Getting publisher: name={}", name);
Publisher result = this.publisherRepository.findByName(name);

if (result == null) throw new PublisherException("No such publisher: name=" + name);

return result;
return this.publisherRepository.findByName(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@

package org.comixed.service.comic;

import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertSame;
import static junit.framework.TestCase.*;

import org.comixed.model.comic.Publisher;
import org.comixed.repositories.comic.PublisherRepository;
Expand All @@ -38,19 +37,19 @@ public class PublisherServiceTest {
@Mock private PublisherRepository publisherRepository;
@Mock private Publisher publisher;

@Test(expected = PublisherException.class)
public void testGetByNameMissing() throws PublisherException {
@Test
public void testGetByNameMissing() {
Mockito.when(publisherRepository.findByName(Mockito.anyString())).thenReturn(null);

try {
publisherService.getByName(TEST_PUBLISHER_NAME);
} finally {
Mockito.verify(publisherRepository, Mockito.times(1)).findByName(TEST_PUBLISHER_NAME);
}
Publisher result = publisherService.getByName(TEST_PUBLISHER_NAME);

assertNull(result);

Mockito.verify(publisherRepository, Mockito.times(1)).findByName(TEST_PUBLISHER_NAME);
}

@Test
public void testGetByName() throws PublisherException {
public void testGetByName() {
Mockito.when(publisherRepository.findByName(Mockito.anyString())).thenReturn(publisher);

Publisher result = publisherService.getByName(TEST_PUBLISHER_NAME);
Expand Down

0 comments on commit fc38976

Please sign in to comment.