summaryrefslogtreecommitdiffstats
path: root/ecomp-portal-BE-common
diff options
context:
space:
mode:
authorHari Om Verma <hv00482922@techmahindra.com>2018-02-22 15:59:31 +0530
committerHari Om Verma <hv00482922@techmahindra.com>2018-02-22 15:59:31 +0530
commitad024fa2186f73cfb54ecc3cd422d57d7ea87f93 (patch)
tree8eef7357836454956683d186006ac0d10777207a /ecomp-portal-BE-common
parenta301b1cc365a4771f7ffd0628b2b77093a4f259c (diff)
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 <hv00482922@techmahindra.com>
Diffstat (limited to 'ecomp-portal-BE-common')
-rw-r--r--ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/service/SharedContextServiceImplTest.java131
1 files changed, 131 insertions, 0 deletions
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<Criterion> restrictionsList = new ArrayList<Criterion>();
+ Criterion contextIdCrit = Restrictions.eq("context_id", contextId);
+ restrictionsList.add(contextIdCrit);
+ List<SharedContext> contextsList = new ArrayList<>();
+ SharedContext sharedContext = new SharedContext();
+ contextsList.add(sharedContext);
+ Mockito.when((List<SharedContext>) dataAccessService.getList(SharedContext.class, null))
+ .thenReturn(contextsList);
+ sharedContextServiceImpl.getSharedContexts("test");
+ }
+
+ @Test
+ public void getSharedContextsTest_usingKey() {
+ String contextId = "test";
+ String key = "key";
+ List<Criterion> restrictionsList = new ArrayList<Criterion>();
+ Criterion contextIdCrit = Restrictions.eq("context_id", "test");
+ Criterion keyCrit = Restrictions.eq("ckey", "key");
+ restrictionsList.add(contextIdCrit);
+ restrictionsList.add(keyCrit);
+ List<SharedContext> contextsList = new ArrayList<>();
+ SharedContext sharedContext = new SharedContext();
+ contextsList.add(sharedContext);
+ Mockito.when((List<SharedContext>) 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<SharedContext> contextsList = new ArrayList<>();
+ SharedContext sharedContext = new SharedContext();
+ contextsList.add(sharedContext);
+ String contextId = "test";
+ List<Criterion> restrictionsList = new ArrayList<Criterion>();
+ Criterion contextIdCrit = Restrictions.eq("context_id", contextId);
+ restrictionsList.add(contextIdCrit);
+ Mockito.when((List<SharedContext>) 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);
+ }
+}