JUnits for coverage
[portal.git] / ecomp-portal-BE-common / src / test / java / org / onap / portalapp / music / util / MusicUtilTest.java
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright © 2018 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software 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  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  *
37  */
38 package org.onap.portalapp.music.util;
39
40 import static org.junit.Assert.assertFalse;
41 import static org.junit.Assert.assertNotNull;
42 import static org.junit.Assert.assertTrue;
43 import static org.mockito.Mockito.mock;
44
45 import java.io.StreamCorruptedException;
46 import java.nio.ByteBuffer;
47 import java.nio.charset.Charset;
48 import java.time.Duration;
49 import java.time.Instant;
50 import java.util.ArrayList;
51 import java.util.Date;
52 import java.util.List;
53
54 import org.junit.Before;
55 import org.junit.Test;
56 import org.junit.runner.RunWith;
57 import org.mockito.Mock;
58 import org.mockito.Mockito;
59 import org.mockito.MockitoAnnotations;
60 import org.onap.portalapp.music.conf.MusicSession;
61 import org.onap.portalapp.music.service.MusicService;
62 import org.powermock.api.mockito.PowerMockito;
63 import org.powermock.core.classloader.annotations.PrepareForTest;
64 import org.powermock.modules.junit4.PowerMockRunner;
65
66 import com.datastax.driver.core.ResultSet;
67 import com.datastax.driver.core.Row;
68
69 @RunWith(PowerMockRunner.class)
70 @PrepareForTest({  MusicProperties.class, MusicSession.class, MusicService.class, MusicCleanUp.class })
71 public class MusicUtilTest {
72
73         ResultSet result = Mockito.mock(ResultSet.class);
74
75         Row rw = Mockito.mock(Row.class);
76         @Mock
77         ByteBuffer buffer;
78         
79         @Before
80         public void setUp() throws Exception {
81                 MusicCleanUp musCleapUp = mock(MusicCleanUp.class);
82                 PowerMockito.mockStatic(MusicProperties.class);
83                 PowerMockito.mockStatic(MusicSession.class);
84                 PowerMockito.mockStatic(MusicService.class);
85                 PowerMockito.mockStatic(MusicCleanUp.class);
86                 Mockito.when(MusicProperties.getProperty(MusicProperties.MUSIC_ATOMIC_PUT)).thenReturn("atomic-put");
87                 Mockito.when(MusicProperties.getProperty(MusicProperties.MUSIC_ATOMIC_GET)).thenReturn("atomic-get");
88                 Mockito.when(MusicProperties.getProperty(MusicProperties.MUSIC_CACHE)).thenReturn("cache");
89                 Mockito.when(MusicProperties.getProperty(MusicProperties.MUSIC_EXCLUDE_API)).thenReturn("test1,test2");
90                 PowerMockito.when(MusicProperties.getProperty(MusicProperties.MUSIC_SERIALIZE_COMPRESS)).thenReturn("compress");
91                 PowerMockito.when(MusicCleanUp.getInstance()).thenReturn(musCleapUp);
92                 PowerMockito.when(musCleapUp.getLastCleanUpTime()).thenReturn(null);
93                 MockitoAnnotations.initMocks(this);
94         }
95
96         @Test
97         public void isSessionMetaAttrTest() {
98                 assertTrue(MusicUtil.isSessionMetaAttr("CREATION_TIME"));
99         }
100
101         @Test
102         public void musicRestResponseDataParsingTest() throws Exception {
103                 List<Row> rows = new ArrayList<Row>();
104                 Mockito.doReturn("creation time").when(rw).getString("CREATION_TIME");
105                 rows.add(rw);
106                 Mockito.doReturn(rows.get(0)).when(result).one();
107                 assertNotNull(MusicUtil.musicRestResponseDataParsing(result, "CREATION_TIME"));
108         }
109         
110         @Test(expected=StreamCorruptedException.class)
111         public void musicRestResponseDataParsingTestBytes() throws Exception {
112                 List<Row> rows = new ArrayList<Row>();
113                 //ByteBuffer byteBuffer = ByteBuffer.allocate(6);
114                 ByteBuffer buff = Charset.forName("UTF-8").encode("Hello, World!");
115                 Mockito.when(rw.getBytes("attribute_bytes")).thenReturn(buff);
116                 rows.add(rw);
117                 Mockito.doReturn(rows.get(0)).when(result).one();
118                 assertNotNull(MusicUtil.musicRestResponseDataParsing(result, "TEST"));
119         }
120         
121         @Test
122         public void testMusicSerialize()throws Exception {
123                 String data="TEST";
124                 MusicUtil.musicSerialize(data);
125                 
126                 
127         }
128         @Test
129         public void testParseMetaData()throws Exception {
130                 
131                 Mockito.when(rw.getString("primary_id")).thenReturn("TestSession");
132                 Mockito.when(rw.getString("creation_time")).thenReturn("2018-07-03T10:15:30.00Z");
133                 Mockito.when(rw.getString("last_access_time")).thenReturn("2018-07-05T10:15:30.00Z");
134                 Mockito.when(rw.getString("max_inactive_interval")).thenReturn("PT20.345S");
135                 MusicSession session=MusicUtil.parseMetaData(rw);
136                 assertNotNull(session);
137                 
138         }
139         
140         @Test
141         public void testMusicSerializeMusicCompress()throws Exception {
142                 PowerMockito.when(MusicProperties.getProperty(MusicProperties.MUSIC_SERIALIZE_COMPRESS)).thenReturn("true");
143                 String data="TEST";
144                 MusicUtil.musicSerialize(data);
145                 
146                 
147         }
148         
149
150         @Test
151         public void getMusicExcludedAPITest() {
152                 assertNotNull(MusicUtil.getMusicExcludedAPI());
153         }
154
155         @Test
156         public void isExcludedApiTest() {
157                 assertTrue(MusicUtil.isExcludedApi("test1"));
158         }
159
160         @Test
161         public void isExcludedApiFalseTest() {
162                 assertFalse(MusicUtil.isExcludedApi("test3"));
163         }
164
165         @Test
166         public void isMusicSerializeCompressReturnFalseTest() {
167                 assertFalse(MusicUtil.isMusicSerializeCompress());
168         }
169
170         @Test
171         public void isAtomicPutTest() {
172                 assertFalse(MusicUtil.isAtomicPut());
173         }
174
175         @Test
176         public void isAtomicGetTest() {
177                 assertFalse(MusicUtil.isAtomicGet());
178         }
179
180         @Test
181         public void isCachedTest() {
182                 assertFalse(MusicUtil.isCached());
183         }
184
185         @Test
186         public void convertHoursToMillSecTest() {
187                 assertNotNull(MusicUtil.convertHoursToMillSec(1));
188         }
189
190         @Test
191         public void cleanUpTest() {
192                 assertFalse(MusicUtil.cleanUp());
193         }
194 }