add libraries server project
This commit is contained in:
9
libraries-server/.gitignore
vendored
Normal file
9
libraries-server/.gitignore
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
*.class
|
||||
|
||||
# Folders #
|
||||
/gensrc
|
||||
/target
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
/bin/
|
||||
19
libraries-server/pom.xml
Normal file
19
libraries-server/pom.xml
Normal file
@@ -0,0 +1,19 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>libraries-server</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.paho</groupId>
|
||||
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
|
||||
<version>1.2.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.baeldung.mqtt;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import org.eclipse.paho.client.mqttv3.IMqttClient;
|
||||
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class EngineTemperatureSensor implements Callable<Void> {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(EngineTemperatureSensor.class);
|
||||
public static final String TOPIC = "engine/temperature";
|
||||
|
||||
private IMqttClient client;
|
||||
private Random rnd = new Random();
|
||||
|
||||
public EngineTemperatureSensor(IMqttClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void call() throws Exception {
|
||||
|
||||
if ( !client.isConnected()) {
|
||||
log.info("[I31] Client not connected.");
|
||||
return null;
|
||||
}
|
||||
|
||||
MqttMessage msg = readEngineTemp();
|
||||
msg.setQos(0);
|
||||
msg.setRetained(true);
|
||||
client.publish(TOPIC,msg);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method simulates reading the engine temperature
|
||||
* @return
|
||||
*/
|
||||
private MqttMessage readEngineTemp() {
|
||||
double temp = 80 + rnd.nextDouble() * 20.0;
|
||||
byte[] payload = String.format("T:%04.2f",temp).getBytes();
|
||||
MqttMessage msg = new MqttMessage(payload);
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package com.baeldung.mqtt;
|
||||
|
||||
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.eclipse.paho.client.mqttv3.MqttClient;
|
||||
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class EngineTemperatureSensorLiveTest {
|
||||
|
||||
private static Logger log = LoggerFactory.getLogger(EngineTemperatureSensorLiveTest.class);
|
||||
|
||||
@Test
|
||||
public void whenSendSingleMessage_thenSuccess() throws Exception {
|
||||
|
||||
String publisherId = UUID.randomUUID().toString();
|
||||
MqttClient publisher = new MqttClient("tcp://iot.eclipse.org:1883",publisherId);
|
||||
|
||||
String subscriberId = UUID.randomUUID().toString();
|
||||
MqttClient subscriber = new MqttClient("tcp://iot.eclipse.org:1883",subscriberId);
|
||||
|
||||
MqttConnectOptions options = new MqttConnectOptions();
|
||||
options.setAutomaticReconnect(true);
|
||||
options.setCleanSession(true);
|
||||
options.setConnectionTimeout(10);
|
||||
|
||||
|
||||
subscriber.connect(options);
|
||||
publisher.connect(options);
|
||||
|
||||
CountDownLatch receivedSignal = new CountDownLatch(1);
|
||||
|
||||
subscriber.subscribe(EngineTemperatureSensor.TOPIC, (topic, msg) -> {
|
||||
byte[] payload = msg.getPayload();
|
||||
log.info("[I46] Message received: topic={}, payload={}", topic, new String(payload));
|
||||
receivedSignal.countDown();
|
||||
});
|
||||
|
||||
|
||||
Callable<Void> target = new EngineTemperatureSensor(publisher);
|
||||
target.call();
|
||||
|
||||
receivedSignal.await(1, TimeUnit.MINUTES);
|
||||
|
||||
log.info("[I56] Success !");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSendMultipleMessages_thenSuccess() throws Exception {
|
||||
|
||||
String publisherId = UUID.randomUUID().toString();
|
||||
MqttClient publisher = new MqttClient("tcp://iot.eclipse.org:1883",publisherId);
|
||||
|
||||
String subscriberId = UUID.randomUUID().toString();
|
||||
MqttClient subscriber = new MqttClient("tcp://iot.eclipse.org:1883",subscriberId);
|
||||
|
||||
|
||||
MqttConnectOptions options = new MqttConnectOptions();
|
||||
options.setAutomaticReconnect(true);
|
||||
options.setCleanSession(true);
|
||||
options.setConnectionTimeout(10);
|
||||
|
||||
|
||||
publisher.connect(options);
|
||||
subscriber.connect(options);
|
||||
|
||||
CountDownLatch receivedSignal = new CountDownLatch(10);
|
||||
|
||||
subscriber.subscribe(EngineTemperatureSensor.TOPIC, (topic, msg) -> {
|
||||
byte[] payload = msg.getPayload();
|
||||
log.info("[I82] Message received: topic={}, payload={}", topic, new String(payload));
|
||||
receivedSignal.countDown();
|
||||
});
|
||||
|
||||
|
||||
Callable<Void> target = new EngineTemperatureSensor(publisher);
|
||||
|
||||
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
|
||||
executor.scheduleAtFixedRate(() -> {
|
||||
try {
|
||||
target.call();
|
||||
}
|
||||
catch(Exception ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}, 1, 1, TimeUnit.SECONDS);
|
||||
|
||||
|
||||
receivedSignal.await(1, TimeUnit.MINUTES);
|
||||
executor.shutdown();
|
||||
|
||||
assertTrue(receivedSignal.getCount() == 0 , "Countdown should be zero");
|
||||
|
||||
log.info("[I105] Success !");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
1
libraries-server/src/test/resources/ABC.txt
Normal file
1
libraries-server/src/test/resources/ABC.txt
Normal file
@@ -0,0 +1 @@
|
||||
Hello World from ABC.txt!!!
|
||||
@@ -0,0 +1,4 @@
|
||||
1,2,3
|
||||
-10, 30, 20
|
||||
15, -5, 10
|
||||
-5, -10, -15
|
||||
|
1
libraries-server/src/test/resources/aaa.txt
Normal file
1
libraries-server/src/test/resources/aaa.txt
Normal file
@@ -0,0 +1 @@
|
||||
Hello World from aaa.txt!!!
|
||||
13
libraries-server/src/test/resources/adder-beans.xml
Normal file
13
libraries-server/src/test/resources/adder-beans.xml
Normal file
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
|
||||
|
||||
<util:properties id="props">
|
||||
<prop key="adder">
|
||||
4
|
||||
</prop>
|
||||
</util:properties>
|
||||
|
||||
</beans>
|
||||
|
||||
3
libraries-server/src/test/resources/book.csv
Normal file
3
libraries-server/src/test/resources/book.csv
Normal file
@@ -0,0 +1,3 @@
|
||||
author,title
|
||||
Dan Simmons,Hyperion
|
||||
Douglas Adams,The Hitchhiker's Guide to the Galaxy
|
||||
|
5
libraries-server/src/test/resources/csv/fourColumn.csv
Normal file
5
libraries-server/src/test/resources/csv/fourColumn.csv
Normal file
@@ -0,0 +1,5 @@
|
||||
ColA,ColB,ColC,ColD
|
||||
A,B,B,B
|
||||
C,D,W,W
|
||||
G,G,E,E
|
||||
G,F,Q,Q
|
||||
|
5
libraries-server/src/test/resources/csv/namedColumn.csv
Normal file
5
libraries-server/src/test/resources/csv/namedColumn.csv
Normal file
@@ -0,0 +1,5 @@
|
||||
name,age
|
||||
adam,1000
|
||||
martin,27
|
||||
gigi,41
|
||||
seraphine,30
|
||||
|
5
libraries-server/src/test/resources/csv/twoColumn.csv
Normal file
5
libraries-server/src/test/resources/csv/twoColumn.csv
Normal file
@@ -0,0 +1,5 @@
|
||||
ColA,ColB
|
||||
A,B
|
||||
C,D
|
||||
G,G
|
||||
G,F
|
||||
|
8
libraries-server/src/test/resources/dockerapi/Dockerfile
Normal file
8
libraries-server/src/test/resources/dockerapi/Dockerfile
Normal file
@@ -0,0 +1,8 @@
|
||||
FROM alpine:3.6
|
||||
|
||||
RUN apk --update add git openssh && \
|
||||
rm -rf /var/lib/apt/lists/* && \
|
||||
rm /var/cache/apk/*
|
||||
|
||||
ENTRYPOINT ["git"]
|
||||
CMD ["--help"]
|
||||
43
libraries-server/src/test/resources/employees.sql
Normal file
43
libraries-server/src/test/resources/employees.sql
Normal file
@@ -0,0 +1,43 @@
|
||||
CREATE TABLE employee(
|
||||
id int NOT NULL PRIMARY KEY auto_increment,
|
||||
firstname varchar(255),
|
||||
lastname varchar(255),
|
||||
salary double,
|
||||
hireddate date
|
||||
);
|
||||
|
||||
CREATE TABLE email(
|
||||
id int NOT NULL PRIMARY KEY auto_increment,
|
||||
employeeid int,
|
||||
address varchar(255)
|
||||
);
|
||||
|
||||
CREATE TABLE employee_legacy(
|
||||
id int NOT NULL PRIMARY KEY auto_increment,
|
||||
first_name varchar(255),
|
||||
last_name varchar(255),
|
||||
salary double,
|
||||
hired_date date
|
||||
);
|
||||
|
||||
|
||||
INSERT INTO employee (firstname,lastname,salary,hireddate) VALUES ('John', 'Doe', 10000.10, to_date('01-01-2001','dd-mm-yyyy'));
|
||||
INSERT INTO employee (firstname,lastname,salary,hireddate) VALUES ('Kevin', 'Smith', 20000.20, to_date('02-02-2002','dd-mm-yyyy'));
|
||||
INSERT INTO employee (firstname,lastname,salary,hireddate) VALUES ('Kim', 'Smith', 30000.30, to_date('03-03-2003','dd-mm-yyyy'));
|
||||
INSERT INTO employee (firstname,lastname,salary,hireddate) VALUES ('Stephen', 'Torvalds', 40000.40, to_date('04-04-2004','dd-mm-yyyy'));
|
||||
INSERT INTO employee (firstname,lastname,salary,hireddate) VALUES ('Christian', 'Reynolds', 50000.50, to_date('05-05-2005','dd-mm-yyyy'));
|
||||
|
||||
INSERT INTO employee_legacy (first_name,last_name,salary,hired_date) VALUES ('John', 'Doe', 10000.10, to_date('01-01-2001','dd-mm-yyyy'));
|
||||
INSERT INTO employee_legacy (first_name,last_name,salary,hired_date) VALUES ('Kevin', 'Smith', 20000.20, to_date('02-02-2002','dd-mm-yyyy'));
|
||||
INSERT INTO employee_legacy (first_name,last_name,salary,hired_date) VALUES ('Kim', 'Smith', 30000.30, to_date('03-03-2003','dd-mm-yyyy'));
|
||||
INSERT INTO employee_legacy (first_name,last_name,salary,hired_date) VALUES ('Stephen', 'Torvalds', 40000.40, to_date('04-04-2004','dd-mm-yyyy'));
|
||||
INSERT INTO employee_legacy (first_name,last_name,salary,hired_date) VALUES ('Christian', 'Reynolds', 50000.50, to_date('05-05-2005','dd-mm-yyyy'));
|
||||
|
||||
INSERT INTO email (employeeid,address) VALUES (1, 'john@baeldung.com');
|
||||
INSERT INTO email (employeeid,address) VALUES (1, 'john@gmail.com');
|
||||
INSERT INTO email (employeeid,address) VALUES (2, 'kevin@baeldung.com');
|
||||
INSERT INTO email (employeeid,address) VALUES (3, 'kim@baeldung.com');
|
||||
INSERT INTO email (employeeid,address) VALUES (3, 'kim@gmail.com');
|
||||
INSERT INTO email (employeeid,address) VALUES (3, 'kim@outlook.com');
|
||||
INSERT INTO email (employeeid,address) VALUES (4, 'stephen@baeldung.com');
|
||||
INSERT INTO email (employeeid,address) VALUES (5, 'christian@gmail.com');
|
||||
1
libraries-server/src/test/resources/fileTest.txt
Normal file
1
libraries-server/src/test/resources/fileTest.txt
Normal file
@@ -0,0 +1 @@
|
||||
Hello World from fileTest.txt!!!
|
||||
0
libraries-server/src/test/resources/ftp/baz.txt
Normal file
0
libraries-server/src/test/resources/ftp/baz.txt
Normal file
1
libraries-server/src/test/resources/input.txt
Normal file
1
libraries-server/src/test/resources/input.txt
Normal file
@@ -0,0 +1 @@
|
||||
This file is merely for testing.
|
||||
1
libraries-server/src/test/resources/output.txt
Normal file
1
libraries-server/src/test/resources/output.txt
Normal file
@@ -0,0 +1 @@
|
||||
Should be copied to OutputStream.
|
||||
2
libraries-server/src/test/resources/sample.txt
Normal file
2
libraries-server/src/test/resources/sample.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
line 1
|
||||
a second line
|
||||
@@ -0,0 +1,13 @@
|
||||
|
||||
Meta:
|
||||
|
||||
Narrative:
|
||||
As a user
|
||||
I want to look up a valid user's profile on github
|
||||
So that I can know the login payload should be the same as username
|
||||
|
||||
Scenario: Github user's profile should have a login payload same as username
|
||||
|
||||
Given github user profile api
|
||||
When looking for eugenp via the api
|
||||
Then github's response contains a 'login' payload same as eugenp
|
||||
@@ -0,0 +1,11 @@
|
||||
Meta:
|
||||
|
||||
Narrative:
|
||||
As user
|
||||
I want to add a number
|
||||
So that I can have the sum
|
||||
|
||||
Scenario: A user can submit a number to adder and get current sum
|
||||
Given a number
|
||||
When I submit another number 5 to adder
|
||||
Then I get a sum of the numbers
|
||||
3
libraries-server/src/test/resources/yaml/customer.yaml
Normal file
3
libraries-server/src/test/resources/yaml/customer.yaml
Normal file
@@ -0,0 +1,3 @@
|
||||
firstName: "John"
|
||||
lastName: "Doe"
|
||||
age: 20
|
||||
@@ -0,0 +1,7 @@
|
||||
firstName: "John"
|
||||
lastName: "Doe"
|
||||
age: 31
|
||||
contactDetails:
|
||||
- { type: "mobile", number: 123456789}
|
||||
- { type: "landline", number: 456786868}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
firstName: "John"
|
||||
lastName: "Doe"
|
||||
age: 31
|
||||
contactDetails:
|
||||
- type: "mobile"
|
||||
number: 123456789
|
||||
- type: "landline"
|
||||
number: 456786868
|
||||
homeAddress:
|
||||
line: "Xyz, DEF Street"
|
||||
city: "City Y"
|
||||
state: "State Y"
|
||||
zip: 345657
|
||||
@@ -0,0 +1,6 @@
|
||||
firstName: "John"
|
||||
lastName: "Doe"
|
||||
age: 31
|
||||
contactDetails:
|
||||
- !contact { type: "mobile", number: 123456789}
|
||||
- !contact { type: "landline", number: 456786868}
|
||||
@@ -0,0 +1,4 @@
|
||||
!!com.baeldung.snakeyaml.Customer
|
||||
firstName: "John"
|
||||
lastName: "Doe"
|
||||
age: 20
|
||||
8
libraries-server/src/test/resources/yaml/customers.yaml
Normal file
8
libraries-server/src/test/resources/yaml/customers.yaml
Normal file
@@ -0,0 +1,8 @@
|
||||
---
|
||||
firstName: "John"
|
||||
lastName: "Doe"
|
||||
age: 20
|
||||
---
|
||||
firstName: "Jack"
|
||||
lastName: "Jones"
|
||||
age: 25
|
||||
Reference in New Issue
Block a user