From: Hari Om Verma Date: Thu, 22 Feb 2018 10:29:31 +0000 (+0530) Subject: Unit Test Case for SharedContextServiceImpl.java X-Git-Tag: v2.2.0~44 X-Git-Url: https://gerrit.onap.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F97%2F32497%2F1;p=portal.git Unit Test Case for SharedContextServiceImpl.java Added JUnit test cases for SharedContextServiceImpl.java Change-Id: I66442b8066f688784a3a13321d82571db9f93902 Issue-ID: PORTAL-204 Signed-off-by: Hari Om Verma --- diff --git a/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/service/SharedContextServiceImplTest.java b/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/service/SharedContextServiceImplTest.java new file mode 100644 index 00000000..417c8b06 --- /dev/null +++ b/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/service/SharedContextServiceImplTest.java @@ -0,0 +1,131 @@ +/* + * ============LICENSE_START======================================================= + * ONAP PORTAL + * ================================================================================ + * Copyright 2018 TechMahindra + *================================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file 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. + * ============LICENSE_END========================================================= + */ +package org.onap.portalapp.portal.service; + +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import org.hibernate.criterion.Criterion; +import org.hibernate.criterion.Restrictions; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.onap.portalapp.portal.core.MockEPUser; +import org.onap.portalapp.portal.domain.SharedContext; +import org.onap.portalsdk.core.service.DataAccessService; + +public class SharedContextServiceImplTest { + + @Mock + DataAccessService dataAccessService; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + } + + @InjectMocks + SharedContextServiceImpl sharedContextServiceImpl = new SharedContextServiceImpl(); + + MockEPUser mockUser = new MockEPUser(); + + @Test + public void getSharedContextsTest() { + String contextId = "test"; + List restrictionsList = new ArrayList(); + Criterion contextIdCrit = Restrictions.eq("context_id", contextId); + restrictionsList.add(contextIdCrit); + List contextsList = new ArrayList<>(); + SharedContext sharedContext = new SharedContext(); + contextsList.add(sharedContext); + Mockito.when((List) dataAccessService.getList(SharedContext.class, null)) + .thenReturn(contextsList); + sharedContextServiceImpl.getSharedContexts("test"); + } + + @Test + public void getSharedContextsTest_usingKey() { + String contextId = "test"; + String key = "key"; + List restrictionsList = new ArrayList(); + Criterion contextIdCrit = Restrictions.eq("context_id", "test"); + Criterion keyCrit = Restrictions.eq("ckey", "key"); + restrictionsList.add(contextIdCrit); + restrictionsList.add(keyCrit); + List contextsList = new ArrayList<>(); + SharedContext sharedContext = new SharedContext(); + contextsList.add(sharedContext); + Mockito.when((List) dataAccessService.getList(SharedContext.class, null)) + .thenReturn(contextsList); + sharedContextServiceImpl.getSharedContext("test", "key"); + } + + @Test + public void addSharedContextTest() { + SharedContext context = new SharedContext(); + context.setContext_id("test"); + context.setCkey("key"); + context.setCvalue("demo"); + context.setId(1l); + Mockito.doNothing().when(dataAccessService).saveDomainObject(context, null); + sharedContextServiceImpl.addSharedContext("test", "key", "demo"); + } + + @Test + public void saveSharedContext() { + SharedContext context = new SharedContext(); + Mockito.doNothing().when(dataAccessService).saveDomainObject(context, null); + sharedContextServiceImpl.saveSharedContext(context);//("test", "key", "demo"); + } + + @Test + public void deleteSharedContextsTest() { + List contextsList = new ArrayList<>(); + SharedContext sharedContext = new SharedContext(); + contextsList.add(sharedContext); + String contextId = "test"; + List restrictionsList = new ArrayList(); + Criterion contextIdCrit = Restrictions.eq("context_id", contextId); + restrictionsList.add(contextIdCrit); + Mockito.when((List) dataAccessService.getList(SharedContext.class, null)) + .thenReturn(contextsList); + sharedContextServiceImpl.deleteSharedContexts("test"); + } + + @Test + public void deleteSharedContextTest() { + SharedContext context = new SharedContext(); + Mockito.doNothing().when(dataAccessService).deleteDomainObject(context, null); + sharedContextServiceImpl.deleteSharedContext(context); + } + + @Test + public void expireSharedContextsTest() { + final SimpleDateFormat dateFormat = new SimpleDateFormat("2018-02-22 10:00:00"); + Date expiredDateTime = new Date(System.currentTimeMillis() - 3600 * 1000); + final String whereClause = " create_time < '" + dateFormat.format(expiredDateTime) + "'"; + Mockito.doNothing().when(dataAccessService).deleteDomainObjects(SharedContext.class, whereClause, null); + sharedContextServiceImpl.expireSharedContexts(3600); + } +}