1 package org.imageconverter.domain.conversion;
2
3 import static javax.validation.Validation.buildDefaultValidatorFactory;
4 import static org.mockito.Mockito.when;
5 import static org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE;
6
7 import java.io.ByteArrayOutputStream;
8 import java.io.File;
9 import java.io.IOException;
10
11 import javax.imageio.ImageIO;
12 import javax.validation.Validator;
13
14 import org.imageconverter.TestConstants;
15 import org.imageconverter.domain.imagetype.ImageTypeRespository;
16 import org.imageconverter.util.BeanUtil;
17 import org.mockito.Mock;
18 import org.mockito.MockitoAnnotations;
19 import org.springframework.beans.factory.ObjectProvider;
20 import org.springframework.context.ApplicationContext;
21 import org.springframework.context.MessageSource;
22 import org.springframework.context.support.ResourceBundleMessageSource;
23 import org.springframework.mock.web.MockMultipartFile;
24
25 import net.sourceforge.tess4j.ITesseract;
26
27
28
29
30
31
32 class ImageConversionConfigTest {
33
34 protected MockMultipartFile mockMultipartFile;
35
36 @Mock
37 protected ApplicationContext applicationContext;
38
39 @Mock
40 protected ImageTypeRespository imageTypeRespository;
41
42 @Mock
43 protected ITesseract tesseractTess4j;
44
45 @Mock
46 protected ObjectProvider<ITesseract> objectProvider;
47
48 protected void setUpSuper() throws IOException {
49
50
51 final var validator = buildDefaultValidatorFactory().getValidator();
52
53
54 final var file = new File("src/test/resources/" + TestConstants.FILE_NAME_IMAGE_PNG);
55
56 final var image = ImageIO.read(file);
57 final var baos = new ByteArrayOutputStream();
58
59 ImageIO.write(image, "png", baos);
60 final var bytes = baos.toByteArray();
61
62 mockMultipartFile = new MockMultipartFile("file", TestConstants.FILE_NAME_IMAGE_PNG, MULTIPART_FORM_DATA_VALUE, bytes);
63
64
65 MockitoAnnotations.openMocks(this);
66
67 BeanUtil.defineContext(applicationContext);
68
69 when(applicationContext.getBean(ImageTypeRespository.class))
70 .thenReturn(imageTypeRespository);
71
72 when(applicationContext.getBean(Validator.class))
73 .thenReturn(validator);
74
75 when(applicationContext.getBeanProvider(ITesseract.class))
76 .thenReturn(objectProvider);
77
78 when(objectProvider.getIfAvailable())
79 .thenReturn(tesseractTess4j);
80
81 when(objectProvider.getObject())
82 .thenReturn(tesseractTess4j);
83
84 when(applicationContext.getBean(TesseractService.class))
85 .thenReturn(new TesseractService());
86
87 final var messageSource = new ResourceBundleMessageSource();
88 messageSource.setBasename("messages");
89 messageSource.setDefaultEncoding("UTF-8");
90
91 when(applicationContext.getBean(MessageSource.class))
92 .thenReturn(messageSource);
93 }
94
95 }