-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ForkStatus): implement ForkStatus API
- Loading branch information
1 parent
136bd53
commit 11a9312
Showing
5 changed files
with
191 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/java/uk/ac/ic/doc/blocc/dashboard/forkstatus/ForkStatusController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package uk.ac.ic.doc.blocc.dashboard.forkstatus; | ||
|
||
import org.hyperledger.fabric.client.GatewayException; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.server.ResponseStatusException; | ||
|
||
@RestController | ||
@Profile({"prod", "fork-status-controller-test"}) | ||
@RequestMapping(path = "/api/v1/forkStatus") | ||
public class ForkStatusController { | ||
|
||
private final ForkStatusService service; | ||
|
||
@Autowired | ||
public ForkStatusController(ForkStatusService service) { | ||
this.service = service; | ||
} | ||
|
||
@GetMapping | ||
public boolean getForkStatus(@RequestParam int containerNum) throws GatewayException { | ||
try { | ||
return service.getForkStatus(containerNum); | ||
} catch (IllegalArgumentException e) { | ||
throw new ResponseStatusException(HttpStatus.NOT_FOUND, e.getMessage()); | ||
} | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/uk/ac/ic/doc/blocc/dashboard/forkstatus/ForkStatusService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package uk.ac.ic.doc.blocc.dashboard.forkstatus; | ||
|
||
import com.google.gson.Gson; | ||
import org.hyperledger.fabric.client.Contract; | ||
import org.hyperledger.fabric.client.GatewayException; | ||
import org.hyperledger.fabric.client.Network; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.stereotype.Service; | ||
import uk.ac.ic.doc.blocc.dashboard.fabric.BloccConnections; | ||
|
||
@Service | ||
@Profile("prod") | ||
public class ForkStatusService { | ||
|
||
private final BloccConnections connections; | ||
private final Gson gson = new Gson(); | ||
|
||
@Autowired | ||
public ForkStatusService(BloccConnections connections) { | ||
this.connections = connections; | ||
} | ||
|
||
public boolean getForkStatus(int containerNum) throws GatewayException { | ||
Network channel = connections.getChannel(containerNum); | ||
Contract bscc = channel.getContract("bscc"); | ||
byte[] checkForkStatuses = bscc.evaluateTransaction("CheckForkStatus", | ||
String.format("channel%d", containerNum)); | ||
return gson.fromJson(new String(checkForkStatuses), Boolean.class); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/test/java/uk/ac/ic/doc/blocc/dashboard/forkstatus/ForkStatusControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package uk.ac.ic.doc.blocc.dashboard.forkstatus; | ||
|
||
import static org.mockito.Mockito.when; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
@WebMvcTest(ForkStatusController.class) | ||
@ActiveProfiles("fork-status-controller-test") | ||
public class ForkStatusControllerTest { | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@MockBean | ||
private ForkStatusService service; | ||
|
||
@Test | ||
public void returnsTrueIfForkDetected() throws Exception { | ||
int containerNum = 5; | ||
when(service.getForkStatus(containerNum)).thenReturn(true); | ||
|
||
mockMvc.perform(get("/api/v1/forkStatus") | ||
.param("containerNum", String.valueOf(containerNum))) | ||
.andExpect(status().isOk()) | ||
.andExpect(content().string("true")); | ||
} | ||
|
||
@Test | ||
public void returnsFalseIfForkNotDetected() throws Exception { | ||
int containerNum = 5; | ||
when(service.getForkStatus(containerNum)).thenReturn(false); | ||
|
||
mockMvc.perform(get("/api/v1/forkStatus") | ||
.param("containerNum", String.valueOf(containerNum))) | ||
.andExpect(status().isOk()) | ||
.andExpect(content().string("false")); | ||
} | ||
|
||
@Test | ||
public void returnsNotFoundWhenIllegalArgumentExceptionThrown() throws Exception { | ||
int containerNum = 5; | ||
when(service.getForkStatus(containerNum)).thenThrow( | ||
new IllegalArgumentException("Entity not found")); | ||
|
||
mockMvc.perform(get("/api/v1/forkStatus") | ||
.param("containerNum", String.valueOf(containerNum))) | ||
.andExpect(status().isNotFound()); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/test/java/uk/ac/ic/doc/blocc/dashboard/forkstatus/ForkStatusServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package uk.ac.ic.doc.blocc.dashboard.forkstatus; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.Mockito.when; | ||
|
||
import org.hyperledger.fabric.client.Contract; | ||
import org.hyperledger.fabric.client.GatewayException; | ||
import org.hyperledger.fabric.client.Network; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import uk.ac.ic.doc.blocc.dashboard.fabric.BloccConnections; | ||
|
||
public class ForkStatusServiceTest { | ||
|
||
@Mock | ||
private BloccConnections connections; | ||
|
||
@Mock | ||
private Network channel; | ||
|
||
@Mock | ||
private Contract bscc; | ||
|
||
@InjectMocks | ||
private ForkStatusService service; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
} | ||
|
||
@Test | ||
public void returnsTrueIfForkDetected() throws GatewayException { | ||
int containerNum = 5; | ||
when(connections.getChannel(containerNum)).thenReturn(channel); | ||
when(channel.getContract("bscc")).thenReturn(bscc); | ||
when(bscc.evaluateTransaction("CheckForkStatus", "channel5")) | ||
.thenReturn("true".getBytes()); | ||
|
||
boolean result = service.getForkStatus(containerNum); | ||
|
||
assertTrue(result); | ||
} | ||
|
||
@Test | ||
public void returnsFalseIfForkNotDetected() throws GatewayException { | ||
int containerNum = 5; | ||
when(connections.getChannel(containerNum)).thenReturn(channel); | ||
when(channel.getContract("bscc")).thenReturn(bscc); | ||
when(bscc.evaluateTransaction("CheckForkStatus", "channel5")) | ||
.thenReturn("false".getBytes()); | ||
|
||
boolean result = service.getForkStatus(containerNum); | ||
|
||
assertFalse(result); | ||
} | ||
} |