Skip to content

Commit 1e5c478

Browse files
committed
validation and test modification
Signed-off-by: n0s09by <[email protected]>
1 parent dddc7a2 commit 1e5c478

File tree

3 files changed

+43
-55
lines changed

3 files changed

+43
-55
lines changed

src/main/java/hlf/java/rest/client/service/impl/NetworkStatusImpl.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public ResponseEntity<ClientResponseModel> generateConfigUpdate(
127127
private ConfigUpdate createConfigUpdate(
128128
String channelName, ChannelUpdateParamsDTO organizationDetails) {
129129
Network network = gateway.getNetwork(channelName);
130-
if (network != null) {
130+
if (network != null && organizationDetails.getMspDTO() != null) {
131131
try {
132132
Channel selectedChannel = network.getChannel();
133133
byte[] channelConfigBytes = selectedChannel.getChannelConfigurationBytes();
@@ -178,7 +178,11 @@ private ConfigUpdate createConfigUpdate(
178178
}
179179
} else {
180180
log.warn(
181-
"Error fetching the channel config: Network cannot be NULL: " + "Network = " + network);
181+
"Error fetching the channel config: Network and MSP details cannot be NULL: "
182+
+ "Network = "
183+
+ network
184+
+ " MSP details = "
185+
+ organizationDetails.getMspDTO());
182186
return ConfigUpdate.newBuilder().build();
183187
}
184188
}
@@ -321,7 +325,7 @@ public ResponseEntity<ClientResponseModel> commitChannelConfigTransaction(
321325
public ResponseEntity<ClientResponseModel> addOrgToChannel(
322326
String channelName, ChannelUpdateParamsDTO organizationDetails) {
323327
Network network = gateway.getNetwork(channelName);
324-
if (network != null && user != null) {
328+
if (network != null && user != null && organizationDetails.getMspDTO() != null) {
325329
try {
326330
Channel selectedChannel = network.getChannel();
327331
ConfigUpdate configUpdate = createConfigUpdate(channelName, organizationDetails);
@@ -358,11 +362,24 @@ public ResponseEntity<ClientResponseModel> addOrgToChannel(
358362
new ClientResponseModel(ErrorConstants.NO_ERROR, ErrorCode.SUCCESS.getReason()),
359363
HttpStatus.OK);
360364
} else {
361-
log.warn("Network and User cannot be NULL: " + "Network = " + network + "and User = " + user);
365+
log.warn(
366+
"Network, User and MSP details cannot be NULL: "
367+
+ "Network = "
368+
+ network
369+
+ " User = "
370+
+ user
371+
+ " and MSP details = "
372+
+ organizationDetails.getMspDTO());
362373
return new ResponseEntity<>(
363374
new ClientResponseModel(
364375
ErrorCode.NOT_FOUND.getValue(),
365-
"Network and User cannot be NULL: " + "Network = " + network + "and User = " + user),
376+
"Network, User and MSP details cannot be NULL: "
377+
+ "Network = "
378+
+ network
379+
+ " User = "
380+
+ user
381+
+ " and MSP details = "
382+
+ organizationDetails.getMspDTO()),
366383
HttpStatus.OK);
367384
}
368385
}

src/main/java/hlf/java/rest/client/service/impl/UpdateChannelImpl.java

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -32,42 +32,21 @@ public ConfigGroup buildWriteset(ConfigGroup readset, ChannelUpdateParamsDTO org
3232
Map<String, ConfigGroup> existingOrganizations =
3333
FabricChannelUtil.getExistingOrgsFromReadset(readset);
3434

35-
ConfigGroup applicationGroup;
36-
37-
if (organizationDetails.getMspDTO() != null) {
38-
// New org addition scenario
39-
// The "Application" group
40-
applicationGroup =
41-
ConfigGroup.newBuilder()
42-
.setModPolicy(FabricClientConstants.CHANNEL_CONFIG_MOD_POLICY_ADMINS)
43-
.putAllPolicies(FabricChannelUtil.setApplicationPolicies(readset))
44-
.putGroups(newOrgMspId, setNewOrgGroup(newOrgMspId, organizationDetails))
45-
// putAllGroups excludes new organization
46-
.putAllGroups(existingOrganizations)
47-
// Application group version
48-
.setVersion(
49-
FabricChannelUtil.retrieveMSPGroupVersionFromReadset(
50-
readset, FabricClientConstants.CHANNEL_CONFIG_GROUP_APPLICATION)
51-
+ 1) // will be tied to current version + 1 for this level
52-
.build();
53-
} else {
54-
// The "Application" group
55-
applicationGroup =
56-
ConfigGroup.newBuilder()
57-
.setModPolicy(FabricClientConstants.CHANNEL_CONFIG_MOD_POLICY_ADMINS)
58-
.putAllPolicies(FabricChannelUtil.setApplicationPolicies(readset))
59-
.putGroups(
60-
newOrgMspId, setAnchorPeerInGroup(newOrgMspId, readset, organizationDetails))
61-
// putAllGroups excludes new organization
62-
.putAllGroups(existingOrganizations)
63-
// Application group version
64-
.setVersion(
65-
FabricChannelUtil.retrieveMSPGroupVersionFromReadset(
66-
readset, FabricClientConstants.CHANNEL_CONFIG_GROUP_APPLICATION)
67-
+ 1) // will be tied to current version + 1 for this level
68-
.build();
69-
}
70-
35+
// New org addition scenario
36+
// The "Application" group
37+
ConfigGroup applicationGroup =
38+
ConfigGroup.newBuilder()
39+
.setModPolicy(FabricClientConstants.CHANNEL_CONFIG_MOD_POLICY_ADMINS)
40+
.putAllPolicies(FabricChannelUtil.setApplicationPolicies(readset))
41+
.putGroups(newOrgMspId, setNewOrgGroup(newOrgMspId, organizationDetails))
42+
// putAllGroups excludes new organization
43+
.putAllGroups(existingOrganizations)
44+
// Application group version
45+
.setVersion(
46+
FabricChannelUtil.retrieveMSPGroupVersionFromReadset(
47+
readset, FabricClientConstants.CHANNEL_CONFIG_GROUP_APPLICATION)
48+
+ 1) // will be tied to current version + 1 for this level
49+
.build();
7150
// the "/Channel" group
7251
return ConfigGroup.newBuilder()
7352
.putGroups(FabricClientConstants.CHANNEL_CONFIG_GROUP_APPLICATION, applicationGroup)

src/test/java/hlf/java/rest/client/service/impl/NetworkStatusImplTest.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import hlf.java.rest.client.model.ChannelUpdateParamsDTO;
1414
import hlf.java.rest.client.model.ClientResponseModel;
1515
import hlf.java.rest.client.model.CommitChannelParamsDTO;
16+
import hlf.java.rest.client.model.MSPDTO;
1617
import hlf.java.rest.client.service.ChannelConfigDeserialization;
1718
import java.util.ArrayList;
1819
import java.util.Collections;
@@ -72,6 +73,7 @@ public class NetworkStatusImplTest {
7273
@Mock private ConfigGroup readset;
7374

7475
@Mock private ConfigGroup writeset;
76+
@Mock private MSPDTO mspdto;
7577

7678
@Mock private Builder builder;
7779

@@ -106,26 +108,15 @@ public void getChannelFromNetworkTest()
106108
}
107109

108110
@Test
109-
public void generateConfigUpdateTest()
110-
throws InvalidProtocolBufferException, InvalidArgumentException, TransactionException {
111+
public void generateConfigUpdateTest() throws InvalidProtocolBufferException {
111112
ResponseEntity<ClientResponseModel> responseEntity =
112113
new ResponseEntity<>(
113114
new ClientResponseModel(ErrorConstants.NO_ERROR, "dGhlX2NvbmZpZw=="), HttpStatus.OK);
114115
Mockito.when(gateway.getNetwork(Mockito.anyString())).thenReturn(network);
115-
Mockito.when(network.getChannel()).thenReturn(channel);
116-
117-
Mockito.when(channel.getChannelConfigurationBytes()).thenReturn(new byte[0]);
118116
staticConfigUpdate
119117
.when(() -> ConfigUpdate.parseFrom(Mockito.any(byte[].class)))
120118
.thenReturn(configUpdate);
121119
staticConfigUpdate.when(() -> ConfigUpdate.newBuilder()).thenReturn(builder);
122-
Mockito.when(configUpdate.getReadSet()).thenReturn(readset);
123-
Mockito.when(builder.setChannelId(Mockito.anyString())).thenReturn(builder);
124-
Mockito.when(builder.setReadSet(Mockito.any(ConfigGroup.class))).thenReturn(builder);
125-
Mockito.when(
126-
updateChannel.buildWriteset(Mockito.any(), Mockito.any(ChannelUpdateParamsDTO.class)))
127-
.thenReturn(readset);
128-
Mockito.when(builder.setWriteSet(Mockito.any(ConfigGroup.class))).thenReturn(builder);
129120
Mockito.when(builder.build()).thenReturn(configUpdate);
130121
staticJsonFormat.when(JsonFormat::printer).thenReturn(printer);
131122
Mockito.when(printer.print(Mockito.any(MessageOrBuilder.class))).thenReturn("the_config");
@@ -201,7 +192,6 @@ public void addOrgToChannelTest() throws InvalidArgumentException, TransactionEx
201192

202193
Mockito.when(gateway.getNetwork(Mockito.anyString())).thenReturn(network);
203194
Mockito.when(network.getChannel()).thenReturn(channel);
204-
205195
Mockito.when(channel.getChannelConfigurationBytes()).thenReturn(new byte[0]);
206196
staticConfigUpdate
207197
.when(() -> ConfigUpdate.parseFrom(Mockito.any(byte[].class)))
@@ -224,10 +214,12 @@ public void addOrgToChannelTest() throws InvalidArgumentException, TransactionEx
224214
Mockito.any(UpdateChannelConfiguration.class), Mockito.any(User.class)))
225215
.thenReturn(outputByteArray);
226216

217+
Mockito.when(channelUpdateParamsDTO.getMspDTO()).thenReturn(mspdto);
218+
227219
assertEquals(
228220
responseEntity.getBody().getMessage(),
229221
networkStatus
230-
.addOrgToChannel("some_channel_name", new ChannelUpdateParamsDTO())
222+
.addOrgToChannel("some_channel_name", channelUpdateParamsDTO)
231223
.getBody()
232224
.getMessage());
233225
}

0 commit comments

Comments
 (0)