HexHelper unit tests
[appc.git] / appc-common / src / test / java / org / onap / appc / encryption / HexHelperTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2018 Nokia Solutions and Networks
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24 package org.onap.appc.encryption;
25
26 import static com.google.common.collect.Lists.newArrayList;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNotEquals;
29 import static org.junit.Assert.assertTrue;
30
31 import java.util.List;
32 import org.junit.Test;
33
34 public class HexHelperTest {
35
36     private final List<Character> hexChars =
37         newArrayList('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B','C', 'D', 'E', 'F');
38
39     @Test(expected = EncryptionException.class)
40     public void convertHexToBytes_should_throw_given_null() throws EncryptionException {
41
42         HexHelper.convertHexToBytes(null);
43     }
44
45     @Test(expected = EncryptionException.class)
46     public void convertHexToBytes_should_throw_given_non_hexadecimal_string() throws EncryptionException {
47
48         HexHelper.convertHexToBytes("125FET4A");
49     }
50
51     @Test
52     public void convertHexToBytes_should_convert_hexadecimal_string_to_byte_array() throws EncryptionException {
53
54         byte[] result = HexHelper.convertHexToBytes("125FE4A");
55
56         assertNotEquals(0, result.length);
57     }
58
59
60     @Test(expected = EncryptionException.class)
61     public void convertBytesToHex_should_throw_given_null() throws EncryptionException {
62
63         HexHelper.convertBytesToHex(null);
64     }
65
66
67     @Test
68     public void convertBytesToHex_should_convert_byte_array_to_hexadecimal_string() throws EncryptionException {
69
70         String resultString = HexHelper.convertBytesToHex(new byte[]{24, -1, 85, 99});
71         for (char ch : resultString.toCharArray()) {
72             assertTrue(hexChars.contains(ch));
73         }
74
75         byte[] resultArray = HexHelper.convertHexToBytes("A56C9ED17");
76         assertEquals("0A56C9ED17", HexHelper.convertBytesToHex(resultArray));
77     }
78
79 }