Skip to content

Commit a4d37d1

Browse files
committed
Splitted handlers into a separate class.
1 parent ff33ea6 commit a4d37d1

File tree

2 files changed

+273
-167
lines changed

2 files changed

+273
-167
lines changed
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
package eu.chargetime.ocpp.test;
2+
/*
3+
ChargeTime.eu - Java-OCA-OCPP
4+
5+
MIT License
6+
7+
Copyright (C) 2016 Thomas Volden <[email protected]>
8+
9+
Permission is hereby granted, free of charge, to any person obtaining a copy
10+
of this software and associated documentation files (the "Software"), to deal
11+
in the Software without restriction, including without limitation the rights
12+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
copies of the Software, and to permit persons to whom the Software is
14+
furnished to do so, subject to the following conditions:
15+
16+
The above copyright notice and this permission notice shall be included in all
17+
copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25+
SOFTWARE.
26+
*/
27+
28+
import eu.chargetime.ocpp.ServerEvents;
29+
import eu.chargetime.ocpp.feature.profile.ServerCoreEventHandler;
30+
import eu.chargetime.ocpp.model.Confirmation;
31+
import eu.chargetime.ocpp.model.Request;
32+
import eu.chargetime.ocpp.model.core.*;
33+
34+
import java.lang.reflect.Type;
35+
import java.util.Calendar;
36+
import java.util.UUID;
37+
import java.util.function.BiConsumer;
38+
39+
public class DummyHandlers {
40+
41+
private boolean riggedToFail;
42+
43+
private Request receivedRequest;
44+
private Confirmation receivedConfirmation;
45+
46+
private String currentIdentifier;
47+
private UUID currentSessionIndex;
48+
49+
public ServerCoreEventHandler createServerCoreEventHandler() {
50+
return new ServerCoreEventHandler() {
51+
@Override
52+
public AuthorizeConfirmation handleAuthorizeRequest(UUID sessionIndex, AuthorizeRequest request) {
53+
receivedRequest = request;
54+
AuthorizeConfirmation confirmation = new AuthorizeConfirmation();
55+
IdTagInfo tagInfo = new IdTagInfo();
56+
tagInfo.setStatus(AuthorizationStatus.Accepted);
57+
Calendar calendar = Calendar.getInstance();
58+
calendar.set(2018, 1, 1, 1, 1, 1);
59+
tagInfo.setExpiryDate(calendar);
60+
confirmation.setIdTagInfo(tagInfo);
61+
return failurePoint(confirmation);
62+
}
63+
64+
@Override
65+
public BootNotificationConfirmation handleBootNotificationRequest(UUID sessionIndex, BootNotificationRequest request) {
66+
receivedRequest = request;
67+
BootNotificationConfirmation confirmation = new BootNotificationConfirmation();
68+
try {
69+
confirmation.setInterval(1);
70+
} catch (Exception e) {
71+
e.printStackTrace();
72+
}
73+
confirmation.setCurrentTime(Calendar.getInstance());
74+
confirmation.setStatus(RegistrationStatus.Accepted);
75+
return failurePoint(confirmation);
76+
}
77+
78+
@Override
79+
public DataTransferConfirmation handleDataTransferRequest(UUID sessionIndex, DataTransferRequest request) {
80+
receivedRequest = request;
81+
DataTransferConfirmation confirmation = new DataTransferConfirmation();
82+
confirmation.setStatus(DataTransferStatus.Accepted);
83+
return failurePoint(confirmation);
84+
}
85+
86+
@Override
87+
public HeartbeatConfirmation handleHeartbeatRequest(UUID sessionIndex, HeartbeatRequest request) {
88+
receivedRequest = request;
89+
HeartbeatConfirmation confirmation = new HeartbeatConfirmation();
90+
confirmation.setCurrentTime(Calendar.getInstance());
91+
return failurePoint(confirmation);
92+
}
93+
94+
@Override
95+
public MeterValuesConfirmation handleMeterValuesRequest(UUID sessionIndex, MeterValuesRequest request) {
96+
receivedRequest = request;
97+
return failurePoint(new MeterValuesConfirmation());
98+
}
99+
100+
@Override
101+
public StartTransactionConfirmation handleStartTransactionRequest(UUID sessionIndex, StartTransactionRequest request) {
102+
receivedRequest = request;
103+
IdTagInfo tagInfo = new IdTagInfo();
104+
tagInfo.setStatus(AuthorizationStatus.Accepted);
105+
106+
StartTransactionConfirmation confirmation = new StartTransactionConfirmation();
107+
confirmation.setIdTagInfo(tagInfo);
108+
return failurePoint(confirmation);
109+
}
110+
111+
@Override
112+
public StatusNotificationConfirmation handleStatusNotificationRequest(UUID sessionIndex, StatusNotificationRequest request) {
113+
receivedRequest = request;
114+
StatusNotificationConfirmation confirmation = new StatusNotificationConfirmation();
115+
return failurePoint(confirmation);
116+
}
117+
118+
@Override
119+
public StopTransactionConfirmation handleStopTransactionRequest(UUID sessionIndex, StopTransactionRequest request) {
120+
receivedRequest = request;
121+
StopTransactionConfirmation confirmation = new StopTransactionConfirmation();
122+
return failurePoint(confirmation);
123+
}
124+
};
125+
}
126+
127+
public ServerEvents generateServerEventsHandler() {
128+
return new ServerEvents() {
129+
@Override
130+
public void newSession(UUID sessionIndex, String identifier) {
131+
currentSessionIndex = sessionIndex;
132+
currentIdentifier = identifier;
133+
}
134+
135+
@Override
136+
public void lostSession(UUID identity) {
137+
currentSessionIndex = null;
138+
currentIdentifier = null;
139+
// clear
140+
receivedConfirmation = null;
141+
receivedRequest = null;
142+
}
143+
};
144+
}
145+
146+
public BiConsumer<Confirmation, Throwable> generateWhenCompleteHandler() {
147+
return (confirmation, throwable) -> receivedConfirmation = confirmation;
148+
}
149+
150+
private <T extends Confirmation> T failurePoint(T confirmation) {
151+
if (riggedToFail) {
152+
riggedToFail = false;
153+
return null;
154+
}
155+
return confirmation;
156+
}
157+
158+
public boolean wasLatestRequest(Type requestType) {
159+
return requestType != null && receivedRequest != null && requestType.equals(receivedRequest.getClass());
160+
}
161+
162+
public <T extends Request> T getReceivedRequest(T requestType) {
163+
if (wasLatestRequest(requestType.getClass()))
164+
return (T) receivedRequest;
165+
return null;
166+
}
167+
168+
public boolean wasLatestConfirmation(Type confirmationType) {
169+
return confirmationType != null && receivedConfirmation != null && confirmationType.equals(receivedConfirmation.getClass());
170+
}
171+
172+
public <T extends Confirmation> T getReceivedConfirmation(T confirmationType) {
173+
if (wasLatestConfirmation(confirmationType.getClass()))
174+
return (T) receivedConfirmation;
175+
return null;
176+
}
177+
178+
179+
public void setRiggedToFail(boolean riggedToFail) {
180+
this.riggedToFail = riggedToFail;
181+
}
182+
183+
public boolean isRiggedToFail() {
184+
return riggedToFail;
185+
}
186+
187+
public String getCurrentIdentifier() {
188+
return currentIdentifier;
189+
}
190+
191+
public UUID getCurrentSessionIndex() {
192+
return currentSessionIndex;
193+
}
194+
}

0 commit comments

Comments
 (0)