MultiContentTranslator.java

1
package fr.sii.ogham.core.translator.content;
2
3
import java.util.ArrayList;
4
import java.util.List;
5
import java.util.stream.Collectors;
6
7
import org.slf4j.Logger;
8
import org.slf4j.LoggerFactory;
9
10
import fr.sii.ogham.core.exception.handler.ContentTranslatorException;
11
import fr.sii.ogham.core.exception.handler.NoContentException;
12
import fr.sii.ogham.core.exception.handler.Recoverable;
13
import fr.sii.ogham.core.message.content.Content;
14
import fr.sii.ogham.core.message.content.MultiContent;
15
16
/**
17
 * <p>
18
 * Decorator that is able to handle {@link MultiContent}. A {@link MultiContent}
19
 * can provide several contents to put into the final message. For example,
20
 * emails can send both HTML and text content.
21
 * </p>
22
 * <p>
23
 * This implementation calls the delegate content translator to apply
24
 * translation on each sub content of the {@link MultiContent}. It can be useful
25
 * to apply content translations on every sub content like it should be applied
26
 * for normal content.
27
 * </p>
28
 * <p>
29
 * The same translator is applied for all sub contents.
30
 * </p>
31
 * <p>
32
 * If the content is not a {@link MultiContent}, then the content is returned
33
 * as-is.
34
 * </p>
35
 * 
36
 * @author Aurélien Baudet
37
 *
38
 */
