Skip to content

Commit 55e3c67

Browse files
committed
#32 : Started work on restart endpoint
1 parent eef0123 commit 55e3c67

File tree

16 files changed

+244
-6
lines changed

16 files changed

+244
-6
lines changed

bxbot-core/src/main/java/com/gazbert/bxbot/BXBot.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
2121
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2222
*/
23-
2423
package com.gazbert.bxbot;
2524

2625
import com.gazbert.bxbot.core.engine.TradingEngine;
@@ -30,7 +29,7 @@
3029
import org.springframework.boot.autoconfigure.SpringBootApplication;
3130

3231
/**
33-
* BX-bot - here be the main boot app.
32+
* The BX-bot application.
3433
*
3534
* @author gazbert
3635
*/

bxbot-rest-api/pom.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@
5757
<groupId>org.springframework.boot</groupId>
5858
<artifactId>spring-boot-starter-log4j2</artifactId>
5959
</dependency>
60+
<dependency>
61+
<groupId>org.springframework.boot</groupId>
62+
<artifactId>spring-boot-starter-actuator</artifactId>
63+
</dependency>
64+
<dependency>
65+
<groupId>org.springframework.cloud</groupId>
66+
<artifactId>spring-cloud-starter</artifactId>
67+
</dependency>
6068
<dependency>
6169
<groupId>com.google.guava</groupId>
6270
<artifactId>guava</artifactId>
@@ -90,6 +98,28 @@
9098
<plugin>
9199
<groupId>org.jacoco</groupId>
92100
<artifactId>jacoco-maven-plugin</artifactId>
101+
<executions>
102+
<execution>
103+
<id>jacoco-check</id>
104+
<goals>
105+
<goal>check</goal>
106+
</goals>
107+
<configuration>
108+
<rules>
109+
<rule>
110+
<element>PACKAGE</element>
111+
<limits>
112+
<limit>
113+
<counter>LINE</counter>
114+
<value>COVEREDRATIO</value>
115+
<minimum>0.8</minimum>
116+
</limit>
117+
</limits>
118+
</rule>
119+
</rules>
120+
</configuration>
121+
</execution>
122+
</executions>
93123
</plugin>
94124
</plugins>
95125
</build>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2019 gazbert
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
* this software and associated documentation files (the "Software"), to deal in
8+
* the Software without restriction, including without limitation the rights to
9+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10+
* the Software, and to permit persons to whom the Software is furnished to do so,
11+
* subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*/
23+
package com.gazbert.bxbot.rest.api.v1.runtime;
24+
25+
import com.gazbert.bxbot.services.runtime.BotRestartService;
26+
import org.apache.logging.log4j.LogManager;
27+
import org.apache.logging.log4j.Logger;
28+
import org.springframework.beans.factory.annotation.Autowired;
29+
import org.springframework.http.HttpStatus;
30+
import org.springframework.http.ResponseEntity;
31+
import org.springframework.security.core.annotation.AuthenticationPrincipal;
32+
import org.springframework.security.core.userdetails.User;;
33+
import org.springframework.web.bind.annotation.RequestMapping;
34+
import org.springframework.web.bind.annotation.RequestMethod;
35+
import org.springframework.web.bind.annotation.RestController;
36+
37+
import static com.gazbert.bxbot.rest.api.v1.runtime.AbstractRuntimeController.RUNTIME_ENDPOINT_BASE_URI;
38+
39+
/**
40+
* Controller for directing Bot restart requests.
41+
*
42+
* @author gazbert
43+
* @since 1.0
44+
*/
45+
@RestController
46+
@RequestMapping(RUNTIME_ENDPOINT_BASE_URI)
47+
public class BotRestartController extends AbstractRuntimeController {
48+
49+
private static final Logger LOG = LogManager.getLogger();
50+
private static final String RESTART_RESOURCE_PATH = "/restart";
51+
private final BotRestartService botRestartService;
52+
53+
@Autowired
54+
public BotRestartController(BotRestartService botRestartService) {
55+
this.botRestartService = botRestartService;
56+
}
57+
58+
/**
59+
* Restarts the bot.
60+
*
61+
* @param user the authenticated user making the request.
62+
* @return 200 OK on success, some other HTTP status code otherwise.
63+
*/
64+
@RequestMapping(value = RESTART_RESOURCE_PATH, method = RequestMethod.POST)
65+
public ResponseEntity<?> restart(@AuthenticationPrincipal User user) {
66+
67+
LOG.info("POST " + RESTART_RESOURCE_PATH + " - restart() - caller: " + user.getUsername());
68+
69+
botRestartService.restart();
70+
return buildResponseEntity(null, HttpStatus.OK);
71+
}
72+
}
73+

bxbot-rest-api/src/main/java/com/gazbert/bxbot/rest/api/v1/runtime/BotStatusController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import static com.gazbert.bxbot.rest.api.v1.runtime.AbstractRuntimeController.RUNTIME_ENDPOINT_BASE_URI;
3838

