Skip to content

Commit

Permalink
[kbss-cvut/termit-ui#553] Use File language (if available) when invok…
Browse files Browse the repository at this point in the history
…ing text analysis.
  • Loading branch information
ledsoft committed Nov 7, 2024
1 parent 0a6fdba commit 8c8a15f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private TextAnalysisInput createAnalysisInput(File file) {
publicUrl.isEmpty() || publicUrl.get().isEmpty() ? config.getRepository().getUrl() : publicUrl.get()
);
input.setVocabularyRepository(repositoryUrl);
input.setLanguage(config.getPersistence().getLanguage());
input.setLanguage(file.getLanguage() != null ? file.getLanguage() : config.getPersistence().getLanguage());
input.setVocabularyRepositoryUserName(config.getRepository().getUsername());
input.setVocabularyRepositoryPassword(config.getRepository().getPassword());
return input;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
import static org.mockito.Mockito.when;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.content;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.header;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.jsonPath;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withServerError;
Expand Down Expand Up @@ -143,14 +144,14 @@ void setUp() throws Exception {
doCallRealMethod().when(documentManagerSpy).loadFileContent(any());
doNothing().when(documentManagerSpy).createBackup(any());
this.sut = new TextAnalysisService(restTemplate, config, documentManagerSpy, annotationGeneratorMock,
textAnalysisRecordDao, eventPublisher);
textAnalysisRecordDao, eventPublisher);
}