39
public class MultiContentTranslator implements ContentTranslator {
40
	private static final Logger LOG = LoggerFactory.getLogger(MultiContentTranslator.class);
41
42
	/**
43
	 * The content translator to apply on each sub content
44
	 */
45
	private ContentTranslator delegate;
46
47
	public MultiContentTranslator(ContentTranslator delegate) {
48
		super();
49
		this.delegate = delegate;
50
	}
51
52
	@Override
53
	public Content translate(Content content) throws ContentTranslatorException {
54 5 1. translate : negated conditional → NO_COVERAGE
2. translate : negated conditional → TIMED_OUT
3. translate : negated conditional → KILLED
4. translate : negated conditional → KILLED
5. translate : negated conditional → KILLED
		if (!(content instanceof MultiContent)) {
55
			LOG.trace("Not a MultiContent => skip it");
56 5 1. translate : replaced return value with null for fr/sii/ogham/core/translator/content/MultiContentTranslator::translate → NO_COVERAGE
2. translate : replaced return value with null for fr/sii/ogham/core/translator/content/MultiContentTranslator::translate → SURVIVED
3. translate : replaced return value with null for fr/sii/ogham/core/translator/content/MultiContentTranslator::translate → TIMED_OUT
4. translate : replaced return value with null for fr/sii/ogham/core/translator/content/MultiContentTranslator::translate → KILLED
5. translate : replaced return value with null for fr/sii/ogham/core/translator/content/MultiContentTranslator::translate → KILLED
			return content;
57
		}
58
		MultiContent result = new MultiContent();
59
		List<ContentTranslatorException> missing = new ArrayList<>();
60
		for (Content c : ((MultiContent) content).getContents()) {
61 3 1. translate : removed call to fr/sii/ogham/core/translator/content/MultiContentTranslator::translate → NO_COVERAGE
2. translate : removed call to fr/sii/ogham/core/translator/content/MultiContentTranslator::translate → KILLED
3. translate : removed call to fr/sii/ogham/core/translator/content/MultiContentTranslator::translate → KILLED
			translate(c, result, missing);
62
		}
63 3 1. translate : negated conditional → NO_COVERAGE
2. translate : negated conditional → KILLED
3. translate : negated conditional → KILLED
		if (result.getContents().isEmpty()) {
64 3 1. translate : removed call to fr/sii/ogham/core/translator/content/MultiContentTranslator::handleEmptyContent → NO_COVERAGE
2. translate : removed call to fr/sii/ogham/core/translator/content/MultiContentTranslator::handleEmptyContent → KILLED
3. translate : removed call to fr/sii/ogham/core/translator/content/MultiContentTranslator::handleEmptyContent → KILLED
			handleEmptyContent(content, missing);
65
		}
66 3 1. translate : replaced return value with null for fr/sii/ogham/core/translator/content/MultiContentTranslator::translate → NO_COVERAGE
2. translate : replaced return value with null for fr/sii/ogham/core/translator/content/MultiContentTranslator::translate → KILLED
3. translate : replaced return value with null for fr/sii/ogham/core/translator/content/MultiContentTranslator::translate → KILLED
		return result;
67
	}
68
69
	@Override
70
	public String toString() {
71 3 1. toString : replaced return value with "" for fr/sii/ogham/core/translator/content/MultiContentTranslator::toString → NO_COVERAGE
2. toString : replaced return value with "" for fr/sii/ogham/core/translator/content/MultiContentTranslator::toString → SURVIVED
3. toString : replaced return value with "" for fr/sii/ogham/core/translator/content/MultiContentTranslator::toString → TIMED_OUT
		return "MultiContentTranslator";
72
	}
73
74
75
	private void translate(Content c, MultiContent result, List<ContentTranslatorException> missing) throws ContentTranslatorException {
76
		LOG.debug("Translate the sub content using {}", delegate);
77
		LOG.trace("sub content: {}", c);
78
		try {
79
			Content translated = delegate.translate(c);
80 3 1. translate : negated conditional → NO_COVERAGE
2. translate : negated conditional → KILLED
3. translate : negated conditional → KILLED
			if (translated == null) {
81
				LOG.debug("Sub content skipped");
82
				LOG.trace("sub-content: {}", c);
83
			} else {
84
				LOG.debug("Sub content added");
85
				LOG.trace("sub-content: {}", c);
86 3 1. translate : removed call to fr/sii/ogham/core/message/content/MultiContent::addContent → NO_COVERAGE
2. translate : removed call to fr/sii/ogham/core/message/content/MultiContent::addContent → KILLED
3. translate : removed call to fr/sii/ogham/core/message/content/MultiContent::addContent → KILLED
				result.addContent(translated);
87
			}
88
		} catch (ContentTranslatorException e) {
89 3 1. translate : removed call to fr/sii/ogham/core/translator/content/MultiContentTranslator::handleException → NO_COVERAGE
2. translate : removed call to fr/sii/ogham/core/translator/content/MultiContentTranslator::handleException → KILLED
3. translate : removed call to fr/sii/ogham/core/translator/content/MultiContentTranslator::handleException → KILLED
			handleException(e, missing);
90
		}
91
	}
92
93
	private static void handleException(ContentTranslatorException e, List<ContentTranslatorException> missing) throws ContentTranslatorException {
94 3 1. handleException : negated conditional → NO_COVERAGE
2. handleException : negated conditional → KILLED
3. handleException : negated conditional → KILLED
		if (!isRecoverable(e)) {
95
			throw e;
96
		}
97
		LOG.info("{} => ignoring", e.getMessage(), e);
98
		missing.add(e);
99
	}
100
101
	private static void handleEmptyContent(Content content, List<ContentTranslatorException> missing) throws NoContentException {
102 3 1. handleEmptyContent : negated conditional → NO_COVERAGE
2. handleEmptyContent : negated conditional → KILLED
3. handleEmptyContent : negated conditional → KILLED
		if (!missing.isEmpty()) {
103
			String notFoundTemplates = missing.stream().map(Exception::getMessage).collect(Collectors.joining("\n"));
104
			throw new NoContentException("The message is empty maybe due to some errors:\n" + notFoundTemplates, (MultiContent) content, missing);
105
		}
106
		throw new NoContentException("The message is empty", (MultiContent) content, missing);
107
	}
108
109
	private static boolean isRecoverable(ContentTranslatorException e) {
110 6 1. isRecoverable : replaced boolean return with false for fr/sii/ogham/core/translator/content/MultiContentTranslator::isRecoverable → NO_COVERAGE
2. isRecoverable : replaced boolean return with true for fr/sii/ogham/core/translator/content/MultiContentTranslator::isRecoverable → NO_COVERAGE
3. isRecoverable : replaced boolean return with false for fr/sii/ogham/core/translator/content/MultiContentTranslator::isRecoverable → KILLED
4. isRecoverable : replaced boolean return with false for fr/sii/ogham/core/translator/content/MultiContentTranslator::isRecoverable → KILLED
5. isRecoverable : replaced boolean return with true for fr/sii/ogham/core/translator/content/MultiContentTranslator::isRecoverable → KILLED
6. isRecoverable : replaced boolean return with true for fr/sii/ogham/core/translator/content/MultiContentTranslator::isRecoverable → KILLED
		return e.getClass().isAnnotationPresent(Recoverable.class);
111
	}
112
113
}

