22
33import de .rwth .idsg .steve .utils .StressTester ;
44import de .rwth .idsg .steve .utils .__DatabasePreparer__ ;
5- import lombok .extern .slf4j .Slf4j ;
65import ocpp .cs ._2015 ._10 .AuthorizationStatus ;
76import ocpp .cs ._2015 ._10 .AuthorizeRequest ;
87import ocpp .cs ._2015 ._10 .AuthorizeResponse ;
98import ocpp .cs ._2015 ._10 .BootNotificationRequest ;
109import ocpp .cs ._2015 ._10 .BootNotificationResponse ;
1110import ocpp .cs ._2015 ._10 .CentralSystemService ;
11+ import ocpp .cs ._2015 ._10 .ChargePointErrorCode ;
12+ import ocpp .cs ._2015 ._10 .ChargePointStatus ;
13+ import ocpp .cs ._2015 ._10 .HeartbeatRequest ;
14+ import ocpp .cs ._2015 ._10 .HeartbeatResponse ;
15+ import ocpp .cs ._2015 ._10 .MeterValue ;
16+ import ocpp .cs ._2015 ._10 .MeterValuesRequest ;
17+ import ocpp .cs ._2015 ._10 .MeterValuesResponse ;
1218import ocpp .cs ._2015 ._10 .RegistrationStatus ;
13- import org .junit .After ;
14- import org .junit .AfterClass ;
19+ import ocpp .cs ._2015 ._10 .SampledValue ;
20+ import ocpp .cs ._2015 ._10 .StartTransactionRequest ;
21+ import ocpp .cs ._2015 ._10 .StartTransactionResponse ;
22+ import ocpp .cs ._2015 ._10 .StatusNotificationRequest ;
23+ import ocpp .cs ._2015 ._10 .StatusNotificationResponse ;
24+ import ocpp .cs ._2015 ._10 .StopTransactionRequest ;
25+ import ocpp .cs ._2015 ._10 .StopTransactionResponse ;
26+ import org .joda .time .DateTime ;
1527import org .junit .Assert ;
16- import org .junit .Before ;
17- import org .junit .BeforeClass ;
18- import org .junit .Test ;
28+
29+ import java .util .ArrayList ;
30+ import java .util .Collections ;
31+ import java .util .List ;
32+ import java .util .concurrent .ThreadLocalRandom ;
1933
2034import static de .rwth .idsg .steve .utils .Helpers .getForOcpp16 ;
2135import static de .rwth .idsg .steve .utils .Helpers .getPath ;
2236import static de .rwth .idsg .steve .utils .Helpers .getRandomString ;
37+ import static de .rwth .idsg .steve .utils .Helpers .getRandomStrings ;
2338
2439/**
2540 * @author Sevket Goekay <goekay@dbis.rwth-aachen.de>
2641 * @since 18.04.2018
2742 */
28- @ Slf4j
2943public class StressTestSoapOCPP16 {
3044
31- private static final String REGISTERED_CHARGE_BOX_ID = __DatabasePreparer__ .getRegisteredChargeBoxId ();
45+ private static final int THREAD_COUNT = 50 ;
46+ private static final int REPEAT_COUNT_PER_THREAD = 50 ;
47+
48+ private static final int ID_TAG_COUNT = 50 ;
49+ private static final int CHARGE_BOX_COUNT = 100 ;
50+ private static final int CONNECTOR_COUNT_PER_CHARGE_BOX = 25 ;
3251
3352 private static final String path = getPath ();
34- private static Application app ;
3553
36- @ BeforeClass
37- public static void initClass () throws Exception {
54+ public static void main (String [] args ) throws Exception {
55+ new StressTestSoapOCPP16 ().attack ();
56+ }
57+
58+ private void attack () throws Exception {
3859 Assert .assertEquals (ApplicationProfile .TEST , SteveConfiguration .CONFIG .getProfile ());
3960 Assert .assertTrue (SteveConfiguration .CONFIG .getOcpp ().isAutoRegisterUnknownStations ());
4061
41- app = new Application ();
42- app .start ();
43- }
62+ __DatabasePreparer__ .prepare ();
4463
45- @ AfterClass
46- public static void destroyClass () throws Exception {
47- if (app != null ) {
48- app .stop ();
64+ Application app = new Application ();
65+ try {
66+ app .start ();
67+ attackInternal ();
68+ } finally {
69+ try {
70+ app .stop ();
71+ } finally {
72+ __DatabasePreparer__ .cleanUp ();
73+ }
4974 }
5075 }
5176
52- @ Before
53- public void init () {
54- __DatabasePreparer__ .prepare ();
55- }
77+ private static void attackInternal () throws Exception {
78+ final List <String > idTags = getRandomStrings (ID_TAG_COUNT );
79+ final List <String > chargeBoxIds = getRandomStrings (CHARGE_BOX_COUNT );
5680
57- @ After
58- public void destroy () {
59- __DatabasePreparer__ .cleanUp ();
60- }
81+ StressTester tester = new StressTester (THREAD_COUNT , REPEAT_COUNT_PER_THREAD );
6182
62- @ Test
63- public void testBootNotification () throws Exception {
64- StressTester tester = new StressTester ( 50 , 5 );
83+ tester . test (() -> {
84+ CentralSystemService client = getForOcpp16 ( path );
85+ ThreadLocalRandom localRandom = ThreadLocalRandom . current ( );
6586
66- CentralSystemService client = getForOcpp16 (path );
87+ String idTag = idTags .get (localRandom .nextInt (idTags .size ()));
88+ String chargeBoxId = chargeBoxIds .get (localRandom .nextInt (chargeBoxIds .size ()));
89+ int connectorId = localRandom .nextInt (1 , CONNECTOR_COUNT_PER_CHARGE_BOX + 1 );
6790
68- tester .test (() -> {
91+ int transactionStart = localRandom .nextInt (0 , Integer .MAX_VALUE );
92+ int transactionStop = localRandom .nextInt (transactionStart + 1 , Integer .MAX_VALUE );
93+
94+ // to insert chargeBoxId into db
6995 BootNotificationResponse boot = client .bootNotification (
7096 new BootNotificationRequest ()
7197 .withChargePointVendor (getRandomString ())
7298 .withChargePointModel (getRandomString ()),
73- getRandomString () );
99+ chargeBoxId );
74100 Assert .assertEquals (RegistrationStatus .ACCEPTED , boot .getStatus ());
101+
102+ HeartbeatResponse heartbeat = client .heartbeat (
103+ new HeartbeatRequest (),
104+ chargeBoxId
105+ );
106+ Assert .assertNotNull (heartbeat );
107+
108+ for (int i = 0 ; i <= CONNECTOR_COUNT_PER_CHARGE_BOX ; i ++) {
109+ StatusNotificationResponse status = client .statusNotification (
110+ new StatusNotificationRequest ()
111+ .withErrorCode (ChargePointErrorCode .NO_ERROR )
112+ .withStatus (ChargePointStatus .AVAILABLE )
113+ .withConnectorId (i )
114+ .withTimestamp (DateTime .now ()),
115+ chargeBoxId
116+ );
117+ Assert .assertNotNull (status );
118+ }
119+
120+ AuthorizeResponse auth = client .authorize (
121+ new AuthorizeRequest ().withIdTag (idTag ),
122+ chargeBoxId
123+ );
124+ Assert .assertNotEquals (AuthorizationStatus .ACCEPTED , auth .getIdTagInfo ().getStatus ());
125+
126+ StartTransactionResponse start = client .startTransaction (
127+ new StartTransactionRequest ()
128+ .withConnectorId (connectorId )
129+ .withIdTag (idTag )
130+ .withTimestamp (DateTime .now ())
131+ .withMeterStart (transactionStart ),
132+ chargeBoxId
133+ );
134+ Assert .assertNotNull (start );
135+
136+ StatusNotificationResponse statusStart = client .statusNotification (
137+ new StatusNotificationRequest ()
138+ .withErrorCode (ChargePointErrorCode .NO_ERROR )
139+ .withStatus (ChargePointStatus .CHARGING )
140+ .withConnectorId (connectorId )
141+ .withTimestamp (DateTime .now ()),
142+ chargeBoxId
143+ );
144+ Assert .assertNotNull (statusStart );
145+
146+ MeterValuesResponse meter = client .meterValues (
147+ new MeterValuesRequest ()
148+ .withConnectorId (connectorId )
149+ .withTransactionId (start .getTransactionId ())
150+ .withMeterValue (getMeterValues (transactionStart , transactionStop )),
151+ chargeBoxId
152+ );
153+ Assert .assertNotNull (meter );
154+
155+ StopTransactionResponse stop = client .stopTransaction (
156+ new StopTransactionRequest ()
157+ .withTransactionId (start .getTransactionId ())
158+ .withTimestamp (DateTime .now ())
159+ .withIdTag (idTag )
160+ .withMeterStop (transactionStop ),
161+ chargeBoxId
162+ );
163+ Assert .assertNotNull (stop );
164+
165+ StatusNotificationResponse statusStop = client .statusNotification (
166+ new StatusNotificationRequest ()
167+ .withErrorCode (ChargePointErrorCode .NO_ERROR )
168+ .withStatus (ChargePointStatus .AVAILABLE )
169+ .withConnectorId (connectorId )
170+ .withTimestamp (DateTime .now ()),
171+ chargeBoxId
172+ );
173+ Assert .assertNotNull (statusStop );
75174 });
76175
77176 tester .shutDown ();
78177 }
79178
80- @ Test
81- public void testAuthorize () throws Exception {
82- StressTester tester = new StressTester (100 , 10 );
83-
84- CentralSystemService client = getForOcpp16 (path );
179+ private static List <MeterValue > getMeterValues (int transactionStart , int transactionStop ) {
180+ final int size = 4 ;
181+ int delta = (transactionStop - transactionStart ) / size ;
182+ if (delta == 0 ) {
183+ return Collections .emptyList ();
184+ }
85185
86- tester .test (() -> {
87- AuthorizeResponse auth = client .authorize (
88- new AuthorizeRequest ().withIdTag (getRandomString ()),
89- REGISTERED_CHARGE_BOX_ID );
90- Assert .assertEquals (AuthorizationStatus .INVALID , auth .getIdTagInfo ().getStatus ());
91- });
186+ List <MeterValue > list = new ArrayList <>(size );
187+ for (int i = 0 ; i < size ; i ++) {
188+ int meterValue = transactionStart + delta * (i + 1 );
189+ list .add (createMeterValue (meterValue ));
190+ }
191+ return list ;
192+ }
92193
93- tester .shutDown ();
194+ private static MeterValue createMeterValue (int val ) {
195+ return new MeterValue ().withTimestamp (DateTime .now ())
196+ .withSampledValue (new SampledValue ().withValue (Integer .toString (val )));
94197 }
95198
96- }
199+ }
0 commit comments