3939
/**
40-
* Controller for directing Bot Status requests.
40+
* Controller for directing Bot status requests.
4141
*
4242
* @author gazbert
4343
* @since 1.0

bxbot-rest-api/src/test/java/com/gazbert/bxbot/rest/api/v1/config/TestEmailAlertsConfigController.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.junit.runner.RunWith;
3232
import org.springframework.boot.test.context.SpringBootTest;
3333
import org.springframework.boot.test.mock.mockito.MockBean;
34+
import org.springframework.cloud.context.restart.RestartEndpoint;
3435
import org.springframework.http.MediaType;
3536
import org.springframework.test.context.junit4.SpringRunner;
3637
import org.springframework.test.context.web.WebAppConfiguration;
@@ -73,6 +74,10 @@ public class TestEmailAlertsConfigController extends AbstractConfigControllerTes
7374
@MockBean
7475
private TradingEngine tradingEngine;
7576

77+
// Need this even though not used in the test directly because Spring loads it on startup...
78+
@MockBean
79+
private RestartEndpoint restartEndpoint;
80+
7681
@Before
7782
public void setupBeforeEachTest() {
7883
mockMvc = MockMvcBuilders.webAppContextSetup(ctx).addFilter(springSecurityFilterChain).build();

bxbot-rest-api/src/test/java/com/gazbert/bxbot/rest/api/v1/config/TestEngineConfigController.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.junit.runner.RunWith;
3232
import org.springframework.boot.test.context.SpringBootTest;
3333
import org.springframework.boot.test.mock.mockito.MockBean;
34+
import org.springframework.cloud.context.restart.RestartEndpoint;
3435
import org.springframework.http.MediaType;
3536
import org.springframework.test.context.junit4.SpringRunner;
3637
import org.springframework.test.context.web.WebAppConfiguration;
@@ -77,6 +78,10 @@ public class TestEngineConfigController extends AbstractConfigControllerTest {
7778
@MockBean
7879
private EmailAlerter emailAlerter;
7980

81+
// Need this even though not used in the test directly because Spring loads it on startup...
82+
@MockBean
83+
private RestartEndpoint restartEndpoint;
84+
8085
@Before
8186
public void setupBeforeEachTest() {
8287
mockMvc = MockMvcBuilders.webAppContextSetup(ctx).addFilter(springSecurityFilterChain).build();

bxbot-rest-api/src/test/java/com/gazbert/bxbot/rest/api/v1/config/TestExchangeConfigController.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.junit.runner.RunWith;
3333
import org.springframework.boot.test.context.SpringBootTest;
3434
import org.springframework.boot.test.mock.mockito.MockBean;
35+
import org.springframework.cloud.context.restart.RestartEndpoint;
3536
import org.springframework.http.MediaType;
3637
import org.springframework.test.context.junit4.SpringRunner;
3738
import org.springframework.test.context.web.WebAppConfiguration;
@@ -95,6 +96,10 @@ public class TestExchangeConfigController extends AbstractConfigControllerTest {
9596
@MockBean
9697
private EmailAlerter emailAlerter;
9798

99+
// Need this even though not used in the test directly because Spring loads it on startup...
100+
@MockBean
101+
private RestartEndpoint restartEndpoint;
102+
98103
@Before
99104
public void setupBeforeEachTest() {
100105
mockMvc = MockMvcBuilders.webAppContextSetup(ctx).addFilter(springSecurityFilterChain).build();

bxbot-rest-api/src/test/java/com/gazbert/bxbot/rest/api/v1/config/TestMarketConfigController.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.junit.runner.RunWith;
3232
import org.springframework.boot.test.context.SpringBootTest;
3333
import org.springframework.boot.test.mock.mockito.MockBean;
34+
import org.springframework.cloud.context.restart.RestartEndpoint;
3435
import org.springframework.http.MediaType;
3536
import org.springframework.test.context.junit4.SpringRunner;
3637
import org.springframework.test.context.web.WebAppConfiguration;
@@ -89,6 +90,9 @@ public class TestMarketConfigController extends AbstractConfigControllerTest {
8990
@MockBean
9091
private TradingEngine tradingEngine;
9192

93+
// Need this even though not used in the test directly because Spring loads it on startup...
94+
@MockBean
95+
private RestartEndpoint restartEndpoint;
9296

9397
@Before
9498
public void setupBeforeEachTest() {

bxbot-rest-api/src/test/java/com/gazbert/bxbot/rest/api/v1/config/TestStrategyConfigController.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.junit.runner.RunWith;
3232
import org.springframework.boot.test.context.SpringBootTest;
3333
import org.springframework.boot.test.mock.mockito.MockBean;
34+
import org.springframework.cloud.context.restart.RestartEndpoint;
3435
import org.springframework.http.MediaType;
3536
import org.springframework.test.context.junit4.SpringRunner;
3637
import org.springframework.test.context.web.WebAppConfiguration;
@@ -92,6 +93,10 @@ public class TestStrategyConfigController extends AbstractConfigControllerTest {
9293
@MockBean
9394
private EmailAlerter emailAlerter;
9495

96+
// Need this even though not used in the test directly because Spring loads it on startup...
97+
@MockBean
98+
private RestartEndpoint restartEndpoint;
99+
95100
@Before
96101
public void setupBeforeEachTest() {
97102
mockMvc = MockMvcBuilders.webAppContextSetup(ctx).addFilter(springSecurityFilterChain).build();

bxbot-rest-api/src/test/java/com/gazbert/bxbot/rest/api/v1/runtime/TestBotStatusController.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.junit.runner.RunWith;
3333
import org.springframework.boot.test.context.SpringBootTest;
3434
import org.springframework.boot.test.mock.mockito.MockBean;
35+
import org.springframework.cloud.context.restart.RestartEndpoint;
3536
import org.springframework.http.MediaType;
3637
import org.springframework.test.context.junit4.SpringRunner;
3738
import org.springframework.test.context.web.WebAppConfiguration;
@@ -78,6 +79,10 @@ public class TestBotStatusController extends AbstractRuntimeControllerTest {
7879
@MockBean
7980
private EmailAlerter emailAlerter;
8081

82+
// Need this even though not used in the test directly because Spring loads it on startup...
83+
@MockBean
84+
private RestartEndpoint restartEndpoint;
85+
8186
@Before
8287
public void setupBeforeEachTest() {
8388
mockMvc = MockMvcBuilders.webAppContextSetup(ctx).addFilter(springSecurityFilterChain).build();

0 commit comments

Comments
 (0)