Mutations

54

1.1
Location : translate
Killed by : oghamjavamail.it.UnreadableAttachmentTest.attachmentUnreadable(oghamjavamail.it.UnreadableAttachmentTest)
negated conditional → KILLED

2.2
Location : translate
Killed by : oghamall.it.configuration.EmptyBuilderTest.emailSenderManuallyRegisteredAndTemplateParsersOnlyRegisteredCantHandleTemplateContentDueToResourceResolutionNotConfigured(oghamall.it.configuration.EmptyBuilderTest)
negated conditional → KILLED

3.3
Location : translate
Killed by : none
negated conditional → TIMED_OUT

4.4
Location : translate
Killed by : oghamcore.ut.core.translator.content.MultiContentTranslatorTest.nullTextShouldReturnOneParsedContent(oghamcore.ut.core.translator.content.MultiContentTranslatorTest)
negated conditional → KILLED

5.5
Location : translate
Killed by : none
negated conditional → NO_COVERAGE

56

1.1
Location : translate
Killed by : none
replaced return value with null for fr/sii/ogham/core/translator/content/MultiContentTranslator::translate → NO_COVERAGE

2.2
Location : translate
Killed by : oghamjavamail.it.UnreadableAttachmentTest.attachmentUnreadable(oghamjavamail.it.UnreadableAttachmentTest)
replaced return value with null for fr/sii/ogham/core/translator/content/MultiContentTranslator::translate → KILLED

3.3
Location : translate
Killed by : none
replaced return value with null for fr/sii/ogham/core/translator/content/MultiContentTranslator::translate → SURVIVED

4.4
Location : translate
Killed by : none
replaced return value with null for fr/sii/ogham/core/translator/content/MultiContentTranslator::translate → TIMED_OUT

5.5
Location : translate
Killed by : oghamall.it.configuration.EmptyBuilderTest.emailSenderManuallyRegisteredAndImageInliningEnabledButUnconfiguredResourceResolutionCantInlineImages(oghamall.it.configuration.EmptyBuilderTest)
replaced return value with null for fr/sii/ogham/core/translator/content/MultiContentTranslator::translate → KILLED

61

1.1
Location : translate
Killed by : oghamcore.ut.core.translator.content.MultiContentTranslatorTest.nullTextShouldReturnOneParsedContent(oghamcore.ut.core.translator.content.MultiContentTranslatorTest)
removed call to fr/sii/ogham/core/translator/content/MultiContentTranslator::translate → KILLED

2.2
Location : translate
Killed by : none
removed call to fr/sii/ogham/core/translator/content/MultiContentTranslator::translate → NO_COVERAGE

3.3
Location : translate
Killed by : oghamall.it.configuration.EmptyBuilderTest.emailSenderManuallyRegisteredAndTemplateParsersOnlyRegisteredCantHandleTemplateContentDueToResourceResolutionNotConfigured(oghamall.it.configuration.EmptyBuilderTest)
removed call to fr/sii/ogham/core/translator/content/MultiContentTranslator::translate → KILLED

63

1.1
Location : translate
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : translate
Killed by : oghamall.it.configuration.EmptyBuilderTest.emailSenderManuallyRegisteredAndTemplateParsersOnlyRegisteredCantHandleTemplateContentDueToResourceResolutionNotConfigured(oghamall.it.configuration.EmptyBuilderTest)
negated conditional → KILLED

