Merge "Fix sql injection vulnerability"
[portal.git] / ecomp-portal-BE-common / src / test / java / org / onap / portalapp / portal / service / SharedContextServiceImplTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP  PORTAL
4  * ================================================================================
5  * Copyright 2018 TechMahindra
6  *=================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.portalapp.portal.service;
21
22 import java.text.SimpleDateFormat;
23 import java.util.ArrayList;
24 import java.util.Date;
25 import java.util.List;
26 import org.hibernate.criterion.Criterion;
27 import org.hibernate.criterion.Restrictions;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.mockito.InjectMocks;
31 import org.mockito.Mock;
32 import org.mockito.Mockito;
33 import org.mockito.MockitoAnnotations;
34 import org.onap.portalapp.portal.core.MockEPUser;
35 import org.onap.portalapp.portal.domain.SharedContext;
36 import org.onap.portalsdk.core.service.DataAccessService;
37
38 public class SharedContextServiceImplTest {
39
40         @Mock
41         DataAccessService dataAccessService;
42
43         @Before
44         public void setup() {
45                 MockitoAnnotations.initMocks(this);
46         }
47
48         @InjectMocks
49         SharedContextServiceImpl sharedContextServiceImpl = new SharedContextServiceImpl();
50
51         MockEPUser mockUser = new MockEPUser();
52
53         @Test
54         public void getSharedContextsTest() {
55                 String contextId = "test";
56                 List<Criterion> restrictionsList = new ArrayList<Criterion>();
57                 Criterion contextIdCrit = Restrictions.eq("context_id", contextId);
58                 restrictionsList.add(contextIdCrit);
59                 List<SharedContext> contextsList = new ArrayList<>();
60                 SharedContext sharedContext = new SharedContext();
61                 contextsList.add(sharedContext);
62                 Mockito.when((List<SharedContext>) dataAccessService.getList(SharedContext.class, null))
63                 .thenReturn(contextsList);
64                 sharedContextServiceImpl.getSharedContexts("test");
65         }
66
67         @Test
68         public void getSharedContextsTest_usingKey() {
69                 String contextId = "test";
70                 String key = "key";
71                 List<Criterion> restrictionsList = new ArrayList<Criterion>();
72                 Criterion contextIdCrit = Restrictions.eq("context_id", "test");
73                 Criterion keyCrit = Restrictions.eq("ckey", "key");
74                 restrictionsList.add(contextIdCrit);
75                 restrictionsList.add(keyCrit);
76                 List<SharedContext> contextsList = new ArrayList<>();
77                 SharedContext sharedContext = new SharedContext();
78                 contextsList.add(sharedContext);
79                 Mockito.when((List<SharedContext>) dataAccessService.getList(SharedContext.class, null))
80                 .thenReturn(contextsList);
81                 sharedContextServiceImpl.getSharedContext("test", "key");
82         }
83
84         @Test
85         public void addSharedContextTest() {
86                 SharedContext context = new SharedContext();
87                 context.setContext_id("test");
88                 context.setCkey("key");
89                 context.setCvalue("demo");
90                 context.setId(1l);
91                 Mockito.doNothing().when(dataAccessService).saveDomainObject(context,  null);
92                 sharedContextServiceImpl.addSharedContext("test", "key", "demo");
93         }
94
95         @Test
96         public void saveSharedContext() {
97                 SharedContext context = new SharedContext();
98                 Mockito.doNothing().when(dataAccessService).saveDomainObject(context,  null);
99                 sharedContextServiceImpl.saveSharedContext(context);//("test", "key", "demo");
100         }
101
102         @Test
103         public void deleteSharedContextsTest() {
104                 List<SharedContext> contextsList = new ArrayList<>();
105                 SharedContext sharedContext = new SharedContext();
106                 contextsList.add(sharedContext);
107                 String contextId = "test";
108                 List<Criterion> restrictionsList = new ArrayList<Criterion>();
109                 Criterion contextIdCrit = Restrictions.eq("context_id", contextId);
110                 restrictionsList.add(contextIdCrit);
111                 Mockito.when((List<SharedContext>) dataAccessService.getList(SharedContext.class, null))
112                 .thenReturn(contextsList);
113                 sharedContextServiceImpl.deleteSharedContexts("test");
114         }
115
116         @Test
117         public void deleteSharedContextTest() {
118                 SharedContext context = new SharedContext();
119                 Mockito.doNothing().when(dataAccessService).deleteDomainObject(context,  null);
120                 sharedContextServiceImpl.deleteSharedContext(context);
121         }
122
123         @Test
124         public void expireSharedContextsTest() {
125                 final SimpleDateFormat dateFormat = new SimpleDateFormat("2018-02-22 10:00:00"); 
126                 Date expiredDateTime = new Date(System.currentTimeMillis() - 3600 * 1000);
127                 final String whereClause = " create_time < '" + dateFormat.format(expiredDateTime) + "'";
128                 Mockito.doNothing().when(dataAccessService).deleteDomainObjects(SharedContext.class, whereClause, null);
129                 sharedContextServiceImpl.expireSharedContexts(3600);
130         }
131 }