@Test
void analyzeFileInvokesTextAnalysisServiceWithDocumentContent() {
mockServer.expect(requestTo(config.getTextAnalysis().getUrl()))
.andExpect(method(HttpMethod.POST)).andExpect(content().string(containsString(CONTENT)))
.andRespond(withSuccess(CONTENT, MediaType.APPLICATION_XML));
.andExpect(method(HttpMethod.POST)).andExpect(content().string(containsString(CONTENT)))
.andRespond(withSuccess(CONTENT, MediaType.APPLICATION_XML));
sut.analyzeFile(file, Collections.singleton(vocabulary.getUri()));
mockServer.verify();
}
Expand All @@ -159,7 +160,8 @@ private void generateFile() throws IOException {
final java.io.File dir = Files.createTempDirectory("termit").toFile();
dir.deleteOnExit();
config.getFile().setStorage(dir.getAbsolutePath());
final java.io.File docDir = new java.io.File(dir.getAbsolutePath() + java.io.File.separator + file.getDirectoryName());
final java.io.File docDir = new java.io.File(
dir.getAbsolutePath() + java.io.File.separator + file.getDirectoryName());
Files.createDirectory(docDir.toPath());
docDir.deleteOnExit();
final java.io.File content = new java.io.File(
Expand All @@ -172,9 +174,9 @@ private void generateFile() throws IOException {
void analyzeFilePassesRepositoryAndVocabularyContextToService() throws Exception {
final TextAnalysisInput input = textAnalysisInput();
mockServer.expect(requestTo(config.getTextAnalysis().getUrl()))
.andExpect(method(HttpMethod.POST))
.andExpect(content().string(objectMapper.writeValueAsString(input)))
.andRespond(withSuccess(CONTENT, MediaType.APPLICATION_XML));
.andExpect(method(HttpMethod.POST))
.andExpect(content().string(objectMapper.writeValueAsString(input)))
.andRespond(withSuccess(CONTENT, MediaType.APPLICATION_XML));
sut.analyzeFile(file, Collections.singleton(vocabulary.getUri()));
mockServer.verify();
}
Expand All @@ -184,8 +186,8 @@ private TextAnalysisInput textAnalysisInput() {
input.setContent(CONTENT);
input.addVocabularyContext(vocabulary.getUri());
URI repositoryUrl = URI.create(
config.getRepository().getPublicUrl()
.orElse(config.getRepository().getUrl())
config.getRepository().getPublicUrl()
.orElse(config.getRepository().getUrl())
);
input.setVocabularyRepository(repositoryUrl);
input.setLanguage(config.getPersistence().getLanguage());
Expand All @@ -198,11 +200,11 @@ private TextAnalysisInput textAnalysisInput() {
void analyzeFilePassesContentTypeAndAcceptHeadersToService() throws Exception {
final TextAnalysisInput input = textAnalysisInput();
mockServer.expect(requestTo(config.getTextAnalysis().getUrl()))
.andExpect(method(HttpMethod.POST))
.andExpect(content().string(objectMapper.writeValueAsString(input)))
.andExpect(header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
.andExpect(header(HttpHeaders.ACCEPT, MediaType.APPLICATION_XML_VALUE))
.andRespond(withSuccess(CONTENT, MediaType.APPLICATION_XML));
.andExpect(method(HttpMethod.POST))
.andExpect(content().string(objectMapper.writeValueAsString(input)))
.andExpect(header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
.andExpect(header(HttpHeaders.ACCEPT, MediaType.APPLICATION_XML_VALUE))
.andRespond(withSuccess(CONTENT, MediaType.APPLICATION_XML));
sut.analyzeFile(file, Collections.singleton(vocabulary.getUri()));
mockServer.verify();
}
Expand All @@ -228,11 +230,11 @@ void analyzeFilePassesRepositoryUsernameAndPasswordToServiceWhenProvided() throw
void analyzeFileThrowsWebServiceIntegrationExceptionOnError() throws Exception {
final TextAnalysisInput input = textAnalysisInput();
mockServer.expect(requestTo(config.getTextAnalysis().getUrl()))
.andExpect(method(HttpMethod.POST))
.andExpect(content().string(objectMapper.writeValueAsString(input)))
.andRespond(withServerError());
.andExpect(method(HttpMethod.POST))
.andExpect(content().string(objectMapper.writeValueAsString(input)))
.andRespond(withServerError());
assertThrows(WebServiceIntegrationException.class,
() -> sut.analyzeFile(file, Collections.singleton(vocabulary.getUri())));
() -> sut.analyzeFile(file, Collections.singleton(vocabulary.getUri())));
mockServer.verify();
}

Expand All @@ -256,19 +258,21 @@ void analyzeFileInvokesAnnotationGeneratorWithResultFromTextAnalysisService() th
void analyzeFileThrowsNotFoundExceptionWhenFileCannotBeFound() {
file.setLabel("unknown.html");
final NotFoundException result = assertThrows(NotFoundException.class,
() -> sut.analyzeFile(file, Collections.singleton(vocabulary.getUri())));
() -> sut.analyzeFile(file, Collections.singleton(
vocabulary.getUri())));
assertThat(result.getMessage(), containsString("not found on file system"));
}

@Test
void analyzeFileThrowsWebServiceIntegrationExceptionWhenRemoteServiceReturnsEmptyBody() throws Exception {
final TextAnalysisInput input = textAnalysisInput();
mockServer.expect(requestTo(config.getTextAnalysis().getUrl()))
.andExpect(method(HttpMethod.POST))
.andExpect(content().string(objectMapper.writeValueAsString(input)))
.andRespond(withSuccess());
.andExpect(method(HttpMethod.POST))
.andExpect(content().string(objectMapper.writeValueAsString(input)))
.andRespond(withSuccess());
final WebServiceIntegrationException result = assertThrows(WebServiceIntegrationException.class,
() -> sut.analyzeFile(file, Collections.singleton(vocabulary.getUri())));
() -> sut.analyzeFile(file, Collections.singleton(
vocabulary.getUri())));
assertThat(result.getMessage(), containsString("empty response"));
mockServer.verify();
}
Expand All @@ -290,13 +294,13 @@ void analyzeFileCreatesFileBackupBeforeInvokingAnnotationGenerator() throws Exce
@Test
void analyzeFilePassesRepositoryAndSpecifiedVocabularyContextsToService() throws Exception {
final Set<URI> vocabs = IntStream.range(0, 5).mapToObj(i -> Generator.generateUri())
.collect(Collectors.toSet());
.collect(Collectors.toSet());
final TextAnalysisInput expected = textAnalysisInput();
expected.setVocabularyContexts(vocabs);
mockServer.expect(requestTo(config.getTextAnalysis().getUrl()))
.andExpect(method(HttpMethod.POST))
.andExpect(content().string(objectMapper.writeValueAsString(expected)))
.andRespond(withSuccess(CONTENT, MediaType.APPLICATION_XML));
.andExpect(method(HttpMethod.POST))
.andExpect(content().string(objectMapper.writeValueAsString(expected)))
.andRespond(withSuccess(CONTENT, MediaType.APPLICATION_XML));
sut.analyzeFile(file, vocabs);
mockServer.verify();
}
Expand All @@ -305,9 +309,9 @@ void analyzeFilePassesRepositoryAndSpecifiedVocabularyContextsToService() throws
void analyzeFileBacksUpFileContentBeforeSavingNewAnalyzedContent() throws Exception {
final TextAnalysisInput input = textAnalysisInput();
mockServer.expect(requestTo(config.getTextAnalysis().getUrl()))
.andExpect(method(HttpMethod.POST))
.andExpect(content().string(objectMapper.writeValueAsString(input)))
.andRespond(withSuccess(CONTENT, MediaType.APPLICATION_XML));
.andExpect(method(HttpMethod.POST))
.andExpect(content().string(objectMapper.writeValueAsString(input)))
.andRespond(withSuccess(CONTENT, MediaType.APPLICATION_XML));
sut.analyzeFile(file, Collections.singleton(vocabulary.getUri()));
mockServer.verify();
final InOrder inOrder = Mockito.inOrder(documentManagerSpy, annotationGeneratorMock);
Expand All @@ -318,8 +322,8 @@ void analyzeFileBacksUpFileContentBeforeSavingNewAnalyzedContent() throws Except
@Test
void analyzeFileCreatesTextAnalysisRecord() {
mockServer.expect(requestTo(config.getTextAnalysis().getUrl()))
.andExpect(method(HttpMethod.POST)).andExpect(content().string(containsString(CONTENT)))
.andRespond(withSuccess(CONTENT, MediaType.APPLICATION_XML));
.andExpect(method(HttpMethod.POST)).andExpect(content().string(containsString(CONTENT)))
.andRespond(withSuccess(CONTENT, MediaType.APPLICATION_XML));
sut.analyzeFile(file, Collections.singleton(vocabulary.getUri()));
final ArgumentCaptor<TextAnalysisRecord> captor = ArgumentCaptor.forClass(TextAnalysisRecord.class);
verify(textAnalysisRecordDao).persist(captor.capture());
Expand Down Expand Up @@ -424,7 +428,8 @@ void analyzeFilePublishesAnalysisFinishedEvent() {
.andRespond(withSuccess(CONTENT, MediaType.APPLICATION_XML));
sut.analyzeFile(file, Collections.singleton(vocabulary.getUri()));

ArgumentCaptor<FileTextAnalysisFinishedEvent> eventCaptor = ArgumentCaptor.forClass(FileTextAnalysisFinishedEvent.class);
ArgumentCaptor<FileTextAnalysisFinishedEvent> eventCaptor = ArgumentCaptor.forClass(
FileTextAnalysisFinishedEvent.class);
verify(eventPublisher).publishEvent(eventCaptor.capture());
assertNotNull(eventCaptor.getValue());
assertEquals(file.getUri(), eventCaptor.getValue().getFileUri());
Expand All @@ -444,10 +449,33 @@ void analyzeTermDefinitionPublishesAnalysisFinishedEvent() throws JsonProcessing

sut.analyzeTermDefinition(term, vocabulary.getUri());

ArgumentCaptor<TermDefinitionTextAnalysisFinishedEvent> eventCaptor = ArgumentCaptor.forClass(TermDefinitionTextAnalysisFinishedEvent.class);
ArgumentCaptor<TermDefinitionTextAnalysisFinishedEvent> eventCaptor = ArgumentCaptor.forClass(
TermDefinitionTextAnalysisFinishedEvent.class);
verify(eventPublisher).publishEvent(eventCaptor.capture());
assertNotNull(eventCaptor.getValue());
assertEquals(term.getUri(), eventCaptor.getValue().getTermUri());
assertEquals(vocabulary.getUri(), eventCaptor.getValue().getVocabularyIri());
}

@Test
void analyzeFileSetsFileLanguageInTextAnalysisInvocationInput() {
file.setLanguage("cs");
mockServer.expect(requestTo(config.getTextAnalysis().getUrl()))
.andExpect(method(HttpMethod.POST))
.andExpect(jsonPath("$.language").value("cs"))
.andRespond(withSuccess(CONTENT, MediaType.APPLICATION_XML));
sut.analyzeFile(file, Collections.singleton(vocabulary.getUri()));
mockServer.verify();
}

@Test
void analyzeFileUsesConfiguredPersistenceLanguageInTextAnalysisInvocationInputWhenFileLanguageIsNotSet() {
file.setLanguage(null);
mockServer.expect(requestTo(config.getTextAnalysis().getUrl()))
.andExpect(method(HttpMethod.POST))
.andExpect(jsonPath("$.language").value(Environment.LANGUAGE))
.andRespond(withSuccess(CONTENT, MediaType.APPLICATION_XML));
sut.analyzeFile(file, Collections.singleton(vocabulary.getUri()));
mockServer.verify();
}
}

0 comments on commit 8c8a15f

Please sign in to comment.