151d7c9f44343f90ae083246b67d7547031f2fed
[aai/babel.git] / src / test / java / org / onap / aai / babel / xml / generator / model / TestGeneratorUtil.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 Nokia Networks Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.aai.babel.xml.generator.model;
21
22 import static org.hamcrest.core.Is.is;
23 import static org.junit.Assert.assertNull;
24 import static org.junit.Assert.assertThat;
25
26 import java.util.Base64;
27 import org.junit.Test;
28 import org.onap.aai.babel.xml.generator.data.GeneratorUtil;
29
30 public class TestGeneratorUtil {
31
32     private static final byte[] TEST_BYTES = "TestBytes".getBytes();
33     private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
34
35     @Test
36     public void shouldEncodeUsingBase64() {
37         byte[] expected = Base64.getEncoder().encode(TEST_BYTES);
38
39         byte[] result = GeneratorUtil.encode(TEST_BYTES);
40
41         assertThat(result, is(expected));
42     }
43
44     @Test
45     public void shouldReturnEmptyByteArrayWhenNullPassedToEncode() {
46         byte[] result = GeneratorUtil.encode(null);
47
48         assertThat(result, is(EMPTY_BYTE_ARRAY));
49     }
50
51     @Test
52     public void shouldDecodeUsingBase64() {
53         byte[] input = Base64.getEncoder().encode(TEST_BYTES);
54         byte[] expected = Base64.getDecoder().decode(input);
55
56         byte[] result = GeneratorUtil.decode(input);
57
58         assertThat(result, is(expected));
59     }
60
61     @Test
62     public void shouldReturnEmptyByteArrayWhenNullPassedToDecode() {
63         byte[] result = GeneratorUtil.decode(null);
64
65         assertThat(result, is(EMPTY_BYTE_ARRAY));
66     }
67
68     @Test
69     public void shouldReturnNullWhenNullPassedToCheckSum() {
70         assertNull(GeneratorUtil.checkSum(null));
71     }
72
73     @Test
74     public void shouldReturnSameCheckSumForIdenticalInput() {
75         String checkSum1 = GeneratorUtil.checkSum(TEST_BYTES);
76         String checkSum2 = GeneratorUtil.checkSum(TEST_BYTES);
77
78         assertThat(checkSum1, is(checkSum2));
79     }
80 }