Skip to content

Commit 6e2f96a

Browse files
committed
The JSON server has problems reconnecting, to prevent this I reuse the the objects better.
1 parent a4d37d1 commit 6e2f96a

35 files changed

+167
-111
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 java.util.Map;
29+
import java.util.concurrent.ConcurrentHashMap;
30+
31+
public class FakeCentral {
32+
33+
public enum serverType {JSON, SOAP}
34+
35+
private static Map<serverType, FakeCentralSystem> systems = new ConcurrentHashMap<serverType, FakeCentralSystem>();
36+
37+
synchronized public static FakeCentralSystem getSystem(serverType type) {
38+
if (!systems.containsKey(type))
39+
systems.put(type, new FakeCentralSystem(type));
40+
return systems.get(type);
41+
}
42+
43+
}

ocpp-v1_6-test/src/main/java/eu/chargetime/ocpp/test/FakeCentralSystem.java

Lines changed: 29 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,38 @@ of this software and associated documentation files (the "Software"), to deal
4040
import eu.chargetime.ocpp.model.firmware.GetDiagnosticsRequest;
4141
import eu.chargetime.ocpp.model.remotetrigger.TriggerMessageRequest;
4242
import eu.chargetime.ocpp.model.remotetrigger.TriggerMessageRequestType;
43-
43+
import eu.chargetime.ocpp.test.FakeCentral.serverType;
4444

