diff options
author | 2017-08-08 14:02:53 +0800 | |
---|---|---|
committer | 2017-08-08 14:02:53 +0800 | |
commit | c0604184b2aa8cff924ca783ec6b36f1f5988775 (patch) | |
tree | 1c70ffe1ad90c915b3382a37eb2fa80901519c7b /wso2/common-util/src/test | |
parent | 706fc9fed496972968fde136c3e4e10f9578a5b3 (diff) |
init code
Change-Id: Icd0948118397b256da70dfbcbbec5520dc5eafd4
Signed-off-by: Zhaoxing <meng.zhaoxing1@zte.com.cn>
Diffstat (limited to 'wso2/common-util/src/test')
6 files changed, 832 insertions, 0 deletions
diff --git a/wso2/common-util/src/test/java/org/openo/baseservice/bus/util/TestRegisterService.java b/wso2/common-util/src/test/java/org/openo/baseservice/bus/util/TestRegisterService.java new file mode 100644 index 0000000..88f5ddd --- /dev/null +++ b/wso2/common-util/src/test/java/org/openo/baseservice/bus/util/TestRegisterService.java @@ -0,0 +1,66 @@ +/* + * Copyright 2016 Huawei Technologies Co., Ltd. + * + * 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 + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openo.baseservice.bus.util; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; + +import javax.ws.rs.core.Response; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.openo.baseservice.util.impl.SystemEnvVariablesDefImpl; + +import junit.framework.Assert; +import mockit.Mock; +import mockit.MockUp; + +public class TestRegisterService { + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void testregisterServce() throws IOException { + + File file = new File(""); + Response res = null; + + final String path = file.getAbsolutePath(); + + new MockUp<SystemEnvVariablesDefImpl>() { + @Mock + public String getAppRoot() { + return path; + } + }; + + try { + res = RegisterService.registerService(path, true); + } catch(Exception e) { + Assert.assertNotNull(e); + } + + } +} + diff --git a/wso2/common-util/src/test/java/org/openo/baseservice/bus/util/TestRegisterServiceListener.java b/wso2/common-util/src/test/java/org/openo/baseservice/bus/util/TestRegisterServiceListener.java new file mode 100644 index 0000000..429d986 --- /dev/null +++ b/wso2/common-util/src/test/java/org/openo/baseservice/bus/util/TestRegisterServiceListener.java @@ -0,0 +1,60 @@ +/* + * Copyright 2016 Huawei Technologies Co., Ltd. + * + * 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 + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openo.baseservice.bus.util; + +import java.io.File; + +import javax.servlet.ServletContextEvent; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import mockit.Mock; +import mockit.MockUp; + +public class TestRegisterServiceListener { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void testRegisterServiceListener() { + + RegisterServiceListener impl = new RegisterServiceListener(); + + ServletContextEvent sce = null; + + new MockUp<File>() { + @Mock + public File[] listFiles() { + File file = new File(""); + File[] filelist = new File[]{file}; + return filelist; + } + + }; + + impl.contextInitialized(sce); + } + +} diff --git a/wso2/common-util/src/test/java/org/openo/baseservice/encrypt/cbb/impl/AesCipherTest.java b/wso2/common-util/src/test/java/org/openo/baseservice/encrypt/cbb/impl/AesCipherTest.java new file mode 100644 index 0000000..c66e36a --- /dev/null +++ b/wso2/common-util/src/test/java/org/openo/baseservice/encrypt/cbb/impl/AesCipherTest.java @@ -0,0 +1,210 @@ +/* + * Copyright 2016 Huawei Technologies Co., Ltd. + * + * 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 + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openo.baseservice.encrypt.cbb.impl; + +import java.security.NoSuchAlgorithmException; +import java.security.spec.InvalidKeySpecException; + +import javax.crypto.Cipher; +import javax.crypto.SecretKeyFactory; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.openo.baseservice.encrypt.cbb.CipherCreator; +import org.openo.baseservice.encrypt.cbb.inf.AbstractCipher; + +import junit.framework.Assert; +import mockit.Mocked; +import mockit.NonStrictExpectations; + +/** + * <br/> + * <p> + * </p> + * + * @author + * @version 02-Jun-2016 + */ +public class AesCipherTest { + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + CipherCreator.instance().setFactory(new AesCipherFactory()); + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @Before + public void setUp() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @After + public void tearDown() throws Exception { + } + + /** + * Test method for + * {@link org.openo.baseservice.encrypt.cbb.impl.AesCipher#encrypt(java.lang.String)}. + */ + @Test + public void testEncrypt() { + final AbstractCipher cipherManager = CipherCreator.instance().create(); + final String encrypted = cipherManager.encrypt("test-encrypt"); + final String decrypted = cipherManager.decrypt(encrypted); + + Assert.assertEquals("test-encrypt", decrypted); + } + + @Test + public void testEncryptException() throws Exception { + new NonStrictExpectations() { + + @Mocked + Cipher cipher; + + { + cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); + result = new InvalidKeySpecException(); + } + }; + final AbstractCipher cipherManager = CipherCreator.instance().create(); + final String encrypted = cipherManager.encrypt("test-encrypt"); + + Assert.assertEquals(null, encrypted); + } + + /** + * Test method for + * {@link org.openo.baseservice.encrypt.cbb.impl.AesCipher#decrypt(java.lang.String)}. + */ + @Test + public void testDecrypt() { + final AbstractCipher cipherManager = CipherCreator.instance().create(); + final String encrypted = cipherManager.encrypt("test-encrypt"); + final String decrypted = cipherManager.decrypt(encrypted); + + Assert.assertEquals("test-encrypt", decrypted); + } + + @Test + public void testDecryptNull() { + final AbstractCipher cipherManager = CipherCreator.instance().create(); + String decrypted = cipherManager.decrypt(null); + Assert.assertEquals(null, decrypted); + + decrypted = cipherManager.decrypt(""); + + Assert.assertEquals(null, decrypted); + } + + /** + * Test method for + * {@link + * org.openo.baseservice.encrypt.cbb.impl.AesCipher#CipherManagerImpl(java.lang.String)} + * . + */ + @Test + public void testCipherManagerImplString() { + final AbstractCipher cipherManager = CipherCreator.instance().create("secret-key"); + final String encrypted = cipherManager.encrypt("test-encrypt"); + final String decrypted = cipherManager.decrypt(encrypted); + + Assert.assertEquals("test-encrypt", decrypted); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testCipherManagerImplStringDiffKey() { + final String encrypted = CipherCreator.instance().create("secret-key").encrypt("test-encrypt"); + final String decrypted = CipherCreator.instance().create("wrong-key").decrypt(encrypted); + + Assert.assertNotSame("test-encrypt", decrypted); + + final String decrypt = CipherCreator.instance().create("secret-key").decrypt(encrypted); + Assert.assertEquals("test-encrypt", decrypt); + } + + @Test + public void testCreateSecretKeyNoSuchAlgorithmException() throws Exception { + new NonStrictExpectations() { + + @Mocked + SecretKeyFactory keyFactory; + + { + keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); + result = new NoSuchAlgorithmException(); + } + }; + + final AbstractCipher cipherManager = CipherCreator.instance().create("secret-key"); + final String encrypted = cipherManager.encrypt("test-encrypt"); + Assert.assertEquals(encrypted, null); + + } + + @Test + public void testCreateSecretKeyInvalidKeySpecException() throws Exception { + new NonStrictExpectations() { + + @Mocked + SecretKeyFactory keyFactory; + + { + keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); + result = new InvalidKeySpecException(); + } + }; + + final AbstractCipher cipherManager = CipherCreator.instance().create("secret-key"); + final String decrypted = cipherManager.decrypt("test-encrypt"); + Assert.assertEquals(decrypted, null); + + } +} diff --git a/wso2/common-util/src/test/java/org/openo/baseservice/encrypt/cbb/sha/Sha256Test.java b/wso2/common-util/src/test/java/org/openo/baseservice/encrypt/cbb/sha/Sha256Test.java new file mode 100644 index 0000000..f7eaf23 --- /dev/null +++ b/wso2/common-util/src/test/java/org/openo/baseservice/encrypt/cbb/sha/Sha256Test.java @@ -0,0 +1,193 @@ +/* + * Copyright 2016 Huawei Technologies Co., Ltd. + * + * 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 + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openo.baseservice.encrypt.cbb.sha; + +import static org.junit.Assert.fail; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.security.InvalidKeyException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; + +import mockit.Mocked; +import mockit.NonStrictExpectations; +import mockit.integration.junit4.JMockit; + +/** + * <br/> + * <p> + * </p> + * + * @author + * @version 03-Jun-2016 + */ +@RunWith(JMockit.class) +public class Sha256Test { + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @Before + public void setUp() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @After + public void tearDown() throws Exception { + } + + /** + * Test method for {@link org.openo.baseservice.encrypt.cbb.sha.Sha256#digest(java.lang.String)} + * . + */ + @Test + public void testDigest() { + String plain = ""; + String expected = "E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855"; + Assert.assertEquals(expected, Sha256.digest(plain)); + + expected = "D7A8FBB307D7809469CA9ABCB0082E4F8D5651E46D3CDB762D02D0BF37C9E592"; + plain = "The quick brown fox jumps over the lazy dog"; + Assert.assertEquals(expected, Sha256.digest(plain)); + } + + @Test + public void testDigestException() throws Exception { + new NonStrictExpectations() { + + @Mocked + MessageDigest md; + + { + md = MessageDigest.getInstance("SHA-256"); + result = new NoSuchAlgorithmException(); + } + }; + final String plain = ""; + final String expected = ""; + Assert.assertEquals(expected, Sha256.digest(plain)); + + } + + /** + * Test method for + * {@link org.openo.baseservice.encrypt.cbb.sha.Sha256#mac(java.lang.String, java.security.Key)} + * . + * + * @throws InvalidKeyException + */ + @Test + public void testMacStringKey() { + final String expected = "F7BC83F430538424B13298E6AA6FB143EF4D59A14946175997479DBC2D1A3CD8"; + final String plain = "The quick brown fox jumps over the lazy dog"; + try { + Assert.assertEquals(expected, Sha256.mac(plain, new SecretKeySpec("key".getBytes(), "HmacSHA256"))); + } catch(final InvalidKeyException e) { + e.printStackTrace(); + fail("testMacStringKey failed" + e.getMessage()); + } + try { + Assert.assertEquals(expected, Sha256.mac(plain, new SecretKeySpec("key".getBytes(), "AES"))); + } catch(final InvalidKeyException e) { + e.printStackTrace(); + fail("testMacStringKey failed" + e.getMessage()); + } + + } + + @Test + public void testMacStringKeyException() throws Exception { + new NonStrictExpectations() { + + @Mocked + Mac mac; + + { + mac = Mac.getInstance("HmacSHA256"); + result = new NoSuchAlgorithmException(); + } + }; + Sha256.mac("dummy", new SecretKeySpec("key".getBytes(), "AES")); + } + + /** + * Test method for + * {@link org.openo.baseservice.encrypt.cbb.sha.Sha256#mac(java.lang.String, byte[])}. + */ + @Test + public void testMacStringByteArray() { + final String expected = "F7BC83F430538424B13298E6AA6FB143EF4D59A14946175997479DBC2D1A3CD8"; + final String plain = "The quick brown fox jumps over the lazy dog"; + Assert.assertEquals(expected, Sha256.mac(plain, "key".getBytes())); + } + + @Test + public void testMacStringByteArrayInvalidKeyException() throws Exception { + final String key = "key"; + new NonStrictExpectations() { + + @Mocked + Mac mac; + + { + mac = Mac.getInstance("HmacSHA256"); + result = new InvalidKeyException(); + } + }; + final String expected = ""; + final String plain = "The quick brown fox jumps over the lazy dog"; + Assert.assertEquals(expected, Sha256.mac(plain, key.getBytes())); + } + +} diff --git a/wso2/common-util/src/test/java/org/openo/baseservice/util/RestUtilsTest.java b/wso2/common-util/src/test/java/org/openo/baseservice/util/RestUtilsTest.java new file mode 100644 index 0000000..56b6eee --- /dev/null +++ b/wso2/common-util/src/test/java/org/openo/baseservice/util/RestUtilsTest.java @@ -0,0 +1,190 @@ +/* + * Copyright 2016 Huawei Technologies Co., Ltd. + * + * 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 + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openo.baseservice.util; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.io.ByteArrayInputStream; +import java.io.IOException; + +import javax.servlet.ServletInputStream; +import javax.servlet.http.HttpServletRequest; + +import junit.framework.Assert; +import mockit.Expectations; +import mockit.Mocked; +import mockit.integration.junit4.JMockit; + +/** + * <br/> + * <p> + * </p> + * + * @author + * @version 08-Jun-2016 + */ +@RunWith(JMockit.class) +public class RestUtilsTest { + + @Mocked + HttpServletRequest mockHttpServletRequest; + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @Before + public void setUp() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @After + public void tearDown() throws Exception { + } + + /** + * Test method for + * {@link org.openo.baseservice.util.RestUtils#getRequestBody(javax.servlet.http.HttpServletRequest)} + * . + * + * @throws IOException + */ + @Test + public void testGetRequestBody() throws IOException { + final String dummy = "this is a dummy data to test request body"; + final ServletInputStream inputStream = new ServletInputStream() { + + final ByteArrayInputStream stream = new ByteArrayInputStream(dummy.getBytes()); + + @Override + public int read() throws IOException { + return stream.read(); + } + + }; + + new Expectations() { + + { + mockHttpServletRequest.getInputStream(); + returns(inputStream); + } + }; + final String body = RestUtils.getRequestBody(mockHttpServletRequest); + + Assert.assertEquals(dummy, body); + } + + @Test + public void testGetRequestBodyNull() throws IOException { + final ServletInputStream inputStream = null; + new Expectations() { + + { + mockHttpServletRequest.getInputStream(); + returns(inputStream); + } + }; + final String body = RestUtils.getRequestBody(mockHttpServletRequest); + + Assert.assertEquals("", body); + } + + @Test + public void testGetRequestBodyIOException() throws IOException { + final ServletInputStream inputStream = new ServletInputStream() { + + @Override + public int read() throws IOException { + throw new IOException(); + } + + }; + + new Expectations() { + + { + mockHttpServletRequest.getInputStream(); + returns(inputStream); + } + }; + final String body = RestUtils.getRequestBody(mockHttpServletRequest); + + Assert.assertEquals("", body); + } + + @Test + public void testGetRequestBodyCloseIOException() throws IOException { + final ServletInputStream inputStream = new ServletInputStream() { + + final ByteArrayInputStream stream = new ByteArrayInputStream("dummy".getBytes()); + + @Override + public int read() throws IOException { + return stream.read(); + } + + @Override + public void close() throws IOException { + throw new IOException(); + } + }; + + new Expectations() { + + { + mockHttpServletRequest.getInputStream(); + returns(inputStream); + } + }; + final String body = RestUtils.getRequestBody(mockHttpServletRequest); + + Assert.assertEquals("dummy", body); + } + +} diff --git a/wso2/common-util/src/test/java/org/openo/baseservice/util/impl/SystemEnvVariablesDefImplTest.java b/wso2/common-util/src/test/java/org/openo/baseservice/util/impl/SystemEnvVariablesDefImplTest.java new file mode 100644 index 0000000..83414a2 --- /dev/null +++ b/wso2/common-util/src/test/java/org/openo/baseservice/util/impl/SystemEnvVariablesDefImplTest.java @@ -0,0 +1,113 @@ +/* + * Copyright 2016 Huawei Technologies Co., Ltd. + * + * 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 + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openo.baseservice.util.impl; + +import java.io.File; +import java.io.IOException; + +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.openo.baseservice.util.inf.SystemEnvVariables; + +import junit.framework.Assert; +import mockit.Mocked; +import mockit.NonStrictExpectations; +import mockit.integration.junit4.JMockit; +import net.jcip.annotations.NotThreadSafe; + +/** + * <br/> + * <p> + * </p> + * + * @author + * @version 08-Jun-2016 + */ +@RunWith(JMockit.class) +@NotThreadSafe +public class SystemEnvVariablesDefImplTest { + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @Before + public void setUp() throws Exception { + } + + @Test + public void testGetAppRootException() throws Exception { + new NonStrictExpectations() { + + @Mocked + File file; + + { + file = new File("."); + file.getCanonicalPath(); + result = new IOException(); + } + + }; + final SystemEnvVariables envVars =new SystemEnvVariablesDefImpl(); + System.setProperty("catalina.base", "."); + final String actual = envVars.getAppRoot(); + Assert.assertEquals(null, actual); + } + + + /** + * Test method for + * {@link org.openo.baseservice.util.impl.SystemEnvVariablesDefImpl#getAppRoot()}. + * + * @throws Exception + */ + @Test + public void testGetAppRoot() throws Exception { + final SystemEnvVariables envVars = new SystemEnvVariablesDefImpl(); + final File file = new File("."); + final String expected = file.getCanonicalPath(); + System.setProperty("catalina.base", "."); + final String actual = envVars.getAppRoot(); + Assert.assertEquals(expected, actual); + } + +} |