Skip to content

Commit 8784c72

Browse files
committed
add "test" maven profile for integration testing
1 parent 795e58b commit 8784c72

File tree

6 files changed

+206
-13
lines changed

6 files changed

+206
-13
lines changed

pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,21 @@
5151
</activation>
5252
<properties>
5353
<envName>prod</envName>
54+
<skipTests>true</skipTests>
5455
</properties>
5556
</profile>
5657
<profile>
5758
<id>dev</id>
5859
<properties>
5960
<envName>dev</envName>
61+
<skipTests>true</skipTests>
62+
</properties>
63+
</profile>
64+
<profile>
65+
<id>test</id>
66+
<properties>
67+
<envName>test</envName>
68+
<skipTests>false</skipTests>
6069
</properties>
6170
</profile>
6271
</profiles>

src/main/java/de/rwth/idsg/steve/ApplicationProfile.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77
public enum ApplicationProfile {
88
DEV,
9+
TEST,
910
PROD;
1011

1112
public static ApplicationProfile fromName(String v) {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Configuration status="WARN">
3+
4+
<Properties>
5+
<Property name="logPattern">[%-5p] %d %c (%t) - %m%n</Property>
6+
</Properties>
7+
8+
<Appenders>
9+
<Console name="CONSOLE" target="SYSTEM_OUT">
10+
<PatternLayout pattern="${logPattern}"/>
11+
</Console>
12+
</Appenders>
13+
14+
<Loggers>
15+
<!-- Disable the Jooq logo in logs -->
16+
<!-- https://github.com/jOOQ/jOOQ/issues/4019 -->
17+
<AsyncLogger name="org.jooq.Constants" level="WARN"/>
18+
19+
<AsyncLogger name="org.apache.cxf" level="INFO"/>
20+
<AsyncLogger name="org.eclipse.jetty" level="INFO"/>
21+
<AsyncLogger name="com.zaxxer.hikari" level="INFO"/>
22+
<AsyncLogger name="org.springframework" level="INFO"/>
23+
<AsyncLogger name="org.springframework.security" level="INFO"/>
24+
<AsyncLogger name="org.springframework.web.servlet" level="INFO"/>
25+
26+
<AsyncLogger name="org.apache.jasper" level="WARN"/>
27+
<AsyncLogger name="org.apache.tomcat.util" level="WARN"/>
28+
<AsyncLogger name="jndi" level="WARN"/>
29+
30+
<AsyncRoot level="DEBUG">
31+
<AppenderRef ref="CONSOLE"/>
32+
</AsyncRoot>
33+
</Loggers>
34+
</Configuration>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Just to be backwards compatible with previous versions, this is set to "steve",
2+
# since there might be already configured chargepoints expecting the older path.
3+
# Otherwise, might as well be changed to something else or be left empty.
4+
#
5+
context.path = steve
6+
7+
# Database configuration
8+
#
9+
db.ip = localhost
10+
db.port = 3306
11+
db.schema = stevedb_test_2aa6a783d47d
12+
db.user = steve
13+
db.password = changeme
14+
15+
# Credentials for Web interface access
16+
#
17+
auth.user = admin
18+
auth.password = 1234
19+
20+
# Jetty configuration
21+
#
22+
server.host = 127.0.0.1
23+
server.gzip.enabled = false
24+
25+
# Jetty HTTP configuration
26+
#
27+
http.enabled = true
28+
http.port = 8080
29+
30+
# Jetty HTTPS configuration
31+
#
32+
https.enabled = false
33+
https.port = 8443
34+
keystore.path =
35+
keystore.password =
36+
37+
# When the WebSocket/Json charge point opens more than one WebSocket connection,
38+
# we need a mechanism/strategy to select one of them for outgoing requests.
39+
# For allowed values see de.rwth.idsg.steve.ocpp.ws.custom.WsSessionSelectStrategyEnum.
40+
#
41+
ws.session.select.strategy = ALWAYS_LAST
42+
43+
### DO NOT MODIFY ###
44+
steve.version = ${project.version}
45+
db.sql.logging = true
46+
profile = test

src/test/java/de/rwth/idsg/steve/ApplicationTest.java

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
import ocpp.cs._2010._08.AuthorizeResponse;
77
import ocpp.cs._2010._08.BootNotificationRequest;
88
import ocpp.cs._2010._08.BootNotificationResponse;
9+
import ocpp.cs._2010._08.RegistrationStatus;
910
import ocpp.cs._2012._06.CentralSystemService;
1011
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
1112
import org.apache.cxf.ws.addressing.WSAddressingFeature;
13+
import org.junit.AfterClass;
1214
import org.junit.Assert;
1315
import org.junit.BeforeClass;
14-
import org.junit.Ignore;
1516
import org.junit.Test;
1617

1718
import javax.xml.ws.soap.SOAPBinding;
@@ -23,18 +24,29 @@
2324
* @author Sevket Goekay <goekay@dbis.rwth-aachen.de>
2425
* @since 10.03.2018
2526
*/
26-
@Ignore
2727
@Slf4j
2828
public class ApplicationTest {
2929

30+
private static final String REGISTERED_CHARGE_BOX_ID = __DatabasePreparer__.getRegisteredChargeBoxId();
31+
private static final String REGISTERED_OCPP_TAG = __DatabasePreparer__.getRegisteredOcppTag();
3032
private static final String path = getPath();
3133

34+
private static Application app;
35+
3236
@BeforeClass
3337
public static void init() throws Exception {
34-
Application app = new Application();
38+
__DatabasePreparer__.prepare();
39+
40+
app = new Application();
3541
app.start();
3642
}
3743

44+
@AfterClass
45+
public static void destroy() throws Exception {
46+
app.stop();
47+
__DatabasePreparer__.cleanUp();
48+
}
49+
3850
@Test
3951
public void testOcpp12() {
4052
ocpp.cs._2010._08.CentralSystemService client = getForOcpp12();
@@ -43,16 +55,16 @@ public void testOcpp12() {
4355
new BootNotificationRequest()
4456
.withChargePointVendor(getRandomString())
4557
.withChargePointModel(getRandomString()),
46-
getRandomString());
58+
REGISTERED_CHARGE_BOX_ID);
4759
Assert.assertNotNull(boot);
48-
Assert.assertEquals(ocpp.cs._2010._08.RegistrationStatus.REJECTED, boot.getStatus());
60+
Assert.assertEquals(RegistrationStatus.ACCEPTED, boot.getStatus());
4961

5062
AuthorizeResponse auth = client.authorize(
5163
new AuthorizeRequest()
52-
.withIdTag(getRandomString()),
53-
getRandomString());
64+
.withIdTag(REGISTERED_OCPP_TAG),
65+
REGISTERED_CHARGE_BOX_ID);
5466
Assert.assertNotNull(auth);
55-
Assert.assertEquals(AuthorizationStatus.INVALID, auth.getIdTagInfo().getStatus());
67+
Assert.assertEquals(AuthorizationStatus.ACCEPTED, auth.getIdTagInfo().getStatus());
5668
}
5769

5870
@Test
@@ -63,16 +75,16 @@ public void testOcpp15() {
6375
new ocpp.cs._2012._06.BootNotificationRequest()
6476
.withChargePointVendor(getRandomString())
6577
.withChargePointModel(getRandomString()),
66-
getRandomString());
78+
REGISTERED_CHARGE_BOX_ID);
6779
Assert.assertNotNull(boot);
68-
Assert.assertEquals(ocpp.cs._2012._06.RegistrationStatus.REJECTED, boot.getStatus());
80+
Assert.assertEquals(ocpp.cs._2012._06.RegistrationStatus.ACCEPTED, boot.getStatus());
6981

