diff --git a/libraries-data-2/README.md b/libraries-data-2/README.md new file mode 100644 index 0000000000..8101138c0e --- /dev/null +++ b/libraries-data-2/README.md @@ -0,0 +1,11 @@ +### Relevant articles +- [Introduction to Apache Flink with Java](http://www.baeldung.com/apache-flink) +- [Guide to the HyperLogLog Algorithm](http://www.baeldung.com/java-hyperloglog) +- [Introduction to Conflict-Free Replicated Data Types](http://www.baeldung.com/java-conflict-free-replicated-data-types) +- [Introduction to javax.measure](http://www.baeldung.com/javax-measure) +- [Introduction To Docx4J](http://www.baeldung.com/docx4j) +- [Interact with Google Sheets from Java](http://www.baeldung.com/google-sheets-java-client) +- [Introduction To OpenCSV](http://www.baeldung.com/opencsv) +- [Introduction to Smooks](http://www.baeldung.com/smooks) +- [A Guide to Infinispan in Java](http://www.baeldung.com/infinispan) + diff --git a/libraries-data-2/log4j.properties b/libraries-data-2/log4j.properties new file mode 100644 index 0000000000..2173c5d96f --- /dev/null +++ b/libraries-data-2/log4j.properties @@ -0,0 +1 @@ +log4j.rootLogger=INFO, stdout diff --git a/libraries-data-2/pom.xml b/libraries-data-2/pom.xml new file mode 100644 index 0000000000..97820859ba --- /dev/null +++ b/libraries-data-2/pom.xml @@ -0,0 +1,128 @@ + + + + 4.0.0 + libraries-http + libraries-http + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + + org.apache.flink + flink-connector-kafka-0.11_2.11 + ${flink.version} + + + org.apache.flink + flink-streaming-java_2.11 + ${flink.version} + + + org.apache.flink + flink-core + ${flink.version} + + + commons-logging + commons-logging + + + + + org.apache.flink + flink-java + ${flink.version} + + + commons-logging + commons-logging + + + + + org.apache.flink + flink-test-utils_2.11 + ${flink.version} + test + + + net.agkn + hll + ${hll.version} + + + com.netopyr.wurmloch + wurmloch-crdt + ${crdt.version} + + + tec.units + unit-ri + ${unit-ri.version} + + + javax.xml.bind + jaxb-api + ${jaxb-api.version} + + + org.docx4j + docx4j + ${docx4j.version} + + + + com.google.api-client + google-api-client + ${google-api.version} + + + com.google.oauth-client + google-oauth-client-jetty + ${google-api.version} + + + com.google.apis + google-api-services-sheets + ${google-sheets.version} + + + + com.opencsv + opencsv + ${opencsv.version} + + + org.milyn + milyn-smooks-all + ${smooks.version} + + + org.infinispan + infinispan-core + ${infinispan.version} + + + + + + 1.5.0 + 1.6.0 + 0.1.0 + 1.0.3 + 3.3.5 + 2.1 + 1.23.0 + 4.1 + 1.7.0 + 9.1.5.Final + v4-rev493-1.21.0 + + \ No newline at end of file diff --git a/libraries-data-2/src/main/com/baeldung/docx/Docx4jExample.java b/libraries-data-2/src/main/com/baeldung/docx/Docx4jExample.java new file mode 100644 index 0000000000..6fe29d26e3 --- /dev/null +++ b/libraries-data-2/src/main/com/baeldung/docx/Docx4jExample.java @@ -0,0 +1,105 @@ +package com.baeldung.docx; + +import org.docx4j.dml.wordprocessingDrawing.Inline; +import org.docx4j.jaxb.Context; +import org.docx4j.model.table.TblFactory; +import org.docx4j.openpackaging.exceptions.Docx4JException; +import org.docx4j.openpackaging.packages.WordprocessingMLPackage; +import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; +import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; +import org.docx4j.wml.BooleanDefaultTrue; +import org.docx4j.wml.Color; +import org.docx4j.wml.Drawing; +import org.docx4j.wml.ObjectFactory; +import org.docx4j.wml.P; +import org.docx4j.wml.R; +import org.docx4j.wml.RPr; +import org.docx4j.wml.Tbl; +import org.docx4j.wml.Tc; +import org.docx4j.wml.Text; +import org.docx4j.wml.Tr; + +import javax.xml.bind.JAXBElement; +import javax.xml.bind.JAXBException; +import java.io.File; +import java.nio.file.Files; +import java.util.List; + +class Docx4jExample { + + void createDocumentPackage(String outputPath, String imagePath) throws Exception { + WordprocessingMLPackage wordPackage = WordprocessingMLPackage.createPackage(); + MainDocumentPart mainDocumentPart = wordPackage.getMainDocumentPart(); + mainDocumentPart.addStyledParagraphOfText("Title", "Hello World!"); + mainDocumentPart.addParagraphOfText("Welcome To Baeldung!"); + + ObjectFactory factory = Context.getWmlObjectFactory(); + P p = factory.createP(); + R r = factory.createR(); + Text t = factory.createText(); + t.setValue("Welcome To Baeldung"); + r.getContent().add(t); + p.getContent().add(r); + RPr rpr = factory.createRPr(); + BooleanDefaultTrue b = new BooleanDefaultTrue(); + rpr.setB(b); + rpr.setI(b); + rpr.setCaps(b); + Color red = factory.createColor(); + red.setVal("green"); + rpr.setColor(red); + r.setRPr(rpr); + mainDocumentPart.getContent().add(p); + + File image = new File(imagePath); + byte[] fileContent = Files.readAllBytes(image.toPath()); + BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordPackage, fileContent); + Inline inline = imagePart.createImageInline("Baeldung Image", "Alt Text", 1, 2, false); + P Imageparagraph = addImageToParagraph(inline); + mainDocumentPart.getContent().add(Imageparagraph); + + int writableWidthTwips = wordPackage.getDocumentModel().getSections().get(0).getPageDimensions().getWritableWidthTwips(); + int columnNumber = 3; + Tbl tbl = TblFactory.createTable(3, 3, writableWidthTwips / columnNumber); + List rows = tbl.getContent(); + for (Object row : rows) { + Tr tr = (Tr) row; + List cells = tr.getContent(); + for (Object cell : cells) { + Tc td = (Tc) cell; + td.getContent().add(p); + } + } + + mainDocumentPart.getContent().add(tbl); + File exportFile = new File(outputPath); + wordPackage.save(exportFile); + } + + boolean isTextExist(String testText) throws Docx4JException, JAXBException { + File doc = new File("helloWorld.docx"); + WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(doc); + MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart(); + String textNodesXPath = "//w:t"; + List paragraphs = mainDocumentPart.getJAXBNodesViaXPath(textNodesXPath, true); + for (Object obj : paragraphs) { + Text text = (Text) ((JAXBElement) obj).getValue(); + String textValue = text.getValue(); + if (textValue != null && textValue.contains(testText)) { + return true; + } + } + return false; + } + + private static P addImageToParagraph(Inline inline) { + ObjectFactory factory = new ObjectFactory(); + P p = factory.createP(); + R r = factory.createR(); + p.getContent().add(r); + Drawing drawing = factory.createDrawing(); + r.getContent().add(drawing); + drawing.getAnchorOrInline().add(inline); + return p; + } +} diff --git a/libraries-data-2/src/main/com/baeldung/flink/FlinkDataPipeline.java b/libraries-data-2/src/main/com/baeldung/flink/FlinkDataPipeline.java new file mode 100644 index 0000000000..d02b1bcb83 --- /dev/null +++ b/libraries-data-2/src/main/com/baeldung/flink/FlinkDataPipeline.java @@ -0,0 +1,82 @@ +package com.baeldung.flink; + + +import com.baeldung.flink.model.Backup; +import com.baeldung.flink.model.InputMessage; +import com.baeldung.flink.operator.BackupAggregator; +import com.baeldung.flink.operator.InputMessageTimestampAssigner; +import com.baeldung.flink.operator.WordsCapitalizer; +import org.apache.flink.streaming.api.TimeCharacteristic; +import org.apache.flink.streaming.api.datastream.DataStream; +import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; +import org.apache.flink.streaming.api.windowing.time.Time; +import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer011; +import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer011; + +import static com.baeldung.flink.connector.Consumers.*; +import static com.baeldung.flink.connector.Producers.*; + +public class FlinkDataPipeline { + + public static void capitalize() throws Exception { + String inputTopic = "flink_input"; + String outputTopic = "flink_output"; + String consumerGroup = "baeldung"; + String address = "localhost:9092"; + + StreamExecutionEnvironment environment = + StreamExecutionEnvironment.getExecutionEnvironment(); + + FlinkKafkaConsumer011 flinkKafkaConsumer = + createStringConsumerForTopic(inputTopic, address, consumerGroup); + flinkKafkaConsumer.setStartFromEarliest(); + + DataStream stringInputStream = + environment.addSource(flinkKafkaConsumer); + + FlinkKafkaProducer011 flinkKafkaProducer = + createStringProducer(outputTopic, address); + + stringInputStream + .map(new WordsCapitalizer()) + .addSink(flinkKafkaProducer); + + environment.execute(); + } + +public static void createBackup () throws Exception { + String inputTopic = "flink_input"; + String outputTopic = "flink_output"; + String consumerGroup = "baeldung"; + String kafkaAddress = "localhost:9092"; + + StreamExecutionEnvironment environment = + StreamExecutionEnvironment.getExecutionEnvironment(); + + environment.setStreamTimeCharacteristic(TimeCharacteristic.EventTime); + + FlinkKafkaConsumer011 flinkKafkaConsumer = + createInputMessageConsumer(inputTopic, kafkaAddress, consumerGroup); + flinkKafkaConsumer.setStartFromEarliest(); + + flinkKafkaConsumer + .assignTimestampsAndWatermarks(new InputMessageTimestampAssigner()); + FlinkKafkaProducer011 flinkKafkaProducer = + createBackupProducer(outputTopic, kafkaAddress); + + DataStream inputMessagesStream = + environment.addSource(flinkKafkaConsumer); + + inputMessagesStream + .timeWindowAll(Time.hours(24)) + .aggregate(new BackupAggregator()) + .addSink(flinkKafkaProducer); + + environment.execute(); +} + + public static void main(String[] args) throws Exception { + createBackup(); + } + +} diff --git a/libraries-data-2/src/main/com/baeldung/flink/LineSplitter.java b/libraries-data-2/src/main/com/baeldung/flink/LineSplitter.java new file mode 100644 index 0000000000..f4e322f1e8 --- /dev/null +++ b/libraries-data-2/src/main/com/baeldung/flink/LineSplitter.java @@ -0,0 +1,18 @@ +package com.baeldung.flink; + +import org.apache.flink.api.common.functions.FlatMapFunction; +import org.apache.flink.api.java.tuple.Tuple2; +import org.apache.flink.util.Collector; + +import java.util.stream.Stream; + +@SuppressWarnings("serial") +public class LineSplitter implements FlatMapFunction> { + + @Override + public void flatMap(String value, Collector> out) { + + String[] tokens = value.toLowerCase().split("\\W+"); + Stream.of(tokens).filter(t -> t.length() > 0).forEach(token -> out.collect(new Tuple2<>(token, 1))); + } +} \ No newline at end of file diff --git a/libraries-data-2/src/main/com/baeldung/flink/WordCount.java b/libraries-data-2/src/main/com/baeldung/flink/WordCount.java new file mode 100644 index 0000000000..fc5064bafa --- /dev/null +++ b/libraries-data-2/src/main/com/baeldung/flink/WordCount.java @@ -0,0 +1,18 @@ +package com.baeldung.flink; + +import org.apache.flink.api.java.DataSet; +import org.apache.flink.api.java.ExecutionEnvironment; +import org.apache.flink.api.java.aggregation.Aggregations; +import org.apache.flink.api.java.tuple.Tuple2; + +import java.util.List; + +public class WordCount { + + public static DataSet> startWordCount(ExecutionEnvironment env, List lines) throws Exception { + DataSet text = env.fromCollection(lines); + + return text.flatMap(new LineSplitter()).groupBy(0).aggregate(Aggregations.SUM, 1); + + } +} \ No newline at end of file diff --git a/libraries-data-2/src/main/com/baeldung/flink/connector/Consumers.java b/libraries-data-2/src/main/com/baeldung/flink/connector/Consumers.java new file mode 100644 index 0000000000..514085f9c4 --- /dev/null +++ b/libraries-data-2/src/main/com/baeldung/flink/connector/Consumers.java @@ -0,0 +1,32 @@ +package com.baeldung.flink.connector; + +import com.baeldung.flink.model.InputMessage; +import com.baeldung.flink.schema.InputMessageDeserializationSchema; +import org.apache.flink.api.common.serialization.SimpleStringSchema; +import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer011; + +import java.util.Properties; + +public class Consumers { + +public static FlinkKafkaConsumer011 createStringConsumerForTopic( + String topic, String kafkaAddress, String kafkaGroup ) { + Properties props = new Properties(); + props.setProperty("bootstrap.servers", kafkaAddress); + props.setProperty("group.id",kafkaGroup); + FlinkKafkaConsumer011 consumer = + new FlinkKafkaConsumer011<>(topic, new SimpleStringSchema(),props); + + return consumer; +} + + public static FlinkKafkaConsumer011 createInputMessageConsumer(String topic, String kafkaAddress, String kafkaGroup ) { + Properties properties = new Properties(); + properties.setProperty("bootstrap.servers", kafkaAddress); + properties.setProperty("group.id",kafkaGroup); + FlinkKafkaConsumer011 consumer = new FlinkKafkaConsumer011( + topic, new InputMessageDeserializationSchema(),properties); + + return consumer; + } +} diff --git a/libraries-data-2/src/main/com/baeldung/flink/connector/Producers.java b/libraries-data-2/src/main/com/baeldung/flink/connector/Producers.java new file mode 100644 index 0000000000..8e6f3f8f37 --- /dev/null +++ b/libraries-data-2/src/main/com/baeldung/flink/connector/Producers.java @@ -0,0 +1,17 @@ +package com.baeldung.flink.connector; + +import com.baeldung.flink.model.Backup; +import com.baeldung.flink.schema.BackupSerializationSchema; +import org.apache.flink.api.common.serialization.SimpleStringSchema; +import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer011; + +public class Producers { + + public static FlinkKafkaProducer011 createStringProducer(String topic, String kafkaAddress) { + return new FlinkKafkaProducer011<>(kafkaAddress, topic, new SimpleStringSchema()); + } + + public static FlinkKafkaProducer011 createBackupProducer(String topic, String kafkaAddress) { + return new FlinkKafkaProducer011(kafkaAddress, topic, new BackupSerializationSchema()); + } +} diff --git a/libraries-data-2/src/main/com/baeldung/flink/model/Backup.java b/libraries-data-2/src/main/com/baeldung/flink/model/Backup.java new file mode 100644 index 0000000000..268ceec7f3 --- /dev/null +++ b/libraries-data-2/src/main/com/baeldung/flink/model/Backup.java @@ -0,0 +1,27 @@ +package com.baeldung.flink.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.time.LocalDateTime; +import java.util.List; +import java.util.UUID; + +public class Backup { + + @JsonProperty("inputMessages") + List inputMessages; + @JsonProperty("backupTimestamp") + LocalDateTime backupTimestamp; + @JsonProperty("uuid") + UUID uuid; + + public Backup(List inputMessages, LocalDateTime backupTimestamp) { + this.inputMessages = inputMessages; + this.backupTimestamp = backupTimestamp; + this.uuid = UUID.randomUUID(); + } + + public List getInputMessages() { + return inputMessages; + } +} diff --git a/libraries-data-2/src/main/com/baeldung/flink/model/InputMessage.java b/libraries-data-2/src/main/com/baeldung/flink/model/InputMessage.java new file mode 100644 index 0000000000..b3f75256ae --- /dev/null +++ b/libraries-data-2/src/main/com/baeldung/flink/model/InputMessage.java @@ -0,0 +1,71 @@ +package com.baeldung.flink.model; + +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import com.google.common.base.Objects; + +import java.time.LocalDateTime; + +@JsonSerialize +public class InputMessage { + String sender; + String recipient; + LocalDateTime sentAt; + String message; + + public InputMessage() { + } + + public String getSender() { + return sender; + } + public void setSender(String sender) { + this.sender = sender; + } + + public String getRecipient() { + return recipient; + } + + public void setRecipient(String recipient) { + this.recipient = recipient; + } + + public LocalDateTime getSentAt() { + return sentAt; + } + + public void setSentAt(LocalDateTime sentAt) { + this.sentAt = sentAt; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public InputMessage(String sender, String recipient, LocalDateTime sentAt, String message) { + this.sender = sender; + this.recipient = recipient; + this.sentAt = sentAt; + this.message = message; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + InputMessage message1 = (InputMessage) o; + return Objects.equal(sender, message1.sender) && + Objects.equal(recipient, message1.recipient) && + Objects.equal(sentAt, message1.sentAt) && + Objects.equal(message, message1.message); + } + + @Override + public int hashCode() { + return Objects.hashCode(sender, recipient, sentAt, message); + } +} diff --git a/libraries-data-2/src/main/com/baeldung/flink/operator/BackupAggregator.java b/libraries-data-2/src/main/com/baeldung/flink/operator/BackupAggregator.java new file mode 100644 index 0000000000..c39b8413d1 --- /dev/null +++ b/libraries-data-2/src/main/com/baeldung/flink/operator/BackupAggregator.java @@ -0,0 +1,34 @@ +package com.baeldung.flink.operator; + +import com.baeldung.flink.model.Backup; +import com.baeldung.flink.model.InputMessage; +import org.apache.flink.api.common.functions.AggregateFunction; + +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; + + public class BackupAggregator implements AggregateFunction, Backup> { + @Override + public List createAccumulator() { + return new ArrayList<>(); + } + + @Override + public List add(InputMessage inputMessage, List inputMessages) { + inputMessages.add(inputMessage); + return inputMessages; + } + + @Override + public Backup getResult(List inputMessages) { + Backup backup = new Backup(inputMessages, LocalDateTime.now()); + return backup; + } + + @Override + public List merge(List inputMessages, List acc1) { + inputMessages.addAll(acc1); + return inputMessages; + } + } diff --git a/libraries-data-2/src/main/com/baeldung/flink/operator/InputMessageTimestampAssigner.java b/libraries-data-2/src/main/com/baeldung/flink/operator/InputMessageTimestampAssigner.java new file mode 100644 index 0000000000..05828d9588 --- /dev/null +++ b/libraries-data-2/src/main/com/baeldung/flink/operator/InputMessageTimestampAssigner.java @@ -0,0 +1,23 @@ +package com.baeldung.flink.operator; + +import com.baeldung.flink.model.InputMessage; +import org.apache.flink.streaming.api.functions.AssignerWithPunctuatedWatermarks; +import org.apache.flink.streaming.api.watermark.Watermark; + +import javax.annotation.Nullable; +import java.time.ZoneId; + +public class InputMessageTimestampAssigner implements AssignerWithPunctuatedWatermarks { + + @Override + public long extractTimestamp(InputMessage element, long previousElementTimestamp) { + ZoneId zoneId = ZoneId.systemDefault(); + return element.getSentAt().atZone(zoneId).toEpochSecond() * 1000; + } + + @Nullable + @Override + public Watermark checkAndGetNextWatermark(InputMessage lastElement, long extractedTimestamp) { + return new Watermark(extractedTimestamp - 15); + } +} diff --git a/libraries-data-2/src/main/com/baeldung/flink/operator/WordsCapitalizer.java b/libraries-data-2/src/main/com/baeldung/flink/operator/WordsCapitalizer.java new file mode 100644 index 0000000000..f9103d225c --- /dev/null +++ b/libraries-data-2/src/main/com/baeldung/flink/operator/WordsCapitalizer.java @@ -0,0 +1,11 @@ +package com.baeldung.flink.operator; + +import org.apache.flink.api.common.functions.MapFunction; + +public class WordsCapitalizer implements MapFunction { + + @Override + public String map(String s) { + return s.toUpperCase(); + } +} diff --git a/libraries-data-2/src/main/com/baeldung/flink/schema/BackupSerializationSchema.java b/libraries-data-2/src/main/com/baeldung/flink/schema/BackupSerializationSchema.java new file mode 100644 index 0000000000..967b266bb6 --- /dev/null +++ b/libraries-data-2/src/main/com/baeldung/flink/schema/BackupSerializationSchema.java @@ -0,0 +1,33 @@ +package com.baeldung.flink.schema; + +import com.baeldung.flink.model.Backup; +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import org.apache.flink.api.common.serialization.SerializationSchema; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class BackupSerializationSchema + implements SerializationSchema { + + static ObjectMapper objectMapper = new ObjectMapper().registerModule(new JavaTimeModule()); + + Logger logger = LoggerFactory.getLogger(BackupSerializationSchema.class); + + @Override + public byte[] serialize(Backup backupMessage) { + if(objectMapper == null) { + objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); + objectMapper = new ObjectMapper().registerModule(new JavaTimeModule()); + } + try { + String json = objectMapper.writeValueAsString(backupMessage); + return json.getBytes(); + } catch (com.fasterxml.jackson.core.JsonProcessingException e) { + logger.error("Failed to parse JSON", e); + } + return new byte[0]; + } +} diff --git a/libraries-data-2/src/main/com/baeldung/flink/schema/InputMessageDeserializationSchema.java b/libraries-data-2/src/main/com/baeldung/flink/schema/InputMessageDeserializationSchema.java new file mode 100644 index 0000000000..9aaf8b9877 --- /dev/null +++ b/libraries-data-2/src/main/com/baeldung/flink/schema/InputMessageDeserializationSchema.java @@ -0,0 +1,32 @@ +package com.baeldung.flink.schema; + +import com.baeldung.flink.model.InputMessage; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import org.apache.flink.api.common.serialization.DeserializationSchema; +import org.apache.flink.api.common.typeinfo.TypeInformation; + +import java.io.IOException; + +public class InputMessageDeserializationSchema implements + DeserializationSchema { + + static ObjectMapper objectMapper = new ObjectMapper().registerModule(new JavaTimeModule()); + + + @Override + public InputMessage deserialize(byte[] bytes) throws IOException { + + return objectMapper.readValue(bytes, InputMessage.class); + } + + @Override + public boolean isEndOfStream(InputMessage inputMessage) { + return false; + } + + @Override + public TypeInformation getProducedType() { + return TypeInformation.of(InputMessage.class); + } +} diff --git a/libraries-data-2/src/main/com/baeldung/google/sheets/GoogleAuthorizeUtil.java b/libraries-data-2/src/main/com/baeldung/google/sheets/GoogleAuthorizeUtil.java new file mode 100644 index 0000000000..641fae42dd --- /dev/null +++ b/libraries-data-2/src/main/com/baeldung/google/sheets/GoogleAuthorizeUtil.java @@ -0,0 +1,34 @@ +package com.baeldung.google.sheets; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.security.GeneralSecurityException; +import java.util.Arrays; +import java.util.List; + +import com.google.api.client.auth.oauth2.Credential; +import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; +import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; +import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; +import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.client.util.store.MemoryDataStoreFactory; +import com.google.api.services.sheets.v4.SheetsScopes; + +public class GoogleAuthorizeUtil { + public static Credential authorize() throws IOException, GeneralSecurityException { + InputStream in = GoogleAuthorizeUtil.class.getResourceAsStream("/google-sheets-client-secret.json"); + GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JacksonFactory.getDefaultInstance(), new InputStreamReader(in)); + + List scopes = Arrays.asList(SheetsScopes.SPREADSHEETS); + + GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), clientSecrets, scopes).setDataStoreFactory(new MemoryDataStoreFactory()) + .setAccessType("offline").build(); + Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user"); + + return credential; + } + +} diff --git a/libraries-data-2/src/main/com/baeldung/google/sheets/SheetsServiceUtil.java b/libraries-data-2/src/main/com/baeldung/google/sheets/SheetsServiceUtil.java new file mode 100644 index 0000000000..8a78d50551 --- /dev/null +++ b/libraries-data-2/src/main/com/baeldung/google/sheets/SheetsServiceUtil.java @@ -0,0 +1,20 @@ +package com.baeldung.google.sheets; + +import java.io.IOException; +import java.security.GeneralSecurityException; + +import com.google.api.client.auth.oauth2.Credential; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.services.sheets.v4.Sheets; + +public class SheetsServiceUtil { + + private static final String APPLICATION_NAME = "Google Sheets Example"; + + public static Sheets getSheetsService() throws IOException, GeneralSecurityException { + Credential credential = GoogleAuthorizeUtil.authorize(); + return new Sheets.Builder(GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), credential).setApplicationName(APPLICATION_NAME).build(); + } + +} diff --git a/libraries-data-2/src/main/com/baeldung/infinispan/CacheConfiguration.java b/libraries-data-2/src/main/com/baeldung/infinispan/CacheConfiguration.java new file mode 100644 index 0000000000..eda511d7a7 --- /dev/null +++ b/libraries-data-2/src/main/com/baeldung/infinispan/CacheConfiguration.java @@ -0,0 +1,70 @@ +package com.baeldung.infinispan; + +import com.baeldung.infinispan.listener.CacheListener; +import org.infinispan.Cache; +import org.infinispan.configuration.cache.Configuration; +import org.infinispan.configuration.cache.ConfigurationBuilder; +import org.infinispan.eviction.EvictionType; +import org.infinispan.manager.DefaultCacheManager; +import org.infinispan.transaction.LockingMode; +import org.infinispan.transaction.TransactionMode; + +import java.util.concurrent.TimeUnit; + +public class CacheConfiguration { + + public static final String SIMPLE_HELLO_WORLD_CACHE = "simple-hello-world-cache"; + public static final String EXPIRING_HELLO_WORLD_CACHE = "expiring-hello-world-cache"; + public static final String EVICTING_HELLO_WORLD_CACHE = "evicting-hello-world-cache"; + public static final String PASSIVATING_HELLO_WORLD_CACHE = "passivating-hello-world-cache"; + public static final String TRANSACTIONAL_CACHE = "transactional-cache"; + + public DefaultCacheManager cacheManager() { + return new DefaultCacheManager(); + } + + public Cache transactionalCache(DefaultCacheManager cacheManager, CacheListener listener) { + return this.buildCache(TRANSACTIONAL_CACHE, cacheManager, listener, transactionalConfiguration()); + } + + public Cache simpleHelloWorldCache(DefaultCacheManager cacheManager, CacheListener listener) { + return this.buildCache(SIMPLE_HELLO_WORLD_CACHE, cacheManager, listener, new ConfigurationBuilder().build()); + } + + public Cache expiringHelloWorldCache(DefaultCacheManager cacheManager, CacheListener listener) { + return this.buildCache(EXPIRING_HELLO_WORLD_CACHE, cacheManager, listener, expiringConfiguration()); + } + + public Cache evictingHelloWorldCache(DefaultCacheManager cacheManager, CacheListener listener) { + return this.buildCache(EVICTING_HELLO_WORLD_CACHE, cacheManager, listener, evictingConfiguration()); + } + + public Cache passivatingHelloWorldCache(DefaultCacheManager cacheManager, CacheListener listener) { + return this.buildCache(PASSIVATING_HELLO_WORLD_CACHE, cacheManager, listener, passivatingConfiguration()); + } + + private Cache buildCache(String cacheName, DefaultCacheManager cacheManager, CacheListener listener, Configuration configuration) { + + cacheManager.defineConfiguration(cacheName, configuration); + Cache cache = cacheManager.getCache(cacheName); + cache.addListener(listener); + return cache; + } + + private Configuration expiringConfiguration() { + return new ConfigurationBuilder().expiration().lifespan(1, TimeUnit.SECONDS).build(); + } + + private Configuration evictingConfiguration() { + return new ConfigurationBuilder().memory().evictionType(EvictionType.COUNT).size(1).build(); + } + + private Configuration passivatingConfiguration() { + return new ConfigurationBuilder().memory().evictionType(EvictionType.COUNT).size(1).persistence().passivation(true).addSingleFileStore().purgeOnStartup(true).location(System.getProperty("java.io.tmpdir")).build(); + } + + private Configuration transactionalConfiguration() { + return new ConfigurationBuilder().transaction().transactionMode(TransactionMode.TRANSACTIONAL).lockingMode(LockingMode.PESSIMISTIC).build(); + } + +} diff --git a/libraries-data-2/src/main/com/baeldung/infinispan/listener/CacheListener.java b/libraries-data-2/src/main/com/baeldung/infinispan/listener/CacheListener.java new file mode 100644 index 0000000000..942a2fb62d --- /dev/null +++ b/libraries-data-2/src/main/com/baeldung/infinispan/listener/CacheListener.java @@ -0,0 +1,53 @@ +package com.baeldung.infinispan.listener; + +import org.infinispan.notifications.Listener; +import org.infinispan.notifications.cachelistener.annotation.*; +import org.infinispan.notifications.cachelistener.event.*; + +@Listener +public class CacheListener { + + @CacheEntryCreated + public void entryCreated(CacheEntryCreatedEvent event) { + this.printLog("Adding key '" + event.getKey() + "' to cache", event); + } + + @CacheEntryExpired + public void entryExpired(CacheEntryExpiredEvent event) { + this.printLog("Expiring key '" + event.getKey() + "' from cache", event); + } + + @CacheEntryVisited + public void entryVisited(CacheEntryVisitedEvent event) { + this.printLog("Key '" + event.getKey() + "' was visited", event); + } + + @CacheEntryActivated + public void entryActivated(CacheEntryActivatedEvent event) { + this.printLog("Activating key '" + event.getKey() + "' on cache", event); + } + + @CacheEntryPassivated + public void entryPassivated(CacheEntryPassivatedEvent event) { + this.printLog("Passivating key '" + event.getKey() + "' from cache", event); + } + + @CacheEntryLoaded + public void entryLoaded(CacheEntryLoadedEvent event) { + this.printLog("Loading key '" + event.getKey() + "' to cache", event); + } + + @CacheEntriesEvicted + public void entriesEvicted(CacheEntriesEvictedEvent event) { + final StringBuilder builder = new StringBuilder(); + event.getEntries().forEach((key, value) -> builder.append(key).append(", ")); + System.out.println("Evicting following entries from cache: " + builder.toString()); + } + + private void printLog(String log, CacheEntryEvent event) { + if (!event.isPre()) { + System.out.println(log); + } + } + +} diff --git a/libraries-data-2/src/main/com/baeldung/infinispan/repository/HelloWorldRepository.java b/libraries-data-2/src/main/com/baeldung/infinispan/repository/HelloWorldRepository.java new file mode 100644 index 0000000000..85c0d539a3 --- /dev/null +++ b/libraries-data-2/src/main/com/baeldung/infinispan/repository/HelloWorldRepository.java @@ -0,0 +1,15 @@ +package com.baeldung.infinispan.repository; + +public class HelloWorldRepository { + + public String getHelloWorld() { + try { + System.out.println("Executing some heavy query"); + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + return "Hello World!"; + } + +} diff --git a/libraries-data-2/src/main/com/baeldung/infinispan/service/HelloWorldService.java b/libraries-data-2/src/main/com/baeldung/infinispan/service/HelloWorldService.java new file mode 100644 index 0000000000..de30cd5c8e --- /dev/null +++ b/libraries-data-2/src/main/com/baeldung/infinispan/service/HelloWorldService.java @@ -0,0 +1,77 @@ +package com.baeldung.infinispan.service; + +import com.baeldung.infinispan.listener.CacheListener; +import com.baeldung.infinispan.repository.HelloWorldRepository; +import org.infinispan.Cache; + +import java.util.concurrent.TimeUnit; + +public class HelloWorldService { + + private final HelloWorldRepository repository; + + private final Cache simpleHelloWorldCache; + private final Cache expiringHelloWorldCache; + private final Cache evictingHelloWorldCache; + private final Cache passivatingHelloWorldCache; + + public HelloWorldService(HelloWorldRepository repository, CacheListener listener, Cache simpleHelloWorldCache, Cache expiringHelloWorldCache, Cache evictingHelloWorldCache, + Cache passivatingHelloWorldCache) { + + this.repository = repository; + + this.simpleHelloWorldCache = simpleHelloWorldCache; + this.expiringHelloWorldCache = expiringHelloWorldCache; + this.evictingHelloWorldCache = evictingHelloWorldCache; + this.passivatingHelloWorldCache = passivatingHelloWorldCache; + } + + public String findSimpleHelloWorld() { + String cacheKey = "simple-hello"; + return simpleHelloWorldCache.computeIfAbsent(cacheKey, k -> repository.getHelloWorld()); + } + + public String findExpiringHelloWorld() { + String cacheKey = "expiring-hello"; + String helloWorld = simpleHelloWorldCache.get(cacheKey); + if (helloWorld == null) { + helloWorld = repository.getHelloWorld(); + simpleHelloWorldCache.put(cacheKey, helloWorld, 1, TimeUnit.SECONDS); + } + return helloWorld; + } + + public String findIdleHelloWorld() { + String cacheKey = "idle-hello"; + String helloWorld = simpleHelloWorldCache.get(cacheKey); + if (helloWorld == null) { + helloWorld = repository.getHelloWorld(); + simpleHelloWorldCache.put(cacheKey, helloWorld, -1, TimeUnit.SECONDS, 10, TimeUnit.SECONDS); + } + return helloWorld; + } + + public String findSimpleHelloWorldInExpiringCache() { + String cacheKey = "simple-hello"; + String helloWorld = expiringHelloWorldCache.get(cacheKey); + if (helloWorld == null) { + helloWorld = repository.getHelloWorld(); + expiringHelloWorldCache.put(cacheKey, helloWorld); + } + return helloWorld; + } + + public String findEvictingHelloWorld(String key) { + String value = evictingHelloWorldCache.get(key); + if (value == null) { + value = repository.getHelloWorld(); + evictingHelloWorldCache.put(key, value); + } + return value; + } + + public String findPassivatingHelloWorld(String key) { + return passivatingHelloWorldCache.computeIfAbsent(key, k -> repository.getHelloWorld()); + } + +} diff --git a/libraries-data-2/src/main/com/baeldung/infinispan/service/TransactionalService.java b/libraries-data-2/src/main/com/baeldung/infinispan/service/TransactionalService.java new file mode 100644 index 0000000000..26862b8d65 --- /dev/null +++ b/libraries-data-2/src/main/com/baeldung/infinispan/service/TransactionalService.java @@ -0,0 +1,55 @@ +package com.baeldung.infinispan.service; + +import org.infinispan.Cache; +import org.springframework.util.StopWatch; + +import javax.transaction.TransactionManager; + +public class TransactionalService { + + private final Cache transactionalCache; + + private static final String KEY = "key"; + + public TransactionalService(Cache transactionalCache) { + this.transactionalCache = transactionalCache; + + transactionalCache.put(KEY, 0); + } + + public Integer getQuickHowManyVisits() { + try { + TransactionManager tm = transactionalCache.getAdvancedCache().getTransactionManager(); + tm.begin(); + Integer howManyVisits = transactionalCache.get(KEY); + howManyVisits++; + System.out.println("Ill try to set HowManyVisits to " + howManyVisits); + StopWatch watch = new StopWatch(); + watch.start(); + transactionalCache.put(KEY, howManyVisits); + watch.stop(); + System.out.println("I was able to set HowManyVisits to " + howManyVisits + " after waiting " + watch.getTotalTimeSeconds() + " seconds"); + + tm.commit(); + return howManyVisits; + } catch (Exception e) { + e.printStackTrace(); + return 0; + } + } + + public void startBackgroundBatch() { + try { + TransactionManager tm = transactionalCache.getAdvancedCache().getTransactionManager(); + tm.begin(); + transactionalCache.put(KEY, 1000); + System.out.println("HowManyVisits should now be 1000, " + "but we are holding the transaction"); + Thread.sleep(1000L); + tm.rollback(); + System.out.println("The slow batch suffered a rollback"); + } catch (Exception e) { + e.printStackTrace(); + } + } + +} diff --git a/libraries-data-2/src/main/com/baeldung/measurement/WaterTank.java b/libraries-data-2/src/main/com/baeldung/measurement/WaterTank.java new file mode 100644 index 0000000000..f3675ae689 --- /dev/null +++ b/libraries-data-2/src/main/com/baeldung/measurement/WaterTank.java @@ -0,0 +1,26 @@ +package com.baeldung.measurement; + +import javax.measure.Quantity; +import javax.measure.quantity.Volume; + +public class WaterTank { + + private Quantity capacityMeasure; + private double capacityDouble; + + public void setCapacityMeasure(Quantity capacityMeasure) { + this.capacityMeasure = capacityMeasure; + } + + public void setCapacityDouble(double capacityDouble) { + this.capacityDouble = capacityDouble; + } + + public Quantity getCapacityMeasure() { + return capacityMeasure; + } + + public double getCapacityDouble() { + return capacityDouble; + } +} diff --git a/libraries-data-2/src/main/com/baeldung/opencsv/Application.java b/libraries-data-2/src/main/com/baeldung/opencsv/Application.java new file mode 100644 index 0000000000..fd7e4d82d4 --- /dev/null +++ b/libraries-data-2/src/main/com/baeldung/opencsv/Application.java @@ -0,0 +1,108 @@ +package com.baeldung.opencsv; + +import com.baeldung.opencsv.beans.NamedColumnBean; +import com.baeldung.opencsv.beans.SimplePositionBean; +import com.baeldung.opencsv.examples.sync.BeanExamples; +import com.baeldung.opencsv.examples.sync.CsvReaderExamples; +import com.baeldung.opencsv.examples.sync.CsvWriterExamples; +import com.baeldung.opencsv.helpers.Helpers; + +import java.io.Reader; +import java.nio.file.Files; +import java.nio.file.Path; + +public class Application { + + /* + * Bean Examples. + */ + + public static String simpleSyncPositionBeanExample() { + Path path = null; + try { + path = Helpers.twoColumnCsvPath(); + } catch (Exception ex) { + Helpers.err(ex); + } + return BeanExamples.beanBuilderExample(path, SimplePositionBean.class).toString(); + } + + public static String namedSyncColumnBeanExample() { + Path path = null; + try { + path = Helpers.namedColumnCsvPath(); + } catch (Exception ex) { + Helpers.err(ex); + } + return BeanExamples.beanBuilderExample(path, NamedColumnBean.class).toString(); + } + + public static String writeSyncCsvFromBeanExample() { + Path path = null; + try { + path = Helpers.fileOutBeanPath(); + } catch (Exception ex) { + Helpers.err(ex); + } + return BeanExamples.writeCsvFromBean(path); + } + + /* + * CSV Reader Examples. + */ + + public static String oneByOneSyncExample() { + Reader reader = null; + try { + reader = Files.newBufferedReader(Helpers.twoColumnCsvPath()); + } catch (Exception ex) { + Helpers.err(ex); + } + return CsvReaderExamples.oneByOne(reader).toString(); + } + + public static String readAllSyncExample() { + Reader reader = null; + try { + reader = Files.newBufferedReader(Helpers.twoColumnCsvPath()); + } catch (Exception ex) { + Helpers.err(ex); + } + return CsvReaderExamples.readAll(reader).toString(); + } + + /* + * CSV Writer Examples. + */ + + + public static String csvWriterSyncOneByOne() { + Path path = null; + try { + path = Helpers.fileOutOnePath(); + } catch (Exception ex) { + Helpers.err(ex); + } + return CsvWriterExamples.csvWriterOneByOne(Helpers.fourColumnCsvString(), path); + } + + public static String csvWriterSyncAll() { + Path path = null; + try { + path = Helpers.fileOutAllPath(); + } catch (Exception ex) { + Helpers.err(ex); + } + return CsvWriterExamples.csvWriterAll(Helpers.fourColumnCsvString(), path); + } + + public static void main(String[] args) { + simpleSyncPositionBeanExample(); + namedSyncColumnBeanExample(); + writeSyncCsvFromBeanExample(); + oneByOneSyncExample(); + readAllSyncExample(); + csvWriterSyncOneByOne(); + csvWriterSyncAll(); + } +} diff --git a/libraries-data-2/src/main/com/baeldung/opencsv/Constants.java b/libraries-data-2/src/main/com/baeldung/opencsv/Constants.java new file mode 100644 index 0000000000..f7978acf24 --- /dev/null +++ b/libraries-data-2/src/main/com/baeldung/opencsv/Constants.java @@ -0,0 +1,17 @@ +package com.baeldung.opencsv; + +public class Constants { + + public static final String GENERIC_EXCEPTION = "EXCEPTION ENCOUNTERED: "; + public static final String GENERIC_SUCCESS = "SUCCESS"; + + public static final String TWO_COLUMN_CSV = "csv/twoColumn.csv"; + public static final String FOUR_COLUMN_CSV = "csv/fourColumn.csv"; + public static final String NAMED_COLUMN_CSV = "csv/namedColumn.csv"; + + public static final String CSV_All = "csv/writtenAll.csv"; + public static final String CSV_BEAN = "csv/writtenBean.csv"; + public static final String CSV_ONE = "csv/writtenOneByOne.csv"; + + +} diff --git a/libraries-data-2/src/main/com/baeldung/opencsv/beans/CsvBean.java b/libraries-data-2/src/main/com/baeldung/opencsv/beans/CsvBean.java new file mode 100644 index 0000000000..af9caaae12 --- /dev/null +++ b/libraries-data-2/src/main/com/baeldung/opencsv/beans/CsvBean.java @@ -0,0 +1,3 @@ +package com.baeldung.opencsv.beans; + +public class CsvBean { } diff --git a/libraries-data-2/src/main/com/baeldung/opencsv/beans/NamedColumnBean.java b/libraries-data-2/src/main/com/baeldung/opencsv/beans/NamedColumnBean.java new file mode 100644 index 0000000000..d9e5f96333 --- /dev/null +++ b/libraries-data-2/src/main/com/baeldung/opencsv/beans/NamedColumnBean.java @@ -0,0 +1,31 @@ +package com.baeldung.opencsv.beans; + +import com.opencsv.bean.CsvBindByName; + +public class NamedColumnBean extends CsvBean { + + @CsvBindByName(column = "name") + private String name; + + //Automatically infer column name as Age + @CsvBindByName + private int age; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + +} diff --git a/libraries-data-2/src/main/com/baeldung/opencsv/beans/SimplePositionBean.java b/libraries-data-2/src/main/com/baeldung/opencsv/beans/SimplePositionBean.java new file mode 100644 index 0000000000..799006e2f9 --- /dev/null +++ b/libraries-data-2/src/main/com/baeldung/opencsv/beans/SimplePositionBean.java @@ -0,0 +1,29 @@ +package com.baeldung.opencsv.beans; + +import com.opencsv.bean.CsvBindByPosition; + +public class SimplePositionBean extends CsvBean { + + @CsvBindByPosition(position = 0) + private String exampleColOne; + + @CsvBindByPosition(position = 1) + private String exampleColTwo; + + public String getExampleColOne() { + return exampleColOne; + } + + private void setExampleColOne(String exampleColOne) { + this.exampleColOne = exampleColOne; + } + + public String getExampleColTwo() { + return exampleColTwo; + } + + private void setExampleCsvTwo (String exampleColTwo) { + this.exampleColTwo = exampleColTwo; + } + +} diff --git a/libraries-data-2/src/main/com/baeldung/opencsv/beans/WriteExampleBean.java b/libraries-data-2/src/main/com/baeldung/opencsv/beans/WriteExampleBean.java new file mode 100644 index 0000000000..f145f67f11 --- /dev/null +++ b/libraries-data-2/src/main/com/baeldung/opencsv/beans/WriteExampleBean.java @@ -0,0 +1,40 @@ +package com.baeldung.opencsv.beans; + +public class WriteExampleBean extends CsvBean { + + private String colA; + + private String colB; + + private String colC; + + public WriteExampleBean(String colA, String colB, String colC) { + this.colA = colA; + this.colB = colB; + this.colC = colC; + } + + public String getColA() { + return colA; + } + + public void setColA(String colA) { + this.colA = colA; + } + + public String getColB() { + return colB; + } + + public void setColB(String colB) { + this.colB = colB; + } + + public String getColC() { + return colC; + } + + public void setColC(String colC) { + this.colC = colC; + } +} diff --git a/libraries-data-2/src/main/com/baeldung/opencsv/examples/sync/BeanExamples.java b/libraries-data-2/src/main/com/baeldung/opencsv/examples/sync/BeanExamples.java new file mode 100644 index 0000000000..086f32677e --- /dev/null +++ b/libraries-data-2/src/main/com/baeldung/opencsv/examples/sync/BeanExamples.java @@ -0,0 +1,63 @@ +package com.baeldung.opencsv.examples.sync; + +import com.baeldung.opencsv.beans.CsvBean; +import com.baeldung.opencsv.beans.WriteExampleBean; +import com.baeldung.opencsv.helpers.Helpers; +import com.baeldung.opencsv.pojos.CsvTransfer; +import com.opencsv.CSVWriter; +import com.opencsv.bean.*; + +import java.io.FileWriter; +import java.io.Reader; +import java.io.Writer; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; + +public class BeanExamples { + + public static List beanBuilderExample(Path path, Class clazz) { + ColumnPositionMappingStrategy ms = new ColumnPositionMappingStrategy(); + return beanBuilderExample(path, clazz, ms); + } + + public static List beanBuilderExample(Path path, Class clazz, MappingStrategy ms) { + CsvTransfer csvTransfer = new CsvTransfer(); + try { + ms.setType(clazz); + + Reader reader = Files.newBufferedReader(path); + CsvToBean cb = new CsvToBeanBuilder(reader).withType(clazz) + .withMappingStrategy(ms) + .build(); + + csvTransfer.setCsvList(cb.parse()); + reader.close(); + + } catch (Exception ex) { + Helpers.err(ex); + } + return csvTransfer.getCsvList(); + } + + public static String writeCsvFromBean(Path path) { + try { + Writer writer = new FileWriter(path.toString()); + + StatefulBeanToCsv sbc = new StatefulBeanToCsvBuilder(writer).withSeparator(CSVWriter.DEFAULT_SEPARATOR) + .build(); + + List list = new ArrayList<>(); + list.add(new WriteExampleBean("Test1", "sfdsf", "fdfd")); + list.add(new WriteExampleBean("Test2", "ipso", "facto")); + + sbc.write(list); + writer.close(); + + } catch (Exception ex) { + Helpers.err(ex); + } + return Helpers.readFile(path); + } +} \ No newline at end of file diff --git a/libraries-data-2/src/main/com/baeldung/opencsv/examples/sync/CsvReaderExamples.java b/libraries-data-2/src/main/com/baeldung/opencsv/examples/sync/CsvReaderExamples.java new file mode 100644 index 0000000000..bacfdcff3d --- /dev/null +++ b/libraries-data-2/src/main/com/baeldung/opencsv/examples/sync/CsvReaderExamples.java @@ -0,0 +1,63 @@ +package com.baeldung.opencsv.examples.sync; + +import com.baeldung.opencsv.helpers.Helpers; +import com.opencsv.CSVParser; +import com.opencsv.CSVParserBuilder; +import com.opencsv.CSVReader; +import com.opencsv.CSVReaderBuilder; + +import java.io.Reader; +import java.util.ArrayList; +import java.util.List; + +public class CsvReaderExamples { + + public static List readAll(Reader reader) { + + CSVParser parser = new CSVParserBuilder() + .withSeparator(',') + .withIgnoreQuotations(true) + .build(); + + CSVReader csvReader = new CSVReaderBuilder(reader) + .withSkipLines(0) + .withCSVParser(parser) + .build(); + + List list = new ArrayList<>(); + try { + list = csvReader.readAll(); + reader.close(); + csvReader.close(); + } catch (Exception ex) { + Helpers.err(ex); + } + return list; + } + + public static List oneByOne(Reader reader) { + List list = new ArrayList<>(); + try { + CSVParser parser = new CSVParserBuilder() + .withSeparator(',') + .withIgnoreQuotations(true) + .build(); + + CSVReader csvReader = new CSVReaderBuilder(reader) + .withSkipLines(0) + .withCSVParser(parser) + .build(); + + String[] line; + while ((line = csvReader.readNext()) != null) { + list.add(line); + } + reader.close(); + csvReader.close(); + } catch (Exception ex) { + Helpers.err(ex); + } + return list; + } + +} diff --git a/libraries-data-2/src/main/com/baeldung/opencsv/examples/sync/CsvWriterExamples.java b/libraries-data-2/src/main/com/baeldung/opencsv/examples/sync/CsvWriterExamples.java new file mode 100644 index 0000000000..6f52160ba8 --- /dev/null +++ b/libraries-data-2/src/main/com/baeldung/opencsv/examples/sync/CsvWriterExamples.java @@ -0,0 +1,35 @@ +package com.baeldung.opencsv.examples.sync; + +import com.baeldung.opencsv.helpers.Helpers; +import com.opencsv.CSVWriter; + +import java.io.FileWriter; +import java.nio.file.Path; +import java.util.List; + +public class CsvWriterExamples { + + public static String csvWriterOneByOne(List stringArray, Path path) { + try { + CSVWriter writer = new CSVWriter(new FileWriter(path.toString())); + for (String[] array : stringArray) { + writer.writeNext(array); + } + writer.close(); + } catch (Exception ex) { + Helpers.err(ex); + } + return Helpers.readFile(path); + } + + public static String csvWriterAll(List stringArray, Path path) { + try { + CSVWriter writer = new CSVWriter(new FileWriter(path.toString())); + writer.writeAll(stringArray); + writer.close(); + } catch (Exception ex) { + Helpers.err(ex); + } + return Helpers.readFile(path); + } +} \ No newline at end of file diff --git a/libraries-data-2/src/main/com/baeldung/opencsv/helpers/Helpers.java b/libraries-data-2/src/main/com/baeldung/opencsv/helpers/Helpers.java new file mode 100644 index 0000000000..9a46fbc495 --- /dev/null +++ b/libraries-data-2/src/main/com/baeldung/opencsv/helpers/Helpers.java @@ -0,0 +1,108 @@ +package com.baeldung.opencsv.helpers; + +import com.baeldung.opencsv.Constants; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.net.URI; +import java.net.URISyntaxException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; + +public class Helpers { + + /** + * Write Files + */ + + public static Path fileOutAllPath() throws URISyntaxException { + URI uri = ClassLoader.getSystemResource(Constants.CSV_All).toURI(); + return Paths.get(uri); + } + + public static Path fileOutBeanPath() throws URISyntaxException { + URI uri = ClassLoader.getSystemResource(Constants.CSV_BEAN).toURI(); + return Paths.get(uri); + } + + public static Path fileOutOnePath() throws URISyntaxException { + URI uri = ClassLoader.getSystemResource(Constants.CSV_ONE).toURI(); + return Paths.get(uri); + } + + /** + * Read Files + */ + + public static Path twoColumnCsvPath() throws URISyntaxException { + URI uri = ClassLoader.getSystemResource(Constants.TWO_COLUMN_CSV).toURI(); + return Paths.get(uri); + } + + public static Path fourColumnCsvPath() throws URISyntaxException { + URI uri = ClassLoader.getSystemResource(Constants.FOUR_COLUMN_CSV).toURI(); + return Paths.get(uri); + } + + public static Path namedColumnCsvPath() throws URISyntaxException { + URI uri = ClassLoader.getSystemResource(Constants.NAMED_COLUMN_CSV).toURI(); + return Paths.get(uri); + } + + /** + * Simple File Reader + */ + + public static String readFile(Path path) { + String response = ""; + try { + FileReader fr = new FileReader(path.toString()); + BufferedReader br = new BufferedReader(fr); + String strLine; + StringBuffer sb = new StringBuffer(); + while ((strLine = br.readLine()) != null) { + sb.append(strLine); + } + response = sb.toString(); + System.out.println(response); + fr.close(); + br.close(); + } catch (Exception ex) { + Helpers.err(ex); + } + return response; + } + + /** + * Dummy Data for Writing. + */ + + public static List twoColumnCsvString() { + List list = new ArrayList<>(); + list.add(new String[]{"ColA", "ColB"}); + list.add(new String[]{"A", "B"}); + return list; + } + + public static List fourColumnCsvString() { + List list = new ArrayList<>(); + list.add(new String[]{"ColA", "ColB", "ColC", "ColD"}); + list.add(new String[]{"A", "B", "A", "B"}); + list.add(new String[]{"BB", "AB", "AA", "B"}); + return list; + } + + /** + * Message Helpers + */ + + public static void print(String msg) { + System.out.println(msg); + } + + public static void err(Exception ex) { + System.out.println(Constants.GENERIC_EXCEPTION + " " + ex); + } +} diff --git a/libraries-data-2/src/main/com/baeldung/opencsv/pojos/CsvTransfer.java b/libraries-data-2/src/main/com/baeldung/opencsv/pojos/CsvTransfer.java new file mode 100644 index 0000000000..79366e3d20 --- /dev/null +++ b/libraries-data-2/src/main/com/baeldung/opencsv/pojos/CsvTransfer.java @@ -0,0 +1,38 @@ +package com.baeldung.opencsv.pojos; + +import com.baeldung.opencsv.beans.CsvBean; + +import java.util.ArrayList; +import java.util.List; + +public class CsvTransfer { + + private List csvStringList; + + private List csvList; + + public CsvTransfer() {} + + public List getCsvStringList() { + if (csvStringList != null) return csvStringList; + return new ArrayList(); + } + + public void addLine(String[] line) { + if (this.csvList == null) this.csvStringList = new ArrayList<>(); + this.csvStringList.add(line); + } + + public void setCsvStringList(List csvStringList) { + this.csvStringList = csvStringList; + } + + public void setCsvList(List csvList) { + this.csvList = csvList; + } + + public List getCsvList() { + if (csvList != null) return csvList; + return new ArrayList(); + } +} \ No newline at end of file diff --git a/libraries-data-2/src/main/com/baeldung/smooks/converter/OrderConverter.java b/libraries-data-2/src/main/com/baeldung/smooks/converter/OrderConverter.java new file mode 100644 index 0000000000..fa317f93b7 --- /dev/null +++ b/libraries-data-2/src/main/com/baeldung/smooks/converter/OrderConverter.java @@ -0,0 +1,44 @@ +package com.baeldung.smooks.converter; + +import com.baeldung.smooks.model.Order; +import org.milyn.Smooks; +import org.milyn.payload.JavaResult; +import org.milyn.payload.StringResult; +import org.xml.sax.SAXException; + +import javax.xml.transform.stream.StreamSource; +import java.io.IOException; + +public class OrderConverter { + + public Order convertOrderXMLToOrderObject(String path) throws IOException, SAXException { + Smooks smooks = new Smooks(OrderConverter.class.getResourceAsStream("/smooks/smooks-mapping.xml")); + try { + JavaResult javaResult = new JavaResult(); + smooks.filterSource(new StreamSource(OrderConverter.class.getResourceAsStream(path)), javaResult); + return (Order) javaResult.getBean("order"); + } finally { + smooks.close(); + } + } + + public String convertOrderXMLtoEDIFACT(String path) throws IOException, SAXException { + return convertDocumentWithTempalte(path, "/smooks/smooks-transform-edi.xml"); + } + + public String convertOrderXMLtoEmailMessage(String path) throws IOException, SAXException { + return convertDocumentWithTempalte(path, "/smooks/smooks-transform-email.xml"); + } + + private String convertDocumentWithTempalte(String path, String config) throws IOException, SAXException { + Smooks smooks = new Smooks(config); + + try { + StringResult stringResult = new StringResult(); + smooks.filterSource(new StreamSource(OrderConverter.class.getResourceAsStream(path)), stringResult); + return stringResult.toString(); + } finally { + smooks.close(); + } + } +} diff --git a/libraries-data-2/src/main/com/baeldung/smooks/converter/OrderValidator.java b/libraries-data-2/src/main/com/baeldung/smooks/converter/OrderValidator.java new file mode 100644 index 0000000000..3975921da0 --- /dev/null +++ b/libraries-data-2/src/main/com/baeldung/smooks/converter/OrderValidator.java @@ -0,0 +1,27 @@ +package com.baeldung.smooks.converter; + +import org.milyn.Smooks; +import org.milyn.payload.JavaResult; +import org.milyn.payload.StringResult; +import org.milyn.validation.ValidationResult; +import org.xml.sax.SAXException; + +import javax.xml.transform.stream.StreamSource; +import java.io.IOException; + +public class OrderValidator { + + public ValidationResult validate(String path) throws IOException, SAXException { + Smooks smooks = new Smooks(OrderValidator.class.getResourceAsStream("/smooks/smooks-validation.xml")); + + try { + StringResult xmlResult = new StringResult(); + JavaResult javaResult = new JavaResult(); + ValidationResult validationResult = new ValidationResult(); + smooks.filterSource(new StreamSource(OrderValidator.class.getResourceAsStream(path)), xmlResult, javaResult, validationResult); + return validationResult; + } finally { + smooks.close(); + } + } +} diff --git a/libraries-data-2/src/main/com/baeldung/smooks/model/Item.java b/libraries-data-2/src/main/com/baeldung/smooks/model/Item.java new file mode 100644 index 0000000000..3e1f4a7ef4 --- /dev/null +++ b/libraries-data-2/src/main/com/baeldung/smooks/model/Item.java @@ -0,0 +1,70 @@ +package com.baeldung.smooks.model; + +public class Item { + + public Item() { + } + + public Item(String code, Double price, Integer quantity) { + this.code = code; + this.price = price; + this.quantity = quantity; + } + + private String code; + private Double price; + private Integer quantity; + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public Double getPrice() { + return price; + } + + public void setPrice(Double price) { + this.price = price; + } + + public Integer getQuantity() { + return quantity; + } + + public void setQuantity(Integer quantity) { + this.quantity = quantity; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + + Item item = (Item) o; + + if (code != null ? !code.equals(item.code) : item.code != null) + return false; + if (price != null ? !price.equals(item.price) : item.price != null) + return false; + return quantity != null ? quantity.equals(item.quantity) : item.quantity == null; + } + + @Override + public int hashCode() { + int result = code != null ? code.hashCode() : 0; + result = 31 * result + (price != null ? price.hashCode() : 0); + result = 31 * result + (quantity != null ? quantity.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "Item{" + "code='" + code + '\'' + ", price=" + price + ", quantity=" + quantity + '}'; + } +} diff --git a/libraries-data-2/src/main/com/baeldung/smooks/model/Order.java b/libraries-data-2/src/main/com/baeldung/smooks/model/Order.java new file mode 100644 index 0000000000..047e1fe8a3 --- /dev/null +++ b/libraries-data-2/src/main/com/baeldung/smooks/model/Order.java @@ -0,0 +1,52 @@ +package com.baeldung.smooks.model; + +import java.util.Date; +import java.util.List; + +public class Order { + private Date creationDate; + private Long number; + private Status status; + private Supplier supplier; + private List items; + + public Date getCreationDate() { + return creationDate; + } + + public void setCreationDate(Date creationDate) { + this.creationDate = creationDate; + } + + public Long getNumber() { + return number; + } + + public void setNumber(Long number) { + this.number = number; + } + + public Status getStatus() { + return status; + } + + public void setStatus(Status status) { + this.status = status; + } + + public Supplier getSupplier() { + return supplier; + } + + public void setSupplier(Supplier supplier) { + this.supplier = supplier; + } + + public List getItems() { + return items; + } + + public void setItems(List items) { + this.items = items; + } +} diff --git a/libraries-data-2/src/main/com/baeldung/smooks/model/Status.java b/libraries-data-2/src/main/com/baeldung/smooks/model/Status.java new file mode 100644 index 0000000000..53c50bdf46 --- /dev/null +++ b/libraries-data-2/src/main/com/baeldung/smooks/model/Status.java @@ -0,0 +1,5 @@ +package com.baeldung.smooks.model; + +public enum Status { + NEW, IN_PROGRESS, FINISHED +} diff --git a/libraries-data-2/src/main/com/baeldung/smooks/model/Supplier.java b/libraries-data-2/src/main/com/baeldung/smooks/model/Supplier.java new file mode 100644 index 0000000000..827a0fc907 --- /dev/null +++ b/libraries-data-2/src/main/com/baeldung/smooks/model/Supplier.java @@ -0,0 +1,52 @@ +package com.baeldung.smooks.model; + +public class Supplier { + + private String name; + private String phoneNumber; + + public Supplier() { + } + + public Supplier(String name, String phoneNumber) { + this.name = name; + this.phoneNumber = phoneNumber; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPhoneNumber() { + return phoneNumber; + } + + public void setPhoneNumber(String phoneNumber) { + this.phoneNumber = phoneNumber; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + + Supplier supplier = (Supplier) o; + + if (name != null ? !name.equals(supplier.name) : supplier.name != null) + return false; + return phoneNumber != null ? phoneNumber.equals(supplier.phoneNumber) : supplier.phoneNumber == null; + } + + @Override + public int hashCode() { + int result = name != null ? name.hashCode() : 0; + result = 31 * result + (phoneNumber != null ? phoneNumber.hashCode() : 0); + return result; + } +} diff --git a/libraries-data-2/src/test/com/baeldung/crdt/CRDTUnitTest.java b/libraries-data-2/src/test/com/baeldung/crdt/CRDTUnitTest.java new file mode 100644 index 0000000000..840263e430 --- /dev/null +++ b/libraries-data-2/src/test/com/baeldung/crdt/CRDTUnitTest.java @@ -0,0 +1,149 @@ +package com.baeldung.crdt; + +import com.netopyr.wurmloch.crdt.GCounter; +import com.netopyr.wurmloch.crdt.GSet; +import com.netopyr.wurmloch.crdt.LWWRegister; +import com.netopyr.wurmloch.crdt.PNCounter; +import com.netopyr.wurmloch.store.LocalCrdtStore; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class CRDTUnitTest { + + @Test + public void givenGrowOnlySet_whenTwoReplicasDiverge_thenShouldMergeItWithoutAConflict() { + // given + final LocalCrdtStore crdtStore1 = new LocalCrdtStore(); + final LocalCrdtStore crdtStore2 = new LocalCrdtStore(); + crdtStore1.connect(crdtStore2); + + final GSet replica1 = crdtStore1.createGSet("ID_1"); + final GSet replica2 = crdtStore2. findGSet("ID_1").get(); + + // when + replica1.add("apple"); + replica2.add("banana"); + + // then + assertThat(replica1).contains("apple", "banana"); + assertThat(replica2).contains("apple", "banana"); + + // when + crdtStore1.disconnect(crdtStore2); + + replica1.add("strawberry"); + replica2.add("pear"); + + assertThat(replica1).contains("apple", "banana", "strawberry"); + assertThat(replica2).contains("apple", "banana", "pear"); + + crdtStore1.connect(crdtStore2); + + // then + assertThat(replica1).contains("apple", "banana", "strawberry", "pear"); + assertThat(replica2).contains("apple", "banana", "strawberry", "pear"); + } + + @Test + public void givenIncrementOnlyCounter_whenTwoReplicasDiverge_thenShouldMergeIt() { + // given + final LocalCrdtStore crdtStore1 = new LocalCrdtStore(); + final LocalCrdtStore crdtStore2 = new LocalCrdtStore(); + crdtStore1.connect(crdtStore2); + + final GCounter replica1 = crdtStore1.createGCounter("ID_1"); + final GCounter replica2 = crdtStore2.findGCounter("ID_1").get(); + + // when + replica1.increment(); + replica2.increment(2L); + + // then + assertThat(replica1.get()).isEqualTo(3L); + assertThat(replica2.get()).isEqualTo(3L); + + // when + crdtStore1.disconnect(crdtStore2); + + replica1.increment(3L); + replica2.increment(5L); + + assertThat(replica1.get()).isEqualTo(6L); + assertThat(replica2.get()).isEqualTo(8L); + + crdtStore1.connect(crdtStore2); + + // then + assertThat(replica1.get()).isEqualTo(11L); + assertThat(replica2.get()).isEqualTo(11L); + } + + @Test + public void givenPNCounter_whenReplicasDiverge_thenShouldMergeWithoutAConflict() { + // given + final LocalCrdtStore crdtStore1 = new LocalCrdtStore(); + final LocalCrdtStore crdtStore2 = new LocalCrdtStore(); + crdtStore1.connect(crdtStore2); + + final PNCounter replica1 = crdtStore1.createPNCounter("ID_1"); + final PNCounter replica2 = crdtStore2.findPNCounter("ID_1").get(); + + // when + replica1.increment(); + replica2.decrement(2L); + + // then + assertThat(replica1.get()).isEqualTo(-1L); + assertThat(replica2.get()).isEqualTo(-1L); + + // when + crdtStore1.disconnect(crdtStore2); + + replica1.decrement(3L); + replica2.increment(5L); + + assertThat(replica1.get()).isEqualTo(-4L); + assertThat(replica2.get()).isEqualTo(4L); + + crdtStore1.connect(crdtStore2); + + // then + assertThat(replica1.get()).isEqualTo(1L); + assertThat(replica2.get()).isEqualTo(1L); + } + + @Test + public void givenLastWriteWinsStrategy_whenReplicasDiverge_thenAfterMergeShouldKeepOnlyLastValue() { + // given + final LocalCrdtStore crdtStore1 = new LocalCrdtStore("N_1"); + final LocalCrdtStore crdtStore2 = new LocalCrdtStore("N_2"); + crdtStore1.connect(crdtStore2); + + final LWWRegister replica1 = crdtStore1.createLWWRegister("ID_1"); + final LWWRegister replica2 = crdtStore2. findLWWRegister("ID_1").get(); + + // when + replica1.set("apple"); + replica2.set("banana"); + + // then + assertThat(replica1.get()).isEqualTo("banana"); + assertThat(replica2.get()).isEqualTo("banana"); + + // when + crdtStore1.disconnect(crdtStore2); + + replica1.set("strawberry"); + replica2.set("pear"); + + assertThat(replica1.get()).isEqualTo("strawberry"); + assertThat(replica2.get()).isEqualTo("pear"); + + crdtStore1.connect(crdtStore2); + + // then + assertThat(replica1.get()).isEqualTo("pear"); + assertThat(replica2.get()).isEqualTo("pear"); + } +} diff --git a/libraries-data-2/src/test/com/baeldung/flink/BackupCreatorIntegrationTest.java b/libraries-data-2/src/test/com/baeldung/flink/BackupCreatorIntegrationTest.java new file mode 100644 index 0000000000..f46fffbb59 --- /dev/null +++ b/libraries-data-2/src/test/com/baeldung/flink/BackupCreatorIntegrationTest.java @@ -0,0 +1,104 @@ +package com.baeldung.flink; + +import com.baeldung.flink.model.Backup; +import com.baeldung.flink.model.InputMessage; +import com.baeldung.flink.operator.BackupAggregator; +import com.baeldung.flink.operator.InputMessageTimestampAssigner; +import com.baeldung.flink.schema.BackupSerializationSchema; +import com.baeldung.flink.schema.InputMessageDeserializationSchema; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import org.apache.commons.collections.ListUtils; +import org.apache.flink.api.common.serialization.DeserializationSchema; +import org.apache.flink.api.common.serialization.SerializationSchema; +import org.apache.flink.streaming.api.TimeCharacteristic; +import org.apache.flink.streaming.api.datastream.DataStreamSource; +import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; +import org.apache.flink.streaming.api.functions.sink.SinkFunction; +import org.apache.flink.streaming.api.windowing.time.Time; +import org.awaitility.Awaitility; +import org.junit.Before; +import org.junit.Test; + +import java.io.IOException; +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; + +public class BackupCreatorIntegrationTest { + public static ObjectMapper mapper; + + @Before + public void setup() { + mapper = new ObjectMapper().registerModule(new JavaTimeModule()); + } + + @Test + public void givenProperJson_whenDeserializeIsInvoked_thenProperObjectIsReturned() throws IOException { + InputMessage message = new InputMessage("Me", "User", LocalDateTime.now(), "Test Message"); + byte[] messageSerialized = mapper.writeValueAsBytes(message); + DeserializationSchema deserializationSchema = new InputMessageDeserializationSchema(); + InputMessage messageDeserialized = deserializationSchema.deserialize(messageSerialized); + + assertEquals(message, messageDeserialized); + } + + @Test + public void givenMultipleInputMessagesFromDifferentDays_whenBackupCreatorIsUser_thenMessagesAreGroupedProperly() throws Exception { + LocalDateTime currentTime = LocalDateTime.now(); + InputMessage message = new InputMessage("Me", "User", currentTime, "First TestMessage"); + InputMessage secondMessage = new InputMessage("Me", "User", currentTime.plusHours(1), "First TestMessage"); + InputMessage thirdMessage = new InputMessage("Me", "User", currentTime.plusHours(2), "First TestMessage"); + InputMessage fourthMessage = new InputMessage("Me", "User", currentTime.plusHours(3), "First TestMessage"); + InputMessage fifthMessage = new InputMessage("Me", "User", currentTime.plusHours(25), "First TestMessage"); + InputMessage sixthMessage = new InputMessage("Me", "User", currentTime.plusHours(26), "First TestMessage"); + + List firstBackupMessages = Arrays.asList(message, secondMessage, thirdMessage, fourthMessage); + List secondBackupMessages = Arrays.asList(fifthMessage, sixthMessage); + List inputMessages = ListUtils.union(firstBackupMessages, secondBackupMessages); + + StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); + env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime); + env.setParallelism(1); + DataStreamSource testDataSet = env.fromCollection(inputMessages); + CollectingSink sink = new CollectingSink(); + testDataSet.assignTimestampsAndWatermarks(new InputMessageTimestampAssigner()) + .timeWindowAll(Time.hours(24)) + .aggregate(new BackupAggregator()) + .addSink(sink); + + env.execute(); + + Awaitility.await().until(() -> sink.backups.size() == 2); + assertEquals(2, sink.backups.size()); + assertEquals(firstBackupMessages, sink.backups.get(0).getInputMessages()); + assertEquals(secondBackupMessages, sink.backups.get(1).getInputMessages()); + + } + + @Test + public void givenProperBackupObject_whenSerializeIsInvoked_thenObjectIsProperlySerialized() throws IOException { + InputMessage message = new InputMessage("Me", "User", LocalDateTime.now(), "Test Message"); + List messages = Arrays.asList(message); + Backup backup = new Backup(messages, LocalDateTime.now()); + byte[] backupSerialized = mapper.writeValueAsBytes(backup); + SerializationSchema serializationSchema = new BackupSerializationSchema(); + byte[] backupProcessed = serializationSchema.serialize(backup); + + assertArrayEquals(backupSerialized, backupProcessed); + } + + private static class CollectingSink implements SinkFunction { + + public static List backups = new ArrayList<>(); + + @Override + public synchronized void invoke(Backup value, Context context) throws Exception { + backups.add(value); + } + } +} diff --git a/libraries-data-2/src/test/com/baeldung/flink/WordCapitalizerIntegrationTest.java b/libraries-data-2/src/test/com/baeldung/flink/WordCapitalizerIntegrationTest.java new file mode 100644 index 0000000000..8a98dae4b5 --- /dev/null +++ b/libraries-data-2/src/test/com/baeldung/flink/WordCapitalizerIntegrationTest.java @@ -0,0 +1,34 @@ +package com.baeldung.flink; + +import com.baeldung.flink.operator.WordsCapitalizer; +import org.apache.flink.api.java.DataSet; +import org.apache.flink.api.java.ExecutionEnvironment; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class WordCapitalizerIntegrationTest { + + @Test + public void givenDataSet_whenExecuteWordCapitalizer_thenReturnCapitalizedWords() throws Exception { + ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment(); + List data = Arrays.asList("dog", "cat", "wolf", "pig"); + + DataSet testDataSet = env.fromCollection(data); + + + List dataProcessed = testDataSet + .map(new WordsCapitalizer()) + .collect(); + + List testDataCapitalized = data.stream() + .map(String::toUpperCase) + .collect(Collectors.toList()); + + Assert.assertEquals(testDataCapitalized, dataProcessed); + } + +} diff --git a/libraries-data-2/src/test/com/baeldung/flink/WordCountIntegrationTest.java b/libraries-data-2/src/test/com/baeldung/flink/WordCountIntegrationTest.java new file mode 100644 index 0000000000..5c788e86d6 --- /dev/null +++ b/libraries-data-2/src/test/com/baeldung/flink/WordCountIntegrationTest.java @@ -0,0 +1,161 @@ +package com.baeldung.flink; + +import org.apache.flink.api.common.operators.Order; +import org.apache.flink.api.java.DataSet; +import org.apache.flink.api.java.ExecutionEnvironment; +import org.apache.flink.api.java.functions.KeySelector; +import org.apache.flink.api.java.tuple.Tuple2; +import org.apache.flink.api.java.tuple.Tuple3; +import org.apache.flink.streaming.api.datastream.DataStream; +import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator; +import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; +import org.apache.flink.streaming.api.functions.timestamps.BoundedOutOfOrdernessTimestampExtractor; +import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows; +import org.apache.flink.streaming.api.windowing.time.Time; +import org.junit.Test; + +import java.time.ZonedDateTime; +import java.util.Arrays; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +public class WordCountIntegrationTest { + private final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment(); + + @Test + public void givenDataSet_whenExecuteWordCount_thenReturnWordCount() throws Exception { + // given + List lines = Arrays.asList("This is a first sentence", "This is a second sentence with a one word"); + + // when + DataSet> result = WordCount.startWordCount(env, lines); + + // then + List> collect = result.collect(); + assertThat(collect).containsExactlyInAnyOrder(new Tuple2<>("a", 3), new Tuple2<>("sentence", 2), new Tuple2<>("word", 1), new Tuple2<>("is", 2), new Tuple2<>("this", 2), new Tuple2<>("second", 1), new Tuple2<>("first", 1), new Tuple2<>("with", 1), + new Tuple2<>("one", 1)); + } + + @Test + public void givenListOfAmounts_whenUseMapReduce_thenSumAmountsThatAreOnlyAboveThreshold() throws Exception { + // given + DataSet amounts = env.fromElements(1, 29, 40, 50); + int threshold = 30; + + // when + List collect = amounts.filter(a -> a > threshold).reduce((integer, t1) -> integer + t1).collect(); + + // then + assertThat(collect.get(0)).isEqualTo(90); + } + + @Test + public void givenDataSetOfComplexObjects_whenMapToGetOneField_thenReturnedListHaveProperElements() throws Exception { + // given + DataSet personDataSource = env.fromCollection(Arrays.asList(new Person(23, "Tom"), new Person(75, "Michael"))); + + // when + List ages = personDataSource.map(p -> p.age).collect(); + + // then + assertThat(ages).hasSize(2); + assertThat(ages).contains(23, 75); + + } + + @Test + public void givenDataSet_whenSortItByOneField_thenShouldReturnSortedDataSet() throws Exception { + // given + Tuple2 secondPerson = new Tuple2<>(4, "Tom"); + Tuple2 thirdPerson = new Tuple2<>(5, "Scott"); + Tuple2 fourthPerson = new Tuple2<>(200, "Michael"); + Tuple2 firstPerson = new Tuple2<>(1, "Jack"); + DataSet> transactions = env.fromElements(fourthPerson, secondPerson, thirdPerson, firstPerson); + + // when + List> sorted = transactions.sortPartition(new IdKeySelectorTransaction(), Order.ASCENDING).collect(); + + // then + assertThat(sorted).containsExactly(firstPerson, secondPerson, thirdPerson, fourthPerson); + } + + @Test + public void giveTwoDataSets_whenJoinUsingId_thenProduceJoinedData() throws Exception { + // given + Tuple3 address = new Tuple3<>(1, "5th Avenue", "London"); + DataSet> addresses = env.fromElements(address); + + Tuple2 firstTransaction = new Tuple2<>(1, "Transaction_1"); + DataSet> transactions = env.fromElements(firstTransaction, new Tuple2<>(12, "Transaction_2")); + + // when + List, Tuple3>> joined = transactions.join(addresses).where(new IdKeySelectorTransaction()).equalTo(new IdKeySelectorAddress()).collect(); + + // then + assertThat(joined).hasSize(1); + assertThat(joined).contains(new Tuple2<>(firstTransaction, address)); + + } + + @Test + public void givenStreamOfEvents_whenProcessEvents_thenShouldPrintResultsOnSinkOperation() throws Exception { + // given + final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); + + DataStream text = env.fromElements("This is a first sentence", "This is a second sentence with a one word"); + + SingleOutputStreamOperator upperCase = text.map(String::toUpperCase); + + upperCase.print(); + + // when + env.execute(); + } + + @Test + public void givenStreamOfEvents_whenProcessEvents_thenShouldApplyWindowingOnTransformation() throws Exception { + // given + final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); + + SingleOutputStreamOperator> windowed = env.fromElements(new Tuple2<>(16, ZonedDateTime.now().plusMinutes(25).toInstant().getEpochSecond()), new Tuple2<>(15, ZonedDateTime.now().plusMinutes(2).toInstant().getEpochSecond())) + .assignTimestampsAndWatermarks(new BoundedOutOfOrdernessTimestampExtractor>(Time.seconds(20)) { + @Override + public long extractTimestamp(Tuple2 element) { + return element.f1 * 1000; + } + }); + + SingleOutputStreamOperator> reduced = windowed.windowAll(TumblingEventTimeWindows.of(Time.seconds(5))).maxBy(0, true); + + reduced.print(); + + // when + env.execute(); + } + + private static class IdKeySelectorTransaction implements KeySelector, Integer> { + @Override + public Integer getKey(Tuple2 value) { + return value.f0; + } + } + + private static class IdKeySelectorAddress implements KeySelector, Integer> { + @Override + public Integer getKey(Tuple3 value) { + return value.f0; + } + } + + private static class Person { + private final int age; + private final String name; + + private Person(int age, String name) { + this.age = age; + this.name = name; + } + } + +} \ No newline at end of file diff --git a/libraries-data-2/src/test/com/baeldung/google/sheets/GoogleSheetsLiveTest.java b/libraries-data-2/src/test/com/baeldung/google/sheets/GoogleSheetsLiveTest.java new file mode 100644 index 0000000000..358b3390f9 --- /dev/null +++ b/libraries-data-2/src/test/com/baeldung/google/sheets/GoogleSheetsLiveTest.java @@ -0,0 +1,96 @@ +package com.baeldung.google.sheets; + +import java.io.IOException; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.junit.BeforeClass; +import org.junit.Test; + +import com.google.api.services.sheets.v4.Sheets; +import com.google.api.services.sheets.v4.model.AppendValuesResponse; +import com.google.api.services.sheets.v4.model.BatchGetValuesResponse; +import com.google.api.services.sheets.v4.model.BatchUpdateSpreadsheetRequest; +import com.google.api.services.sheets.v4.model.BatchUpdateValuesRequest; +import com.google.api.services.sheets.v4.model.BatchUpdateValuesResponse; +import com.google.api.services.sheets.v4.model.CopyPasteRequest; +import com.google.api.services.sheets.v4.model.GridRange; +import com.google.api.services.sheets.v4.model.Request; +import com.google.api.services.sheets.v4.model.Spreadsheet; +import com.google.api.services.sheets.v4.model.SpreadsheetProperties; +import com.google.api.services.sheets.v4.model.UpdateSpreadsheetPropertiesRequest; +import com.google.api.services.sheets.v4.model.UpdateValuesResponse; +import com.google.api.services.sheets.v4.model.ValueRange; + +import static org.assertj.core.api.Assertions.*; + +public class GoogleSheetsLiveTest { + + private static Sheets sheetsService; + + // this id can be replaced with your spreadsheet id + // otherwise be advised that multiple people may run this test and update the public spreadsheet + private static final String SPREADSHEET_ID = "1sILuxZUnyl_7-MlNThjt765oWshN3Xs-PPLfqYe4DhI"; + + @BeforeClass + public static void setup() throws GeneralSecurityException, IOException { + sheetsService = SheetsServiceUtil.getSheetsService(); + } + + @Test + public void whenWriteSheet_thenReadSheetOk() throws IOException { + ValueRange body = new ValueRange().setValues(Arrays.asList(Arrays.asList("Expenses January"), Arrays.asList("books", "30"), Arrays.asList("pens", "10"), Arrays.asList("Expenses February"), Arrays.asList("clothes", "20"), Arrays.asList("shoes", "5"))); + UpdateValuesResponse result = sheetsService.spreadsheets().values().update(SPREADSHEET_ID, "A1", body).setValueInputOption("RAW").execute(); + + List data = new ArrayList<>(); + data.add(new ValueRange().setRange("D1").setValues(Arrays.asList(Arrays.asList("January Total", "=B2+B3")))); + data.add(new ValueRange().setRange("D4").setValues(Arrays.asList(Arrays.asList("February Total", "=B5+B6")))); + + BatchUpdateValuesRequest batchBody = new BatchUpdateValuesRequest().setValueInputOption("USER_ENTERED").setData(data); + BatchUpdateValuesResponse batchResult = sheetsService.spreadsheets().values().batchUpdate(SPREADSHEET_ID, batchBody).execute(); + + List ranges = Arrays.asList("E1", "E4"); + BatchGetValuesResponse readResult = sheetsService.spreadsheets().values().batchGet(SPREADSHEET_ID).setRanges(ranges).execute(); + + ValueRange januaryTotal = readResult.getValueRanges().get(0); + assertThat(januaryTotal.getValues().get(0).get(0)).isEqualTo("40"); + + ValueRange febTotal = readResult.getValueRanges().get(1); + assertThat(febTotal.getValues().get(0).get(0)).isEqualTo("25"); + + ValueRange appendBody = new ValueRange().setValues(Arrays.asList(Arrays.asList("Total", "=E1+E4"))); + AppendValuesResponse appendResult = sheetsService.spreadsheets().values().append(SPREADSHEET_ID, "A1", appendBody).setValueInputOption("USER_ENTERED").setInsertDataOption("INSERT_ROWS").setIncludeValuesInResponse(true).execute(); + + ValueRange total = appendResult.getUpdates().getUpdatedData(); + assertThat(total.getValues().get(0).get(1)).isEqualTo("65"); + } + + @Test + public void whenUpdateSpreadSheetTitle_thenOk() throws IOException { + + UpdateSpreadsheetPropertiesRequest updateRequest = new UpdateSpreadsheetPropertiesRequest().setFields("*").setProperties(new SpreadsheetProperties().setTitle("Expenses")); + + CopyPasteRequest copyRequest = new CopyPasteRequest().setSource(new GridRange().setSheetId(0).setStartColumnIndex(0).setEndColumnIndex(2).setStartRowIndex(0).setEndRowIndex(1)) + .setDestination(new GridRange().setSheetId(1).setStartColumnIndex(0).setEndColumnIndex(2).setStartRowIndex(0).setEndRowIndex(1)).setPasteType("PASTE_VALUES"); + + List requests = new ArrayList<>(); + + requests.add(new Request().setCopyPaste(copyRequest)); + requests.add(new Request().setUpdateSpreadsheetProperties(updateRequest)); + + BatchUpdateSpreadsheetRequest body = new BatchUpdateSpreadsheetRequest().setRequests(requests); + + sheetsService.spreadsheets().batchUpdate(SPREADSHEET_ID, body).execute(); + } + + @Test + public void whenCreateSpreadSheet_thenIdOk() throws IOException { + Spreadsheet spreadSheet = new Spreadsheet().setProperties(new SpreadsheetProperties().setTitle("My Spreadsheet")); + Spreadsheet result = sheetsService.spreadsheets().create(spreadSheet).execute(); + + assertThat(result.getSpreadsheetId()).isNotNull(); + } + +} diff --git a/libraries-data-2/src/test/com/baeldung/hll/HLLLongRunningManualTest.java b/libraries-data-2/src/test/com/baeldung/hll/HLLLongRunningManualTest.java new file mode 100644 index 0000000000..2c8113771e --- /dev/null +++ b/libraries-data-2/src/test/com/baeldung/hll/HLLLongRunningManualTest.java @@ -0,0 +1,59 @@ +package com.baeldung.hll; + +import com.google.common.hash.HashFunction; +import com.google.common.hash.Hashing; +import net.agkn.hll.HLL; +import org.assertj.core.data.Offset; +import org.junit.Test; + +import java.util.stream.LongStream; + +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; + +public class HLLLongRunningManualTest { + + @Test + public void givenHLL_whenAddHugeAmountOfNumbers_thenShouldReturnEstimatedCardinality() { + // given + long numberOfElements = 100_000_000; + long toleratedDifference = 1_000_000; + HashFunction hashFunction = Hashing.murmur3_128(); + HLL hll = new HLL(14, 5); + + // when + LongStream.range(0, numberOfElements).forEach(element -> { + long hashedValue = hashFunction.newHasher().putLong(element).hash().asLong(); + hll.addRaw(hashedValue); + }); + + // then + long cardinality = hll.cardinality(); + assertThat(cardinality).isCloseTo(numberOfElements, Offset.offset(toleratedDifference)); + } + + @Test + public void givenTwoHLLs_whenAddHugeAmountOfNumbers_thenShouldReturnEstimatedCardinalityForUnionOfHLLs() { + // given + long numberOfElements = 100_000_000; + long toleratedDifference = 1_000_000; + HashFunction hashFunction = Hashing.murmur3_128(); + HLL firstHll = new HLL(15, 5); + HLL secondHLL = new HLL(15, 5); + + // when + LongStream.range(0, numberOfElements).forEach(element -> { + long hashedValue = hashFunction.newHasher().putLong(element).hash().asLong(); + firstHll.addRaw(hashedValue); + }); + + LongStream.range(numberOfElements, numberOfElements * 2).forEach(element -> { + long hashedValue = hashFunction.newHasher().putLong(element).hash().asLong(); + secondHLL.addRaw(hashedValue); + }); + + // then + firstHll.union(secondHLL); + long cardinality = firstHll.cardinality(); + assertThat(cardinality).isCloseTo(numberOfElements * 2, Offset.offset(toleratedDifference * 2)); + } +} diff --git a/libraries-data-2/src/test/com/baeldung/infinispan/AbstractIntegrationTest.java b/libraries-data-2/src/test/com/baeldung/infinispan/AbstractIntegrationTest.java new file mode 100644 index 0000000000..2a9d430430 --- /dev/null +++ b/libraries-data-2/src/test/com/baeldung/infinispan/AbstractIntegrationTest.java @@ -0,0 +1,57 @@ +package com.baeldung.infinispan; + +import com.baeldung.infinispan.listener.CacheListener; +import com.baeldung.infinispan.repository.HelloWorldRepository; +import com.baeldung.infinispan.service.HelloWorldService; +import com.baeldung.infinispan.service.TransactionalService; +import org.infinispan.Cache; +import org.infinispan.manager.DefaultCacheManager; +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; + +import java.util.function.Supplier; + +@Ignore +public abstract class AbstractIntegrationTest { + + private DefaultCacheManager cacheManager; + + private HelloWorldRepository repository = new HelloWorldRepository(); + + protected HelloWorldService helloWorldService; + protected TransactionalService transactionalService; + + @Before + public void setup() { + CacheConfiguration configuration = new CacheConfiguration(); + CacheListener listener = new CacheListener(); + + cacheManager = configuration.cacheManager(); + + Cache transactionalCache = configuration.transactionalCache(cacheManager, listener); + + Cache simpleHelloWorldCache = configuration.simpleHelloWorldCache(cacheManager, listener); + + Cache expiringHelloWorldCache = configuration.expiringHelloWorldCache(cacheManager, listener); + + Cache evictingHelloWorldCache = configuration.evictingHelloWorldCache(cacheManager, listener); + + Cache passivatingHelloWorldCache = configuration.passivatingHelloWorldCache(cacheManager, listener); + + this.helloWorldService = new HelloWorldService(repository, listener, simpleHelloWorldCache, expiringHelloWorldCache, evictingHelloWorldCache, passivatingHelloWorldCache); + + this.transactionalService = new TransactionalService(transactionalCache); + } + + @After + public void tearDown() { + cacheManager.stop(); + } + + protected long timeThis(Supplier supplier) { + long millis = System.currentTimeMillis(); + supplier.get(); + return System.currentTimeMillis() - millis; + } +} diff --git a/libraries-data-2/src/test/com/baeldung/infinispan/service/HelloWorldServiceTemporaryLiveTest.java b/libraries-data-2/src/test/com/baeldung/infinispan/service/HelloWorldServiceTemporaryLiveTest.java new file mode 100644 index 0000000000..46cc77cbba --- /dev/null +++ b/libraries-data-2/src/test/com/baeldung/infinispan/service/HelloWorldServiceTemporaryLiveTest.java @@ -0,0 +1,51 @@ +package com.baeldung.infinispan.service; + +import com.baeldung.infinispan.AbstractIntegrationTest; +import org.junit.Test; + +import static org.assertj.core.api.Java6Assertions.assertThat; + +public class HelloWorldServiceTemporaryLiveTest extends AbstractIntegrationTest { + + @Test + public void whenGetIsCalledTwoTimes_thenTheSecondShouldHitTheCache() { + assertThat(timeThis(() -> helloWorldService.findSimpleHelloWorld())).isGreaterThanOrEqualTo(1000); + + assertThat(timeThis(() -> helloWorldService.findSimpleHelloWorld())).isLessThan(100); + } + + @Test + public void whenGetIsCalledTwoTimesQuickly_thenTheSecondShouldHitTheCache() { + assertThat(timeThis(() -> helloWorldService.findExpiringHelloWorld())).isGreaterThanOrEqualTo(1000); + + assertThat(timeThis(() -> helloWorldService.findExpiringHelloWorld())).isLessThan(100); + } + + @Test + public void whenGetIsCalledTwoTimesSparsely_thenNeitherShouldHitTheCache() throws InterruptedException { + assertThat(timeThis(() -> helloWorldService.findExpiringHelloWorld())).isGreaterThanOrEqualTo(1000); + + Thread.sleep(1100); + + assertThat(timeThis(() -> helloWorldService.findExpiringHelloWorld())).isGreaterThanOrEqualTo(1000); + } + + @Test + public void givenOneEntryIsConfigured_whenTwoAreAdded_thenFirstShouldntBeAvailable() { + assertThat(timeThis(() -> helloWorldService.findEvictingHelloWorld("key 1"))).isGreaterThanOrEqualTo(1000); + + assertThat(timeThis(() -> helloWorldService.findEvictingHelloWorld("key 2"))).isGreaterThanOrEqualTo(1000); + + assertThat(timeThis(() -> helloWorldService.findEvictingHelloWorld("key 1"))).isGreaterThanOrEqualTo(1000); + } + + @Test + public void givenOneEntryIsConfigured_whenTwoAreAdded_thenTheFirstShouldBeAvailable() { + assertThat(timeThis(() -> helloWorldService.findPassivatingHelloWorld("key 1"))).isGreaterThanOrEqualTo(1000); + + assertThat(timeThis(() -> helloWorldService.findPassivatingHelloWorld("key 2"))).isGreaterThanOrEqualTo(1000); + + assertThat(timeThis(() -> helloWorldService.findPassivatingHelloWorld("key 1"))).isLessThan(100); + } + +} diff --git a/libraries-data-2/src/test/com/baeldung/infinispan/service/TransactionalServiceIntegrationTest.java b/libraries-data-2/src/test/com/baeldung/infinispan/service/TransactionalServiceIntegrationTest.java new file mode 100644 index 0000000000..800c6c2775 --- /dev/null +++ b/libraries-data-2/src/test/com/baeldung/infinispan/service/TransactionalServiceIntegrationTest.java @@ -0,0 +1,21 @@ +package com.baeldung.infinispan.service; + +import com.baeldung.infinispan.AbstractIntegrationTest; +import org.junit.Test; + +import static org.assertj.core.api.Java6Assertions.assertThat; + +public class TransactionalServiceIntegrationTest extends AbstractIntegrationTest { + + @Test + public void whenLockingAnEntry_thenItShouldBeInaccessible() throws InterruptedException { + Runnable backGroundJob = () -> transactionalService.startBackgroundBatch(); + Thread backgroundThread = new Thread(backGroundJob); + transactionalService.getQuickHowManyVisits(); + backgroundThread.start(); + Thread.sleep(100); // lets wait our thread warm up + + assertThat(timeThis(() -> transactionalService.getQuickHowManyVisits())).isGreaterThan(500).isLessThan(1000); + } + +} diff --git a/libraries-data-2/src/test/com/baeldung/measurement/WaterTankUnitTest.java b/libraries-data-2/src/test/com/baeldung/measurement/WaterTankUnitTest.java new file mode 100644 index 0000000000..e23eea1714 --- /dev/null +++ b/libraries-data-2/src/test/com/baeldung/measurement/WaterTankUnitTest.java @@ -0,0 +1,86 @@ +package com.baeldung.measurement; + +import javax.measure.Quantity; +import javax.measure.quantity.Area; +import javax.measure.quantity.Length; +import javax.measure.quantity.Pressure; +import javax.measure.quantity.Volume; + +import javax.measure.Unit; +import javax.measure.UnitConverter; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import org.junit.Test; + +import com.baeldung.measurement.WaterTank; + +import tec.units.ri.format.SimpleUnitFormat; +import tec.units.ri.quantity.Quantities; +import tec.units.ri.unit.MetricPrefix; +import static tec.units.ri.unit.Units.*; + +public class WaterTankUnitTest { + + @Test + public void givenQuantity_whenGetUnitAndConvertValue_thenSuccess() { + WaterTank waterTank = new WaterTank(); + waterTank.setCapacityMeasure(Quantities.getQuantity(9.2, LITRE)); + assertEquals(LITRE, waterTank.getCapacityMeasure().getUnit()); + + Quantity waterCapacity = waterTank.getCapacityMeasure(); + double volumeInLitre = waterCapacity.getValue().doubleValue(); + assertEquals(9.2, volumeInLitre, 0.0f); + + double volumeInMilliLitre = waterCapacity.to(MetricPrefix.MILLI(LITRE)).getValue().doubleValue(); + assertEquals(9200.0, volumeInMilliLitre, 0.0f); + + // compilation error + // volumeInMilliLitre = waterCapacity.to(MetricPrefix.MILLI(KILOGRAM)); + + Unit Kilometer = MetricPrefix.KILO(METRE); + + // compilation error + // Unit Centimeter = MetricPrefix.CENTI(LITRE); + } + + @Test + public void givenUnit_whenAlternateUnit_ThenGetAlternateUnit() { + + Unit PASCAL = NEWTON.divide(METRE.pow(2)).alternate("Pa").asType(Pressure.class); + assertTrue(SimpleUnitFormat.getInstance().parse("Pa").equals(PASCAL)); + } + + @Test + public void givenUnit_whenProduct_ThenGetProductUnit() { + Unit squareMetre = METRE.multiply(METRE).asType(Area.class); + Quantity line = Quantities.getQuantity(2, METRE); + assertEquals(line.multiply(line).getUnit(), squareMetre); + } + + @Test + public void givenMeters_whenConvertToKilometer_ThenConverted() { + double distanceInMeters = 50.0; + UnitConverter metreToKilometre = METRE.getConverterTo(MetricPrefix.KILO(METRE)); + double distanceInKilometers = metreToKilometre.convert(distanceInMeters); + assertEquals(0.05, distanceInKilometers, 0.00f); + } + + @Test + public void givenSymbol_WhenCompareToSystemUnit_ThenSuccess() { + assertTrue(SimpleUnitFormat.getInstance().parse("kW").equals(MetricPrefix.KILO(WATT))); + assertTrue(SimpleUnitFormat.getInstance().parse("ms").equals(SECOND.divide(1000))); + } + + @Test + public void givenUnits_WhenAdd_ThenSuccess() { + Quantity total = Quantities.getQuantity(2, METRE).add(Quantities.getQuantity(3, METRE)); + assertEquals(total.getValue().intValue(), 5); + + // compilation error + // Quantity total = Quantities.getQuantity(2, METRE).add(Quantities.getQuantity(3, LITRE)); + + Quantity totalKm = Quantities.getQuantity(2, METRE).add(Quantities.getQuantity(3, MetricPrefix.KILO(METRE))); + assertEquals(totalKm.getValue().intValue(), 3002); + } +} diff --git a/libraries-data-2/src/test/com/baeldung/opencsv/OpenCsvIntegrationTest.java b/libraries-data-2/src/test/com/baeldung/opencsv/OpenCsvIntegrationTest.java new file mode 100644 index 0000000000..fca96684ad --- /dev/null +++ b/libraries-data-2/src/test/com/baeldung/opencsv/OpenCsvIntegrationTest.java @@ -0,0 +1,66 @@ +package com.baeldung.opencsv; + +import com.baeldung.opencsv.helpers.Helpers; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class OpenCsvIntegrationTest { + + private Object testReadCsv(Object result) { + assert (result != null); + assert (result instanceof String); + assert (!((String) result).isEmpty()); + System.out.println(result); + return result; + } + + private Object testWriteCsv(Object result) { + assert (result instanceof String); + assert (!((String) result).isEmpty()); + return result; + } + + @Before + public void setup() { + } + + @Test + public void positionExampleTest() { + testReadCsv(Application.simpleSyncPositionBeanExample()); + } + + @Test + public void namedColumnExampleTest() { + testReadCsv(Application.namedSyncColumnBeanExample()); + } + + @Test + public void writeCsvUsingBeanBuilderTest() { + testWriteCsv(Application.writeSyncCsvFromBeanExample()); + } + + @Test + public void oneByOneExampleTest() { + testReadCsv(Application.oneByOneSyncExample()); + } + + @Test + public void readAllExampleTest() { + testReadCsv(Application.readAllSyncExample()); + } + + @Test + public void csvWriterOneByOneTest() { + testWriteCsv(Application.csvWriterSyncOneByOne()); + } + + @Test + public void csvWriterAllTest() { + testWriteCsv(Application.csvWriterSyncAll()); + } + + @After + public void close() { + } +} \ No newline at end of file diff --git a/libraries-data-2/src/test/com/baeldung/smooks/converter/SmooksIntegrationTest.java b/libraries-data-2/src/test/com/baeldung/smooks/converter/SmooksIntegrationTest.java new file mode 100644 index 0000000000..df7fea58f8 --- /dev/null +++ b/libraries-data-2/src/test/com/baeldung/smooks/converter/SmooksIntegrationTest.java @@ -0,0 +1,60 @@ +package com.baeldung.smooks.converter; + +import com.baeldung.smooks.model.Item; +import com.baeldung.smooks.model.Order; +import com.baeldung.smooks.model.Status; +import com.baeldung.smooks.model.Supplier; +import org.junit.Test; +import org.milyn.validation.ValidationResult; +import java.text.SimpleDateFormat; +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; + +public class SmooksIntegrationTest { + + private static final String EDIFACT_MESSAGE = "UNA:+.? '" + System.lineSeparator() + "UNH+771+IN_PROGRESS+2018-01-14'" + System.lineSeparator() + "CTA+CompanyX+1234567'" + System.lineSeparator() + "LIN+1+PX1234+9.99'" + System.lineSeparator() + + "LIN+2+RX990+120.32'" + System.lineSeparator(); + private static final String EMAIL_MESSAGE = "Hi," + System.lineSeparator() + "Order number #771 created on 2018-01-14 is currently in IN_PROGRESS status." + System.lineSeparator() + "Consider contact supplier \"CompanyX\" with phone number: \"1234567\"." + + System.lineSeparator() + "Order items:" + System.lineSeparator() + "1 X PX1234 (total price 9.99)" + System.lineSeparator() + "2 X RX990 (total price 240.64)" + System.lineSeparator(); + + @Test + public void givenOrderXML_whenConvert_thenPOJOsConstructedCorrectly() throws Exception { + + OrderConverter xmlToJavaOrderConverter = new OrderConverter(); + Order order = xmlToJavaOrderConverter.convertOrderXMLToOrderObject("/smooks/order.xml"); + + assertThat(order.getNumber(), is(771L)); + assertThat(order.getStatus(), is(Status.IN_PROGRESS)); + assertThat(order.getCreationDate(), is(new SimpleDateFormat("yyyy-MM-dd").parse("2018-01-14"))); + assertThat(order.getSupplier(), is(new Supplier("CompanyX", "1234567"))); + assertThat(order.getItems(), containsInAnyOrder(new Item("PX1234", 9.99, 1), new Item("RX990", 120.32, 2))); + + } + + @Test + public void givenIncorrectOrderXML_whenValidate_thenExpectValidationErrors() throws Exception { + OrderValidator orderValidator = new OrderValidator(); + ValidationResult validationResult = orderValidator.validate("/smooks/order.xml"); + + assertThat(validationResult.getErrors(), hasSize(1)); + // 1234567 didn't match ^[0-9\\-\\+]{9,15}$ + assertThat(validationResult.getErrors() + .get(0) + .getFailRuleResult() + .getRuleName(), is("supplierPhone")); + } + + @Test + public void givenOrderXML_whenApplyEDITemplate_thenConvertedToEDIFACT() throws Exception { + OrderConverter orderConverter = new OrderConverter(); + String edifact = orderConverter.convertOrderXMLtoEDIFACT("/smooks/order.xml"); + assertThat(edifact, is(EDIFACT_MESSAGE)); + } + + @Test + public void givenOrderXML_whenApplyEmailTemplate_thenConvertedToEmailMessage() throws Exception { + OrderConverter orderConverter = new OrderConverter(); + String emailMessage = orderConverter.convertOrderXMLtoEmailMessage("/smooks/order.xml"); + assertThat(emailMessage, is(EMAIL_MESSAGE)); + } +} \ No newline at end of file