From b23fe19735267fadd80b22fe1ed75b50b0541f5c Mon Sep 17 00:00:00 2001 From: n0s09by Date: Tue, 20 Feb 2024 10:43:40 +0530 Subject: [PATCH] Changed the response model of show anchor peer API Signed-off-by: n0s09by --- .../client/controller/FabricOperationsController.java | 6 ++++-- .../hlf/java/rest/client/service/NetworkStatus.java | 3 ++- .../rest/client/service/impl/NetworkStatusImpl.java | 10 +++------- src/test/java/hlf/java/rest/client/IT/ChannelIT.java | 11 +++++------ .../controller/FabricOperationsControllerTest.java | 7 +++++-- .../client/service/impl/NetworkStatusImplTest.java | 7 +------ 6 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/main/java/hlf/java/rest/client/controller/FabricOperationsController.java b/src/main/java/hlf/java/rest/client/controller/FabricOperationsController.java index c9a0971..735675d 100644 --- a/src/main/java/hlf/java/rest/client/controller/FabricOperationsController.java +++ b/src/main/java/hlf/java/rest/client/controller/FabricOperationsController.java @@ -5,8 +5,10 @@ import hlf.java.rest.client.model.CommitChannelParamsDTO; import hlf.java.rest.client.service.NetworkStatus; import hlf.java.rest.client.util.SerializationUtil; +import java.util.Set; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.GetMapping; @@ -33,9 +35,9 @@ public class FabricOperationsController { * channel */ @GetMapping(value = "/channel/{channelName}/anchor_peers") - public ResponseEntity getAnchorPeerForChannel( + public ResponseEntity> getAnchorPeerForChannel( @PathVariable @Validated String channelName) { - return networkStatus.getAnchorPeerForChannel(channelName); + return new ResponseEntity<>(networkStatus.getAnchorPeerForChannel(channelName), HttpStatus.OK); } /** * Obtain the channel configuration details. diff --git a/src/main/java/hlf/java/rest/client/service/NetworkStatus.java b/src/main/java/hlf/java/rest/client/service/NetworkStatus.java index ee0cf96..0050707 100644 --- a/src/main/java/hlf/java/rest/client/service/NetworkStatus.java +++ b/src/main/java/hlf/java/rest/client/service/NetworkStatus.java @@ -3,13 +3,14 @@ import hlf.java.rest.client.model.ChannelUpdateParamsDTO; import hlf.java.rest.client.model.ClientResponseModel; import hlf.java.rest.client.model.CommitChannelParamsDTO; +import java.util.Set; import org.springframework.http.ResponseEntity; public interface NetworkStatus { ResponseEntity getChannelFromNetwork(String channelName); - ResponseEntity getAnchorPeerForChannel(String channelName); + Set getAnchorPeerForChannel(String channelName); ResponseEntity signChannelConfigTransaction( String channelName, String configUpdate); diff --git a/src/main/java/hlf/java/rest/client/service/impl/NetworkStatusImpl.java b/src/main/java/hlf/java/rest/client/service/impl/NetworkStatusImpl.java index f0f2c36..62a96ba 100644 --- a/src/main/java/hlf/java/rest/client/service/impl/NetworkStatusImpl.java +++ b/src/main/java/hlf/java/rest/client/service/impl/NetworkStatusImpl.java @@ -17,7 +17,6 @@ import hlf.java.rest.client.service.UpdateChannel; import hlf.java.rest.client.util.FabricClientConstants; import java.io.IOException; -import java.io.Serializable; import java.util.ArrayList; import java.util.Base64; import java.util.Collection; @@ -90,9 +89,8 @@ private Set getAnchorPeersFromOrgConfigGroup( } @Override - public ResponseEntity getAnchorPeerForChannel(String channelName) { + public Set getAnchorPeerForChannel(String channelName) { Network network = gateway.getNetwork(channelName); - Set anchorPeersSet = new HashSet<>(); try { if (network != null) { Channel selectedChannel = network.getChannel(); @@ -105,7 +103,7 @@ public ResponseEntity getAnchorPeerForChannel(String channe // Get all MSP IDs for a particular channel Collection peersOrganizationMSPIDs = selectedChannel.getPeersOrganizationMSPIDs(); log.debug("peersOrganizationMSPIDs: {}", peersOrganizationMSPIDs.toString()); - anchorPeersSet = getAnchorPeersFromOrgConfigGroup(peersOrganizationMSPIDs, application); + return getAnchorPeersFromOrgConfigGroup(peersOrganizationMSPIDs, application); } } catch (InvalidArgumentException e) { @@ -126,9 +124,7 @@ public ResponseEntity getAnchorPeerForChannel(String channe throw new ServiceException( ErrorCode.HYPERLEDGER_FABRIC_TRANSACTION_ERROR, "Error retrieving anchor peers", e); } - return new ResponseEntity<>( - new ClientResponseModel(ErrorConstants.NO_ERROR, (Serializable) anchorPeersSet), - HttpStatus.OK); + return new HashSet<>(); } @Override diff --git a/src/test/java/hlf/java/rest/client/IT/ChannelIT.java b/src/test/java/hlf/java/rest/client/IT/ChannelIT.java index 56d2fa1..b2d7ae7 100644 --- a/src/test/java/hlf/java/rest/client/IT/ChannelIT.java +++ b/src/test/java/hlf/java/rest/client/IT/ChannelIT.java @@ -303,7 +303,7 @@ public void channelJoinForTwoOrgsTest() { } @Test - @Order(6) + @Order(7) public void addOrgToChannelTest() { ChannelUpdateParamsDTO newOrgParamsDTO = new ChannelUpdateParamsDTO(); try { @@ -339,7 +339,7 @@ public void addOrgToChannelTest() { } @Test - @Order(7) + @Order(6) public void addAnchorPeersToChannelTest() { ChannelUpdateParamsDTO channelUpdateParamsDTO = new ChannelUpdateParamsDTO(); channelUpdateParamsDTO.setOrganizationMspId(ORG_1_MSP); @@ -356,9 +356,8 @@ public void addAnchorPeersToChannelTest() { @Test @Order(8) - public void getAnchorPeerForChannelTest() { - ResponseEntity responseModel = - networkStatus.getAnchorPeerForChannel(CHANNEL_NAME_TWO_ORGS); - Assertions.assertEquals(new Integer(200), responseModel.getStatusCodeValue()); + public void getAnchorPeerForChannelWithoutAnchorPeersTest() { + Set anchorPeersSet = networkStatus.getAnchorPeerForChannel(CHANNEL_NAME); + Assertions.assertEquals(new Integer(0), anchorPeersSet.size()); } } diff --git a/src/test/java/hlf/java/rest/client/controller/FabricOperationsControllerTest.java b/src/test/java/hlf/java/rest/client/controller/FabricOperationsControllerTest.java index e90b75c..af9ce71 100644 --- a/src/test/java/hlf/java/rest/client/controller/FabricOperationsControllerTest.java +++ b/src/test/java/hlf/java/rest/client/controller/FabricOperationsControllerTest.java @@ -6,6 +6,8 @@ import hlf.java.rest.client.model.CommitChannelParamsDTO; import hlf.java.rest.client.service.NetworkStatus; import hlf.java.rest.client.util.SerializationUtil; +import java.util.HashSet; +import java.util.Set; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -26,9 +28,10 @@ public class FabricOperationsControllerTest { @Test public void getAnchorPeerForChannelTest() { - ResponseEntity responseEntity = new ResponseEntity(HttpStatus.OK); + ResponseEntity> responseEntity = + new ResponseEntity<>(new HashSet<>(), HttpStatus.OK); Mockito.when(networkStatus.getAnchorPeerForChannel(Mockito.anyString())) - .thenReturn(responseEntity); + .thenReturn(new HashSet<>()); assertEquals( responseEntity, fabricOperationsController.getAnchorPeerForChannel("some_channel_name")); } diff --git a/src/test/java/hlf/java/rest/client/service/impl/NetworkStatusImplTest.java b/src/test/java/hlf/java/rest/client/service/impl/NetworkStatusImplTest.java index 4540004..f1cbbb7 100644 --- a/src/test/java/hlf/java/rest/client/service/impl/NetworkStatusImplTest.java +++ b/src/test/java/hlf/java/rest/client/service/impl/NetworkStatusImplTest.java @@ -99,9 +99,6 @@ public class NetworkStatusImplTest { @Test public void getAnchorPeerForChannelTest() throws InvalidArgumentException, TransactionException { - ResponseEntity responseEntity = - new ResponseEntity<>( - new ClientResponseModel(ErrorConstants.NO_ERROR, new HashSet<>()), HttpStatus.OK); Mockito.when(gateway.getNetwork(Mockito.anyString())).thenReturn(network); Mockito.when(network.getChannel()).thenReturn(channel); Mockito.when(channel.getChannelConfigurationBytes()).thenReturn(new byte[0]); @@ -110,9 +107,7 @@ public void getAnchorPeerForChannelTest() throws InvalidArgumentException, Trans Mockito.when(configTxConfigGroup.getGroupsMap()).thenReturn(groupMap); Mockito.when(groupMap.get(FabricClientConstants.CHANNEL_CONFIG_GROUP_APPLICATION)) .thenReturn(readset); - assertEquals( - responseEntity.getStatusCode(), - networkStatus.getAnchorPeerForChannel("some_channelname").getStatusCode()); + assertEquals(new HashSet<>(), networkStatus.getAnchorPeerForChannel("some_channelname")); } @Test