View Javadoc
1   package org.imageconverter.application;
2   
3   import static java.text.MessageFormat.format;
4   import static org.apache.commons.lang3.StringUtils.deleteWhitespace;
5   import static org.apache.commons.lang3.math.NumberUtils.LONG_ZERO;
6   import static org.assertj.core.api.Assertions.assertThat;
7   import static org.assertj.core.api.Assertions.assertThatThrownBy;
8   import static org.imageconverter.application.ImageConversionService.HEADER_FILE;
9   import static org.springframework.test.context.jdbc.SqlConfig.ErrorMode.CONTINUE_ON_ERROR;
10  
11  import java.io.IOException;
12  
13  import org.imageconverter.TestConstants;
14  import org.imageconverter.domain.conversion.ExecutionType;
15  import org.imageconverter.domain.conversion.ImageConversion;
16  import org.imageconverter.infra.exception.ElementNotFoundException;
17  import org.imageconverter.util.controllers.imageconverter.ImageConverterRequest;
18  import org.imageconverter.util.controllers.imageconverter.ImageConverterRequestArea;
19  import org.junit.jupiter.api.DisplayName;
20  import org.junit.jupiter.api.Order;
21  import org.junit.jupiter.api.Tag;
22  import org.junit.jupiter.api.Test;
23  import org.springframework.beans.factory.annotation.Autowired;
24  import org.springframework.beans.factory.annotation.Value;
25  import org.springframework.boot.test.context.SpringBootTest;
26  import org.springframework.core.io.Resource;
27  import org.springframework.data.domain.PageRequest;
28  import org.springframework.data.domain.Pageable;
29  import org.springframework.data.jpa.domain.Specification;
30  import org.springframework.http.MediaType;
31  import org.springframework.mock.web.MockMultipartFile;
32  import org.springframework.test.context.ActiveProfiles;
33  import org.springframework.test.context.jdbc.Sql;
34  import org.springframework.test.context.jdbc.SqlConfig;
35  
36  /**
37   * Test the {@link ImageConversionService} happy path
38   * 
39   * @author Fernando Romulo da Silva
40   */
41  @SpringBootTest
42  @ActiveProfiles("test")
43  @Sql(scripts = "classpath:db/db-data-test.sql", config = @SqlConfig(errorMode = CONTINUE_ON_ERROR))
44  //
45  @Tag("integration")
46  @DisplayName("Integration Test for ImageConversionService, happy path :D ")
47  class ImageConversionServiceHappyPathTest {
48  
49      private final ImageConversionService imageConversionService;
50  
51      private final Resource imageFile;
52      
53      private final Pageable pageable = PageRequest.of(0, 10);
54  
55      ImageConversionServiceHappyPathTest( //
56  		    @Autowired //
57  		    final ImageConversionService imageConversionService, //
58  		    //
59  		    @Value("classpath:bill01.png") //
60  		    final Resource imageFile) {
61  	super();
62  	this.imageConversionService = imageConversionService;
63  	this.imageFile = imageFile;
64      }
65  
66      @Test
67      @Order(1)
68      @DisplayName("get a image conversion by id")
69      void findImageConversionByIdTest() {
70  
71  	// already on db, due to the db-data-test.sql
72  	final var id = 1000L;
73  
74  	final var response = imageConversionService.findById(id);
75  
76  	assertThat(response) //
77  			.as(format("Check the 'response' has fileName ''{0}'' and conversion txt ''{1}''", id, TestConstants.DB_CONVERSION_NUMBER)) //
78  			.extracting( //
79  					$ -> $.id(), //
80  					$ -> $.text().replaceAll("[^x0-9]", "")) //
81  			.containsExactly(id, TestConstants.DB_CONVERSION_NUMBER) //
82  	;
83      }
84  
85      @Test
86      @Order(2)
87      @DisplayName("get all image conversions")
88      void findAllImageConversionTest() {
89  
90  	// already on db, due to the db-data-test.sql
91  	final var id = 1000L;
92  
93  	final var responses = imageConversionService.findAll();
94  
95  	assertThat(responses) //
96  			.as(format("Check if there's a id ''{0}'' on the result", id))//
97  			.map(imageConverterResponse -> imageConverterResponse.id()) //
98  			.contains(id);
99  
100 	assertThat(responses) //
101 			.as(format("Check if there's a text conversion ''{0}'' on the result", id))//
102 			.map(imageConverterResponse -> imageConverterResponse.text()) //
103 			.containsAnyOf(TestConstants.DB_CONVERSION_NUMBER);
104     }
105 
106     @Test
107     @Order(3)
108     @DisplayName("get a image conversion by specification")
109     void findImageConversionBySpecificationTest() {
110 
111 	// already on db, due to the db-data-test.sql
112 	final var fileName = "image_test.jpg";
113 
114 	// Specification<ImageConversion> equalsFileName = (rt, cq, cb) -> cb.equal(rt.get("fileName"), fileName);
115 
116 	final var responses1 = imageConversionService.findBySpecification(equalsFileName(fileName), pageable);
117 
118 	assertThat(responses1.getContent()) //
119 			.as(format("Check if there's a response file name ''{0}'' on the result", fileName))//
120 			.map(imageConverterResponse -> imageConverterResponse.fileName()) //
121 			.containsAnyOf(fileName);
122 
123 	final var responses2 = imageConversionService.findBySpecification(null, pageable);
124 
125 	final var responseQty = 1;
126 
127 	assertThat(responses2) //
128 			.as(format("Check if the resutl's qty is ''{0}''", responseQty))//
129 			.hasSize(responseQty);
130     }
131 
132     static Specification<ImageConversion> equalsFileName(final String fileName) {
133 	return (root, query, builder) -> builder.equal(root.get("fileName"), fileName);
134     }
135 
136     @Test
137     @Order(4)
138     @DisplayName("convert image")
139     void convertTest() throws IOException {
140 
141 	final var multipartFile = new MockMultipartFile("file", imageFile.getFilename(), MediaType.MULTIPART_FORM_DATA_VALUE, imageFile.getInputStream());
142 
143 	final var request = new ImageConverterRequest(multipartFile.getOriginalFilename(), multipartFile.getBytes(), ExecutionType.WS);
144 
145 	final var response = imageConversionService.convert(request);
146 
147 	assertThat(response.id()) //
148 			.as(format("Check if the response's id is greater than Zero ''{0}''", response.id()))//
149 			.isGreaterThan(LONG_ZERO);
150 
151 	assertThat(deleteWhitespace(response.text()).replaceAll("[^x0-9]", "")) //
152 			.as(format("Check if the response's text is equal to ''{0}''", TestConstants.IMAGE_PNG_CONVERSION_NUMBER))//
153 			.containsIgnoringCase(TestConstants.IMAGE_PNG_CONVERSION_NUMBER);
154     }
155 
156     @Test
157     @Order(5)
158     @DisplayName("convert image with area")
159     void convertAreaTest() throws IOException {
160 
161 	final var multipartFile = new MockMultipartFile("file", imageFile.getFilename(), MediaType.MULTIPART_FORM_DATA_VALUE, imageFile.getInputStream());
162 
163 	final var request = new ImageConverterRequestArea(multipartFile.getOriginalFilename(), multipartFile.getBytes(), ExecutionType.BATCH, 885, 1417, 1426, 57);
164 
165 	final var response = imageConversionService.convert(request);
166 
167 	assertThat(response.id()) //
168 			.as(format("Check if the response's id is greater than Zero ''{0}''", response.id()))//
169 			.isGreaterThan(LONG_ZERO);
170 
171 	assertThat(deleteWhitespace(response.text()).replaceAll("[^x0-9]", "")) //
172 			.as(format("Check if the response's text is equal to ''{0}''", TestConstants.IMAGE_PNG_CONVERSION_NUMBER))//
173 			.containsIgnoringCase(TestConstants.IMAGE_PNG_CONVERSION_NUMBER);
174     }
175 
176     @Test
177     @Order(6)
178     @DisplayName("Delete a image conversion")
179     void deleteImageTypeTest() throws IOException {
180 
181 	// given
182 	final var multipartFile = new MockMultipartFile("file", imageFile.getFilename(), MediaType.MULTIPART_FORM_DATA_VALUE, imageFile.getInputStream());
183 
184 	final var request = new ImageConverterRequestArea(multipartFile.getOriginalFilename(), multipartFile.getBytes(), ExecutionType.WEB, 885, 1417, 1426, 57);
185 
186 	final var createResponse = imageConversionService.convert(request);
187 
188 	// ----------------------------------------------------------
189 
190 	// when
191 	imageConversionService.deleteImageConversion(createResponse.id());
192 
193 	// ----------------------------------------------------------
194 
195 	// then
196 	assertThatThrownBy(() -> imageConversionService.findById(createResponse.id())) //
197 			.as(format("Check the ImageConversion id ''{0}'' was deleted", createResponse.id()))//
198 			.isInstanceOfAny(ElementNotFoundException.class);
199     }
200     
201     @Test
202     @Order(7)
203     @DisplayName("Test image conversion csv file")
204     void createImageConversionCsvFileTest() {
205 
206 	// already on db, due to the db-data-test.sql
207 	final var id = 1000L;
208 	
209 	// given
210 	final var headerString = String.join(";", HEADER_FILE);
211 	final var conversion = imageConversionService.findById(id);
212 	
213 	// when
214 	final var bytes = imageConversionService.findBySpecificationToCsv(null);
215 	
216 	// then
217 	assertThat(new String(bytes))
218 		.as("Check if the file contains the header") //
219 		.contains(headerString) //
220 		.as("Check if the contains the conversion 1000")
221 		.contains(conversion.id().toString(), conversion.fileName(), conversion.text());
222 	
223     }
224 }