Skip to content

Commit

Permalink
Merge changes Ia88d6471,Ic93ace54
Browse files Browse the repository at this point in the history
* changes:
  FAB-4454 Increase junit coverage for Config.java
  FAB-4454 Remove unused and duplicated imports
  • Loading branch information
cr22rc authored and Gerrit Code Review committed Jun 9, 2017
2 parents 8ba882d + 584f456 commit c2602e8
Show file tree
Hide file tree
Showing 4 changed files with 319 additions and 30 deletions.
8 changes: 0 additions & 8 deletions src/main/java/org/hyperledger/fabric/sdk/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@
import org.hyperledger.fabric.protos.orderer.Ab.SeekInfo;
import org.hyperledger.fabric.protos.orderer.Ab.SeekPosition;
import org.hyperledger.fabric.protos.orderer.Ab.SeekSpecified;
import org.hyperledger.fabric.protos.peer.Configuration.AnchorPeer;
import org.hyperledger.fabric.protos.peer.Configuration.AnchorPeers;
import org.hyperledger.fabric.protos.peer.FabricProposal;
import org.hyperledger.fabric.protos.peer.FabricProposal.SignedProposal;
import org.hyperledger.fabric.protos.peer.FabricProposalResponse;
Expand Down Expand Up @@ -104,12 +102,6 @@
import org.hyperledger.fabric.sdk.transaction.UpgradeProposalBuilder;

import static java.lang.String.format;
import static org.hyperledger.fabric.protos.common.Common.HeaderType;
import static org.hyperledger.fabric.protos.common.Common.SignatureHeader;
import static org.hyperledger.fabric.protos.common.Common.Status;
import static org.hyperledger.fabric.protos.common.Configtx.ConfigValue;
import static org.hyperledger.fabric.protos.common.Policies.SignaturePolicy;
import static org.hyperledger.fabric.protos.common.Policies.SignaturePolicyEnvelope;
import static org.hyperledger.fabric.sdk.helper.Utils.checkGrpcUrl;
import static org.hyperledger.fabric.sdk.helper.Utils.toHexString;
import static org.hyperledger.fabric.sdk.transaction.ProtoUtils.createChannelHeader;
Expand Down
56 changes: 50 additions & 6 deletions src/test/java/org/hyperledger/fabric/sdk/TestConfigHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -13,6 +13,8 @@
*/
package org.hyperledger.fabric.sdk;

import java.util.Properties;

import org.hyperledger.fabric.sdk.helper.Config;

/**
Expand All @@ -27,30 +29,72 @@ public class TestConfigHelper {
* clearConfig "resets" Config so that the Config testcases can run without interference from other test suites.
* Depending on what order JUnit decides to run the tests, Config could have been instantiated earlier and could
* contain values that make the tests here fail.
*
* @throws SecurityException
* @throws NoSuchFieldException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*
*/
public void clearConfig() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
public void clearConfig()
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {

Config config = Config.getConfig();

// Set the private static variable Config.config = null
java.lang.reflect.Field configInstance = config.getClass().getDeclaredField("config");
configInstance.setAccessible(true);
configInstance.set(null, null);

// Clear the sdkProperties map - Config.sdkProperties.clear()
java.lang.reflect.Field sdkPropInstance = config.getClass().getDeclaredField("sdkProperties");
sdkPropInstance.setAccessible(true);
Properties sdkProperties = (Properties) sdkPropInstance.get(config);
sdkProperties.clear();

}

/**
* clearCaConfig "resets" Config used by fabric_ca so that the Config testcases can run without interference from
* other test suites.
*
* @throws SecurityException
* @throws NoSuchFieldException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*
* @see #clearConfig()
*/
public void clearCaConfig()
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {

org.hyperledger.fabric_ca.sdk.helper.Config config = org.hyperledger.fabric_ca.sdk.helper.Config.getConfig();

// Set the private static variable Config.config = null
java.lang.reflect.Field configInstance = config.getClass().getDeclaredField("config");
configInstance.setAccessible(true);
configInstance.set(null, null);

// Clear the sdkProperties map - Config.sdkProperties.clear()
java.lang.reflect.Field sdkPropInstance = config.getClass().getDeclaredField("sdkProperties");
sdkPropInstance.setAccessible(true);
Properties sdkProperties = (Properties) sdkPropInstance.get(config);
sdkProperties.clear();

}

