Skip to content

Commit

Permalink
[kbss-cvut/termit-ui#553] Set File language when it is not provided o…
Browse files Browse the repository at this point in the history
…n creation.
  • Loading branch information
ledsoft committed Nov 7, 2024
1 parent 4d9c4e8 commit 0a6fdba
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import cz.cvut.kbss.termit.service.document.html.UnconfirmedTermOccurrenceRemover;
import cz.cvut.kbss.termit.service.repository.ChangeRecordService;
import cz.cvut.kbss.termit.service.repository.ResourceRepositoryService;
import cz.cvut.kbss.termit.util.Configuration;
import cz.cvut.kbss.termit.util.TypeAwareResource;
import jakarta.annotation.Nonnull;
import org.slf4j.Logger;
Expand Down Expand Up @@ -80,22 +81,26 @@ public class ResourceService

private final ChangeRecordService changeRecordService;

private final Configuration config;

private ApplicationEventPublisher eventPublisher;

@Autowired
public ResourceService(ResourceRepositoryService repositoryService, DocumentManager documentManager,
TextAnalysisService textAnalysisService, VocabularyService vocabularyService,
ChangeRecordService changeRecordService) {
ChangeRecordService changeRecordService, Configuration config) {
this.repositoryService = repositoryService;
this.documentManager = documentManager;
this.textAnalysisService = textAnalysisService;
this.vocabularyService = vocabularyService;
this.changeRecordService = changeRecordService;
this.config = config;
}

/**
* Ensures that document gets removed during Vocabulary removal
*/
@Transactional
@EventListener
public void onVocabularyRemoval(VocabularyWillBeRemovedEvent event) {
vocabularyService.find(event.getVocabularyIri()).ifPresent(vocabulary -> {
Expand Down Expand Up @@ -239,6 +244,9 @@ public void addFileToDocument(Resource document, File file) {
throw new UnsupportedAssetOperationException("Cannot add file to the specified resource " + document);
}
doc.addFile(file);
if (file.getLanguage() == null) {
file.setLanguage(config.getPersistence().getLanguage());
}
if (doc.getVocabulary() != null) {
final Vocabulary vocabulary = vocabularyService.getReference(doc.getVocabulary());
repositoryService.persist(file, vocabulary);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import cz.cvut.kbss.termit.service.document.TextAnalysisService;
import cz.cvut.kbss.termit.service.repository.ChangeRecordService;
import cz.cvut.kbss.termit.service.repository.ResourceRepositoryService;
import cz.cvut.kbss.termit.util.Configuration;
import cz.cvut.kbss.termit.util.Constants;
import cz.cvut.kbss.termit.util.TypeAwareByteArrayResource;
import cz.cvut.kbss.termit.util.TypeAwareResource;
import cz.cvut.kbss.termit.util.Utils;
Expand All @@ -47,6 +49,7 @@
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -96,6 +99,9 @@ class ResourceServiceTest {
@Mock
private ApplicationEventPublisher eventPublisher;

@Spy
private Configuration config = new Configuration();

@InjectMocks
private ResourceService sut;

Expand Down Expand Up @@ -515,4 +521,37 @@ void getContentWithoutUnconfirmedOccurrencesRemovesUnconfirmedOccurrencesFromFil
final org.jsoup.nodes.Document doc = Jsoup.parse(result.getInputStream(), StandardCharsets.UTF_8.name(), "");
assertTrue(doc.select("span[score]").isEmpty());
}

@Test
void addFileToDocumentSetsFileLanguageToDefaultConfiguredWhenNotProvided() {
config.getPersistence().setLanguage(Constants.DEFAULT_LANGUAGE);
final Vocabulary vocabulary = Generator.generateVocabularyWithId();
final Document document = Generator.generateDocumentWithId();
document.setVocabulary(vocabulary.getUri());
final File file = Generator.generateFileWithId("test.hml");
when(resourceRepositoryService.exists(document.getUri())).thenReturn(true);
when(resourceRepositoryService.findRequired(document.getUri())).thenReturn(document);
when(vocabularyService.getReference(vocabulary.getUri())).thenReturn(vocabulary);

sut.addFileToDocument(document, file);
verify(resourceRepositoryService).persist(file, vocabulary);
assertEquals(config.getPersistence().getLanguage(), file.getLanguage());
}

@Test
void addFileToDocumentDoesNotModifyLanguageWhenItIsAlreadySet() {
config.getPersistence().setLanguage(Constants.DEFAULT_LANGUAGE);
final Vocabulary vocabulary = Generator.generateVocabularyWithId();
final Document document = Generator.generateDocumentWithId();
document.setVocabulary(vocabulary.getUri());
final File file = Generator.generateFileWithId("test.hml");
file.setLanguage("cs");
when(resourceRepositoryService.exists(document.getUri())).thenReturn(true);
when(resourceRepositoryService.findRequired(document.getUri())).thenReturn(document);
when(vocabularyService.getReference(vocabulary.getUri())).thenReturn(vocabulary);

sut.addFileToDocument(document, file);
verify(resourceRepositoryService).persist(file, vocabulary);
assertEquals("cs", file.getLanguage());
}
}

0 comments on commit 0a6fdba

Please sign in to comment.