dimanche 6 janvier 2019

How to test images when calling external api?

I'm calling from my rest endpoint an external api. Something like this:

 private byte[] retrieveImageFromExernalAPI() {

            byte[] imageBytes = null;

                URL url = new URL("https://cdau:6443/rest/services/targetEndpoint");
                BufferedImage bufferedImage = ImageIO.read(url);
                imageBytes = convertBufferedImageToByte(bufferedImage, "png");

            } catch (IOException e) {
                //logger.error(e);
                throw new RuntimeException(e);
            }
            return imageBytes;
        }

As you can see in the code above, I call the external API and receive a BufferedImage object in Java as response. Then I call the method convertBufferedImageToByte(bufferedImage, "png"); which converts the BufferedImage response into an image (byte[]). Something like this:

 private byte[] convertBufferedImageToByte(BufferedImage image, String type) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(image, type, baos);
        byte[] bytes = baos.toByteArray();
        return bytes;
    }

The thing is, I don't know really how to test this. Should I use mockito to mock the external api or just jUnit? I heard of hoverfly, but still trying to understand how it would fit in this situation.

Any help would be much appreciated!

Aucun commentaire:

Enregistrer un commentaire