/**
* customizeConfig() sets up the properties listed by env var CONFIG_OVERRIDES
* The value of the env var is <i>property1=value1,property2=value2</i> and so on where each <i>property</i> is a property
* from the SDK's config file.
* customizeConfig() sets up the properties listed by env var CONFIG_OVERRIDES The value of the env var is
* <i>property1=value1,property2=value2</i> and so on where each <i>property</i> is a property from the SDK's config
* file.
*
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public void customizeConfig() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
public void customizeConfig()
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
String fabricSdkConfig = System.getenv(CONFIG_OVERRIDES);
if (fabricSdkConfig != null && fabricSdkConfig.length() > 0) {
String[] configs = fabricSdkConfig.split(",");
Expand Down
137 changes: 121 additions & 16 deletions src/test/java/org/hyperledger/fabric/sdk/helper/ConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,149 @@

package org.hyperledger.fabric.sdk.helper;

import org.apache.log4j.Level;

import org.hyperledger.fabric.sdk.TestConfigHelper;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class ConfigTest {

private TestConfigHelper configHelper = new TestConfigHelper();
private String originalValue;
// private String originalHashAlgorithm;

@Before
public void setUp() throws Exception {
originalValue = Config.getConfig().getHashAlgorithm();
// reset Config before each test
configHelper.clearConfig();
}

@After
public void tearDown() throws Exception {
// reset Config after each test. We do not want to interfere with the next test or the next test suite
// reset Config after each test. We do not want to interfere with the
// next test or the next test suite
configHelper.clearConfig();
}

// Tests that Config.getConfig properly loads a value from a system property
@Test
public void testGetConfigSystemProperty() throws Exception {

final String propName = Config.HASH_ALGORITHM;

// Get the original value of the property so that we can restore it later
final String originalValue = System.getProperty(propName);

try {

// Configure the system property with the value to be tested
final String newVal = "XXX";
System.setProperty(propName, newVal);

// getConfig should load the property from System
Config config = Config.getConfig();
Assert.assertEquals(config.getHashAlgorithm(), newVal);
} finally {
// Restore the system property
setSystemProperty(propName, originalValue);
}

}

// Note: This unit test is of questionable value
// It may be better to actually set the individual values (via a system property)
// and make sure they come back again!
@Test
public void testGetters() {
Config config = Config.getConfig();

// Numeric params
Assert.assertTrue(config.getSecurityLevel() > 0);
Assert.assertTrue(config.getProposalWaitTime() > 0);
Assert.assertTrue(config.getGenesisBlockWaitTime() > 0);
Assert.assertTrue(config.getSymmetricKeyByteCount() > 0);
Assert.assertTrue(config.getMACKeyByteCount() > 0);

Assert.assertTrue(config.maxLogStringLength() > 0);

// Boolean params
// Not sure how best to deal with these, as they will always return either true or false
// So, for coverage, let's simply call the method to ensure they don't throw exceptions...
config.getProposalConsistencyValidation();

// String params
Assert.assertNotNull(config.getHashAlgorithm());
Assert.assertNotNull(config.getAsymmetricKeyType());
Assert.assertNotNull(config.getSymmetricKeyType());
Assert.assertNotNull(config.getKeyAgreementAlgorithm());
Assert.assertNotNull(config.getSymmetricAlgorithm());
Assert.assertNotNull(config.getSignatureAlgorithm());
Assert.assertNotNull(config.getCertificateFormat());

// Arrays
Assert.assertNotNull(config.getPeerCACerts());
}

@Test
public void testGetConfig() {
System.setProperty(Config.HASH_ALGORITHM, "XXX");
public void testExtraLogLevel() {
Config config = Config.getConfig();
assertEquals(config.getSecurityLevel(), 256);
assertEquals(config.getHashAlgorithm(), "XXX");
String[] cacerts = config.getPeerCACerts();
assertEquals(cacerts[0], "/genesisblock/peercacert.pem");

// Clean up so that other tests can get valid values from the Config singleton.
// configHelper.clearConfig() is not enough. System.clearProperty() is not enough.
// Have to set the system property back to the original value as well.
System.setProperty(Config.HASH_ALGORITHM, originalValue);
Assert.assertTrue(config.extraLogLevel(-99));
Assert.assertFalse(config.extraLogLevel(99));
}

@Test
public void testLogLevelTrace() {
testLogLevelAny("TRACE", org.apache.log4j.Level.TRACE);
}

@Test
public void testLogLevelDebug() {
testLogLevelAny("DEBUG", org.apache.log4j.Level.DEBUG);
}

@Test
public void testLogLevelInfo() {
testLogLevelAny("INFO", org.apache.log4j.Level.INFO);
}

@Test
public void testLogLevelWarn() {
testLogLevelAny("WARN", org.apache.log4j.Level.WARN);
}

@Test
public void testLogLevelError() {
testLogLevelAny("ERROR", org.apache.log4j.Level.ERROR);
}

// ==========================================================================================
// Helper methods
// ==========================================================================================

// Helper method to set the value of a system property
private Object setSystemProperty(String propName, String propValue) {
if (propValue == null) {
System.clearProperty(propName);
} else {
System.setProperty(propName, propValue);
}
return propValue;
}

// Helper function to test one of the possible log levels
private void testLogLevelAny(String levelString, Level level) {
String originalValue = System.setProperty(Config.LOGGERLEVEL, levelString);
try {
// Dummy call to ensure that a config instance is created and the
// underlying logging level is set...
Config.getConfig();
Assert.assertEquals(level, org.apache.log4j.Logger.getLogger("org.hyperledger.fabric").getLevel());
} finally {
// Restore the original value so that other tests run consistently
setSystemProperty(Config.LOGGERLEVEL, originalValue);
}
}

}
Loading

0 comments on commit c2602e8

Please sign in to comment.