/* * ============LICENSE_START========================================== * ONAP Portal SDK * =================================================================== * Copyright © 2018 AT&T Intellectual Property. All rights reserved. * =================================================================== * * Unless otherwise specified, all software contained herein is licensed * under the Apache License, Version 2.0 (the "License"); * you may not use this software except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * Unless otherwise specified, all documentation contained herein is licensed * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); * you may not use this documentation except in compliance with the License. * You may obtain a copy of the License at * * https://creativecommons.org/licenses/by/4.0/ * * Unless required by applicable law or agreed to in writing, documentation * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * ============LICENSE_END============================================ * * */ package org.onap.portalapp.music.service; import java.nio.ByteBuffer; import java.time.Duration; import java.time.Instant; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.Mockito; import org.onap.music.datastore.PreparedQueryObject; import org.onap.music.exceptions.MusicLockingException; import org.onap.music.exceptions.MusicServiceException; import org.onap.music.main.MusicCore; import org.onap.music.main.ReturnType; import org.onap.portalapp.music.util.MusicProperties; import org.onap.portalapp.music.util.MusicUtil; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import org.springframework.http.HttpEntity; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.session.Session; import org.springframework.test.util.ReflectionTestUtils; import org.springframework.web.client.RestTemplate; import com.datastax.driver.core.ResultSet; import com.datastax.driver.core.Row; @RunWith(PowerMockRunner.class) @PrepareForTest({MusicUtil.class, MusicCore.class, MusicProperties.class}) public class MusicServiceTest { @Mock ReturnType returnType; @Mock ResultSet resultSet; @Mock RestTemplate restTemplate; @Mock Session session; @Mock Row row; @SuppressWarnings("unchecked") @Before public void setUp() throws MusicLockingException, MusicServiceException { PowerMockito.mockStatic(MusicUtil.class); PowerMockito.mockStatic(MusicCore.class); Mockito.when(returnType.getMessage()).thenReturn("test"); Mockito.when(row.getString(Mockito.anyString())).thenReturn("test"); Mockito.when(resultSet.one()).thenReturn(row); Mockito.when(MusicCore.atomicPut(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(PreparedQueryObject.class), Mockito.any(MusicCore.Condition.class))).thenReturn(returnType); Mockito.when(MusicCore.eventualPut(Mockito.any(PreparedQueryObject.class))).thenReturn(returnType); Mockito.when(MusicCore.atomicGet(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(PreparedQueryObject.class))).thenReturn(resultSet); Mockito.when(MusicCore.get(Mockito.any(PreparedQueryObject.class))).thenReturn(resultSet); Mockito.when(session.getAttribute(Mockito.anyString())).thenReturn("test"); ResponseEntity entityResponse = Mockito.mock(ResponseEntity.class); Mockito.when(entityResponse.getBody()).thenReturn("Success"); Mockito.when(restTemplate.exchange(Mockito.any(String.class), Mockito.any(HttpMethod.class), Mockito.any(HttpEntity.class), Mockito.eq(String.class))).thenReturn(entityResponse); Mockito.when(session.getCreationTime()).thenReturn(Instant.EPOCH); Mockito.when(session.getLastAccessedTime()).thenReturn(Instant.EPOCH); Mockito.when(session.getMaxInactiveInterval()).thenReturn(Duration.ZERO); Mockito.when(MusicUtil.cleanUp()).thenReturn(true); } @Test public void testSetAttribute_WhenIsMetaIsTrue() throws Exception { ReflectionTestUtils.setField(MusicService.class, "isAtomicPut", true); Mockito.when(MusicUtil.isSessionMetaAttr(Mockito.anyString())).thenReturn(true); MusicService.setAttribute("test", "test", "1"); } @Test public void testSetAttribute_WhenIsMetaIsFalse() throws Exception { ReflectionTestUtils.setField(MusicService.class, "isAtomicPut", false); Mockito.when(MusicUtil.isSessionMetaAttr(Mockito.anyString())).thenReturn(false); ByteBuffer bb = Mockito.mock(ByteBuffer.class); Mockito.when(MusicUtil.musicSerialize(Mockito.anyObject())).thenReturn(bb); MusicService.setAttribute("test", "test", "1"); } @Test public void testSetMetaAttribute_WhenIsAutomicPutIsTrue() throws Exception { ReflectionTestUtils.setField(MusicService.class, "isAtomicPut", true); MusicService.setMetaAttribute(session); } @Test public void testSetMetaAttribute_WhenIsAutomicPutIsFalse() throws Exception { ReflectionTestUtils.setField(MusicService.class, "isAtomicPut", false); MusicService.setMetaAttribute(session); } @Test public void testGetMetaAttribute_WhenIsAutomicPutIsTrue() throws Exception { ReflectionTestUtils.setField(MusicService.class, "isAtomicGet", true); MusicService.getMetaAttribute("test"); } @Test public void testGetMetaAttribute_WhenIsAutomicPutIsFalse() throws Exception { ReflectionTestUtils.setField(MusicService.class, "isAtomicGet", false); MusicService.getMetaAttribute("test"); } @Test public void testGetAttribute_WhenIsAutomicPutIsTrue() throws Exception { ReflectionTestUtils.setField(MusicService.class, "isAtomicGet", true); Mockito.when(MusicUtil.isSessionMetaAttr(Mockito.anyString())).thenReturn(true); MusicService.getAttribute("test", "1"); } @Test public void testGetAttribute_WhenIsAutomicPutIsFalse() throws Exception { ReflectionTestUtils.setField(MusicService.class, "isAtomicGet", false); Mockito.when(MusicUtil.isSessionMetaAttr(Mockito.anyString())).thenReturn(false); MusicService.getAttribute("test", "1"); } @Test public void testRemoveAttribute_WhenIsMetaIsTrue() throws Exception { ReflectionTestUtils.setField(MusicService.class, "isAtomicPut", true); Mockito.when(MusicUtil.isSessionMetaAttr(Mockito.anyString())).thenReturn(true); MusicService.removeAttribute("test", "1"); } @Test public void testRemoveAttribute_WhenIsMetaIsFalse() throws Exception { ReflectionTestUtils.setField(MusicService.class, "isAtomicPut", false); Mockito.when(MusicUtil.isSessionMetaAttr(Mockito.anyString())).thenReturn(false); ByteBuffer bb = Mockito.mock(ByteBuffer.class); Mockito.when(MusicUtil.musicSerialize(Mockito.anyObject())).thenReturn(bb); MusicService.removeAttribute("test", "1"); } @Test public void testRemoveSession_WhenIsMetaIsTrue() throws Exception { ReflectionTestUtils.setField(MusicService.class, "isAtomicPut", true); Mockito.when(MusicUtil.isSessionMetaAttr(Mockito.anyString())).thenReturn(true); MusicService.removeSession("1"); } @Test public void testRemoveSession_WhenIsMetaIsFalse() throws Exception { ReflectionTestUtils.setField(MusicService.class, "isAtomicPut", false); Mockito.when(MusicUtil.isSessionMetaAttr(Mockito.anyString())).thenReturn(false); ByteBuffer bb = Mockito.mock(ByteBuffer.class); Mockito.when(MusicUtil.musicSerialize(Mockito.anyObject())).thenReturn(bb); MusicService.removeSession("1"); } @Test public void testSetAttributeAPI_WhenIsMetaIsTrue() throws Exception { ReflectionTestUtils.setField(MusicService.class, "template", restTemplate); MusicService.setAttributeAPI("test", "test", session, "test", "test", true); } @Test public void testSetAttributeAPI_WhenIsMetaIsFalse() throws Exception { ReflectionTestUtils.setField(MusicService.class, "template", null); MusicService.setAttributeAPI("test", "test", session, "test", "test", false); } @Test public void testGetAttributeAPI_WhenIsMetaIsTrue() throws Exception { ReflectionTestUtils.setField(MusicService.class, "template", restTemplate); MusicService.getAttributeAPI("test", session, "test", true); } @Test public void testGetAttributeAPI_WhenIsMetaIsFalse() throws Exception { ReflectionTestUtils.setField(MusicService.class, "template", null); MusicService.getAttributeAPI("test", session, "test", false); } @Test public void testRemoveAttributeAPI_WhenIsMetaIsTrue() throws Exception { ReflectionTestUtils.setField(MusicService.class, "template", restTemplate); MusicService.removeAttributeAPI("test", "1", true); } @Test public void testRemoveAttributeAPI_WhenIsMetaIsFalse() throws Exception { ReflectionTestUtils.setField(MusicService.class, "template", null); MusicService.removeAttributeAPI("test", "1", false); } @Test public void testcleanUpMusic_WhenIsAutomicFlagsTrue() throws Exception { ReflectionTestUtils.setField(MusicService.class, "isAtomicPut", true); ReflectionTestUtils.setField(MusicService.class, "isAtomicGet", true); Mockito.when(resultSet.one()).thenReturn(null); PowerMockito.mockStatic(MusicProperties.class); Mockito.when(MusicProperties.getProperty("music.cleanup.threshold")).thenReturn("0"); MusicService.cleanUpMusic(); } @Test public void testcleanUpMusic_WhenIsAutomicFlagsFalse() throws Exception { ReflectionTestUtils.setField(MusicService.class, "isAtomicPut", false); ReflectionTestUtils.setField(MusicService.class, "isAtomicGet", false); Mockito.when(resultSet.one()).thenReturn(null); PowerMockito.mockStatic(MusicProperties.class); Mockito.when(MusicProperties.getProperty("music.cleanup.threshold")).thenReturn("0"); MusicService.cleanUpMusic(); } }