diff --git a/src/main/java/com/justinefactory/cloud/communication/service/CloudObjectTransferService.java b/src/main/java/com/justinefactory/cloud/communication/service/CloudObjectTransferService.java new file mode 100644 index 0000000..3ae3f39 --- /dev/null +++ b/src/main/java/com/justinefactory/cloud/communication/service/CloudObjectTransferService.java @@ -0,0 +1,35 @@ +package com.justinefactory.cloud.communication.service; + + +import com.justinefactory.cloud.communication.transfer.CloudObjectContentGetter; +import com.justinefactory.deserialization.deserializers.Deserializer; +import com.justinefactory.domain.AwsInfo; +import com.justinefactory.reading.exceptions.AwsContentReadingException; +import com.justinefactory.reading.exceptions.ContentDeserializationException; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.io.IOException; +import java.lang.invoke.MethodHandles; + +class CloudObjectTransferService { + + private final CloudObjectContentGetter cloudObjectContentGetter; + private final Deserializer deserializer; + private static final Logger logger = LogManager.getLogger(MethodHandles.lookup().lookupClass()); + + CloudObjectTransferService(CloudObjectContentGetter cloudObjectContentGetter, Deserializer deserializer) { + logger.info("Transfer service - initialization"); + this.cloudObjectContentGetter = cloudObjectContentGetter; + this.deserializer = deserializer; + } + + public DeserializedContent getAndDeserializeCloudObject(AwsInfo info, Class classToDeserialize) throws IOException, AwsContentReadingException, ContentDeserializationException { + ContentType input = cloudObjectContentGetter.getObjectContent(info); + logger.debug("Transfer Service - extracting content from cloud object - success."); + DeserializedContent deserializedContent = deserializer.deserialize(input, classToDeserialize); + logger.info("Transfer Service - finished with success."); + return deserializedContent; + } + +} diff --git a/src/main/java/com/justinefactory/cloud/communication/distribution/service/CloudObjectsManagementService.java b/src/main/java/com/justinefactory/cloud/communication/service/CloudObjectsManagementService.java similarity index 77% rename from src/main/java/com/justinefactory/cloud/communication/distribution/service/CloudObjectsManagementService.java rename to src/main/java/com/justinefactory/cloud/communication/service/CloudObjectsManagementService.java index 4863989..9e6c7e1 100644 --- a/src/main/java/com/justinefactory/cloud/communication/distribution/service/CloudObjectsManagementService.java +++ b/src/main/java/com/justinefactory/cloud/communication/service/CloudObjectsManagementService.java @@ -1,4 +1,4 @@ -package com.justinefactory.cloud.communication.distribution.service; +package com.justinefactory.cloud.communication.service; import com.amazonaws.services.s3.AmazonS3; import com.justinefactory.cloud.communication.distribution.ServerObjectDownloadByUrlCreator; @@ -6,7 +6,7 @@ import com.justinefactory.reading.exceptions.ContentReadingException; import com.justinefactory.sending.service.ContentSendingService; import com.justinefactory.stats.exceptions.StatsCalculatingException; -import com.justinefactory.writing.domain.ContentStorage; +import com.justinefactory.writing.domain.Content; import com.justinefactory.writing.exceptions.ContentWritingException; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -14,13 +14,13 @@ import java.lang.invoke.MethodHandles; import java.time.Duration; -public class CloudObjectsManagementService { +public class CloudObjectsManagementService { - private final ContentSendingService> sendingService; + private final ContentSendingService> sendingService; private final ServerObjectDownloadByUrlCreator urlCreator; private static final Logger logger = LogManager.getLogger(MethodHandles.lookup().lookupClass()); - public CloudObjectsManagementService(ContentSendingService> sendingService, ServerObjectDownloadByUrlCreator urlCreator) { + public CloudObjectsManagementService(ContentSendingService> sendingService, ServerObjectDownloadByUrlCreator urlCreator) { logger.debug("Creating, sending and management of server objects - initialization"); this.sendingService = sendingService; this.urlCreator = urlCreator; diff --git a/src/main/java/com/justinefactory/cloud/communication/transfer/CloudObjectContentGetter.java b/src/main/java/com/justinefactory/cloud/communication/transfer/CloudObjectContentGetter.java new file mode 100644 index 0000000..2378cf6 --- /dev/null +++ b/src/main/java/com/justinefactory/cloud/communication/transfer/CloudObjectContentGetter.java @@ -0,0 +1,11 @@ +package com.justinefactory.cloud.communication.transfer; + +import com.justinefactory.domain.AwsInfo; +import com.justinefactory.reading.exceptions.AwsContentReadingException; + +import java.io.IOException; + +public interface CloudObjectContentGetter { + + ContentType getObjectContent(AwsInfo info) throws IOException, AwsContentReadingException; +} diff --git a/src/main/java/com/justinefactory/cloud/communication/transfer/ContentExtractor.java b/src/main/java/com/justinefactory/cloud/communication/transfer/ContentExtractor.java new file mode 100644 index 0000000..b0529d4 --- /dev/null +++ b/src/main/java/com/justinefactory/cloud/communication/transfer/ContentExtractor.java @@ -0,0 +1,9 @@ +package com.justinefactory.cloud.communication.transfer; + +import com.justinefactory.reading.exceptions.AwsContentReadingException; + +public interface ContentExtractor { + + ContentType extractContent(Object object) throws AwsContentReadingException; + +} diff --git a/src/main/java/com/justinefactory/cloud/communication/transfer/ContentExtractorFromS3ObjectInputStream.java b/src/main/java/com/justinefactory/cloud/communication/transfer/ContentExtractorFromS3ObjectInputStream.java new file mode 100644 index 0000000..7f11bee --- /dev/null +++ b/src/main/java/com/justinefactory/cloud/communication/transfer/ContentExtractorFromS3ObjectInputStream.java @@ -0,0 +1,7 @@ +package com.justinefactory.cloud.communication.transfer; + +import com.amazonaws.services.s3.model.S3ObjectInputStream; + +public interface ContentExtractorFromS3ObjectInputStream extends ContentExtractor { + +} diff --git a/src/main/java/com/justinefactory/cloud/communication/transfer/JsonExtractorFromS3ObjectInputStream.java b/src/main/java/com/justinefactory/cloud/communication/transfer/JsonExtractorFromS3ObjectInputStream.java new file mode 100644 index 0000000..77be433 --- /dev/null +++ b/src/main/java/com/justinefactory/cloud/communication/transfer/JsonExtractorFromS3ObjectInputStream.java @@ -0,0 +1,29 @@ +package com.justinefactory.cloud.communication.transfer; + +import com.amazonaws.services.s3.model.S3ObjectInputStream; +import com.amazonaws.util.IOUtils; +import com.justinefactory.reading.exceptions.AwsContentReadingException; +import com.justinefactory.writing.domain.JsonContent; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.io.IOException; + + +public class JsonExtractorFromS3ObjectInputStream implements ContentExtractorFromS3ObjectInputStream { + + private final Logger logger = LogManager.getLogger(this.getClass()); + + @Override + public JsonContent extractContent(S3ObjectInputStream inputStream) throws AwsContentReadingException { + if (inputStream == null) { + throw new AwsContentReadingException("Trouble while processing inputStream - input was empty."); + } + try { + return new JsonContent(IOUtils.toString(inputStream)); + } catch (IOException e) { + logger.warn("Trouble while extracting json object from inputStream {}", inputStream, e); + throw new AwsContentReadingException(e, "Trouble while extracting json object from inputStream " + inputStream); + } + } +} diff --git a/src/main/java/com/justinefactory/cloud/communication/transfer/S3CloudObjectContentGetter.java b/src/main/java/com/justinefactory/cloud/communication/transfer/S3CloudObjectContentGetter.java new file mode 100644 index 0000000..0a65a5e --- /dev/null +++ b/src/main/java/com/justinefactory/cloud/communication/transfer/S3CloudObjectContentGetter.java @@ -0,0 +1,39 @@ +package com.justinefactory.cloud.communication.transfer; + +import com.amazonaws.AmazonServiceException; +import com.amazonaws.SdkClientException; +import com.amazonaws.services.s3.AmazonS3; +import com.amazonaws.services.s3.model.GetObjectRequest; +import com.amazonaws.services.s3.model.S3ObjectInputStream; +import com.justinefactory.domain.AwsInfo; +import com.justinefactory.reading.exceptions.AwsContentReadingException; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.io.IOException; + + +public class S3CloudObjectContentGetter implements CloudObjectContentGetter { + + private final AmazonS3 client; + private final ContentExtractorFromS3ObjectInputStream contentExtractor; + private final Logger logger = LogManager.getLogger(this.getClass()); + + public S3CloudObjectContentGetter(AmazonS3 awsClient, ContentExtractorFromS3ObjectInputStream contentExtractor) { + client = awsClient; + this.contentExtractor = contentExtractor; + } + + @Override + public ContentType getObjectContent(AwsInfo info) throws IOException, AwsContentReadingException { + try (S3ObjectInputStream inputStream = client.getObject(new GetObjectRequest(info.getBucketName(), info.getKeyName())).getObjectContent()) { + return contentExtractor.extractContent(inputStream); + } catch (AmazonServiceException e) { + logger.warn("Trouble with Amazon S3 while processing get request of {}.", info.getURI()); + throw new AmazonServiceException("Trouble with Amazon S3 while processing request on ." + info.getURI(), e); + } catch (SdkClientException e) { + logger.warn("Trouble with Amazon S3 connection while processing get request on {}.", info.getURI()); + throw new SdkClientException("Trouble with Amazon S3 connection.", e); + } + } +} diff --git a/src/main/java/com/justinefactory/deserialization/deserializers/Deserializer.java b/src/main/java/com/justinefactory/deserialization/deserializers/Deserializer.java new file mode 100644 index 0000000..14059b3 --- /dev/null +++ b/src/main/java/com/justinefactory/deserialization/deserializers/Deserializer.java @@ -0,0 +1,11 @@ +package com.justinefactory.deserialization.deserializers; + +import com.justinefactory.reading.exceptions.ContentDeserializationException; + +import java.lang.reflect.Type; + +public interface Deserializer { + + ContentType deserialize(Object object, Class type) throws ContentDeserializationException; + +} diff --git a/src/main/java/com/justinefactory/deserialization/deserializers/JsonDeserializer.java b/src/main/java/com/justinefactory/deserialization/deserializers/JsonDeserializer.java new file mode 100644 index 0000000..6457b1a --- /dev/null +++ b/src/main/java/com/justinefactory/deserialization/deserializers/JsonDeserializer.java @@ -0,0 +1,24 @@ +package com.justinefactory.deserialization.deserializers; + +import com.google.gson.Gson; +import com.justinefactory.reading.exceptions.ContentDeserializationException; +import com.justinefactory.writing.domain.JsonContent; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +public class JsonDeserializer implements Deserializer { + + private final Logger logger = LogManager.getLogger(this.getClass()); + + @Override + public ContentType deserialize(JsonContent jsonContent, Class classTypeToDeserializeJson) throws ContentDeserializationException { + Gson gson = new Gson(); + try { + return gson.fromJson(jsonContent.getContent(), classTypeToDeserializeJson); + } catch (Throwable e) { + logger.warn("Trouble while performing deserialization of json: {}", jsonContent.getContent(), e); + throw new ContentDeserializationException(e, "Trouble while performing deserialization of json: " + jsonContent.getContent()); + } + } + +} diff --git a/src/main/java/com/justinefactory/reading/exceptions/AwsContentReadingException.java b/src/main/java/com/justinefactory/reading/exceptions/AwsContentReadingException.java new file mode 100644 index 0000000..dc352bc --- /dev/null +++ b/src/main/java/com/justinefactory/reading/exceptions/AwsContentReadingException.java @@ -0,0 +1,16 @@ +package com.justinefactory.reading.exceptions; + +public class AwsContentReadingException extends ContentReadingException { + + public AwsContentReadingException(Throwable cause) { + super(cause); + } + + public AwsContentReadingException(Throwable cause, String message) { + super(cause, message); + } + + public AwsContentReadingException(String message) { + super(message); + } +} diff --git a/src/main/java/com/justinefactory/reading/exceptions/ContentDeserializationException.java b/src/main/java/com/justinefactory/reading/exceptions/ContentDeserializationException.java new file mode 100644 index 0000000..376c018 --- /dev/null +++ b/src/main/java/com/justinefactory/reading/exceptions/ContentDeserializationException.java @@ -0,0 +1,8 @@ +package com.justinefactory.reading.exceptions; + +public class ContentDeserializationException extends ContentReadingException { + + public ContentDeserializationException(Throwable cause, String message) { + super(cause, message); + } +} diff --git a/src/main/java/com/justinefactory/reading/parsers/ContentParser.java b/src/main/java/com/justinefactory/reading/parsers/ContentParser.java index dd344be..bd69dfd 100644 --- a/src/main/java/com/justinefactory/reading/parsers/ContentParser.java +++ b/src/main/java/com/justinefactory/reading/parsers/ContentParser.java @@ -1,8 +1,9 @@ package com.justinefactory.reading.parsers; import com.justinefactory.reading.exceptions.ContentParsingException; +import com.justinefactory.writing.domain.Content; -public interface ContentParser { +public interface ContentParser { - public Content parseLine(Line line) throws ContentParsingException; + Content parse(RawContent rawContent) throws ContentParsingException; } diff --git a/src/main/java/com/justinefactory/reading/parsers/CsvLineParser.java b/src/main/java/com/justinefactory/reading/parsers/CsvLineParser.java index 3ecb786..c9ba23d 100644 --- a/src/main/java/com/justinefactory/reading/parsers/CsvLineParser.java +++ b/src/main/java/com/justinefactory/reading/parsers/CsvLineParser.java @@ -1,5 +1,7 @@ package com.justinefactory.reading.parsers; -public interface CsvLineParser extends ContentParser { +import com.justinefactory.writing.domain.CsvContent; + +public interface CsvLineParser extends ContentParser { } \ No newline at end of file diff --git a/src/main/java/com/justinefactory/reading/parsers/IntegerPlainContentParser.java b/src/main/java/com/justinefactory/reading/parsers/IntegerPlainContentParser.java index b4cd0bc..0260e7f 100644 --- a/src/main/java/com/justinefactory/reading/parsers/IntegerPlainContentParser.java +++ b/src/main/java/com/justinefactory/reading/parsers/IntegerPlainContentParser.java @@ -1,6 +1,8 @@ package com.justinefactory.reading.parsers; import com.justinefactory.reading.exceptions.ContentParsingException; +import com.justinefactory.writing.domain.Content; +import com.justinefactory.writing.domain.PlainContent; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -11,7 +13,15 @@ public class IntegerPlainContentParser implements PlainContentParser { private final static Logger logger = LogManager.getLogger(MethodHandles.lookup().lookupClass()); @Override - public Integer parseLine(String rawLine) throws ContentParsingException { + public Content parse(PlainContent strings) throws ContentParsingException { + Content content = new Content<>(); + for (String item : strings.getContent()) { + content.addContent(parseLine(item)); + } + return content; + } + + private Integer parseLine(String rawLine) throws ContentParsingException { if (rawLine.isEmpty()) { logger.warn("Parsing file line to Integer - failed. Line: {} does not equal required number of columns (1).", rawLine); throw new ContentParsingException("Parsing file line to Integer - failed. The line " + rawLine + " does not equal required number of columns (1)."); diff --git a/src/main/java/com/justinefactory/reading/parsers/PlainContentParser.java b/src/main/java/com/justinefactory/reading/parsers/PlainContentParser.java index 4ea4cdd..a43158c 100644 --- a/src/main/java/com/justinefactory/reading/parsers/PlainContentParser.java +++ b/src/main/java/com/justinefactory/reading/parsers/PlainContentParser.java @@ -1,5 +1,7 @@ package com.justinefactory.reading.parsers; -public interface PlainContentParser extends ContentParser { +import com.justinefactory.writing.domain.PlainContent; + +public interface PlainContentParser extends ContentParser { } diff --git a/src/main/java/com/justinefactory/reading/parsers/ThreeElementCsvParser.java b/src/main/java/com/justinefactory/reading/parsers/ThreeElementCsvParser.java index 3f709dd..f8f96b4 100644 --- a/src/main/java/com/justinefactory/reading/parsers/ThreeElementCsvParser.java +++ b/src/main/java/com/justinefactory/reading/parsers/ThreeElementCsvParser.java @@ -2,18 +2,30 @@ import com.justinefactory.domain.ThreeElemContent; import com.justinefactory.reading.exceptions.ContentParsingException; +import com.justinefactory.writing.domain.Content; +import com.justinefactory.writing.domain.CsvContent; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.lang.invoke.MethodHandles; import java.util.Arrays; +import java.util.List; public class ThreeElementCsvParser implements CsvLineParser { private final Logger logger = LogManager.getLogger(MethodHandles.lookup().lookupClass()); @Override - public ThreeElemContent parseLine(String[] csvLine) throws ContentParsingException { + public Content parse(CsvContent strings) throws ContentParsingException { + Content content = new Content<>(); + for(String[] item : strings.getContent()){ + content.addContent(parseLine(item)); + } + return content; + } + + + private ThreeElemContent parseLine(String[] csvLine) throws ContentParsingException { logger.debug("Parsing csvFile line to ThreeElemContent class."); if (csvLine.length != 3) { logger.warn("Parsing csvFile line to ThreeElemContent class - CSV line: {} does not equal required number of columns (3).", csvLine); @@ -32,4 +44,6 @@ public ThreeElemContent parseLine(String[] csvLine) throws ContentParsingExcepti } } + + } diff --git a/src/main/java/com/justinefactory/reading/readers/ContentReader.java b/src/main/java/com/justinefactory/reading/readers/ContentReader.java index c8807f3..79ecddc 100644 --- a/src/main/java/com/justinefactory/reading/readers/ContentReader.java +++ b/src/main/java/com/justinefactory/reading/readers/ContentReader.java @@ -1,10 +1,10 @@ package com.justinefactory.reading.readers; import com.justinefactory.reading.exceptions.ContentReadingException; -import com.justinefactory.writing.domain.ContentStorage; +import com.justinefactory.writing.domain.Content; -public interface ContentReader { +public interface ContentReader { - ContentStorage readContent() throws ContentReadingException; + ContentType readContent() throws ContentReadingException; } diff --git a/src/main/java/com/justinefactory/reading/readers/CsvContentReader.java b/src/main/java/com/justinefactory/reading/readers/CsvContentReader.java index 6fd4c59..4d47efd 100644 --- a/src/main/java/com/justinefactory/reading/readers/CsvContentReader.java +++ b/src/main/java/com/justinefactory/reading/readers/CsvContentReader.java @@ -4,7 +4,7 @@ import com.justinefactory.reading.exceptions.ContentReadingException; import com.justinefactory.reading.exceptions.ReadingContentFromFileException; import com.justinefactory.reading.exceptions.SourceFileIsEmptyException; -import com.justinefactory.writing.domain.ContentStorage; +import com.justinefactory.writing.domain.CsvContent; import com.opencsv.CSVReader; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -12,12 +12,10 @@ import java.io.Reader; import java.lang.invoke.MethodHandles; import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; import static com.justinefactory.util.FileNotEmptyAndExistChecker.checkIfFileExistsIsNotDirAndNotEmpty; -class CsvContentReader implements ContentReader { +class CsvContentReader implements ContentReader { private final PathInfo fileData; private final Logger logger = LogManager.getLogger(MethodHandles.lookup().lookupClass()); @@ -28,7 +26,7 @@ class CsvContentReader implements ContentReader { @Override - public ContentStorage readContent() throws ContentReadingException { + public CsvContent readContent() throws ContentReadingException { logger.debug("Reading data from file id {}", fileData.getId()); if (!checkIfFileExistsIsNotDirAndNotEmpty(fileData.getPath())) { logger.warn("Reading data from file id {} failed. File does not exist or is empty.", fileData.getId()); @@ -38,7 +36,7 @@ public ContentStorage readContent() throws ContentReadingException { try (Reader reader = Files.newBufferedReader(fileData.getPath()); CSVReader csvReader = new CSVReader(reader) ) { - ContentStorage rawContent = new ContentStorage<>(csvReader.readAll()); + CsvContent rawContent = new CsvContent(csvReader.readAll()); logger.debug("Reading data from file id {} - success.", fileData.getId()); return rawContent; } catch (Throwable e) { diff --git a/src/main/java/com/justinefactory/reading/readers/PlainContentReader.java b/src/main/java/com/justinefactory/reading/readers/PlainContentReader.java index 61405bd..48e1e6f 100644 --- a/src/main/java/com/justinefactory/reading/readers/PlainContentReader.java +++ b/src/main/java/com/justinefactory/reading/readers/PlainContentReader.java @@ -4,7 +4,7 @@ import com.justinefactory.reading.exceptions.ContentReadingException; import com.justinefactory.reading.exceptions.ReadingContentFromFileException; import com.justinefactory.reading.exceptions.SourceFileIsEmptyException; -import com.justinefactory.writing.domain.ContentStorage; +import com.justinefactory.writing.domain.PlainContent; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -13,7 +13,7 @@ import static com.justinefactory.util.FileNotEmptyAndExistChecker.checkIfFileExistsIsNotDirAndNotEmpty; -class PlainContentReader implements ContentReader { +class PlainContentReader implements ContentReader { private final PathInfo fileData; private final Logger logger = LogManager.getLogger(MethodHandles.lookup().lookupClass()); @@ -25,14 +25,14 @@ class PlainContentReader implements ContentReader { @Override - public ContentStorage readContent() throws ContentReadingException { + public PlainContent readContent() throws ContentReadingException { logger.debug("Reading data from file id {}", fileData.getId()); if (!checkIfFileExistsIsNotDirAndNotEmpty(fileData.getPath())) { logger.warn("Reading data from file id {} failed. File does not exist or is empty.", fileData.getId()); throw new SourceFileIsEmptyException("Reading data from file id " + fileData.getId() + " - failed. File does not exist or is empty."); } - try{ - ContentStorage rawContent = new ContentStorage<>(Files.readAllLines(fileData.getPath())); + try { + PlainContent rawContent = new PlainContent(Files.readAllLines(fileData.getPath())); logger.debug("Reading data from file id {} - success.", fileData.getId()); return rawContent; } catch (Throwable e) { diff --git a/src/main/java/com/justinefactory/reading/service/ContentReadingService.java b/src/main/java/com/justinefactory/reading/service/ContentReadingService.java index c2298ed..ff85fb2 100644 --- a/src/main/java/com/justinefactory/reading/service/ContentReadingService.java +++ b/src/main/java/com/justinefactory/reading/service/ContentReadingService.java @@ -3,7 +3,7 @@ import com.justinefactory.reading.exceptions.ContentReadingException; import com.justinefactory.reading.parsers.ContentParser; import com.justinefactory.reading.readers.ContentReader; -import com.justinefactory.writing.domain.ContentStorage; +import com.justinefactory.writing.domain.Content; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -23,12 +23,9 @@ public ContentReadingService(ContentReader cr, ContentParser processContent() throws ContentReadingException { - ContentStorage rawContent = contentReader.readContent(); - ContentStorage content = new ContentStorage<>(); - for (int i = 0; i < rawContent.getContentSize(); i++) { - content.addContent(contentParser.parseLine(rawContent.getContent(i))); - } + public Content processContent() throws ContentReadingException { + RawContent rawContent = contentReader.readContent(); + Content content = contentParser.parse(rawContent); logger.info("Reading service - finished processing successfully."); return content; } diff --git a/src/main/java/com/justinefactory/sending/domain/ContentAndStatsStorage.java b/src/main/java/com/justinefactory/sending/domain/ContentAndStats.java similarity index 53% rename from src/main/java/com/justinefactory/sending/domain/ContentAndStatsStorage.java rename to src/main/java/com/justinefactory/sending/domain/ContentAndStats.java index 211decc..e5664ab 100644 --- a/src/main/java/com/justinefactory/sending/domain/ContentAndStatsStorage.java +++ b/src/main/java/com/justinefactory/sending/domain/ContentAndStats.java @@ -1,33 +1,35 @@ package com.justinefactory.sending.domain; import com.justinefactory.stats.domain.Stats; -import com.justinefactory.writing.domain.ContentStorage; +import com.justinefactory.writing.domain.Content; import java.util.Objects; -public class ContentAndStatsStorage { +public class ContentAndStats { - private final ContentStorage content; - private final Stats stats; - public ContentAndStatsStorage(ContentStorage ct, Stats st) throws IllegalArgumentException { + private final Content content; + private final Stats stats; + + public ContentAndStats(Content ct, Stats st) throws IllegalArgumentException { checkIfIsNullOrEmpty(ct, st); content = ct; stats = st; } - private void checkIfIsNullOrEmpty(ContentStorage content, Stats stats) throws IllegalArgumentException { - if (content == null || content.getAllContent() == null || content.getAllContent().isEmpty() || stats == null) { + private void checkIfIsNullOrEmpty(Content content, Stats stats) throws IllegalArgumentException { + if (content == null || content.getContent() == null || content.getContent().isEmpty() || stats == null) { throw new IllegalArgumentException("Trouble while writing content: " + content + " and stats: " + stats + " to ContentStorage. Content or stats are empty or null."); } } - public ContentStorage getContent() { + + public Content getContent() { return content; } - public Stats getStats() { + public Stats getStats() { return stats; } @@ -35,7 +37,7 @@ public Stats getStats() { public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - ContentAndStatsStorage that = (ContentAndStatsStorage) o; + ContentAndStats that = (ContentAndStats) o; return Objects.equals(content, that.content) && Objects.equals(stats, that.stats); } diff --git a/src/main/java/com/justinefactory/sending/domain/IntegerAndStats.java b/src/main/java/com/justinefactory/sending/domain/IntegerAndStats.java new file mode 100644 index 0000000..a54f5f5 --- /dev/null +++ b/src/main/java/com/justinefactory/sending/domain/IntegerAndStats.java @@ -0,0 +1,13 @@ +package com.justinefactory.sending.domain; + +import com.justinefactory.stats.domain.Stats; +import com.justinefactory.writing.domain.Content; + +public class IntegerAndStats extends ContentAndStats { + + + public IntegerAndStats(Content ct, Stats st) throws IllegalArgumentException { + super(ct, st); + } + +} diff --git a/src/main/java/com/justinefactory/sending/domain/ThreeElementContentAndStats.java b/src/main/java/com/justinefactory/sending/domain/ThreeElementContentAndStats.java new file mode 100644 index 0000000..b8dbba6 --- /dev/null +++ b/src/main/java/com/justinefactory/sending/domain/ThreeElementContentAndStats.java @@ -0,0 +1,12 @@ +package com.justinefactory.sending.domain; + +import com.justinefactory.domain.ThreeElemContent; +import com.justinefactory.stats.domain.Stats; +import com.justinefactory.writing.domain.Content; + +public class ThreeElementContentAndStats extends ContentAndStats { + + public ThreeElementContentAndStats(Content ct, Stats st) throws IllegalArgumentException { + super(ct, st); + } +} diff --git a/src/main/java/com/justinefactory/sending/service/ContentSendingService.java b/src/main/java/com/justinefactory/sending/service/ContentSendingService.java index d5ed720..8b7ec5b 100644 --- a/src/main/java/com/justinefactory/sending/service/ContentSendingService.java +++ b/src/main/java/com/justinefactory/sending/service/ContentSendingService.java @@ -2,27 +2,27 @@ import com.justinefactory.reading.exceptions.ContentReadingException; import com.justinefactory.reading.service.ContentReadingService; -import com.justinefactory.sending.domain.ContentAndStatsStorage; +import com.justinefactory.sending.domain.ContentAndStats; import com.justinefactory.stats.calculators.StatsCalculator; import com.justinefactory.stats.domain.Stats; import com.justinefactory.stats.exceptions.StatsCalculatingException; -import com.justinefactory.writing.domain.ContentStorage; +import com.justinefactory.writing.domain.Content; import com.justinefactory.writing.exceptions.ContentWritingException; -import com.justinefactory.writing.writers.ContentWritingService; +import com.justinefactory.writing.service.ContentWritingService; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.lang.invoke.MethodHandles; -public class ContentSendingService { +public class ContentSendingService { - private final ContentReadingService contentReadingService; - private final StatsCalculator statsCalculator; - private final ContentWritingService, ReadyToSendContent> contentWritingService; + private final ContentReadingService contentReadingService; + private final StatsCalculator statsCalculator; + private final ContentWritingService, ReadyToSendContent> contentWritingService; private static final Logger logger = LogManager.getLogger(MethodHandles.lookup().lookupClass()); - public ContentSendingService(ContentReadingService crs, StatsCalculator sc, ContentWritingService, ReadyToSendContent> cw) { + public ContentSendingService(ContentReadingService crs, StatsCalculator sc, ContentWritingService, ReadyToSendContent> cw) { logger.info("Content sending service - initialization."); contentReadingService = crs; statsCalculator = sc; @@ -31,9 +31,9 @@ public ContentSendingService(ContentReadingService crs, Sta public void sendContent() throws ContentReadingException, StatsCalculatingException, ContentWritingException { - ContentStorage content = contentReadingService.processContent(); - Stats stats = statsCalculator.calculateStats(content); - ContentAndStatsStorage contentAndStats = new ContentAndStatsStorage<>(content, stats); + Content content = contentReadingService.processContent(); + Stats stats = statsCalculator.calculateStats(content); + ContentAndStats contentAndStats = new ContentAndStats<>(content, stats); contentWritingService.writeContent(contentAndStats); logger.info("Content sending service - success."); } diff --git a/src/main/java/com/justinefactory/stats/calculators/IntegerContentStatsCalculator.java b/src/main/java/com/justinefactory/stats/calculators/IntegerContentStatsCalculator.java index b65a0fb..3f60378 100644 --- a/src/main/java/com/justinefactory/stats/calculators/IntegerContentStatsCalculator.java +++ b/src/main/java/com/justinefactory/stats/calculators/IntegerContentStatsCalculator.java @@ -2,7 +2,7 @@ import com.justinefactory.stats.domain.Stats; import com.justinefactory.stats.exceptions.StatsCalculatingException; -import com.justinefactory.writing.domain.ContentStorage; +import com.justinefactory.writing.domain.Content; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -16,7 +16,7 @@ public class IntegerContentStatsCalculator implements StatsCalculator { private final Logger logger = LogManager.getLogger(MethodHandles.lookup().lookupClass()); @Override - public Stats calculateStats(ContentStorage content) throws StatsCalculatingException { + public Stats calculateStats(Content content) throws StatsCalculatingException { logger.debug("Calculating stats from Integer content."); if (content == null || content.isEmpty()) { logger.warn("Calculating stats from Integer content - failed. Collection {} was empty.", content); @@ -31,17 +31,17 @@ public Stats calculateStats(ContentStorage content) throws Sta } - private Integer calculateCount(ContentStorage content) { + private Integer calculateCount(Content content) { return content.getContentSize(); } - private Integer calculateUniqueCount(ContentStorage content) { - Set uniqueContent = new HashSet<>(content.getAllContent()); + private Integer calculateUniqueCount(Content content) { + Set uniqueContent = new HashSet<>(content.getContent()); return uniqueContent.size(); } - private Integer calculateMax(ContentStorage content) { - return Collections.max(content.getAllContent()); + private Integer calculateMax(Content content) { + return Collections.max(content.getContent()); } diff --git a/src/main/java/com/justinefactory/stats/calculators/StatsCalculator.java b/src/main/java/com/justinefactory/stats/calculators/StatsCalculator.java index 0054b76..7ea52e6 100644 --- a/src/main/java/com/justinefactory/stats/calculators/StatsCalculator.java +++ b/src/main/java/com/justinefactory/stats/calculators/StatsCalculator.java @@ -2,10 +2,10 @@ import com.justinefactory.stats.domain.Stats; import com.justinefactory.stats.exceptions.StatsCalculatingException; -import com.justinefactory.writing.domain.ContentStorage; +import com.justinefactory.writing.domain.Content; -public interface StatsCalculator { +public interface StatsCalculator { - Stats calculateStats(ContentStorage content) throws StatsCalculatingException; + Stats calculateStats(Content content) throws StatsCalculatingException; } diff --git a/src/main/java/com/justinefactory/stats/calculators/ThreeElementContentStatsCalculator.java b/src/main/java/com/justinefactory/stats/calculators/ThreeElementContentStatsCalculator.java index 225650e..96e6914 100644 --- a/src/main/java/com/justinefactory/stats/calculators/ThreeElementContentStatsCalculator.java +++ b/src/main/java/com/justinefactory/stats/calculators/ThreeElementContentStatsCalculator.java @@ -3,7 +3,7 @@ import com.justinefactory.domain.ThreeElemContent; import com.justinefactory.stats.domain.Stats; import com.justinefactory.stats.exceptions.StatsCalculatingException; -import com.justinefactory.writing.domain.ContentStorage; +import com.justinefactory.writing.domain.Content; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -16,7 +16,7 @@ public class ThreeElementContentStatsCalculator implements StatsCalculator calculateStats(ContentStorage content) throws StatsCalculatingException { + public Stats calculateStats(Content content) throws StatsCalculatingException { logger.debug("Calculating stats from ThreeElementContent content."); if (content == null || content.isEmpty()) { logger.warn("Calculating stats from ThreeElementContent content - failed. Storage class {} is empty or does not exist.", content); @@ -30,22 +30,22 @@ public Stats calculateStats(ContentStorage c return stats; } - private Integer calculateCount(ContentStorage content) { + private Integer calculateCount(Content content) { return content.getContentSize(); } - private Integer calculateUniqueCount(ContentStorage content) { + private Integer calculateUniqueCount(Content content) { Set uniqueContent = new HashSet<>(); - for (ThreeElemContent item : content.getAllContent()) { + for (ThreeElemContent item : content.getContent()) { uniqueContent.add(new StringIntContent(item)); } return uniqueContent.size(); } - private ThreeElemContent calculateMax(ContentStorage content) { - return Collections.max(content.getAllContent(), comparator); + private ThreeElemContent calculateMax(Content content) { + return Collections.max(content.getContent(), comparator); } diff --git a/src/main/java/com/justinefactory/stats/domain/Stats.java b/src/main/java/com/justinefactory/stats/domain/Stats.java index c6a485f..89f0da3 100644 --- a/src/main/java/com/justinefactory/stats/domain/Stats.java +++ b/src/main/java/com/justinefactory/stats/domain/Stats.java @@ -2,14 +2,14 @@ import java.util.Objects; -public class Stats { +public class Stats { private final Integer count; private final Integer distinctCount; - private final Content max; + private final ContentType max; - public Stats(Integer count, Integer distinctCount, Content max) { + public Stats(Integer count, Integer distinctCount, ContentType max) { this.count = count; this.distinctCount = distinctCount; this.max = max; @@ -19,7 +19,7 @@ public Integer getDistinctCount() { return distinctCount; } - public Content getMax() { + public ContentType getMax() { return max; } diff --git a/src/main/java/com/justinefactory/writing/converters/ContentAndStatsToJsonConverter.java b/src/main/java/com/justinefactory/writing/converters/ContentAndStatsToJsonConverter.java index b6ad50f..07d2c25 100644 --- a/src/main/java/com/justinefactory/writing/converters/ContentAndStatsToJsonConverter.java +++ b/src/main/java/com/justinefactory/writing/converters/ContentAndStatsToJsonConverter.java @@ -2,11 +2,11 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; -import com.justinefactory.sending.domain.ContentAndStatsStorage; -import com.justinefactory.writing.domain.ContentStorage; +import com.justinefactory.sending.domain.ContentAndStats; +import com.justinefactory.writing.domain.JsonContent; import com.justinefactory.writing.exceptions.ContentConversion2ReadyToWriteException; -public class ContentAndStatsToJsonConverter implements ContentConverter, ContentStorage> { +public class ContentAndStatsToJsonConverter implements ContentConverter, JsonContent> { private static final Gson gson = new GsonBuilder() .setPrettyPrinting() @@ -14,12 +14,12 @@ public class ContentAndStatsToJsonConverter implements ContentConverter @Override - public ContentStorage convertContent(ContentAndStatsStorage content) throws ContentConversion2ReadyToWriteException { + public JsonContent convertContent(ContentAndStats content) throws ContentConversion2ReadyToWriteException { if (content == null) { throw new ContentConversion2ReadyToWriteException("Converting content " + content + " into line - failed. Content is empty or does not exist."); } String json = gson.toJson(content); - return new ContentStorage<>(json); + return new JsonContent(json); } } diff --git a/src/main/java/com/justinefactory/writing/converters/ContentConverter.java b/src/main/java/com/justinefactory/writing/converters/ContentConverter.java index 40bf98b..4155c82 100644 --- a/src/main/java/com/justinefactory/writing/converters/ContentConverter.java +++ b/src/main/java/com/justinefactory/writing/converters/ContentConverter.java @@ -2,8 +2,8 @@ import com.justinefactory.writing.exceptions.ContentConversion2ReadyToWriteException; -public interface ContentConverter { +public interface ContentConverter { - public ReadyToWriteContent convertContent(Content content) throws ContentConversion2ReadyToWriteException; + ReadyToWriteContent convertContent(ContentType content) throws ContentConversion2ReadyToWriteException; } diff --git a/src/main/java/com/justinefactory/writing/converters/ContentToCsvLinesConverter.java b/src/main/java/com/justinefactory/writing/converters/ContentToCsvLinesConverter.java deleted file mode 100644 index 43840fa..0000000 --- a/src/main/java/com/justinefactory/writing/converters/ContentToCsvLinesConverter.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.justinefactory.writing.converters; - -import com.justinefactory.writing.domain.ContentStorage; - -public interface ContentToCsvLinesConverter extends ContentConverter> { - -} diff --git a/src/main/java/com/justinefactory/writing/converters/IntegersToLinesConverter.java b/src/main/java/com/justinefactory/writing/converters/IntegersToLinesConverter.java index 012e935..41420a2 100644 --- a/src/main/java/com/justinefactory/writing/converters/IntegersToLinesConverter.java +++ b/src/main/java/com/justinefactory/writing/converters/IntegersToLinesConverter.java @@ -1,21 +1,22 @@ package com.justinefactory.writing.converters; -import com.justinefactory.writing.domain.ContentStorage; +import com.justinefactory.writing.domain.Content; +import com.justinefactory.writing.domain.PlainContent; import com.justinefactory.writing.exceptions.ContentConversion2ReadyToWriteException; -public class IntegersToLinesConverter implements ContentConverter, ContentStorage> { +public class IntegersToLinesConverter implements ContentConverter, PlainContent> { @Override - public ContentStorage convertContent(ContentStorage content) throws ContentConversion2ReadyToWriteException { + public PlainContent convertContent(Content content) throws ContentConversion2ReadyToWriteException { checkIfContentNull(content); - ContentStorage readyToWriteContent = new ContentStorage<>(); - for (Integer item : content.getAllContent()) { + PlainContent readyToWriteContent = new PlainContent(); + for (Integer item : content.getContent()) { readyToWriteContent.addContent(item.toString()); } return readyToWriteContent; } - private void checkIfContentNull(ContentStorage content) throws ContentConversion2ReadyToWriteException { + private void checkIfContentNull(Content content) throws ContentConversion2ReadyToWriteException { if (content == null || content.isEmpty()) { throw new ContentConversion2ReadyToWriteException("Converting content " + content + " into csvFile line - failed. Content is empty or null."); } diff --git a/src/main/java/com/justinefactory/writing/converters/ThreeElementContentToCsvLinesConverter.java b/src/main/java/com/justinefactory/writing/converters/ThreeElementContentToCsvLinesConverter.java index 9f4abb8..f1bc65e 100644 --- a/src/main/java/com/justinefactory/writing/converters/ThreeElementContentToCsvLinesConverter.java +++ b/src/main/java/com/justinefactory/writing/converters/ThreeElementContentToCsvLinesConverter.java @@ -1,30 +1,31 @@ package com.justinefactory.writing.converters; import com.justinefactory.domain.ThreeElemContent; -import com.justinefactory.writing.domain.ContentStorage; +import com.justinefactory.writing.domain.Content; +import com.justinefactory.writing.domain.CsvContent; import com.justinefactory.writing.exceptions.ContentConversion2ReadyToWriteException; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.lang.invoke.MethodHandles; -public class ThreeElementContentToCsvLinesConverter implements ContentToCsvLinesConverter> { +public class ThreeElementContentToCsvLinesConverter implements ContentConverter, CsvContent> { private final Logger logger = LogManager.getLogger(MethodHandles.lookup().lookupClass()); @Override - public ContentStorage convertContent(ContentStorage content) throws ContentConversion2ReadyToWriteException { + public CsvContent convertContent(Content content) throws ContentConversion2ReadyToWriteException { checkIfContentEmpty(content); - ContentStorage readyToWriteContent = new ContentStorage<>(); - for (ThreeElemContent item : content.getAllContent()) { + CsvContent readyToWriteContent = new CsvContent(); + for (ThreeElemContent item : content.getContent()) { readyToWriteContent.addContent(new String[]{item.getTimeStamp().toString(), item.getRandomInt().toString(), item.getRandomString()}); logger.debug("Converting ThreeElemContent object {} into csvFile lines - success.", content); } return readyToWriteContent; } - private void checkIfContentEmpty(ContentStorage content) throws ContentConversion2ReadyToWriteException { - if (content.getAllContent().isEmpty()) { + private void checkIfContentEmpty(Content content) throws ContentConversion2ReadyToWriteException { + if (content.isEmpty()) { throw new ContentConversion2ReadyToWriteException("Converting content " + content + " into csvFile lines - failed. Content was empty."); } } diff --git a/src/main/java/com/justinefactory/writing/domain/ContentStorage.java b/src/main/java/com/justinefactory/writing/domain/Content.java similarity index 68% rename from src/main/java/com/justinefactory/writing/domain/ContentStorage.java rename to src/main/java/com/justinefactory/writing/domain/Content.java index 8cdfe79..9818828 100644 --- a/src/main/java/com/justinefactory/writing/domain/ContentStorage.java +++ b/src/main/java/com/justinefactory/writing/domain/Content.java @@ -5,37 +5,37 @@ import java.util.List; import java.util.Objects; -public class ContentStorage { +public class Content { - private final List content; + private final List content; - public ContentStorage() { + public Content() { content = new ArrayList<>(); } - public ContentStorage(List rc) throws IllegalArgumentException { + public Content(List rc) throws IllegalArgumentException { if (rc == null) throw new IllegalArgumentException("Trouble while writing content " + rc + " into Storage. Content is null."); - for (Content item : rc) { + for (ContentType item : rc) { checkIfContentNull(item); } content = rc; } - public ContentStorage(Content rc) throws IllegalArgumentException { + public Content(ContentType rc) throws IllegalArgumentException { checkIfContentNull(rc); content = new ArrayList<>(); content.add(rc); } - private void checkIfContentNull(Content content) throws IllegalArgumentException { + private void checkIfContentNull(ContentType content) throws IllegalArgumentException { if (content == null) { throw new IllegalArgumentException("Trouble while writing content " + content + " into Storage. Content is null."); } } - public Content getContent(int i) { + public ContentType getContent(int i) { return content.get(i); } @@ -43,12 +43,13 @@ public int getContentSize() { return content.size(); } - public void addContent(Content content) throws IllegalArgumentException { + public void addContent(ContentType content) throws IllegalArgumentException { checkIfContentNull(content); this.content.add(content); } - public Collection getAllContent() { + + public Collection getContent() { return content; } @@ -61,7 +62,7 @@ public boolean isEmpty() { public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - ContentStorage that = (ContentStorage) o; + Content that = (Content) o; if (content.size() != that.content.size()) return false; return content.equals(that.content); } diff --git a/src/main/java/com/justinefactory/writing/domain/CsvContent.java b/src/main/java/com/justinefactory/writing/domain/CsvContent.java new file mode 100644 index 0000000..218cfe2 --- /dev/null +++ b/src/main/java/com/justinefactory/writing/domain/CsvContent.java @@ -0,0 +1,71 @@ +package com.justinefactory.writing.domain; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Objects; + +public class CsvContent { + + private final List content; + + public CsvContent() { + content = new ArrayList<>(); + } + + public CsvContent(String[] rc) throws IllegalArgumentException { + checkIfContentNull(rc); + content = new ArrayList<>(); + content.add(rc); + } + + public CsvContent(List list) { + for (String[] item : list) { + checkIfContentNull(item); + } + content = new ArrayList<>(); + content.addAll(list); + } + + public String[] getContent(int i) { + return content.get(i); + } + + + public void addContent(String[] content) throws IllegalArgumentException { + checkIfContentNull(content); + this.content.add(content); + } + + private void checkIfContentNull(String[] content) throws IllegalArgumentException { + if (content == null) { + throw new IllegalArgumentException("Trouble while writing content " + content + " into Storage. Content is null."); + } + } + + public Collection getContent() { + return content; + } + + public boolean isEmpty() { + return content.size() == 0; + } + + public Integer size() { + return content.size(); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + CsvContent that = (CsvContent) o; + if (content.size() != that.content.size()) return false; + return content.equals(that.content); + } + + @Override + public int hashCode() { + return Objects.hash(content); + } +} diff --git a/src/main/java/com/justinefactory/writing/domain/JsonContent.java b/src/main/java/com/justinefactory/writing/domain/JsonContent.java new file mode 100644 index 0000000..1eac68d --- /dev/null +++ b/src/main/java/com/justinefactory/writing/domain/JsonContent.java @@ -0,0 +1,22 @@ +package com.justinefactory.writing.domain; + +public class JsonContent { + + private final String json; + + public JsonContent(String json) { + ensureJsonNotEmptyAndNotNull(json); + this.json = json; + } + + private void ensureJsonNotEmptyAndNotNull(String json) throws IllegalArgumentException { + if (json == null || json.isEmpty()) { + throw new IllegalArgumentException("Trouble while writing json: " + json + ". The argument cannot be empty or null."); + } + } + + public String getContent() { + return json; + } + +} diff --git a/src/main/java/com/justinefactory/writing/domain/PlainContent.java b/src/main/java/com/justinefactory/writing/domain/PlainContent.java new file mode 100644 index 0000000..0bdd01a --- /dev/null +++ b/src/main/java/com/justinefactory/writing/domain/PlainContent.java @@ -0,0 +1,65 @@ +package com.justinefactory.writing.domain; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Objects; + +public class PlainContent { + + private final List content; + + public PlainContent() { + content = new ArrayList<>(); + } + + public PlainContent(List rc) throws IllegalArgumentException { + if (rc == null) + throw new IllegalArgumentException("Trouble while writing content " + rc + " into Storage. Content is null."); + for (String item : rc) { + checkIfContentNull(item); + } + content = rc; + } + + + public String getContent(int i) { + return content.get(i); + } + + + public void addContent(String content) throws IllegalArgumentException { + checkIfContentNull(content); + this.content.add(content); + } + + private void checkIfContentNull(String content) throws IllegalArgumentException { + if (content == null) { + throw new IllegalArgumentException("Trouble while writing content " + content + " into Storage. Content is null."); + } + } + + public Collection getContent() { + return content; + } + + public boolean isEmpty() { + return content.size() == 0; + } + + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + PlainContent that = (PlainContent) o; + if (content.size() != that.content.size()) return false; + return content.equals(that.content); + } + + @Override + public int hashCode() { + return Objects.hash(content); + } + +} diff --git a/src/main/java/com/justinefactory/writing/generators/ContentGenerator.java b/src/main/java/com/justinefactory/writing/generators/ContentGenerator.java index 744778b..5912fd0 100644 --- a/src/main/java/com/justinefactory/writing/generators/ContentGenerator.java +++ b/src/main/java/com/justinefactory/writing/generators/ContentGenerator.java @@ -1,10 +1,10 @@ package com.justinefactory.writing.generators; -import com.justinefactory.writing.domain.ContentStorage; +import com.justinefactory.writing.domain.Content; import com.justinefactory.writing.exceptions.ContentGeneratingException; -public interface ContentGenerator { +public interface ContentGenerator { - ContentStorage generateContent(int nLines) throws ContentGeneratingException; + Content generateContent(int nLines) throws ContentGeneratingException; } diff --git a/src/main/java/com/justinefactory/writing/generators/RandomIntegerGenerator.java b/src/main/java/com/justinefactory/writing/generators/RandomIntegerGenerator.java index db7ab81..332f228 100644 --- a/src/main/java/com/justinefactory/writing/generators/RandomIntegerGenerator.java +++ b/src/main/java/com/justinefactory/writing/generators/RandomIntegerGenerator.java @@ -1,6 +1,6 @@ package com.justinefactory.writing.generators; -import com.justinefactory.writing.domain.ContentStorage; +import com.justinefactory.writing.domain.Content; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -17,9 +17,9 @@ public RandomIntegerGenerator(Random newNumber) { } @Override - public ContentStorage generateContent(int nLines) { + public Content generateContent(int nLines) { logger.debug("Generating random Integers."); - ContentStorage randomContent = new ContentStorage<>(); + Content randomContent = new Content<>(); for (int i = 0; i < nLines; i++) { randomContent.addContent(newNumber.nextInt()); } diff --git a/src/main/java/com/justinefactory/writing/generators/RandomStringGeneratorFromFile.java b/src/main/java/com/justinefactory/writing/generators/RandomStringGeneratorFromFile.java index 4346670..26aac0c 100644 --- a/src/main/java/com/justinefactory/writing/generators/RandomStringGeneratorFromFile.java +++ b/src/main/java/com/justinefactory/writing/generators/RandomStringGeneratorFromFile.java @@ -1,7 +1,7 @@ package com.justinefactory.writing.generators; import com.justinefactory.domain.PathInfo; -import com.justinefactory.writing.domain.ContentStorage; +import com.justinefactory.writing.domain.Content; import com.justinefactory.writing.domain.TwoElemContent; import com.justinefactory.writing.exceptions.ContentGeneratingException; import com.justinefactory.writing.exceptions.ContentInitializationException; @@ -32,9 +32,9 @@ public RandomStringGeneratorFromFile(Random newNumber, PathInfo fileWithRandomSt @Override - public ContentStorage generateContent(int nLines) { + public Content generateContent(int nLines) { logger.debug("Generating random strings from file."); - ContentStorage randomContent = new ContentStorage<>(); + Content randomContent = new Content<>(); for (int i = 0; i < nLines; i++) { diff --git a/src/main/java/com/justinefactory/writing/generators/ThreeElementContentGenerator.java b/src/main/java/com/justinefactory/writing/generators/ThreeElementContentGenerator.java index 85c32af..72c31b8 100644 --- a/src/main/java/com/justinefactory/writing/generators/ThreeElementContentGenerator.java +++ b/src/main/java/com/justinefactory/writing/generators/ThreeElementContentGenerator.java @@ -1,7 +1,7 @@ package com.justinefactory.writing.generators; import com.justinefactory.domain.ThreeElemContent; -import com.justinefactory.writing.domain.ContentStorage; +import com.justinefactory.writing.domain.Content; import com.justinefactory.writing.domain.TwoElemContent; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -22,11 +22,11 @@ public ThreeElementContentGenerator(RandomIntegerGenerator randomIntegerGenerato @Override - public ContentStorage generateContent(int nLines) { + public Content generateContent(int nLines) { logger.debug("Generating {} items of 3 element content.", nLines); - ContentStorage randomInt = intGenerator.generateContent(nLines); - ContentStorage randomStrTStamp = strAndTStampGenerator.generateContent(nLines); - ContentStorage randomContent = new ContentStorage<>(); + Content randomInt = intGenerator.generateContent(nLines); + Content randomStrTStamp = strAndTStampGenerator.generateContent(nLines); + Content randomContent = new Content<>(); for (int i = 0; i < nLines; i++) { ThreeElemContent threeContent = createThreeElemContentObject(randomInt.getContent(i), randomStrTStamp.getContent(i)); diff --git a/src/main/java/com/justinefactory/writing/service/ContentGeneratingAndWritingService.java b/src/main/java/com/justinefactory/writing/service/ContentGeneratingAndWritingService.java index 5b6382c..c4c4f87 100644 --- a/src/main/java/com/justinefactory/writing/service/ContentGeneratingAndWritingService.java +++ b/src/main/java/com/justinefactory/writing/service/ContentGeneratingAndWritingService.java @@ -1,24 +1,23 @@ package com.justinefactory.writing.service; -import com.justinefactory.writing.domain.ContentStorage; +import com.justinefactory.writing.domain.Content; import com.justinefactory.writing.exceptions.ContentGeneratingException; import com.justinefactory.writing.exceptions.ContentWritingException; import com.justinefactory.writing.generators.ContentGenerator; -import com.justinefactory.writing.writers.ContentWritingService; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.lang.invoke.MethodHandles; -class ContentGeneratingAndWritingService { +class ContentGeneratingAndWritingService { - private final ContentGenerator contentGenerator; - private final ContentWritingService, ContentStorage> contentWritingService; + private final ContentGenerator contentGenerator; + private final ContentWritingService, Content> contentWritingService; private static final Logger logger = LogManager.getLogger(MethodHandles.lookup().lookupClass()); - public ContentGeneratingAndWritingService(ContentWritingService, ContentStorage> fw, ContentGenerator rig) { + public ContentGeneratingAndWritingService(ContentWritingService, Content> fw, ContentGenerator rig) { logger.info("Writing service - initialization."); this.contentWritingService = fw; this.contentGenerator = rig; @@ -26,7 +25,7 @@ public ContentGeneratingAndWritingService(ContentWritingService content = contentGenerator.generateContent(nLines); + Content content = contentGenerator.generateContent(nLines); contentWritingService.writeContent(content); logger.info("Writing service - finished processing successfully."); } diff --git a/src/main/java/com/justinefactory/writing/writers/ContentWritingService.java b/src/main/java/com/justinefactory/writing/service/ContentWritingService.java similarity index 80% rename from src/main/java/com/justinefactory/writing/writers/ContentWritingService.java rename to src/main/java/com/justinefactory/writing/service/ContentWritingService.java index 4821611..6c1c173 100644 --- a/src/main/java/com/justinefactory/writing/writers/ContentWritingService.java +++ b/src/main/java/com/justinefactory/writing/service/ContentWritingService.java @@ -1,26 +1,27 @@ -package com.justinefactory.writing.writers; +package com.justinefactory.writing.service; import com.justinefactory.domain.WritingInfo; import com.justinefactory.writing.converters.ContentConverter; import com.justinefactory.writing.exceptions.ContentWritingException; import com.justinefactory.writing.util.CheckerIfContentAlreadyWritten; import com.justinefactory.writing.util.StorageContainerCreator; +import com.justinefactory.writing.writers.ContentWriter; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.lang.invoke.MethodHandles; -public class ContentWritingService { +public class ContentWritingService { private final WritingInfo writingInfo; private final CheckerIfContentAlreadyWritten checkerIfContentWritten; private final StorageContainerCreator storageContainerCreator; - private final ContentConverter converter; + private final ContentConverter converter; private final ContentWriter writer; private final Logger logger = LogManager.getLogger(MethodHandles.lookup().lookupClass()); - ContentWritingService(WritingInfo fd, CheckerIfContentAlreadyWritten cicaw, StorageContainerCreator scc, ContentConverter cr, ContentWriter wt) { + ContentWritingService(WritingInfo fd, CheckerIfContentAlreadyWritten cicaw, StorageContainerCreator scc, ContentConverter cr, ContentWriter wt) { writingInfo = fd; checkerIfContentWritten = cicaw; storageContainerCreator = scc; @@ -28,7 +29,7 @@ public class ContentWritingService { writer = wt; } - public void writeContent(Content content) throws ContentWritingException { + public void writeContent(ContentType content) throws ContentWritingException { logger.info("Writing content to path {} - initialization.", writingInfo.getURI()); if (content == null) { throw new ContentWritingException("Problem while Writing content to path " + writingInfo.getURI() + " content " + content + " is empty or does not exist."); @@ -39,7 +40,6 @@ public void writeContent(Content content) throws ContentWritingException { ReadyToWriteContent readyToWriteContent = converter.convertContent(content); logger.debug("Writing content to path {} - content has been converted to lines.", writingInfo.getURI()); writer.writeContent(readyToWriteContent, writingInfo); - logger.debug("Writing content to path {} - lines have been written and appended into path.", writingInfo.getURI()); logger.info("Writing content to path {} - success.", writingInfo.getURI()); } diff --git a/src/main/java/com/justinefactory/writing/writers/ContentWriter.java b/src/main/java/com/justinefactory/writing/writers/ContentWriter.java index 68d738d..171f1a4 100644 --- a/src/main/java/com/justinefactory/writing/writers/ContentWriter.java +++ b/src/main/java/com/justinefactory/writing/writers/ContentWriter.java @@ -3,8 +3,8 @@ import com.justinefactory.domain.WritingInfo; import com.justinefactory.writing.exceptions.ContentWritingException; -public interface ContentWriter { +public interface ContentWriter { - void writeContent(Content content, WritingInfo info) throws ContentWritingException; + void writeContent(ContentType content, WritingInfo info) throws ContentWritingException; } diff --git a/src/main/java/com/justinefactory/writing/writers/server/writers/JsonAwsWriter.java b/src/main/java/com/justinefactory/writing/writers/tocloud/JsonAwsWriter.java similarity index 68% rename from src/main/java/com/justinefactory/writing/writers/server/writers/JsonAwsWriter.java rename to src/main/java/com/justinefactory/writing/writers/tocloud/JsonAwsWriter.java index e0ae649..c68b970 100644 --- a/src/main/java/com/justinefactory/writing/writers/server/writers/JsonAwsWriter.java +++ b/src/main/java/com/justinefactory/writing/writers/tocloud/JsonAwsWriter.java @@ -1,14 +1,14 @@ -package com.justinefactory.writing.writers.server.writers; +package com.justinefactory.writing.writers.tocloud; import com.amazonaws.services.s3.AmazonS3; import com.justinefactory.domain.AwsInfo; -import com.justinefactory.writing.domain.ContentStorage; +import com.justinefactory.writing.domain.JsonContent; import com.justinefactory.writing.exceptions.AwsContentWritingException; import com.justinefactory.writing.writers.ContentWriter; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -public class JsonAwsWriter implements ContentWriter, AwsInfo> { +public class JsonAwsWriter implements ContentWriter { private final AmazonS3 client; private final Logger logger = LogManager.getLogger(this.getClass()); @@ -18,8 +18,8 @@ public JsonAwsWriter(AmazonS3 awsClient) { } @Override - public void writeContent(ContentStorage json, AwsInfo awsInfo) throws AwsContentWritingException { - String jsonToSend = String.join(System.lineSeparator(), json.getAllContent()); + public void writeContent(JsonContent jsonContentStorage, AwsInfo awsInfo) throws AwsContentWritingException { + String jsonToSend = jsonContentStorage.getContent(); try { client.putObject(awsInfo.getBucketName(), awsInfo.getKeyName(), jsonToSend); } catch (Throwable e) { diff --git a/src/main/java/com/justinefactory/writing/writers/file/writers/CsvFileWriter.java b/src/main/java/com/justinefactory/writing/writers/tofile/CsvFileWriter.java similarity index 72% rename from src/main/java/com/justinefactory/writing/writers/file/writers/CsvFileWriter.java rename to src/main/java/com/justinefactory/writing/writers/tofile/CsvFileWriter.java index f58e186..cd5de11 100644 --- a/src/main/java/com/justinefactory/writing/writers/file/writers/CsvFileWriter.java +++ b/src/main/java/com/justinefactory/writing/writers/tofile/CsvFileWriter.java @@ -1,7 +1,7 @@ -package com.justinefactory.writing.writers.file.writers; +package com.justinefactory.writing.writers.tofile; import com.justinefactory.domain.PathInfo; -import com.justinefactory.writing.domain.ContentStorage; +import com.justinefactory.writing.domain.CsvContent; import com.justinefactory.writing.exceptions.ContentWritingException; import com.justinefactory.writing.writers.ContentWriter; import com.opencsv.CSVWriter; @@ -13,17 +13,17 @@ import java.lang.invoke.MethodHandles; import java.nio.file.Files; -public class CsvFileWriter implements ContentWriter, PathInfo> { +public class CsvFileWriter implements ContentWriter { private final Logger logger = LogManager.getLogger(MethodHandles.lookup().lookupClass()); @Override - public void writeContent(ContentStorage content, PathInfo fileData) throws ContentWritingException { + public void writeContent(CsvContent content, PathInfo fileData) throws ContentWritingException { try (BufferedWriter writer = Files.newBufferedWriter(fileData.getPath()); CSVWriter csvWriter = new CSVWriter(writer) ) { - for (String[] item : content.getAllContent()) { + for (String[] item : content.getContent()) { csvWriter.writeNext(item); } diff --git a/src/main/java/com/justinefactory/writing/writers/tofile/JsonFileWriter.java b/src/main/java/com/justinefactory/writing/writers/tofile/JsonFileWriter.java new file mode 100644 index 0000000..fec66d2 --- /dev/null +++ b/src/main/java/com/justinefactory/writing/writers/tofile/JsonFileWriter.java @@ -0,0 +1,28 @@ +package com.justinefactory.writing.writers.tofile; + +import com.justinefactory.domain.PathInfo; +import com.justinefactory.writing.domain.JsonContent; +import com.justinefactory.writing.exceptions.ContentWritingException; +import com.justinefactory.writing.writers.ContentWriter; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.io.BufferedWriter; +import java.io.IOException; +import java.lang.invoke.MethodHandles; +import java.nio.file.Files; + +public class JsonFileWriter implements ContentWriter { + + private final Logger logger = LogManager.getLogger(MethodHandles.lookup().lookupClass()); + + @Override + public void writeContent(JsonContent jsonContent, PathInfo pathInfo) throws ContentWritingException { + try (BufferedWriter writer = Files.newBufferedWriter(pathInfo.getPath())) { + writer.write(jsonContent.getContent()); + } catch (IOException e) { + logger.warn("Problem while writing to file. Message: {}. Writing aborted.", e.getMessage()); + throw new ContentWritingException(e, "Problem while writing to file. Writing aborted."); + } + } +} diff --git a/src/main/java/com/justinefactory/writing/writers/file/writers/PlainFileWriter.java b/src/main/java/com/justinefactory/writing/writers/tofile/PlainFileWriter.java similarity index 71% rename from src/main/java/com/justinefactory/writing/writers/file/writers/PlainFileWriter.java rename to src/main/java/com/justinefactory/writing/writers/tofile/PlainFileWriter.java index 1f7c5b7..53acdad 100644 --- a/src/main/java/com/justinefactory/writing/writers/file/writers/PlainFileWriter.java +++ b/src/main/java/com/justinefactory/writing/writers/tofile/PlainFileWriter.java @@ -1,7 +1,7 @@ -package com.justinefactory.writing.writers.file.writers; +package com.justinefactory.writing.writers.tofile; import com.justinefactory.domain.PathInfo; -import com.justinefactory.writing.domain.ContentStorage; +import com.justinefactory.writing.domain.PlainContent; import com.justinefactory.writing.exceptions.ContentWritingException; import com.justinefactory.writing.writers.ContentWriter; import org.apache.logging.log4j.LogManager; @@ -12,14 +12,14 @@ import java.lang.invoke.MethodHandles; import java.nio.file.Files; -public class PlainFileWriter implements ContentWriter, PathInfo> { +public class PlainFileWriter implements ContentWriter { private final Logger logger = LogManager.getLogger(MethodHandles.lookup().lookupClass()); @Override - public void writeContent(ContentStorage content, PathInfo fileData) throws ContentWritingException { + public void writeContent(PlainContent content, PathInfo fileData) throws ContentWritingException { try (BufferedWriter writer = Files.newBufferedWriter(fileData.getPath())) { - for (String item : content.getAllContent()) { + for (String item : content.getContent()) { writer.write(item); writer.newLine(); } diff --git a/src/test/java/com/justinefactory/cloud/communication/transfer/S3CloudObjectContentGetterTest.java b/src/test/java/com/justinefactory/cloud/communication/transfer/S3CloudObjectContentGetterTest.java new file mode 100644 index 0000000..4bf3dbe --- /dev/null +++ b/src/test/java/com/justinefactory/cloud/communication/transfer/S3CloudObjectContentGetterTest.java @@ -0,0 +1,57 @@ +package com.justinefactory.cloud.communication.transfer; + +import com.amazonaws.AmazonServiceException; +import com.amazonaws.services.s3.AmazonS3; +import com.justinefactory.domain.AwsInfo; +import com.justinefactory.reading.exceptions.AwsContentReadingException; +import com.justinefactory.sending.exceptions.AwsSecurityCredentialsException; +import com.justinefactory.writing.domain.JsonContent; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.IOException; + +import static com.justinefactory.testutil.AwsClientCreatorBeforeEach.createAwsClient; +import static org.junit.jupiter.api.Assertions.*; + +class S3CloudObjectContentGetterTest { + + private static AmazonS3 awsClient; + private static AwsInfo info; + + @BeforeEach + public void createAwsClientAndAwsInfo() throws AwsSecurityCredentialsException { + awsClient = createAwsClient(); + info = new AwsInfo("com.justyna.lisok.factory.content-bucket", "three-element-content-test.json"); + } + + + @Test + public void getObjectWhenMeetingConditions() throws IOException, AwsContentReadingException { + //given + JsonExtractorFromS3ObjectInputStream extractor = new JsonExtractorFromS3ObjectInputStream(); + S3CloudObjectContentGetter getter = new S3CloudObjectContentGetter<>(awsClient, extractor); + + //when + JsonContent storage = getter.getObjectContent(info); + + //then + assertNotNull(storage.getContent()); + assertFalse(storage.getContent().isEmpty()); + } + + + @Test + public void getObjectWhenNoSuchFile() { + //given + JsonExtractorFromS3ObjectInputStream extractor = new JsonExtractorFromS3ObjectInputStream(); + S3CloudObjectContentGetter getter = new S3CloudObjectContentGetter<>(awsClient, extractor); + + //when + AwsInfo infoNoSuchFile = new AwsInfo("com.justyna.lisok.factory.content-bucket", "three-element-content-test-no-such-file.json"); + + //then + assertThrows(AmazonServiceException.class, () -> getter.getObjectContent(infoNoSuchFile)); + } + +} \ No newline at end of file diff --git a/src/test/java/com/justinefactory/deserialization/deserializers/JsonContentDeserializerTest.java b/src/test/java/com/justinefactory/deserialization/deserializers/JsonContentDeserializerTest.java new file mode 100644 index 0000000..9e6029c --- /dev/null +++ b/src/test/java/com/justinefactory/deserialization/deserializers/JsonContentDeserializerTest.java @@ -0,0 +1,62 @@ +package com.justinefactory.deserialization.deserializers; + +import com.justinefactory.domain.ThreeElemContent; +import com.justinefactory.reading.exceptions.ContentDeserializationException; +import com.justinefactory.sending.domain.ContentAndStats; +import com.justinefactory.sending.domain.IntegerAndStats; +import com.justinefactory.sending.domain.ThreeElementContentAndStats; +import com.justinefactory.stats.domain.Stats; +import com.justinefactory.writing.converters.ContentAndStatsToJsonConverter; +import com.justinefactory.writing.domain.Content; +import com.justinefactory.writing.domain.JsonContent; +import com.justinefactory.writing.exceptions.ContentConversion2ReadyToWriteException; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class JsonContentDeserializerTest { + + + @Test + void deserializeToThreeElemContentAndStats() throws ContentDeserializationException, ContentConversion2ReadyToWriteException { + // given + Content content = new Content<>(new ThreeElemContent(1590147349818750700L, -840762737, "ChristopherRobin")); + Stats stats = new Stats<>(1, 1, content.getContent(0)); + ThreeElementContentAndStats expectedStorage = new ThreeElementContentAndStats(content, stats); + JsonContent jsonContent = serializeContentAndStats(expectedStorage); + JsonDeserializer deserializer = new JsonDeserializer<>(); + Class classType = ThreeElementContentAndStats.class; + + //when + ThreeElementContentAndStats actualStorage = deserializer.deserialize(jsonContent, classType); + + //then + assertEquals(expectedStorage, actualStorage); + + } + + @Test + void deserializeToIntegerContentAndStats() throws ContentDeserializationException, ContentConversion2ReadyToWriteException { + // given + Content content = new Content<>(8); + Stats stats = new Stats<>(1, 1, 8); + IntegerAndStats expectedStorage = new IntegerAndStats(content, stats); + JsonContent jsonContent = serializeContentAndStats(expectedStorage); + JsonDeserializer deserializer = new JsonDeserializer<>(); + Class classType = IntegerAndStats.class; + + //when + IntegerAndStats actualStorage = deserializer.deserialize(jsonContent, classType); + + //then + assertEquals(expectedStorage, actualStorage); + + } + + + private static JsonContent serializeContentAndStats(ContentAndStats storage) throws ContentConversion2ReadyToWriteException { + ContentAndStatsToJsonConverter converter = new ContentAndStatsToJsonConverter<>(); + return converter.convertContent(storage); + } + +} \ No newline at end of file diff --git a/src/test/java/com/justinefactory/reading/parsers/IntegerPlainContentParserTest.java b/src/test/java/com/justinefactory/reading/parsers/IntegerPlainContentParserTest.java index 8c13d11..594ac81 100644 --- a/src/test/java/com/justinefactory/reading/parsers/IntegerPlainContentParserTest.java +++ b/src/test/java/com/justinefactory/reading/parsers/IntegerPlainContentParserTest.java @@ -1,46 +1,56 @@ package com.justinefactory.reading.parsers; import com.justinefactory.reading.exceptions.ContentParsingException; +import com.justinefactory.writing.domain.Content; +import com.justinefactory.writing.domain.PlainContent; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; -import org.junit.jupiter.params.provider.ValueSource; +import java.util.List; import java.util.stream.Stream; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; class IntegerPlainContentParserTest { @ParameterizedTest @MethodSource("parseLineWhenContentMeetsConditionsTestData") - void parseLineWhenContentMeetsConditions(String content, Integer expectedContent) throws Exception { + void parseLineWhenContentMeetsConditions(PlainContent content, Content expectedContent) throws Exception { //when IntegerPlainContentParser contentParser = new IntegerPlainContentParser(); - Integer integer = contentParser.parseLine(content); + Content integer = contentParser.parse(content); //then - assertEquals(integer, expectedContent); + assertEquals(expectedContent, integer); } static Stream parseLineWhenContentMeetsConditionsTestData() { return Stream.of( - Arguments.arguments("40000000", 40000000), - Arguments.arguments("800000", 800000), - Arguments.arguments("3245", 3245), - Arguments.arguments("0", 0), - Arguments.arguments("-765432",-765432) + Arguments.arguments(new PlainContent(List.of("40000000")), new Content<>(40000000)), + Arguments.arguments(new PlainContent(List.of("800000")), new Content<>(800000)), + Arguments.arguments(new PlainContent(List.of("3245")), new Content<>(3245)), + Arguments.arguments(new PlainContent(List.of("0")), new Content<>(0)), + Arguments.arguments(new PlainContent(List.of("-765432")), new Content<>(-765432)) ); } @ParameterizedTest - @ValueSource(strings = {"alex", "brian", "charles","-543,88763"}) - void parseLineWhenContentDoesNotMeetConditions(String content) { + @MethodSource("parseLineWhenContentDoesNotMeetConditionsTestData") + void parseLineWhenContentDoesNotMeetConditions(PlainContent content) { //when IntegerPlainContentParser contentParser = new IntegerPlainContentParser(); //then - assertThrows(ContentParsingException.class, () -> contentParser.parseLine(content)); + assertThrows(ContentParsingException.class, () -> contentParser.parse(content)); + } + + static Stream parseLineWhenContentDoesNotMeetConditionsTestData() { + return Stream.of( + Arguments.arguments(new PlainContent(List.of("Plain"))), + Arguments.arguments(new PlainContent(List.of("Raw"))) + ); } } \ No newline at end of file diff --git a/src/test/java/com/justinefactory/reading/parsers/ThreeElementCsvParserTest.java b/src/test/java/com/justinefactory/reading/parsers/ThreeElementCsvParserTest.java index bc5312e..8b93935 100644 --- a/src/test/java/com/justinefactory/reading/parsers/ThreeElementCsvParserTest.java +++ b/src/test/java/com/justinefactory/reading/parsers/ThreeElementCsvParserTest.java @@ -2,6 +2,8 @@ import com.justinefactory.domain.ThreeElemContent; import com.justinefactory.reading.exceptions.ContentParsingException; +import com.justinefactory.writing.domain.Content; +import com.justinefactory.writing.domain.CsvContent; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -15,57 +17,57 @@ class ThreeElementCsvParserTest { @ParameterizedTest @MethodSource("parseLineWhenContentMeetsConditionsTestData") - void parseLineWhenContentMeetsConditions(String[] content, ThreeElemContent expectedContent) throws Exception { + void parseLineWhenContentMeetsConditions(CsvContent content, Content expectedContent) throws Exception { //when ThreeElementCsvParser csvParser = new ThreeElementCsvParser(); //then - ThreeElemContent csvLine = csvParser.parseLine(content); + Content csvLine = csvParser.parse(content); assertEquals(expectedContent, csvLine); } static Stream parseLineWhenContentMeetsConditionsTestData() { return Stream.of( - Arguments.arguments(new String[]{"1590147349818750700", "-840762737", "ChristopherRobin"}, new ThreeElemContent(1590147349818750700L, -840762737, "ChristopherRobin")), - Arguments.arguments(new String[]{"1590147349820800800", "-1345882450", "Owl"}, new ThreeElemContent(1590147349820800800L, -1345882450, "Owl")), - Arguments.arguments(new String[]{"1590147349822277700", "1434010513", "Heffalumps"}, new ThreeElemContent(1590147349822277700L, 1434010513, "Heffalumps")), - Arguments.arguments(new String[]{"1590147349823733800", "1822510187", "Woozles"}, new ThreeElemContent(1590147349823733800L, 1822510187, "Woozles")) + Arguments.arguments(new CsvContent(new String[]{"1590147349818750700", "-840762737", "ChristopherRobin"}), new Content<>(new ThreeElemContent(1590147349818750700L, -840762737, "ChristopherRobin"))), + Arguments.arguments(new CsvContent(new String[]{"1590147349820800800", "-1345882450", "Owl"}), new Content<>(new ThreeElemContent(1590147349820800800L, -1345882450, "Owl"))), + Arguments.arguments(new CsvContent(new String[]{"1590147349822277700", "1434010513", "Heffalumps"}), new Content<>(new ThreeElemContent(1590147349822277700L, 1434010513, "Heffalumps"))), + Arguments.arguments(new CsvContent(new String[]{"1590147349823733800", "1822510187", "Woozles"}), new Content<>(new ThreeElemContent(1590147349823733800L, 1822510187, "Woozles"))) ); } @ParameterizedTest @MethodSource("parseLineWhenLineInFileCorruptTestData") - void parseLineWhenLineInFileCorrupt(String[] content) { + void parseLineWhenLineInFileCorrupt(CsvContent content) { //when ThreeElementCsvParser csvParser = new ThreeElementCsvParser(); //then - assertThrows(ContentParsingException.class, () -> csvParser.parseLine(content)); + assertThrows(ContentParsingException.class, () -> csvParser.parse(content)); } static Stream parseLineWhenLineInFileCorruptTestData() { return Stream.of( - Arguments.of((Object) new String[]{"1590147349818750700", "ChristopherRobin"}), - Arguments.of((Object) new String[]{"1590147349818750700", "840762737", "ChristopherRobin", "ChristopherRobin"}) + Arguments.arguments(new CsvContent(new String[]{"1590147349818750700", "ChristopherRobin"})), + Arguments.arguments(new CsvContent(new String[]{"1590147349818750700", "ChristopherRobin", "ChristopherRobin"})) ); } @ParameterizedTest @MethodSource("parseLineWhenColumnsAreMisplacedInFileTestData") - void parseLineWhenColumnsAreMisplacedInFile(String[] content) { + void parseLineWhenColumnsAreMisplacedInFile(CsvContent content) { //when ThreeElementCsvParser csvParser = new ThreeElementCsvParser(); //then - assertThrows(ContentParsingException.class, () -> csvParser.parseLine(content)); + assertThrows(ContentParsingException.class, () -> csvParser.parse(content)); } static Stream parseLineWhenColumnsAreMisplacedInFileTestData() { return Stream.of( - Arguments.of((Object) new String[]{"-1345882450", "Owl", "1590147349820800800"}) - ); + Arguments.arguments(new CsvContent(new String[]{"-1345882450", "Owl", "1590147349820800800"})) + ); } } diff --git a/src/test/java/com/justinefactory/reading/readers/ContentReadingServiceTest.java b/src/test/java/com/justinefactory/reading/readers/ContentReadingServiceTest.java index ca5350f..883c8fb 100644 --- a/src/test/java/com/justinefactory/reading/readers/ContentReadingServiceTest.java +++ b/src/test/java/com/justinefactory/reading/readers/ContentReadingServiceTest.java @@ -5,7 +5,9 @@ import com.justinefactory.reading.parsers.IntegerPlainContentParser; import com.justinefactory.reading.parsers.ThreeElementCsvParser; import com.justinefactory.reading.service.ContentReadingService; -import com.justinefactory.writing.domain.ContentStorage; +import com.justinefactory.writing.domain.Content; +import com.justinefactory.writing.domain.CsvContent; +import com.justinefactory.writing.domain.PlainContent; import org.junit.jupiter.api.Test; import java.nio.file.Path; @@ -19,7 +21,7 @@ class ContentReadingServiceTest { void processContentWhenProcessingCSVFile() throws Exception { //given Path filePath = getPathToResource("example-csv-file-for-tests.csv"); - ContentStorage expectedContent = new ContentStorage<>(); + Content expectedContent = new Content<>(); expectedContent.addContent(new ThreeElemContent(Long.parseLong("1590147349818750700"), -840762737, "ChristopherRobin")); expectedContent.addContent(new ThreeElemContent(Long.parseLong("1590147349820800800"), -1345882450, "Owl")); expectedContent.addContent(new ThreeElemContent(Long.parseLong("1590147349822277700"), 1434010513, "Heffalumps")); @@ -30,8 +32,8 @@ void processContentWhenProcessingCSVFile() throws Exception { PathInfo fileData = new PathInfo(filePath); CsvContentReader contentReader = new CsvContentReader(fileData); ThreeElementCsvParser csvParser = new ThreeElementCsvParser(); - ContentReadingService contentReadingService = new ContentReadingService(contentReader, csvParser); - ContentStorage content = contentReadingService.processContent(); + ContentReadingService contentReadingService = new ContentReadingService<>(contentReader, csvParser); + Content content = contentReadingService.processContent(); //then @@ -42,7 +44,7 @@ void processContentWhenProcessingCSVFile() throws Exception { void processContentWhenProcessingPlainFile() throws Exception { //given PathInfo fileData = new PathInfo(getPathToResource("txt-file-content-integers.txt")); - ContentStorage expectedContent = new ContentStorage<>(); + Content expectedContent = new Content<>(); expectedContent.addContent(40000000); expectedContent.addContent(800000); expectedContent.addContent(3245); @@ -51,8 +53,8 @@ void processContentWhenProcessingPlainFile() throws Exception { //when PlainContentReader contentReader = new PlainContentReader(fileData); IntegerPlainContentParser intParser = new IntegerPlainContentParser(); - ContentReadingService contentReadingService = new ContentReadingService(contentReader, intParser); - ContentStorage content = contentReadingService.processContent(); + ContentReadingService contentReadingService = new ContentReadingService<>(contentReader, intParser); + Content content = contentReadingService.processContent(); //then diff --git a/src/test/java/com/justinefactory/reading/readers/CsvContentReaderTest.java b/src/test/java/com/justinefactory/reading/readers/CsvContentReaderTest.java index 367d25e..80db532 100644 --- a/src/test/java/com/justinefactory/reading/readers/CsvContentReaderTest.java +++ b/src/test/java/com/justinefactory/reading/readers/CsvContentReaderTest.java @@ -2,7 +2,7 @@ import com.justinefactory.domain.PathInfo; import com.justinefactory.reading.exceptions.SourceFileIsEmptyException; -import com.justinefactory.writing.domain.ContentStorage; +import com.justinefactory.writing.domain.CsvContent; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -20,9 +20,9 @@ void readContentWhenFileMeetsConditions() throws Exception { //when PathInfo fileData = new PathInfo(filePath); CsvContentReader contentReader = new CsvContentReader(fileData); - ContentStorage content = contentReader.readContent(); + CsvContent content = contentReader.readContent(); //then - assertNotEquals(0, content.getContentSize()); + assertNotEquals(0, content.size()); } diff --git a/src/test/java/com/justinefactory/reading/readers/PlainContentReaderTest.java b/src/test/java/com/justinefactory/reading/readers/PlainContentReaderTest.java index 8788b7d..d315ae7 100644 --- a/src/test/java/com/justinefactory/reading/readers/PlainContentReaderTest.java +++ b/src/test/java/com/justinefactory/reading/readers/PlainContentReaderTest.java @@ -2,7 +2,7 @@ import com.justinefactory.domain.PathInfo; import com.justinefactory.reading.exceptions.SourceFileIsEmptyException; -import com.justinefactory.writing.domain.ContentStorage; +import com.justinefactory.writing.domain.PlainContent; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -18,11 +18,11 @@ class PlainContentReaderTest { void readContentWhenFileContentMeetsConditions() throws Exception { //given PathInfo fileData = new PathInfo(getPathToResource("txt-file-content-integers.txt")); - ContentStorage expectedContent = new ContentStorage<>(Arrays.asList("40000000", "800000", "3245", "2143567")); + PlainContent expectedContent = new PlainContent(Arrays.asList("40000000", "800000", "3245", "2143567")); //when PlainContentReader contentReader = new PlainContentReader(fileData); - ContentStorage content = contentReader.readContent(); + PlainContent content = contentReader.readContent(); //then assertEquals(content, expectedContent); diff --git a/src/test/java/com/justinefactory/sending/domain/ContentAndStatsStorageTest.java b/src/test/java/com/justinefactory/sending/domain/ContentAndStatsStorageTest.java index fc93254..d4dc13b 100644 --- a/src/test/java/com/justinefactory/sending/domain/ContentAndStatsStorageTest.java +++ b/src/test/java/com/justinefactory/sending/domain/ContentAndStatsStorageTest.java @@ -1,7 +1,7 @@ package com.justinefactory.sending.domain; import com.justinefactory.stats.domain.Stats; -import com.justinefactory.writing.domain.ContentStorage; +import com.justinefactory.writing.domain.Content; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -12,11 +12,11 @@ class ContentAndStatsStorageTest { @Test void createContentAndStatsStorageWhenContentMeetsConditions() { // given - ContentStorage content = new ContentStorage<>(50); + Content content = new Content<>(50); Stats stats = new Stats<>(1, 1, 50); //when - ContentAndStatsStorage contentAndStatsStorage = new ContentAndStatsStorage<>(content, stats); + ContentAndStats contentAndStatsStorage = new ContentAndStats<>(content, stats); //then assertEquals(content, contentAndStatsStorage.getContent()); @@ -26,11 +26,11 @@ void createContentAndStatsStorageWhenContentMeetsConditions() { @Test void createContentAndStatsStorageWhenContentEmpty() { // given - ContentStorage content = new ContentStorage<>(); + Content content = new Content<>(); Stats stats = new Stats<>(1, 1, 50); //then - assertThrows(IllegalArgumentException.class, () -> new ContentAndStatsStorage<>(content, stats)); + assertThrows(IllegalArgumentException.class, () -> new ContentAndStats<>(content, stats)); } @Test @@ -39,15 +39,15 @@ void createContentAndStatsStorageWhenContentIsNull() { Stats stats = new Stats<>(1, 1, 50); //then - assertThrows(IllegalArgumentException.class, () -> new ContentAndStatsStorage<>(null, stats)); + assertThrows(IllegalArgumentException.class, () -> new ContentAndStats<>(null, stats)); } @Test void createContentAndStatsStorageWhenStatsAreNull() { // given - ContentStorage content = new ContentStorage<>(50); + Content content = new Content<>(50); //then - assertThrows(IllegalArgumentException.class, () -> new ContentAndStatsStorage<>(content, null)); + assertThrows(IllegalArgumentException.class, () -> new ContentAndStats<>(content, null)); } } \ No newline at end of file diff --git a/src/test/java/com/justinefactory/stats/calculators/IntegerContentStatsCalculatorTest.java b/src/test/java/com/justinefactory/stats/calculators/IntegerContentStatsCalculatorTest.java index 4b79340..3458a4c 100644 --- a/src/test/java/com/justinefactory/stats/calculators/IntegerContentStatsCalculatorTest.java +++ b/src/test/java/com/justinefactory/stats/calculators/IntegerContentStatsCalculatorTest.java @@ -2,7 +2,7 @@ import com.justinefactory.stats.domain.Stats; import com.justinefactory.stats.exceptions.StatsCalculatingException; -import com.justinefactory.writing.domain.ContentStorage; +import com.justinefactory.writing.domain.Content; import org.junit.jupiter.api.Test; import java.util.List; @@ -24,7 +24,7 @@ void calculateStatsWhenContentIsNull() { @Test void calculateStatsWhenContentEmpty() { //given - ContentStorage content = new ContentStorage<>(); + Content content = new Content<>(); //when IntegerContentStatsCalculator calculator = new IntegerContentStatsCalculator(); @@ -37,7 +37,7 @@ void calculateStatsWhenContentEmpty() { @Test void calculateStatsWhenContentHas1Element() throws StatsCalculatingException { //given - ContentStorage content = new ContentStorage<>(10); + Content content = new Content<>(10); Stats expectedStats = new Stats<>(1, 1, 10); IntegerContentStatsCalculator calculator = new IntegerContentStatsCalculator(); @@ -52,7 +52,7 @@ void calculateStatsWhenContentHas1Element() throws StatsCalculatingException { @Test void calculateStatsWhenContentHasMoreElementsAndAllUnique() throws StatsCalculatingException { //given - ContentStorage content = new ContentStorage<>(List.of(10, 50, 40, 20, 70, 130, 1)); + Content content = new Content<>(List.of(10, 50, 40, 20, 70, 130, 1)); Stats expectedStats = new Stats<>(7, 7, 130); IntegerContentStatsCalculator calculator = new IntegerContentStatsCalculator(); @@ -67,7 +67,7 @@ void calculateStatsWhenContentHasMoreElementsAndAllUnique() throws StatsCalculat @Test void calculateStatsWhenContentHasMoreNonUniqueElements() throws StatsCalculatingException { //given - ContentStorage content = new ContentStorage<>(List.of(10, 20, 14, 20, 20, 12, 1)); + Content content = new Content<>(List.of(10, 20, 14, 20, 20, 12, 1)); Stats expectedStats = new Stats<>(7, 5, 20); IntegerContentStatsCalculator calculator = new IntegerContentStatsCalculator(); diff --git a/src/test/java/com/justinefactory/stats/calculators/ThreeElementContentStatsCalculatorTest.java b/src/test/java/com/justinefactory/stats/calculators/ThreeElementContentStatsCalculatorTest.java index 6dbfefd..5e7a085 100644 --- a/src/test/java/com/justinefactory/stats/calculators/ThreeElementContentStatsCalculatorTest.java +++ b/src/test/java/com/justinefactory/stats/calculators/ThreeElementContentStatsCalculatorTest.java @@ -3,7 +3,7 @@ import com.justinefactory.domain.ThreeElemContent; import com.justinefactory.stats.domain.Stats; import com.justinefactory.stats.exceptions.StatsCalculatingException; -import com.justinefactory.writing.domain.ContentStorage; +import com.justinefactory.writing.domain.Content; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -23,7 +23,7 @@ void calculateStatsWhenCollectionIsNull() { @Test void calculateStatsWhenCollectionIsEmpty() { //given - ContentStorage content = new ContentStorage<>(); + Content content = new Content<>(); //when ThreeElementContentStatsCalculator calculator = new ThreeElementContentStatsCalculator(); @@ -36,7 +36,7 @@ void calculateStatsWhenCollectionIsEmpty() { void calculateStatsWhen1Element() throws StatsCalculatingException { //given ThreeElemContent maxThreeElemContent = new ThreeElemContent(1590147349818750700L, -840762737, "ChristopherRobin"); - ContentStorage content = new ContentStorage<>(); + Content content = new Content<>(); content.addContent(maxThreeElemContent); Stats expectedStats = new Stats<>(1, 1, maxThreeElemContent); @@ -52,7 +52,7 @@ void calculateStatsWhen1Element() throws StatsCalculatingException { void calculateStatsWhen2DistinctElements() throws StatsCalculatingException { //given ThreeElemContent maxThreeElemContent = new ThreeElemContent(1590147349818750700L, 1345882450, "Owl"); - ContentStorage content = new ContentStorage<>(); + Content content = new Content<>(); content.addContent(new ThreeElemContent(1590147349818750700L, -840762737, "ChristopherRobin")); content.addContent(maxThreeElemContent); Stats expectedStats = new Stats<>(2, 2, maxThreeElemContent); @@ -69,7 +69,7 @@ void calculateStatsWhen2DistinctElements() throws StatsCalculatingException { void calculateStatsWhen3ElementsBut2Distinct() throws StatsCalculatingException { //given ThreeElemContent maxThreeElemContent = new ThreeElemContent(1590147349818750700L, 1345882450, "Owl"); - ContentStorage content = new ContentStorage<>(); + Content content = new Content<>(); content.addContent(new ThreeElemContent(1590147349818750700L, -840762737, "ChristopherRobin")); content.addContent(maxThreeElemContent); content.addContent(maxThreeElemContent); @@ -87,7 +87,7 @@ void calculateStatsWhen3ElementsBut2Distinct() throws StatsCalculatingException void calculateStatsWhen2ElementsBut1Distinct_MakingSureTimeStampIsExcludedFromDefiningDistinct() throws StatsCalculatingException { //given ThreeElemContent maxThreeElemContent = new ThreeElemContent(1590147349818750700L, 1434010513, "Owl"); - ContentStorage content = new ContentStorage<>(); + Content content = new Content<>(); content.addContent(maxThreeElemContent); content.addContent(new ThreeElemContent(1590147349818750888L, 1434010513, "Owl")); @@ -105,7 +105,7 @@ void calculateStatsWhen2ElementsBut1Distinct_MakingSureTimeStampIsExcludedFromDe void calculateStatsWhen2DistinctElements_MakingSureACombinationOfStringAndRandomIntIsTakenToDefineDistinct() throws StatsCalculatingException { //given ThreeElemContent maxThreeElementContent = new ThreeElemContent(1590147349818750800L, 1434010513, "ChristopherRobin"); - ContentStorage content = new ContentStorage<>(); + Content content = new Content<>(); content.addContent(new ThreeElemContent(1590147349818750700L, -840762737, "ChristopherRobin")); content.addContent(maxThreeElementContent); diff --git a/src/test/java/com/justinefactory/writing/converters/ContentAndStatsToJsonConverterTest.java b/src/test/java/com/justinefactory/writing/converters/ContentAndStatsToJsonConverterTest.java index 9453646..5afda7f 100644 --- a/src/test/java/com/justinefactory/writing/converters/ContentAndStatsToJsonConverterTest.java +++ b/src/test/java/com/justinefactory/writing/converters/ContentAndStatsToJsonConverterTest.java @@ -1,9 +1,10 @@ package com.justinefactory.writing.converters; import com.justinefactory.domain.ThreeElemContent; -import com.justinefactory.sending.domain.ContentAndStatsStorage; +import com.justinefactory.sending.domain.ContentAndStats; import com.justinefactory.stats.domain.Stats; -import com.justinefactory.writing.domain.ContentStorage; +import com.justinefactory.writing.domain.Content; +import com.justinefactory.writing.domain.JsonContent; import com.justinefactory.writing.exceptions.ContentConversion2ReadyToWriteException; import com.justinefactory.writing.exceptions.ContentWritingException; import org.junit.jupiter.api.Test; @@ -48,20 +49,20 @@ void createContentWhenThreeElemContentConvertedAndMeetsConditions() throws Conte " }\n" + " }\n" + "}"; - ContentStorage threeElemContentContentStorage = new ContentStorage<>(); + Content threeElemContentContentStorage = new Content<>(); ThreeElemContent threeElemContentContent = new ThreeElemContent(1590147349818750700L, -840762737, "ChristopherRobin"); threeElemContentContentStorage.addContent(threeElemContentContent); Stats stats = new Stats<>(1, 1, threeElemContentContent); - ContentAndStatsStorage content = new ContentAndStatsStorage<>(threeElemContentContentStorage, stats); + ContentAndStats content = new ContentAndStats<>(threeElemContentContentStorage, stats); ContentAndStatsToJsonConverter converter = new ContentAndStatsToJsonConverter<>(); //when - ContentStorage json = converter.convertContent(content); + JsonContent jsonContent = converter.convertContent(content); //then - assertEquals(expectedContent, json.getContent(0)); + assertEquals(expectedContent, jsonContent.getContent()); } diff --git a/src/test/java/com/justinefactory/writing/converters/IntegersToLinesConverterTest.java b/src/test/java/com/justinefactory/writing/converters/IntegersToLinesConverterTest.java index a651b1d..f876a50 100644 --- a/src/test/java/com/justinefactory/writing/converters/IntegersToLinesConverterTest.java +++ b/src/test/java/com/justinefactory/writing/converters/IntegersToLinesConverterTest.java @@ -1,6 +1,7 @@ package com.justinefactory.writing.converters; -import com.justinefactory.writing.domain.ContentStorage; +import com.justinefactory.writing.domain.Content; +import com.justinefactory.writing.domain.PlainContent; import com.justinefactory.writing.exceptions.ContentConversion2ReadyToWriteException; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; @@ -30,7 +31,7 @@ void convertDataWhenNull() { void convertDataWhenIsEmpty() { //given IntegersToLinesConverter converter = new IntegersToLinesConverter(); - ContentStorage input = new ContentStorage<>(); + Content input = new Content<>(); //then assertThrows(ContentConversion2ReadyToWriteException.class, () -> converter.convertContent(input)); @@ -39,12 +40,12 @@ void convertDataWhenIsEmpty() { @ParameterizedTest @MethodSource("conversionData") - void convertDataWhenContentMeetsConditions(ContentStorage input, ContentStorage expectedContent) throws ContentConversion2ReadyToWriteException { + void convertDataWhenContentMeetsConditions(Content input, PlainContent expectedContent) throws ContentConversion2ReadyToWriteException { //given IntegersToLinesConverter converter = new IntegersToLinesConverter(); //when - ContentStorage actualContent = converter.convertContent(input); + PlainContent actualContent = converter.convertContent(input); //then assertEquals(actualContent, expectedContent); @@ -52,9 +53,9 @@ void convertDataWhenContentMeetsConditions(ContentStorage input, Conten static Stream conversionData() { return Stream.of( - Arguments.arguments(new ContentStorage<>(Collections.singletonList(1)), new ContentStorage<>(Collections.singletonList("1"))), - Arguments.arguments(new ContentStorage<>(Arrays.asList(1, 2)), new ContentStorage<>(Arrays.asList("1", "2"))), - Arguments.arguments(new ContentStorage<>(Arrays.asList(1, 2, 3, 4)), new ContentStorage<>(Arrays.asList("1", "2", "3", "4"))) + Arguments.arguments(new Content<>(Collections.singletonList(1)), new PlainContent(Collections.singletonList("1"))), + Arguments.arguments(new Content<>(Arrays.asList(1, 2)), new PlainContent(Arrays.asList("1", "2"))), + Arguments.arguments(new Content<>(Arrays.asList(1, 2, 3, 4)), new PlainContent(Arrays.asList("1", "2", "3", "4"))) ); } diff --git a/src/test/java/com/justinefactory/writing/converters/ThreeElementContentToCsvLinesConverterTest.java b/src/test/java/com/justinefactory/writing/converters/ThreeElementContentToCsvLinesConverterTest.java index 496b74c..8edd962 100644 --- a/src/test/java/com/justinefactory/writing/converters/ThreeElementContentToCsvLinesConverterTest.java +++ b/src/test/java/com/justinefactory/writing/converters/ThreeElementContentToCsvLinesConverterTest.java @@ -1,7 +1,8 @@ package com.justinefactory.writing.converters; import com.justinefactory.domain.ThreeElemContent; -import com.justinefactory.writing.domain.ContentStorage; +import com.justinefactory.writing.domain.Content; +import com.justinefactory.writing.domain.CsvContent; import com.justinefactory.writing.exceptions.ContentConversion2ReadyToWriteException; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; @@ -19,7 +20,7 @@ class ThreeElementContentToCsvLinesConverterTest { void convertDataWhenContentIsEmpty() { //given ThreeElementContentToCsvLinesConverter converter = new ThreeElementContentToCsvLinesConverter(); - ContentStorage input = new ContentStorage<>(); + Content input = new Content<>(); //then assertThrows(ContentConversion2ReadyToWriteException.class, () -> converter.convertContent(input)); @@ -28,15 +29,15 @@ void convertDataWhenContentIsEmpty() { @ParameterizedTest @MethodSource("conversionData") - void convertDataWhenContentMeetsConditions(ContentStorage input, ContentStorage expectedContent) throws ContentConversion2ReadyToWriteException { + void convertDataWhenContentMeetsConditions(Content input, CsvContent expectedContent) throws ContentConversion2ReadyToWriteException { //given ThreeElementContentToCsvLinesConverter converter = new ThreeElementContentToCsvLinesConverter(); //when - ContentStorage actualContent = converter.convertContent(input); + CsvContent actualContent = converter.convertContent(input); //then - for (int i = 0; i < expectedContent.getContentSize(); i++) { + for (int i = 0; i < expectedContent.getContent().size(); i++) { assertEquals(expectedContent.getContent(i)[0], actualContent.getContent(i)[0]); assertEquals(expectedContent.getContent(i)[1], actualContent.getContent(i)[1]); assertEquals(expectedContent.getContent(i)[2], actualContent.getContent(i)[2]); @@ -47,14 +48,14 @@ static Stream conversionData() { ThreeElemContent firstThreeElemContent = new ThreeElemContent(1590147349818750700L, 1345882450, "Owl"); ThreeElemContent secondThreeElemContent = new ThreeElemContent(1590147349818759790L, -45882470, "Fluff"); - ContentStorage firstInput = new ContentStorage<>(); + Content firstInput = new Content<>(); firstInput.addContent(firstThreeElemContent); - ContentStorage secondInput = firstInput; + Content secondInput = firstInput; secondInput.addContent(secondThreeElemContent); - ContentStorage firstString = new ContentStorage<>(); + CsvContent firstString = new CsvContent(); firstString.addContent(new String[]{firstThreeElemContent.getTimeStamp().toString(), firstThreeElemContent.getRandomInt().toString(), firstThreeElemContent.getRandomString()}); - ContentStorage secondString = firstString; + CsvContent secondString = firstString; secondString.addContent(new String[]{secondThreeElemContent.getTimeStamp().toString(), secondThreeElemContent.getRandomInt().toString(), secondThreeElemContent.getRandomString()}); diff --git a/src/test/java/com/justinefactory/writing/domain/ContentStorageTest.java b/src/test/java/com/justinefactory/writing/domain/ContentStorageTest.java index 0c040b7..12fa84c 100644 --- a/src/test/java/com/justinefactory/writing/domain/ContentStorageTest.java +++ b/src/test/java/com/justinefactory/writing/domain/ContentStorageTest.java @@ -16,7 +16,7 @@ void createContentStorageWhenInstertingContentAsNull() { String string = null; //then - assertThrows(IllegalArgumentException.class, () -> new ContentStorage<>(string)); + assertThrows(IllegalArgumentException.class, () -> new Content<>(string)); } @Test @@ -25,7 +25,7 @@ void createContentStorageWhenInstertingContentThatMeetsConditions() { String string = "Tiger"; //when - ContentStorage contentStorage = new ContentStorage<>(string); + Content contentStorage = new Content<>(string); //then assertEquals(string, contentStorage.getContent(0)); @@ -39,7 +39,7 @@ void createContentStorageWhenInstertingListOfContentWithNullItem() { list.add(null); //then - assertThrows(IllegalArgumentException.class, () -> new ContentStorage<>(list)); + assertThrows(IllegalArgumentException.class, () -> new Content<>(list)); } @Test @@ -48,7 +48,7 @@ void createContentStorageWhenInstertingListOfContentNull() { List list = null; //then - assertThrows(IllegalArgumentException.class, () -> new ContentStorage<>(list)); + assertThrows(IllegalArgumentException.class, () -> new Content<>(list)); } @Test @@ -57,17 +57,17 @@ void createContentStorageWhenInstertingListOfContentThatMeetsConditions() { List list = new ArrayList<>(List.of(1, 15, 32)); //when - ContentStorage contentStorage = new ContentStorage<>(list); + Content contentStorage = new Content<>(list); //then - assertEquals(list, contentStorage.getAllContent()); + assertEquals(list, contentStorage.getContent()); } @Test void createContentStorageWhenAddingNull() { //given - ContentStorage content = new ContentStorage<>(); + Content content = new Content<>(); //then assertThrows(IllegalArgumentException.class, () -> content.addContent(null)); @@ -77,7 +77,7 @@ void createContentStorageWhenAddingNull() { @Test void createContentStorageWhenAddingContentThatMeetsConditions() { //given - ContentStorage content = new ContentStorage<>(); + Content content = new Content<>(); String string = "Tiger"; //when diff --git a/src/test/java/com/justinefactory/writing/domain/JsonContentTest.java b/src/test/java/com/justinefactory/writing/domain/JsonContentTest.java new file mode 100644 index 0000000..9c119af --- /dev/null +++ b/src/test/java/com/justinefactory/writing/domain/JsonContentTest.java @@ -0,0 +1,48 @@ +package com.justinefactory.writing.domain; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class JsonContentTest { + + @Test + public void createJsonContentWhenJsonIsNull() { + //given + String json = null; + + //then + assertThrows(IllegalArgumentException.class, () -> new JsonContent(json)); + } + + + @Test + public void createJsonContentWhenJsonIsEmpty() { + //given + String json = ""; + + //then + assertThrows(IllegalArgumentException.class, () -> new JsonContent(json)); + } + + + @Test + public void createJsonContentWhenJsonMeetsConditions() { + //given + String json = "{\"content\": [\n" + + " {\n" + + " \"timeStamp\": 1590147349818750700,\n" + + " \"randomInt\": -840762737,\n" + + " \"randomString\": \"ChristopherRobin\"\n" + + " }\n" + + " ]\n"; + + //when + JsonContent jsonContent = new JsonContent(json); + + //then + assertEquals(json, jsonContent.getContent()); + } + +} \ No newline at end of file diff --git a/src/test/java/com/justinefactory/writing/generators/RandomIntegerGeneratorTest.java b/src/test/java/com/justinefactory/writing/generators/RandomIntegerGeneratorTest.java index b3ae51c..4b570be 100644 --- a/src/test/java/com/justinefactory/writing/generators/RandomIntegerGeneratorTest.java +++ b/src/test/java/com/justinefactory/writing/generators/RandomIntegerGeneratorTest.java @@ -1,6 +1,6 @@ package com.justinefactory.writing.generators; -import com.justinefactory.writing.domain.ContentStorage; +import com.justinefactory.writing.domain.Content; import org.junit.jupiter.api.Test; import java.util.Random; @@ -17,7 +17,7 @@ void generateRandomContent() { //when int nLines = 5; - ContentStorage newContent = newGenerator.generateContent(nLines); + Content newContent = newGenerator.generateContent(nLines); //then assertEquals(newContent.getContentSize(), nLines); @@ -31,7 +31,7 @@ void generateRandomContentWhen0() { //when int nLines = 0; - ContentStorage content = randomGenerator.generateContent(nLines); + Content content = randomGenerator.generateContent(nLines); //then assertEquals(content.getContentSize(), 0); diff --git a/src/test/java/com/justinefactory/writing/generators/RandomStringGeneratorFromFileTest.java b/src/test/java/com/justinefactory/writing/generators/RandomStringGeneratorFromFileTest.java index df24902..a1b3a8a 100644 --- a/src/test/java/com/justinefactory/writing/generators/RandomStringGeneratorFromFileTest.java +++ b/src/test/java/com/justinefactory/writing/generators/RandomStringGeneratorFromFileTest.java @@ -1,7 +1,7 @@ package com.justinefactory.writing.generators; import com.justinefactory.domain.PathInfo; -import com.justinefactory.writing.domain.ContentStorage; +import com.justinefactory.writing.domain.Content; import com.justinefactory.writing.domain.TwoElemContent; import com.justinefactory.writing.exceptions.FileWithStringsToGenerateContentIsEmptyException; import org.junit.jupiter.api.Assertions; @@ -40,7 +40,7 @@ void generateStringContentWhen1String() throws Exception { //when int nLines = 5; - ContentStorage newContent = newGenerator.generateContent(nLines); + Content newContent = newGenerator.generateContent(nLines); //then assertEquals(newContent.getContentSize(), nLines); @@ -61,7 +61,7 @@ void generateTimeStampContent() throws Exception { //when Long timeStart = getCurrentTimeInNanoSeconds(); int nLines = 3; - ContentStorage newContent = newGenerator.generateContent(nLines); + Content newContent = newGenerator.generateContent(nLines); Long timeStop = getCurrentTimeInNanoSeconds(); //then diff --git a/src/test/java/com/justinefactory/writing/generators/ThreeElementContentGeneratorTest.java b/src/test/java/com/justinefactory/writing/generators/ThreeElementContentGeneratorTest.java index d85bb26..fd2ee04 100644 --- a/src/test/java/com/justinefactory/writing/generators/ThreeElementContentGeneratorTest.java +++ b/src/test/java/com/justinefactory/writing/generators/ThreeElementContentGeneratorTest.java @@ -2,7 +2,7 @@ import com.justinefactory.domain.PathInfo; import com.justinefactory.domain.ThreeElemContent; -import com.justinefactory.writing.domain.ContentStorage; +import com.justinefactory.writing.domain.Content; import org.junit.jupiter.api.Test; import java.nio.file.Path; @@ -25,7 +25,7 @@ void generateContentWhenRandom() throws Exception { //when int nLines = 12; - ContentStorage new3ElemContent = new3ElemGenerator.generateContent(nLines); + Content new3ElemContent = new3ElemGenerator.generateContent(nLines); //then assertEquals(new3ElemContent.getContentSize(), nLines); diff --git a/src/test/java/com/justinefactory/writing/writers/PlainFileWriterTest.java b/src/test/java/com/justinefactory/writing/writers/PlainFileWriterTest.java deleted file mode 100644 index 6d65ede..0000000 --- a/src/test/java/com/justinefactory/writing/writers/PlainFileWriterTest.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.justinefactory.writing.writers; - -import com.justinefactory.domain.PathInfo; -import com.justinefactory.testutil.CreateAndDeleteFilesBeforeAfterAll; -import com.justinefactory.writing.domain.ContentStorage; -import com.justinefactory.writing.writers.file.writers.PlainFileWriter; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; - -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.List; - -import static org.junit.jupiter.api.Assertions.assertTrue; - - -class PlainFileWriterTest { - - static Path dir; - @BeforeAll - static void createDirsBeforeAll() throws Exception { - dir = CreateAndDeleteFilesBeforeAfterAll.createTemporaryDirectory(); - } - - @AfterAll - static void removeDirsAfterAll() throws Exception { - CreateAndDeleteFilesBeforeAfterAll.removeAllDirs(dir); - } - - @Test - void write2FileWhenWritingRandomInteger() throws Exception { - //given - Path filePath = dir.resolve("doc.csv"); - PathInfo file2writeData = new PathInfo(filePath); - ContentStorage readyToWriteContent = new ContentStorage<>(List.of("1","2")); - - //when - PlainFileWriter writer = new PlainFileWriter(); - writer.writeContent(readyToWriteContent, file2writeData); - - //then - assertTrue(Files.exists(filePath)); - assertTrue(Files.size(filePath) > 0); - } - - - @Test - void write2FileWhenWritingJSON() throws Exception { - //given - Path filePath = dir.resolve("doc.json"); - PathInfo file2writeData = new PathInfo(filePath); - ContentStorage readyToWriteContent = new ContentStorage<>(); - readyToWriteContent.addContent("\"{\\n\" +\n" + - " \" \\\"content\\\": {\\n\" +\n" + - " \" \\\"content\\\": [\\n\" +\n" + - " \" {\\n\" +\n" + - " \" \\\"timeStamp\\\": 1590147349818750700,\\n\" +\n" + - " \" \\\"randomInt\\\": -840762737,\\n\" +\n" + - " \" \\\"randomString\\\": \\\"ChristopherRobin\\\"\\n\" +\n" + - " \" }\\n\" +\n" + - " \" ]\\n\" +\n" + - " \" },\\n\" +\n" + - " \" \\\"stats\\\": {\\n\" +\n" + - " \" \\\"count\\\": 1,\\n\" +\n" + - " \" \\\"distinctCount\\\": 1,\\n\" +\n" + - " \" \\\"max\\\": {\\n\" +\n" + - " \" \\\"timeStamp\\\": 1590147349818750700,\\n\" +\n" + - " \" \\\"randomInt\\\": -840762737,\\n\" +\n" + - " \" \\\"randomString\\\": \\\"ChristopherRobin\\\"\\n\" +\n" + - " \" }\\n\" +\n" + - " \" }\\n\" +\n" + - " \"}\";"); - - //when - PlainFileWriter writer = new PlainFileWriter(); - writer.writeContent(readyToWriteContent, file2writeData); - - //then - assertTrue(Files.exists(filePath)); - assertTrue(Files.size(filePath) > 0); - } -} \ No newline at end of file diff --git a/src/test/java/com/justinefactory/writing/writers/server/writers/JsonAwsWriterTest.java b/src/test/java/com/justinefactory/writing/writers/tocloud/JsonContentAwsWriterTest.java similarity index 89% rename from src/test/java/com/justinefactory/writing/writers/server/writers/JsonAwsWriterTest.java rename to src/test/java/com/justinefactory/writing/writers/tocloud/JsonContentAwsWriterTest.java index 0b16b13..1e335be 100644 --- a/src/test/java/com/justinefactory/writing/writers/server/writers/JsonAwsWriterTest.java +++ b/src/test/java/com/justinefactory/writing/writers/tocloud/JsonContentAwsWriterTest.java @@ -1,10 +1,10 @@ -package com.justinefactory.writing.writers.server.writers; +package com.justinefactory.writing.writers.tocloud; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.model.DeleteObjectRequest; import com.justinefactory.domain.AwsInfo; import com.justinefactory.sending.exceptions.AwsSecurityCredentialsException; -import com.justinefactory.writing.domain.ContentStorage; +import com.justinefactory.writing.domain.JsonContent; import com.justinefactory.writing.exceptions.AwsContentWritingException; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeEach; @@ -13,7 +13,7 @@ import static com.justinefactory.testutil.AwsClientCreatorBeforeEach.createAwsClient; import static org.junit.jupiter.api.Assertions.assertTrue; -class JsonAwsWriterTest { +class JsonContentAwsWriterTest { private static AmazonS3 awsClient; private static String jsonName = "content.json"; @@ -29,12 +29,12 @@ public static void removeObjectFromAws() { awsClient.deleteObject(new DeleteObjectRequest(bucketName, jsonName)); } + @Test void writeContentWhenContentDoesNotExist() throws AwsContentWritingException { //given AwsInfo info = new AwsInfo(bucketName, jsonName); - ContentStorage readyToWriteContent = new ContentStorage<>(); - readyToWriteContent.addContent("{\n" + + JsonContent readyToWriteContent = new JsonContent("{\n" + " \"content\": {\n" + " \"content\": [\n" + " {\n" + diff --git a/src/test/java/com/justinefactory/writing/writers/CsvFileWriterTest.java b/src/test/java/com/justinefactory/writing/writers/tofile/CsvFileWriterTest.java similarity index 78% rename from src/test/java/com/justinefactory/writing/writers/CsvFileWriterTest.java rename to src/test/java/com/justinefactory/writing/writers/tofile/CsvFileWriterTest.java index 173e561..57755e8 100644 --- a/src/test/java/com/justinefactory/writing/writers/CsvFileWriterTest.java +++ b/src/test/java/com/justinefactory/writing/writers/tofile/CsvFileWriterTest.java @@ -1,9 +1,8 @@ -package com.justinefactory.writing.writers; +package com.justinefactory.writing.writers.tofile; import com.justinefactory.domain.PathInfo; import com.justinefactory.testutil.CreateAndDeleteFilesBeforeAfterAll; -import com.justinefactory.writing.domain.ContentStorage; -import com.justinefactory.writing.writers.file.writers.CsvFileWriter; +import com.justinefactory.writing.domain.CsvContent; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -33,7 +32,7 @@ void write2FileWhenFileDoesNotExist() throws Exception { //given Path filePath = dir.resolve("doc.csv"); PathInfo file2writeData = new PathInfo(filePath); - ContentStorage readyToWriteContent = new ContentStorage<>(new String[]{"1590147349818750700", "1345882450", "Owl"}); + CsvContent readyToWriteContent = new CsvContent(new String[]{"1590147349818750700", "1345882450", "Owl"}); CsvFileWriter newFileWriter = new CsvFileWriter(); //when diff --git a/src/test/java/com/justinefactory/writing/writers/tofile/JsonFileWriterTest.java b/src/test/java/com/justinefactory/writing/writers/tofile/JsonFileWriterTest.java new file mode 100644 index 0000000..2af92a7 --- /dev/null +++ b/src/test/java/com/justinefactory/writing/writers/tofile/JsonFileWriterTest.java @@ -0,0 +1,64 @@ +package com.justinefactory.writing.writers.tofile; + +import com.justinefactory.domain.PathInfo; +import com.justinefactory.testutil.CreateAndDeleteFilesBeforeAfterAll; +import com.justinefactory.writing.domain.JsonContent; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.nio.file.Files; +import java.nio.file.Path; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +class JsonFileWriterTest { + + static Path dir; + + @BeforeAll + static void createDirsBeforeAll() throws Exception { + dir = CreateAndDeleteFilesBeforeAfterAll.createTemporaryDirectory(); + } + + @AfterAll + static void removeDirsAfterAll() throws Exception { + CreateAndDeleteFilesBeforeAfterAll.removeAllDirs(dir); + } + + @Test + void write2FileWhenWritingJSON() throws Exception { + //given + Path filePath = dir.resolve("doc.json"); + PathInfo file2writeData = new PathInfo(filePath); + JsonContent readyToWriteContent = new JsonContent("{\n" + + " \"content\": {\n" + + " \"content\": [\n" + + " {\n" + + " \"timeStamp\": 1590147349818750700,\n" + + " \"randomInt\": -840762737,\n" + + " \"randomString\": \"ChristopherRobin\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"stats\": {\n" + + " \"count\": 1,\n" + + " \"distinctCount\": 1,\n" + + " \"max\": {\n" + + " \"timeStamp\": 1590147349818750700,\n" + + " \"randomInt\": -840762737,\n" + + " \"randomString\": \"ChristopherRobin\"\n" + + " }\n" + + " }\n" + + "}"); + + //when + JsonFileWriter writer = new JsonFileWriter(); + writer.writeContent(readyToWriteContent, file2writeData); + + //then + assertTrue(Files.exists(filePath)); + assertTrue(Files.size(filePath) > 0); + } + +} \ No newline at end of file diff --git a/src/test/java/com/justinefactory/writing/writers/tofile/PlainFileWriterTest.java b/src/test/java/com/justinefactory/writing/writers/tofile/PlainFileWriterTest.java new file mode 100644 index 0000000..316a0c2 --- /dev/null +++ b/src/test/java/com/justinefactory/writing/writers/tofile/PlainFileWriterTest.java @@ -0,0 +1,48 @@ +package com.justinefactory.writing.writers.tofile; + +import com.justinefactory.domain.PathInfo; +import com.justinefactory.testutil.CreateAndDeleteFilesBeforeAfterAll; +import com.justinefactory.writing.domain.PlainContent; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertTrue; + + +class PlainFileWriterTest { + + static Path dir; + + @BeforeAll + static void createDirsBeforeAll() throws Exception { + dir = CreateAndDeleteFilesBeforeAfterAll.createTemporaryDirectory(); + } + + @AfterAll + static void removeDirsAfterAll() throws Exception { + CreateAndDeleteFilesBeforeAfterAll.removeAllDirs(dir); + } + + @Test + void write2FileWhenWritingRandomInteger() throws Exception { + //given + Path filePath = dir.resolve("doc.csv"); + PathInfo file2writeData = new PathInfo(filePath); + PlainContent readyToWriteContent = new PlainContent(List.of("1", "2")); + + //when + PlainFileWriter writer = new PlainFileWriter(); + writer.writeContent(readyToWriteContent, file2writeData); + + //then + assertTrue(Files.exists(filePath)); + assertTrue(Files.size(filePath) > 0); + } + + +} \ No newline at end of file