summaryrefslogtreecommitdiffstats
path: root/ecomp-portal-BE-common/src/test/java/org/openecomp/portalapp/portal/service/EPRoleServiceImplTest.java
blob: 766d51be9e1ff15db3b83b0abfa9eb6ddd0474c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package org.openecomp.portalapp.portal.service;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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.openecomp.portalapp.portal.domain.EPRole;
import org.openecomp.portalapp.portal.service.EPRoleServiceImpl;
import org.openecomp.portalapp.portal.core.MockEPUser;
import org.openecomp.portalapp.portal.framework.MockitoTestSuite;
import org.openecomp.portalsdk.core.domain.RoleFunction;
import org.openecomp.portalsdk.core.service.DataAccessService;

public class EPRoleServiceImplTest {

	@Mock
	DataAccessService dataAccessService;

	@Before
	public void setup() {
		MockitoAnnotations.initMocks(this);
	}

	@InjectMocks
	EPRoleServiceImpl ePRoleServiceImpl = new EPRoleServiceImpl();

	MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();

	HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
	HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
	NullPointerException nullPointerException = new NullPointerException();
	MockEPUser mockUser = new MockEPUser();

	@Test
	public void getRoleFunctionsTest() {
		List<RoleFunction> roleFunctionList = new ArrayList<>();
		Mockito.when(dataAccessService.getList(RoleFunction.class, null)).thenReturn(roleFunctionList);
		List<RoleFunction> expectedRoleFunctionList = ePRoleServiceImpl.getRoleFunctions();
		assertEquals(roleFunctionList, expectedRoleFunctionList);
	}

	@Test
	public void getAvailableChildRolesIfRoleIdIsNullTest() {
		List<EPRole> roleList = new ArrayList<>();
		EPRole role = new EPRole();
		EPRole role1 = new EPRole();
		role.addChildRole(role1);
		roleList.add(role);
		Mockito.when(dataAccessService.getList(EPRole.class, null)).thenReturn(roleList);
		List<EPRole> expectedRoleList = ePRoleServiceImpl.getAvailableChildRoles(null);
		assertEquals(roleList, expectedRoleList);
	}

	// @Test
	// public void getAvailableChildRolesIfRoleIdNotNullTest()
	// {
	// List<EPRole> roleList = new ArrayList<>();
	// EPRole role = new EPRole();
	// EPRole role1= new EPRole();
	// role.addChildRole(role1);
	// roleList.add(role);
	// Mockito.when(dataAccessService.getDomainObject(EPRole.class, 1,
	// null)).thenReturn(role);
	// Mockito.when(dataAccessService.getList(EPRole.class,
	// null)).thenReturn(roleList);
	//
	// List<EPRole> expectedRoleList =
	// ePRoleServiceImpl.getAvailableChildRoles((long) 1);
	// System.out.println(expectedRoleList);
	// assertEquals(roleList,expectedRoleList);
	// }
	//
	@Test
	public void getRoleFunctionTest() {
		RoleFunction roleFunction = new RoleFunction();
		Mockito.when(dataAccessService.getDomainObject(RoleFunction.class, "test", null)).thenReturn(roleFunction);
		RoleFunction expectedRoleFunction = ePRoleServiceImpl.getRoleFunction("test");
		assertEquals(expectedRoleFunction, roleFunction);
	}

	@Test
	public void saveRoleFunctionTest() {
		EPRole role = new EPRole();
		Mockito.doNothing().when(dataAccessService).saveDomainObject(role, null);
		ePRoleServiceImpl.saveRole(role);
	}

	@Test
	public void deleteRoleFunctionTest() {
		RoleFunction roleFunction = new RoleFunction();
		Mockito.doNothing().when(dataAccessService).deleteDomainObject(roleFunction, null);
		ePRoleServiceImpl.deleteRoleFunction(roleFunction);
	}

	@Test
	public void getRoleTest() {
		EPRole role = null;
		Mockito.when(dataAccessService.getDomainObject(EPRole.class, 1, null)).thenReturn(role);
		EPRole expectedRole = ePRoleServiceImpl.getRole((long) 1);
		assertEquals(expectedRole, role);
	}

