1 package com.acumenvelocity.ath.integration;
2
3 import com.fasterxml.jackson.databind.JsonNode;
4 import com.fasterxml.jackson.databind.node.ObjectNode;
5 import org.junit.jupiter.api.MethodOrderer;
6 import org.junit.jupiter.api.Order;
7 import org.junit.jupiter.api.Test;
8 import org.junit.jupiter.api.TestMethodOrder;
9
10 import java.nio.file.Files;
11 import java.nio.file.Path;
12 import java.util.UUID;
13
14 import static org.junit.jupiter.api.Assertions.*;
15
16
17
18
19 @TestMethodOrder(MethodOrderer.OrderAnnotation.class)
20 public class TestFullWorkflow_IT extends BaseIntegrationTest {
21
22 private static UUID tmId;
23 private static UUID docId;
24 private static UUID userId;
25
26 @Test
27 @Order(1)
28 public void testCompleteTranslationWorkflow() throws Exception {
29 userId = UUID.randomUUID();
30 tmId = UUID.randomUUID();
31 docId = UUID.randomUUID();
32
33 System.out.println("\n=== Starting Complete Translation Workflow ===\n");
34
35
36 System.out.println("Step 1: Creating Translation Memory...");
37 createTranslationMemory();
38
39
40 System.out.println("Step 2: Verifying Translation Memory...");
41 verifyTranslationMemory();
42
43
44 System.out.println("Step 3: Importing Document...");
45 importDocument();
46
47
48 System.out.println("Step 4: Verifying Document Segments...");
49 verifyDocumentSegments();
50
51
52 System.out.println("Step 5: Updating Segment (Human Translation)...");
53 updateDocumentSegment();
54
55
56 System.out.println("Step 6: Exporting Translated Document...");
57 exportDocument();
58
59
60 System.out.println("Step 7: Exporting Translated Document with TM update...");
61 exportDocumentWithTmUpdate();
62
63
64 System.out.println("Step 8: Downloading Translated Document...");
65 downloadDocument();
66
67
68 System.out.println("Step 9: Get Translation Memory info...");
69 getTmInfo();
70
71 System.out.println("\n=== Complete Translation Workflow Successful ===\n");
72 }
73
74 private void createTranslationMemory() throws Exception {
75 Path tmxFile = createSampleTmxFile();
76
77 MultipartRequestBody body = new MultipartRequestBody();
78 body.addPart("tm_id", tmId.toString());
79 body.addFilePart("tm_file", "sample.tmx", tmxFile, "application/xml");
80 body.addPart("tm_file_name", "sample.tmx");
81 body.addPart("src_lang", "en");
82 body.addPart("trg_lang", "es");
83 body.addPart("user_id", userId.toString());
84
85 HttpResponse response = sendMultipartRequest("/resources/translation-memory", body);
86 assertEquals(201, response.getStatusCode(), "TM creation failed");
87
88 System.out.println(" ✓ Translation Memory created: " + tmId);
89 }
90
91 private void verifyTranslationMemory() throws Exception {
92 HttpResponse response = sendGetRequest("/resources/translation-memory/" + tmId + "/info");
93 assertEquals(200, response.getStatusCode(), "TM info retrieval failed");
94
95 JsonNode json = parseJson(response.getBody());
96 JsonNode tmInfo = json.get("translation_memory_info");
97
98 assertTrue(tmInfo.get("segments_count").asLong() > 0, "TM should have segments");
99 System.out
100 .println(" ✓ TM verified with " + tmInfo.get("segments_count").asLong() + " segments");
101 }
102
103 private void importDocument() throws Exception {
104 Path docFile = createSampleDocument();
105
106 MultipartRequestBody body = new MultipartRequestBody();
107 body.addPart("doc_id", docId.toString());
108 body.addFilePart("doc_file", "sample.txt", docFile, "text/plain");
109 body.addPart("doc_file_name", "sample.txt");
110 body.addPart("doc_storage_name", "storage-" + docId + ".txt");
111 body.addPart("doc_file_encoding", "UTF-8");
112 body.addPart("src_lang", "en");
113 body.addPart("trg_lang", "es");
114 body.addPart("tm_id", tmId.toString());
115 body.addPart("tm_threshold", "75");
116 body.addPart("user_id", userId.toString());
117
118 HttpResponse response = sendMultipartRequest("/document", body);
119 assertEquals(202, response.getStatusCode(), "Document import failed");
120
121 JsonNode json = parseJson(response.getBody());
122 String statusUrl = json.get("status_url").asText();
123
124
125 waitForAsyncOperation(statusUrl, 120);
126
127 System.out.println(" ✓ Document imported: " + docId);
128 }
129
130 private void verifyDocumentSegments() throws Exception {
131 HttpResponse response = sendGetRequest("/document/" + docId + "/segment");
132 assertEquals(200, response.getStatusCode(), "Segment retrieval failed");
133
134 JsonNode json = parseJson(response.getBody());
135 JsonNode segments = json.get("document_segments");
136
137 assertTrue(segments.size() > 0, "Document should have segments");
138
139
140 int tmMatches = 0;
141 for (JsonNode segment : segments) {
142 if (segment.has("origin") && "TM".equals(segment.get("origin").asText())) {
143 tmMatches++;
144 }
145 }
146
147 System.out.println(" ✓ Document has " + segments.size() + " segments");
148 System.out.println(" ✓ Found " + tmMatches + " TM matches");
149 }
150
151 private void updateDocumentSegment() throws Exception {
152
153 HttpResponse getResponse = sendGetRequest("/document/" + docId + "/segment");
154 JsonNode json = parseJson(getResponse.getBody());
155 JsonNode firstSegment = json.get("document_segments").get(0);
156
157 UUID segmentId = UUID.fromString(firstSegment.get("doc_seg_id").asText());
158
159
160 ObjectNode requestBody = objectMapper.createObjectNode();
161
162 ObjectNode target = objectMapper.createObjectNode();
163 target.put("language", "es");
164 target.put("text", "Texto traducido por humano");
165 target.set("codes", objectMapper.createArrayNode());
166 requestBody.set("target", target);
167
168 requestBody.put("origin", "HT");
169 requestBody.put("user_id", userId.toString());
170
171 HttpResponse updateResponse = sendPutRequest(
172 "/document/" + docId + "/segment/" + segmentId,
173 toJson(requestBody));
174
175 assertEquals(200, updateResponse.getStatusCode(), "Segment update failed");
176 System.out.println(" ✓ Segment updated with human translation");
177 }
178
179 private void exportDocument() throws Exception {
180 ObjectNode requestBody = objectMapper.createObjectNode();
181 requestBody.put("doc_out_storage_name", "output-" + docId + ".txt");
182 requestBody.put("user_id", userId.toString());
183
184 HttpResponse response = sendPostRequest(
185 "/document/" + docId + "/export",
186 toJson(requestBody));
187
188 assertEquals(202, response.getStatusCode(), "Document export failed");
189
190 JsonNode json = parseJson(response.getBody());
191 String statusUrl = json.get("status_url").asText();
192
193
194 waitForAsyncOperation(statusUrl, 120);
195
196 System.out.println(" ✓ Document exported");
197 }
198
199 private void exportDocumentWithTmUpdate() throws Exception {
200 ObjectNode requestBody = objectMapper.createObjectNode();
201 requestBody.put("doc_out_storage_name", "output-" + docId + ".txt");
202 requestBody.put("tm_id", tmId.toString());
203 requestBody.put("user_id", userId.toString());
204
205 HttpResponse response = sendPostRequest(
206 "/document/" + docId + "/export",
207 toJson(requestBody));
208
209 assertEquals(202, response.getStatusCode(), "Document export failed");
210
211 JsonNode json = parseJson(response.getBody());
212 String statusUrl = json.get("status_url").asText();
213
214
215 waitForAsyncOperation(statusUrl, 120);
216
217 System.out.println(" ✓ Document exported");
218 }
219
220 private void downloadDocument() throws Exception {
221 HttpResponse response = sendGetRequest("/document/" + docId);
222 assertEquals(200, response.getStatusCode(), "Document download failed");
223
224 assertFalse(response.getBody().isEmpty(), "Downloaded document should not be empty");
225 System.out.println(" ✓ Translated document downloaded");
226 }
227
228 private void getTmInfo() throws Exception {
229 HttpResponse response = sendGetRequest("/resources/translation-memory/" + tmId + "/info");
230 JsonNode json = parseJson(response.getBody());
231 JsonNode tmInfo = json.get("translation_memory_info");
232
233 long segmentCount = tmInfo.get("segments_count").asLong();
234 System.out.println(" ✓ TM now has " + segmentCount + " segments");
235 }
236
237 private Path createSampleTmxFile() throws Exception {
238 String tmxContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
239 "<tmx version=\"1.4\">\n" +
240 " <header creationtool=\"test\" creationtoolversion=\"1.0\" " +
241 "datatype=\"plaintext\" segtype=\"sentence\" " +
242 "adminlang=\"en\" srclang=\"en\"/>\n" +
243 " <body>\n" +
244 " <tu>\n" +
245 " <tuv xml:lang=\"en\"><seg>Welcome to our application</seg></tuv>\n" +
246 " <tuv xml:lang=\"es\"><seg>Bienvenido a nuestra aplicación</seg></tuv>\n" +
247 " </tu>\n" +
248 " <tu>\n" +
249 " <tuv xml:lang=\"en\"><seg>Please enter your name</seg></tuv>\n" +
250 " <tuv xml:lang=\"es\"><seg>Por favor ingrese su nombre</seg></tuv>\n" +
251 " </tu>\n" +
252 " <tu>\n" +
253 " <tuv xml:lang=\"en\"><seg>Thank you</seg></tuv>\n" +
254 " <tuv xml:lang=\"es\"><seg>Gracias</seg></tuv>\n" +
255 " </tu>\n" +
256 " </body>\n" +
257 "</tmx>";
258
259 Path tmxFile = Files.createTempFile("sample", ".tmx");
260 Files.write(tmxFile, tmxContent.getBytes(java.nio.charset.StandardCharsets.UTF_8));
261 tmxFile.toFile().deleteOnExit();
262
263 return tmxFile;
264 }
265
266 private Path createSampleDocument() throws Exception {
267 String content = "Welcome to our application.\n" +
268 "Please enter your name.\n" +
269 "This is a new sentence that needs translation.\n" +
270 "Thank you for using our service.";
271
272 Path file = Files.createTempFile("sample-doc", ".txt");
273 Files.write(file, content.getBytes(java.nio.charset.StandardCharsets.UTF_8));
274 file.toFile().deleteOnExit();
275
276 return file;
277 }
278 }