2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2018 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
38 package org.onap.portalapp.music.util;
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;
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;
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;
66 import com.datastax.driver.core.ResultSet;
67 import com.datastax.driver.core.Row;
69 @RunWith(PowerMockRunner.class)
70 @PrepareForTest({ MusicProperties.class, MusicSession.class, MusicService.class, MusicCleanUp.class })
71 public class MusicUtilTest {
73 ResultSet result = Mockito.mock(ResultSet.class);
75 Row rw = Mockito.mock(Row.class);
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);
97 public void isSessionMetaAttrTest() {
98 assertTrue(MusicUtil.isSessionMetaAttr("CREATION_TIME"));
102 public void musicRestResponseDataParsingTest() throws Exception {
103 List<Row> rows = new ArrayList<Row>();
104 Mockito.doReturn("creation time").when(rw).getString("CREATION_TIME");
106 Mockito.doReturn(rows.get(0)).when(result).one();
107 assertNotNull(MusicUtil.musicRestResponseDataParsing(result, "CREATION_TIME"));
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);
117 Mockito.doReturn(rows.get(0)).when(result).one();
118 assertNotNull(MusicUtil.musicRestResponseDataParsing(result, "TEST"));
122 public void testMusicSerialize()throws Exception {
124 MusicUtil.musicSerialize(data);
129 public void testParseMetaData()throws Exception {
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);
141 public void testMusicSerializeMusicCompress()throws Exception {
142 PowerMockito.when(MusicProperties.getProperty(MusicProperties.MUSIC_SERIALIZE_COMPRESS)).thenReturn("true");
144 MusicUtil.musicSerialize(data);
151 public void getMusicExcludedAPITest() {
152 assertNotNull(MusicUtil.getMusicExcludedAPI());
156 public void isExcludedApiTest() {
157 assertTrue(MusicUtil.isExcludedApi("test1"));
161 public void isExcludedApiFalseTest() {
162 assertFalse(MusicUtil.isExcludedApi("test3"));
166 public void isMusicSerializeCompressReturnFalseTest() {
167 assertFalse(MusicUtil.isMusicSerializeCompress());
171 public void isAtomicPutTest() {
172 assertFalse(MusicUtil.isAtomicPut());
176 public void isAtomicGetTest() {
177 assertFalse(MusicUtil.isAtomicGet());
181 public void isCachedTest() {
182 assertFalse(MusicUtil.isCached());
186 public void convertHoursToMillSecTest() {
187 assertNotNull(MusicUtil.convertHoursToMillSec(1));
191 public void cleanUpTest() {
192 assertFalse(MusicUtil.cleanUp());