4545
public class FakeCentralSystem {
4646
private Server server;
4747

4848
DummyHandlers dummyHandlers;
49+
private boolean isStarted;
4950

50-
private static FakeCentralSystem instance;
51+
FakeCentralSystem(serverType type) {
52+
dummyHandlers = new DummyHandlers();
5153

52-
public static FakeCentralSystem getInstance() {
53-
if (instance == null)
54-
instance = new FakeCentralSystem();
54+
ServerCoreProfile serverCoreProfile = new ServerCoreProfile(dummyHandlers.createServerCoreEventHandler());
55+
56+
if (type == serverType.JSON) {
57+
server = new JSONServer(serverCoreProfile);
58+
} else {
59+
server = new SOAPServer(serverCoreProfile);
60+
}
5561

56-
return instance;
62+
initializeServer();
63+
isStarted = false;
5764
}
5865

59-
private FakeCentralSystem() {
60-
dummyHandlers = new DummyHandlers();
66+
private void initializeServer() {
67+
ServerSmartChargingProfile smartChargingProfile = new ServerSmartChargingProfile();
68+
server.addFeatureProfile(smartChargingProfile);
69+
70+
ServerRemoteTriggerProfile remoteTriggerProfile = new ServerRemoteTriggerProfile();
71+
server.addFeatureProfile(remoteTriggerProfile);
72+
73+
ServerFirmwareManagementProfile firmwareManagementProfile = new ServerFirmwareManagementProfile();
74+
server.addFeatureProfile(firmwareManagementProfile);
6175
}
6276

6377
public boolean connected() {
@@ -68,59 +82,16 @@ public void clientLost() {
6882
server.closeSession(dummyHandlers.getCurrentSessionIndex());
6983
}
7084

71-
public enum serverType {JSON, SOAP}
72-
7385
public void started() throws Exception {
74-
started(serverType.JSON);
75-
}
7686

77-
private boolean matchServerType(serverType type) {
78-
boolean result = false;
79-
switch (type) {
80-
case JSON:
81-
result = server instanceof JSONServer;
82-
break;
83-
case SOAP:
84-
result = server instanceof SOAPServer;
85-
}
86-
return result;
87-
}
88-
89-
public void started(serverType type) throws Exception {
90-
if (server != null) {
91-
if (matchServerType(type)) {
92-
return;
93-
} else {
94-
server.close();
95-
}
96-
}
97-
98-
99-
ServerCoreProfile serverCoreProfile = new ServerCoreProfile(dummyHandlers.createServerCoreEventHandler());
100-
101-
ServerSmartChargingProfile smartChargingProfile = new ServerSmartChargingProfile();
102-
103-
ServerRemoteTriggerProfile remoteTriggerProfile = new ServerRemoteTriggerProfile();
104-
105-
ServerFirmwareManagementProfile firmwareManagementProfile = new ServerFirmwareManagementProfile();
106-
107-
int port = 0;
108-
switch (type) {
109-
case JSON:
110-
server = new JSONServer(serverCoreProfile);
87+
if (!isStarted) {
88+
int port = 8890;
89+
if (server instanceof JSONServer)
11190
port = 8887;
112-
break;
113-
case SOAP:
114-
server = new SOAPServer(serverCoreProfile);
115-
port = 8890;
116-
break;
117-
}
11891

119-
server.addFeatureProfile(smartChargingProfile);
120-
server.addFeatureProfile(remoteTriggerProfile);
121-
server.addFeatureProfile(firmwareManagementProfile);
122-
123-
server.open("localhost", port, dummyHandlers.generateServerEventsHandler());
92+
server.open("localhost", port, dummyHandlers.generateServerEventsHandler());
93+
isStarted = true;
94+
}
12495
}
12596

12697
public void stopped() {
@@ -285,7 +256,7 @@ public boolean hasReceivedUnlockConnectorConfirmation(String status) {
285256
boolean result = false;
286257
UnlockConnectorConfirmation confirmation = dummyHandlers.getReceivedConfirmation(new UnlockConnectorConfirmation());
287258
if (confirmation != null)
288-
result &= confirmation.getStatus().toString().equals(status);
259+
result = confirmation.getStatus().toString().equals(status);
289260
return result;
290261
}
291262

ocpp-v1_6-test/src/test/groovy/eu/chargetime/ocpp/test/core/json/JSONAuthorizeSpec.groovy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package eu.chargetime.ocpp.test.core.json
22

33
import eu.chargetime.ocpp.OccurenceConstraintException
4+
import eu.chargetime.ocpp.test.FakeCentral
45
import eu.chargetime.ocpp.test.FakeCentralSystem
56
import eu.chargetime.ocpp.test.FakeChargePoint
67
import spock.lang.Shared
@@ -9,7 +10,7 @@ import spock.util.concurrent.PollingConditions
910

1011
class JSONAuthorizeSpec extends Specification {
1112
@Shared
12-
FakeCentralSystem centralSystem = FakeCentralSystem.instance
13+
FakeCentralSystem centralSystem = FakeCentral.getSystem(FakeCentral.serverType.JSON)
1314
@Shared
1415
FakeChargePoint chargePoint = new FakeChargePoint()
1516

ocpp-v1_6-test/src/test/groovy/eu/chargetime/ocpp/test/core/json/JSONBootNotificationSpec.groovy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package eu.chargetime.ocpp.test.core.json
22

3+
import eu.chargetime.ocpp.test.FakeCentral
34
import eu.chargetime.ocpp.test.FakeCentralSystem
45
import eu.chargetime.ocpp.test.FakeChargePoint
56
import spock.lang.Shared
@@ -9,7 +10,7 @@ import spock.util.concurrent.PollingConditions
910
class JSONBootNotificationSpec extends Specification
1011
{
1112
@Shared
12-
FakeCentralSystem centralSystem = FakeCentralSystem.instance
13+
FakeCentralSystem centralSystem = FakeCentral.getSystem(FakeCentral.serverType.JSON)
1314
@Shared
1415
FakeChargePoint chargePoint = new FakeChargePoint()
1516

ocpp-v1_6-test/src/test/groovy/eu/chargetime/ocpp/test/core/json/JSONChangeAvailabilitySpec.groovy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package eu.chargetime.ocpp.test.core.json
22

33
import eu.chargetime.ocpp.model.core.AvailabilityType
4+
import eu.chargetime.ocpp.test.FakeCentral
45
import eu.chargetime.ocpp.test.FakeCentralSystem
56
import eu.chargetime.ocpp.test.FakeChargePoint
67
import spock.lang.Shared
@@ -10,7 +11,7 @@ import spock.util.concurrent.PollingConditions
1011
class JSONChangeAvailabilitySpec extends Specification
1112
{
1213
@Shared
13-
FakeCentralSystem centralSystem = FakeCentralSystem.instance
14+
FakeCentralSystem centralSystem = FakeCentral.getSystem(FakeCentral.serverType.JSON)
1415
@Shared
1516
FakeChargePoint chargePoint = new FakeChargePoint()
1617

ocpp-v1_6-test/src/test/groovy/eu/chargetime/ocpp/test/core/json/JSONChangeConfigurationSpec.groovy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package eu.chargetime.ocpp.test.core.json
22

3+
import eu.chargetime.ocpp.test.FakeCentral
34
import eu.chargetime.ocpp.test.FakeCentralSystem
45
import eu.chargetime.ocpp.test.FakeChargePoint
56
import spock.lang.Shared
@@ -9,7 +10,7 @@ import spock.util.concurrent.PollingConditions
910
class JSONChangeConfigurationSpec extends Specification
1011
{
1112
@Shared
12-
FakeCentralSystem centralSystem = FakeCentralSystem.instance
13+
FakeCentralSystem centralSystem = FakeCentral.getSystem(FakeCentral.serverType.JSON)
1314
@Shared
1415
FakeChargePoint chargePoint = new FakeChargePoint()
1516

ocpp-v1_6-test/src/test/groovy/eu/chargetime/ocpp/test/core/json/JSONClearCacheSpec.groovy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package eu.chargetime.ocpp.test.core.json
22

3+
import eu.chargetime.ocpp.test.FakeCentral
34
import eu.chargetime.ocpp.test.FakeCentralSystem
45
import eu.chargetime.ocpp.test.FakeChargePoint
56
import spock.lang.Shared
@@ -9,7 +10,7 @@ import spock.util.concurrent.PollingConditions
910
class JSONClearCacheSpec extends Specification
1011
{
1112
@Shared
12-
FakeCentralSystem centralSystem = FakeCentralSystem.getInstance()
13+
FakeCentralSystem centralSystem = FakeCentral.getSystem(FakeCentral.serverType.JSON)
1314
@Shared
1415
FakeChargePoint chargePoint = new FakeChargePoint()
1516

ocpp-v1_6-test/src/test/groovy/eu/chargetime/ocpp/test/core/json/JSONDataTransferSpec.groovy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package eu.chargetime.ocpp.test.core.json
22

3+
import eu.chargetime.ocpp.test.FakeCentral
34
import eu.chargetime.ocpp.test.FakeCentralSystem
45
import eu.chargetime.ocpp.test.FakeChargePoint
56
import spock.lang.Shared
@@ -9,7 +10,7 @@ import spock.util.concurrent.PollingConditions
910
class JSONDataTransferSpec extends Specification
1011
{
1112
@Shared
12-
FakeCentralSystem centralSystem = FakeCentralSystem.getInstance()
13+
FakeCentralSystem centralSystem = FakeCentral.getSystem(FakeCentral.serverType.JSON)
1314
@Shared
1415
FakeChargePoint chargePoint = new FakeChargePoint()
1516

ocpp-v1_6-test/src/test/groovy/eu/chargetime/ocpp/test/core/json/JSONGetConfigurationSpec.groovy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package eu.chargetime.ocpp.test.core.json
22

3+
import eu.chargetime.ocpp.test.FakeCentral
34
import eu.chargetime.ocpp.test.FakeCentralSystem
45
import eu.chargetime.ocpp.test.FakeChargePoint
56
import spock.lang.Shared
@@ -9,7 +10,7 @@ import spock.util.concurrent.PollingConditions
910
class JSONGetConfigurationSpec extends Specification
1011
{
1112
@Shared
12-
FakeCentralSystem centralSystem = FakeCentralSystem.getInstance()
13+
FakeCentralSystem centralSystem = FakeCentral.getSystem(FakeCentral.serverType.JSON)
1314
@Shared
1415
FakeChargePoint chargePoint = new FakeChargePoint()
1516

ocpp-v1_6-test/src/test/groovy/eu/chargetime/ocpp/test/core/json/JSONHeartbeatSpec.groovy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package eu.chargetime.ocpp.test.core.json
22

3+
import eu.chargetime.ocpp.test.FakeCentral
34
import eu.chargetime.ocpp.test.FakeCentralSystem
45
import eu.chargetime.ocpp.test.FakeChargePoint
56
import spock.lang.Shared
@@ -9,7 +10,7 @@ import spock.util.concurrent.PollingConditions
910
class JSONHeartbeatSpec extends Specification
1011
{
1112
@Shared
12-
FakeCentralSystem centralSystem = FakeCentralSystem.getInstance()
13+
FakeCentralSystem centralSystem = FakeCentral.getSystem(FakeCentral.serverType.JSON)
1314
@Shared
1415
FakeChargePoint chargePoint = new FakeChargePoint()
1516

0 commit comments

Comments
 (0)