	@Test
	public void getRoleIfappIdNullTest() {
		assertNull(ePRoleServiceImpl.getRole(null, null));

	}

	@Test
	public void getRoleIfappIdNotNullTest() {
		List<EPRole> roles = new ArrayList<>();
		EPRole role = new EPRole();
		roles.add(role);
		String sql = "SELECT * FROM fn_role where APP_ID = 1 AND APP_ROLE_ID = 1";
		Mockito.when(dataAccessService.executeSQLQuery(sql, EPRole.class, null)).thenReturn(roles);
		EPRole expectedRole = ePRoleServiceImpl.getRole((long) 1, (long) 1);
		assertEquals(expectedRole, role);

	}

	@Test
	public void getRoleIfListSizeIsMoreThan1Test() {
		List<EPRole> roles = new ArrayList<>();
		EPRole role = new EPRole();
		EPRole role1 = new EPRole();
		roles.add(role);
		roles.add(role1);
		String sql = "SELECT * FROM fn_role where APP_ID = 1 AND APP_ROLE_ID = 1";
		Mockito.when(dataAccessService.executeSQLQuery(sql, EPRole.class, null)).thenReturn(roles);
		EPRole expectedRole = ePRoleServiceImpl.getRole((long) 1, (long) 1);
		assertEquals(expectedRole, role);

	}

	@Test
	public void getRoleIfListSizeIsEmptyTest() {
		List<EPRole> roles = new ArrayList<>();
		String sql = "SELECT * FROM fn_role where APP_ID = 1 AND APP_ROLE_ID = 1";
		Mockito.when(dataAccessService.executeSQLQuery(sql, EPRole.class, null)).thenReturn(roles);
		assertNull(ePRoleServiceImpl.getRole((long) 1, (long) 1));

	}

	@Test
	public void saveRoleTest() {
		EPRole role = new EPRole();
		Mockito.doNothing().when(dataAccessService).saveDomainObject(role, null);
		ePRoleServiceImpl.saveRole(role);
	}

	@Test
	public void deleteRoleTest() {
		EPRole role = new EPRole();
		Mockito.doNothing().when(dataAccessService).deleteDomainObject(role, null);
		ePRoleServiceImpl.deleteRole(role);
	}

	@Test
	public void getAvailableRolesTest() {
		List<EPRole> roleList = new ArrayList<>();
		Mockito.when(dataAccessService.getList(EPRole.class, null)).thenReturn(roleList);
		List<EPRole> expectedRoleList = ePRoleServiceImpl.getAvailableRoles();
		assertEquals(expectedRoleList, roleList);
	}

	@Test
	public void getAppRolesTest() {
		final Map<String, String> portalParams = null;
		List<EPRole> roleList = new ArrayList<>();
		Mockito.when(dataAccessService.executeNamedQuery("getPortalAppRoles", portalParams, null)).thenReturn(roleList);
		assertNull(ePRoleServiceImpl.getAppRole("test", (long) 1));

	}

	@SuppressWarnings("unchecked")
	@Test
	public void getAppRolesIfNotPortalTest() {
		final Map<String, String> params = null;
		List<EPRole> roleList = new ArrayList<>();
		EPRole role = new EPRole();
		EPRole role1 = new EPRole();
		roleList.add(role);
		roleList.add(role1);
		Mockito.when((List<EPRole>) dataAccessService.executeNamedQuery("getAppRoles", params, null))
				.thenReturn(roleList);
		List<EPRole> expectedRoleList = (List<EPRole>) ePRoleServiceImpl.getAppRole("test", (long) 10);
		System.out.println(expectedRoleList);

	}

	@Test
	public void saveRoleFunction() {
		RoleFunction domainRoleFunction = new RoleFunction();
		Mockito.doNothing().when(dataAccessService).saveDomainObject(domainRoleFunction, null);
		ePRoleServiceImpl.saveRoleFunction(domainRoleFunction);
	}
}