3.3
Location : translate
Killed by : oghamcore.ut.core.translator.content.MultiContentTranslatorTest.nullTextShouldReturnOneParsedContent(oghamcore.ut.core.translator.content.MultiContentTranslatorTest)
negated conditional → KILLED

64

1.1
Location : translate
Killed by : oghamall.it.configuration.EmptyBuilderTest.emailSenderManuallyRegisteredAndTemplateParsersOnlyRegisteredCantHandleTemplateContentDueToResourceResolutionNotConfigured(oghamall.it.configuration.EmptyBuilderTest)
removed call to fr/sii/ogham/core/translator/content/MultiContentTranslator::handleEmptyContent → KILLED

2.2
Location : translate
Killed by : none
removed call to fr/sii/ogham/core/translator/content/MultiContentTranslator::handleEmptyContent → NO_COVERAGE

3.3
Location : translate
Killed by : oghamcore.ut.core.translator.content.MultiContentTranslatorTest.bothTemplatesNotFoundShouldThrowNoContentExceptionWithDetails(oghamcore.ut.core.translator.content.MultiContentTranslatorTest)
removed call to fr/sii/ogham/core/translator/content/MultiContentTranslator::handleEmptyContent → KILLED

66

1.1
Location : translate
Killed by : none
replaced return value with null for fr/sii/ogham/core/translator/content/MultiContentTranslator::translate → NO_COVERAGE

2.2
Location : translate
Killed by : oghamall.it.email.EmailExtractSubjectTest.subjectExtractedFromTextWithDefaultSubjectShouldSendWithSubjectExtractedFromText(oghamall.it.email.EmailExtractSubjectTest)
replaced return value with null for fr/sii/ogham/core/translator/content/MultiContentTranslator::translate → KILLED

3.3
Location : translate
Killed by : oghamcore.ut.core.translator.content.MultiContentTranslatorTest.nullTextShouldReturnOneParsedContent(oghamcore.ut.core.translator.content.MultiContentTranslatorTest)
replaced return value with null for fr/sii/ogham/core/translator/content/MultiContentTranslator::translate → KILLED

71

1.1
Location : toString
Killed by : none
replaced return value with "" for fr/sii/ogham/core/translator/content/MultiContentTranslator::toString → TIMED_OUT

2.2
Location : toString
Killed by : none
replaced return value with "" for fr/sii/ogham/core/translator/content/MultiContentTranslator::toString → NO_COVERAGE

3.3
Location : toString
Killed by : none
replaced return value with "" for fr/sii/ogham/core/translator/content/MultiContentTranslator::toString → SURVIVED

80

1.1
Location : translate
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : translate
Killed by : oghamall.it.email.EmailExtractSubjectTest.subjectExtractedFromTextWithDefaultSubjectShouldSendWithSubjectExtractedFromText(oghamall.it.email.EmailExtractSubjectTest)
negated conditional → KILLED

3.3
Location : translate
Killed by : oghamcore.ut.core.translator.content.MultiContentTranslatorTest.nullTextShouldReturnOneParsedContent(oghamcore.ut.core.translator.content.MultiContentTranslatorTest)
negated conditional → KILLED

86

1.1
Location : translate
Killed by : none
removed call to fr/sii/ogham/core/message/content/MultiContent::addContent → NO_COVERAGE

2.2
Location : translate
Killed by : oghamall.it.email.EmailExtractSubjectTest.subjectExtractedFromTextWithDefaultSubjectShouldSendWithSubjectExtractedFromText(oghamall.it.email.EmailExtractSubjectTest)
removed call to fr/sii/ogham/core/message/content/MultiContent::addContent → KILLED

3.3
Location : translate
Killed by : oghamcore.ut.core.translator.content.MultiContentTranslatorTest.nullTextShouldReturnOneParsedContent(oghamcore.ut.core.translator.content.MultiContentTranslatorTest)
removed call to fr/sii/ogham/core/message/content/MultiContent::addContent → KILLED

89

1.1
Location : translate
Killed by : none
removed call to fr/sii/ogham/core/translator/content/MultiContentTranslator::handleException → NO_COVERAGE

