Skip to content

Package: TestBase

TestBase

nameinstructionbranchcomplexitylinemethod
TestBase()
M: 23 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
getCurrentParentId()
M: 14 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getCurrentScopeId()
M: 25 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getCurrentUserId()
M: 25 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getKapuaId()
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getKapuaId(String)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getKapuaId(int)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getRandomString()
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isIntegrationTest()
M: 13 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isUnitTest()
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
parseDateString(String)
M: 51 C: 0
0%
M: 6 C: 0
0%
M: 5 C: 0
0%
M: 16 C: 0
0%
M: 1 C: 0
0%
primeException()
M: 11 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 14 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
verifyAssertionError(AssertionError)
M: 102 C: 0
0%
M: 18 C: 0
0%
M: 10 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
verifyException(Exception)
M: 102 C: 0
0%
M: 18 C: 0
0%
M: 10 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2018, 2020 Eurotech and/or its affiliates and others
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License v1.0
6: * which accompanies this distribution, and is available at
7: * http://www.eclipse.org/legal/epl-v10.html
8: *
9: * Contributors:
10: * Eurotech - initial API and implementation
11: *******************************************************************************/
12: package org.eclipse.kapua.qa.common;
13:
14: import cucumber.api.Scenario;
15: import org.eclipse.kapua.commons.model.id.KapuaEid;
16: import org.eclipse.kapua.commons.util.RandomUtils;
17: import org.eclipse.kapua.locator.KapuaLocator;
18: import org.eclipse.kapua.model.id.KapuaId;
19: import org.eclipse.kapua.service.account.Account;
20: import org.junit.Assert;
21:
22: import java.math.BigInteger;
23: import java.text.DateFormat;
24: import java.text.ParseException;
25: import java.text.SimpleDateFormat;
26: import java.time.Duration;
27: import java.time.Instant;
28: import java.util.Date;
29: import java.util.Random;
30:
31: public class TestBase extends Assert {
32:
33: private static final String LAST_ACCOUNT = "LastAccount";
34:
35: /**
36: * Common locator instance
37: */
38: public KapuaLocator locator;
39:
40: /**
41: * Inter step data scratchpad.
42: */
43: public StepData stepData;
44:
45: /**
46: * Common database helper
47: */
48: public DBHelper database;
49:
50: /**
51: * Current scenario scope
52: */
53: public Scenario scenario;
54:
55: /**
56: * Current test type
57: * Either unit or integration
58: */
59: private String testType;
60:
61: /**
62: * Random number generator
63: */
64: public Random random = RandomUtils.getInstance();
65:
66: /**
67: * Commonly used constants
68: */
69: protected static final KapuaId SYS_SCOPE_ID = KapuaId.ONE;
70: protected static final KapuaId SYS_USER_ID = new KapuaEid(BigInteger.ONE);
71: protected static final int DEFAULT_SCOPE_ID = 42;
72: protected static final KapuaId DEFAULT_ID = new KapuaEid(BigInteger.valueOf(DEFAULT_SCOPE_ID));
73:
74: public TestBase() {
75:
76: testType = System.getProperty("test.type");
77:• if (testType != null) {
78: testType = testType.trim().toLowerCase();
79: } else {
80: testType = "";
81: }
82: }
83:
84: public KapuaId getKapuaId() {
85: return new KapuaEid(BigInteger.valueOf(random.nextLong()).abs());
86: }
87:
88: public KapuaId getKapuaId(int id) {
89: return new KapuaEid(BigInteger.valueOf(id));
90: }
91:
92: public KapuaId getKapuaId(String id) {
93: return new KapuaEid(new BigInteger(id));
94: }
95:
96: public KapuaId getCurrentScopeId() {
97:
98:• if (stepData.contains("LastAccountId")) {
99: return (KapuaId) stepData.get("LastAccountId");
100:• } else if (stepData.get(LAST_ACCOUNT) != null) {
101: return ((Account) stepData.get(LAST_ACCOUNT)).getId();
102: } else {
103: return SYS_SCOPE_ID;
104: }
105: }
106:
107: public KapuaId getCurrentParentId() {
108:
109:• if (stepData.get(LAST_ACCOUNT) != null) {
110: return ((Account) stepData.get(LAST_ACCOUNT)).getScopeId();
111: } else {
112: return SYS_SCOPE_ID;
113: }
114: }
115:
116: public KapuaId getCurrentUserId() {
117:
118:• if (stepData.contains("LastUserId")) {
119: return (KapuaId) stepData.get("LastUserId");
120:• } else if (stepData.get("LastUser") != null) {
121: return ((Account) stepData.get("LastUser")).getId();
122: } else {
123: return SYS_USER_ID;
124: }
125: }
126:
127: public boolean isUnitTest() {
128: return testType.equals("unit");
129: }
130:
131: public boolean isIntegrationTest() {
132:• return testType.isEmpty() || testType.equals("integration");
133: }
134:
135: public void primeException() {
136: stepData.put("ExceptionCaught", false);
137: stepData.remove("Exception");
138: }
139:
140: /**
141: * Check the exception that was caught. In case the exception was expected the type and message is shown in the cucumber logs.
142: * Otherwise the exception is rethrown failing the test and dumping the stack trace to help resolving problems.
143: */
144: public void verifyException(Exception ex)
145: throws Exception {
146:
147:• boolean exceptionExpected = stepData.contains("ExceptionExpected") ? (boolean) stepData.get("ExceptionExpected") : false;
148:• String exceptionName = stepData.contains("ExceptionName") ? ((String) stepData.get("ExceptionName")).trim() : "";
149:• String exceptionMessage = stepData.contains("ExceptionMessage") ? ((String) stepData.get("ExceptionMessage")).trim() : "";
150:
151:• if (!exceptionExpected ||
152:• (!exceptionName.isEmpty() && !ex.getClass().toGenericString().contains(exceptionName)) ||
153:• (!exceptionMessage.isEmpty() && !exceptionMessage.trim().contentEquals("*") && !ex.getMessage().contains(exceptionMessage))) {
154: scenario.write("An unexpected exception was raised!");
155: throw (ex);
156: }
157:
158: scenario.write("Exception raised as expected: " + ex.getClass().getCanonicalName() + ", " + ex.getMessage());
159: stepData.put("ExceptionCaught", true);
160: stepData.put("Exception", ex);
161: }
162:
163: public void verifyAssertionError(AssertionError assetError)
164: throws AssertionError {
165:
166:• boolean assertErrorExpected = stepData.contains("AssertErrorExpected") ? (boolean) stepData.get("AssertErrorExpected") : false;
167:• String assertErrorName = stepData.contains("AssertErrorName") ? ((String) stepData.get("AssertErrorName")).trim() : "";
168:• String assertErrorMessage = stepData.contains("AssertErrorMessage") ? ((String) stepData.get("AssertErrorMessage")).trim() : "";
169:
170:• if (!assertErrorExpected ||
171:• (!assertErrorName.isEmpty() && !assetError.getClass().toGenericString().contains(assertErrorName)) ||
172:• (!assertErrorMessage.isEmpty() && !assertErrorMessage.trim().contentEquals("*") && !assetError.getMessage().contains(assertErrorMessage))) {
173: scenario.write("An unexpected assert error was raised!");
174: throw (assetError);
175: }
176:
177: scenario.write("Assert error raised as expected: " + assetError.getClass().getCanonicalName() + ", " + assetError.getMessage());
178: stepData.put("AssertErrorCaught", true);
179: stepData.put("AssertError", assetError);
180: }
181:
182:
183: public Date parseDateString(String date) {
184: DateFormat df = new SimpleDateFormat("dd/mm/yyyy");
185: Date expDate = null;
186: Instant now = Instant.now();
187:
188:• if (date == null) {
189: return null;
190: }
191: // Special keywords for date
192:• switch (date.trim().toLowerCase()) {
193: case "yesterday":
194: expDate = Date.from(now.minus(Duration.ofDays(1)));
195: break;
196: case "today":
197: expDate = Date.from(now);
198: break;
199: case "tomorrow":
200: expDate = Date.from(now.plus(Duration.ofDays(1)));
201: break;
202: case "null":
203: break;
204: }
205:
206: // Not one of the special cases. Just parse the date.
207: try {
208: expDate = df.parse(date.trim().toLowerCase());
209: } catch (ParseException | NullPointerException e) {
210: // skip, leave date null
211: }
212:
213: return expDate;
214: }
215:
216: public String getRandomString() {
217:
218: return String.valueOf(random.nextInt());
219: }
220: }