Skip to content

Commit

Permalink
feat(ForkStatus): implement ForkStatus API
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyWu3027 committed Aug 20, 2023
1 parent 136bd53 commit 11a9312
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,14 @@ public void connectToChannel(int channelNum) {
}

public Network getChannel(int channelNum) {
return channels.get(channelNum);
Network channel = channels.get(channelNum);

if (channel == null) {
throw new IllegalArgumentException(
String.format("No connection was established to channel%d", channelNum));
}

return channel;
}

public void connectToChannels(Iterable<Integer> channelNums) {
Expand Down
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());
}
}

}
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);
}
}
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());
}
}
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);
}
}

0 comments on commit 11a9312

Please sign in to comment.