2.2
Location : translate
Killed by : oghamall.it.configuration.EmptyBuilderTest.emailSenderManuallyRegisteredAndTemplateParsersOnlyRegisteredCantHandleTemplateContentDueToResourceResolutionNotConfigured(oghamall.it.configuration.EmptyBuilderTest)
removed call to fr/sii/ogham/core/translator/content/MultiContentTranslator::handleException → KILLED

3.3
Location : translate
Killed by : oghamcore.ut.core.translator.content.MultiContentTranslatorTest.parsingTextTemplateFailsShouldThrowAnError(oghamcore.ut.core.translator.content.MultiContentTranslatorTest)
removed call to fr/sii/ogham/core/translator/content/MultiContentTranslator::handleException → KILLED

94

1.1
Location : handleException
Killed by : oghamcore.ut.core.translator.content.MultiContentTranslatorTest.parsingTextTemplateFailsShouldThrowAnError(oghamcore.ut.core.translator.content.MultiContentTranslatorTest)
negated conditional → KILLED

2.2
Location : handleException
Killed by : none
negated conditional → NO_COVERAGE

3.3
Location : handleException
Killed by : oghamall.it.configuration.EmptyBuilderTest.emailSenderManuallyRegisteredAndTemplateParsersOnlyRegisteredCantHandleTemplateContentDueToResourceResolutionNotConfigured(oghamall.it.configuration.EmptyBuilderTest)
negated conditional → KILLED

102

1.1
Location : handleEmptyContent
Killed by : oghamcore.ut.core.translator.content.MultiContentTranslatorTest.bothTemplatesNotFoundShouldThrowNoContentExceptionWithDetails(oghamcore.ut.core.translator.content.MultiContentTranslatorTest)
negated conditional → KILLED

2.2
Location : handleEmptyContent
Killed by : oghamall.it.configuration.EmptyBuilderTest.emailSenderManuallyRegisteredAndTemplateParsersOnlyRegisteredCantHandleTemplateContentDueToResourceResolutionNotConfigured(oghamall.it.configuration.EmptyBuilderTest)
negated conditional → KILLED

3.3
Location : handleEmptyContent
Killed by : none
negated conditional → NO_COVERAGE

110

1.1
Location : isRecoverable
Killed by : oghamcore.ut.core.translator.content.MultiContentTranslatorTest.bothTemplatesNotFoundShouldThrowNoContentExceptionWithDetails(oghamcore.ut.core.translator.content.MultiContentTranslatorTest)
replaced boolean return with false for fr/sii/ogham/core/translator/content/MultiContentTranslator::isRecoverable → KILLED

2.2
Location : isRecoverable
Killed by : none
replaced boolean return with false for fr/sii/ogham/core/translator/content/MultiContentTranslator::isRecoverable → NO_COVERAGE

3.3
Location : isRecoverable
Killed by : oghamall.it.configuration.EmptyBuilderTest.emailSenderManuallyRegisteredAndTemplateParsersOnlyRegisteredCantHandleTemplateContentDueToResourceResolutionNotConfigured(oghamall.it.configuration.EmptyBuilderTest)
replaced boolean return with false for fr/sii/ogham/core/translator/content/MultiContentTranslator::isRecoverable → KILLED

4.4
Location : isRecoverable
Killed by : oghamall.it.email.EmailMultiTemplateTest.withThymeleafOneVariantWithParsingError(oghamall.it.email.EmailMultiTemplateTest)
replaced boolean return with true for fr/sii/ogham/core/translator/content/MultiContentTranslator::isRecoverable → KILLED

5.5
Location : isRecoverable
Killed by : none
replaced boolean return with true for fr/sii/ogham/core/translator/content/MultiContentTranslator::isRecoverable → NO_COVERAGE

6.6
Location : isRecoverable
Killed by : oghamcore.ut.core.translator.content.MultiContentTranslatorTest.parsingTextTemplateFailsShouldThrowAnError(oghamcore.ut.core.translator.content.MultiContentTranslatorTest)
replaced boolean return with true for fr/sii/ogham/core/translator/content/MultiContentTranslator::isRecoverable → KILLED

Active mutators

Tests examined


Report generated by PIT OGHAM