60bcc7e4a34f9d8bab807930d731e0e0fbbc34a1
[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
21 package org.onap.aai.babel.xml.generator.model;
22
23 import static org.hamcrest.core.Is.is;
24 import static org.junit.Assert.assertNull;
25 import static org.junit.Assert.assertThat;
26
27 import java.util.Base64;
28 import org.junit.Test;
29 import org.onap.aai.babel.xml.generator.data.GeneratorUtil;
30
31 public class TestGeneratorUtil {
32
33     private static final byte[] TEST_BYTES = "TestBytes".getBytes();
34     private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
35
36     @Test
37     public void shouldEncodeUsingBase64() {
38         byte[] expected = Base64.getEncoder().encode(TEST_BYTES);
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 }