7082
ocpp.cs._2012._06.AuthorizeResponse auth = client.authorize(
7183
new ocpp.cs._2012._06.AuthorizeRequest()
72-
.withIdTag(getRandomString()),
73-
getRandomString());
84+
.withIdTag(REGISTERED_OCPP_TAG),
85+
REGISTERED_CHARGE_BOX_ID);
7486
Assert.assertNotNull(auth);
75-
Assert.assertEquals(ocpp.cs._2012._06.AuthorizationStatus.INVALID, auth.getIdTagInfo().getStatus());
87+
Assert.assertEquals(ocpp.cs._2012._06.AuthorizationStatus.ACCEPTED, auth.getIdTagInfo().getStatus());
7688
}
7789

7890

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package de.rwth.idsg.steve;
2+
3+
import com.google.common.collect.Sets;
4+
import de.rwth.idsg.steve.config.BeanConfiguration;
5+
import jooq.steve.db.DefaultCatalog;
6+
import jooq.steve.db.tables.SchemaVersion;
7+
import jooq.steve.db.tables.Settings;
8+
import org.jooq.DSLContext;
9+
import org.jooq.Table;
10+
import org.jooq.impl.DSL;
11+
12+
import java.util.Set;
13+
import java.util.function.Consumer;
14+
15+
import static jooq.steve.db.tables.ChargeBox.CHARGE_BOX;
16+
import static jooq.steve.db.tables.OcppTag.OCPP_TAG;
17+
18+
/**
19+
* This is a dangerous class. It performs database operations no class should do, like truncating all tables and
20+
* inserting data while bypassing normal mechanisms of SteVe. However, for integration testing with reproducible
21+
* results we need a clean and isolated database.
22+
*
23+
* @author Sevket Goekay <goekay@dbis.rwth-aachen.de>
24+
* @since 21.03.2018
25+
*/
26+
public class __DatabasePreparer__ {
27+
28+
private static final String SCHEMA_TO_TRUNCATE = "stevedb_test_2aa6a783d47d";
29+
private static final String REGISTERED_CHARGE_BOX_ID = "charge_box_2aa6a783d47d";
30+
private static final String REGISTERED_OCPP_TAG = "id_tag_2aa6a783d47d";
31+
32+
public static void prepare() {
33+
runOperation(ctx -> {
34+
truncateTables(ctx);
35+
insertChargeBox(ctx);
36+
insertOcppIdTag(ctx);
37+
});
38+
}
39+
40+
public static void cleanUp() {
41+
runOperation(__DatabasePreparer__::truncateTables);
42+
}
43+
44+
public static String getRegisteredChargeBoxId() {
45+
return REGISTERED_CHARGE_BOX_ID;
46+
}
47+
48+
public static String getRegisteredOcppTag() {
49+
return REGISTERED_OCPP_TAG;
50+
}
51+
52+
private static void runOperation(Consumer<DSLContext> consumer) {
53+
BeanConfiguration beanConfiguration = new BeanConfiguration();
54+
DSLContext ctx = beanConfiguration.dslContext();
55+
try {
56+
consumer.accept(ctx);
57+
} finally {
58+
beanConfiguration.shutDown();
59+
}
60+
}
61+
62+
private static void truncateTables(DSLContext ctx) {
63+
Set<Table<?>> skipList = Sets.newHashSet(SchemaVersion.SCHEMA_VERSION, Settings.SETTINGS);
64+
ctx.transaction(configuration -> {
65+
DSLContext internalCtx = DSL.using(configuration);
66+
internalCtx.execute("SET FOREIGN_KEY_CHECKS=0");
67+
DefaultCatalog.DEFAULT_CATALOG.getSchemas()
68+
.stream()
69+
.filter(schema -> SCHEMA_TO_TRUNCATE.equals(schema.getName()))
70+
.forEach(schema -> schema.getTables()
71+
.stream()
72+
.filter(t -> !skipList.contains(t))
73+
.forEach(t -> internalCtx.truncate(t).execute()));
74+
internalCtx.execute("SET FOREIGN_KEY_CHECKS=1");
75+
});
76+
}
77+
78+
private static void insertChargeBox(DSLContext ctx) {
79+
ctx.insertInto(CHARGE_BOX)
80+
.set(CHARGE_BOX.CHARGE_BOX_ID, getRegisteredChargeBoxId())
81+
.execute();
82+
}
83+
84+
private static void insertOcppIdTag(DSLContext ctx) {
85+
ctx.insertInto(OCPP_TAG)
86+
.set(OCPP_TAG.ID_TAG, getRegisteredOcppTag())
87+
.set(OCPP_TAG.BLOCKED, false)
88+
.set(OCPP_TAG.IN_TRANSACTION, false)
89+
.execute();
90+
}
91+
}

0 commit comments

Comments
 (0)