aboutsummaryrefslogtreecommitdiffstats
path: root/integrity-monitor/src/test/java/org/onap/policy/common/im/IntegrityMonitorTestBase.java
blob: ba1fc34470916af5f879aadb98430431fcf461bc (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
 * ============LICENSE_START=======================================================
 * Integrity Audit
 * ================================================================================
 * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * 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.policy.common.im;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.onap.policy.common.im.IntegrityMonitor.Factory;
import org.onap.policy.common.utils.jpa.EntityTransCloser;
import org.onap.policy.common.utils.test.log.logback.ExtractAppender;
import org.onap.policy.common.utils.time.CurrentTime;
import org.onap.policy.common.utils.time.TestTime;
import org.powermock.reflect.Whitebox;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * All JUnits are designed to run in the local development environment where they have write
 * privileges and can execute time-sensitive tasks. <p/> Many of the test verification steps are
 * performed by scanning for items written to the log file. Rather than actually scan the log file,
 * an {@link ExtractAppender} is used to monitor events that are logged and extract relevant items.
 * In order to attach the appender to the debug log, it assumes that the debug log is a
 * <i>logback</i> Logger configured per EELF. <p/> These tests use a temporary, in-memory DB, which
 * is dropped once the tests complete.
 */
public class IntegrityMonitorTestBase {
    private static Logger logger = LoggerFactory.getLogger(IntegrityMonitorTestBase.class);
    
    /**
     * Name of the factory field within the IntegrityMonitor class.
     */
    public static final String FACTORY_FIELD = "factory";
    
    /**
     * Name of the instance field within the MonitorTime class.
     */
    public static final String TIME_INSTANCE_FIELD = "instance";

    /**
     * Directory containing the slf4j log files.
     */
    private static final String SLF4J_LOG_DIR = "logs";

    private static final String JMX_PORT_PROP = "com.sun.management.jmxremote.port";

    /**
     * Max time, in milliseconds, to wait for a latch to be triggered.
     */
    protected static final long WAIT_MS = 5000L;

    /**
     * Milliseconds between state refreshes.
     */
    protected static final long REFRESH_INTERVAL_MS = 3L * IntegrityMonitor.CYCLE_INTERVAL_MILLIS;

    public static final String DEFAULT_DB_URL_PREFIX = "jdbc:h2:mem:";

    protected static final String dbDriver = "org.h2.Driver";
    protected static final String dbUser = "testu";
    protected static final String dbPwd = "testp";
    protected static final String siteName = "SiteA";
    protected static final String nodeType = "pap";

    // will be defined by the test *Classes*
    protected static String dbUrl;

    /**
     * Persistence unit.
     */
    protected static final String PERSISTENCE_UNIT = "schemaPU";

    /**
     * Properties to be used in all tests.
     */
    protected static Properties properties;

    /**
     * Entity manager factory pointing to the in-memory DB for A_SEQ_PU.
     */
    protected static EntityManagerFactory emf;

    /**
     * Entity manager factory pointing to the in-memory DB associated with emf.
     */
    protected static EntityManager em;
    
    /**
     * Test time used by tests in lieu of CurrentTime.
     */
    private static TestTime testTime;

    /**
     * Saved JMX port from system properties, to be restored once all tests complete.
     */
    private static Object savedJmxPort;

    /**
     * Saved factory, to be restored once all tests complete.
     */
    private static Factory savedFactory;

    /**
     * Saved time accessor, to be restored once all tests complete.
     */
    private static CurrentTime savedTime;


    /**
     * Saves current configuration information and then sets new values.
     * 
     * @param dbDriver the name of the DB Driver class
     * @param dbUrl the URL to the DB
     * @throws IOException if an IO error occurs
     */
    protected static void setUpBeforeClass(String dbUrl) throws IOException {
        logger.info("setup");

        final Properties systemProps = System.getProperties();

        // truncate the logs
        new FileOutputStream(SLF4J_LOG_DIR + "/audit.log").close();
        new FileOutputStream(SLF4J_LOG_DIR + "/debug.log").close();
        new FileOutputStream(SLF4J_LOG_DIR + "/error.log").close();
        new FileOutputStream(SLF4J_LOG_DIR + "/metrics.log").close();

        IntegrityMonitorTestBase.dbUrl = dbUrl;

        // save data that we have to restore at the end of the test
        savedFactory = Whitebox.getInternalState(IntegrityMonitor.class, FACTORY_FIELD);
        savedJmxPort = systemProps.get(JMX_PORT_PROP);
        savedTime = MonitorTime.getInstance();

        systemProps.put(JMX_PORT_PROP, "9797");

        Whitebox.setInternalState(IntegrityMonitor.class, FACTORY_FIELD, new TestFactory());

        IntegrityMonitor.setUnitTesting(true);
        
        testTime = new TestTime();
        Whitebox.setInternalState(MonitorTime.class, TIME_INSTANCE_FIELD, testTime);
        
        properties = new Properties();
        properties.put(IntegrityMonitorProperties.DB_DRIVER, dbDriver);
        properties.put(IntegrityMonitorProperties.DB_URL, dbUrl);
        properties.put(IntegrityMonitorProperties.DB_USER, dbUser);
        properties.put(IntegrityMonitorProperties.DB_PWD, dbPwd);
        properties.put(IntegrityMonitorProperties.SITE_NAME, siteName);
        properties.put(IntegrityMonitorProperties.NODE_TYPE, nodeType);
        properties.put(IntegrityMonitorProperties.REFRESH_STATE_AUDIT_INTERVAL_MS,
                String.valueOf(REFRESH_INTERVAL_MS));

        emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT, makeProperties());

        // keep this open so the in-memory DB stays around until all tests are
        // done
        em = emf.createEntityManager();

        stopMonitor();
    }

    /**
     * Restores the configuration to what it was before the test.
     */
    protected static void tearDownAfterClass() {
        Properties systemProps = System.getProperties();
        if (savedJmxPort == null) {
            systemProps.remove(JMX_PORT_PROP);

        } else {
            systemProps.put(JMX_PORT_PROP, savedJmxPort);
        }

        Whitebox.setInternalState(MonitorTime.class, TIME_INSTANCE_FIELD, savedTime);
        Whitebox.setInternalState(IntegrityMonitor.class, FACTORY_FIELD, savedFactory);

        IntegrityMonitor.setUnitTesting(false);

        stopMonitor();

        // this should result in the in-memory DB being deleted
        em.close();
        emf.close();
    }

    /**
     * Sets up for a test, which includes deleting all records from the IntegrityAuditEntity table.
     */
    protected void setUpTest() {

        // Clean up the DB
        try (EntityTransCloser et = new EntityTransCloser(em.getTransaction())) {

            em.createQuery("Delete from StateManagementEntity").executeUpdate();
            em.createQuery("Delete from ForwardProgressEntity").executeUpdate();
            em.createQuery("Delete from ResourceRegistrationEntity").executeUpdate();

            // commit transaction
            et.commit();
        }
    }

    /**
     * Cleans up after a test, removing any ExtractAppenders from the logger and stopping any
     * AuditThreads.
     */
    protected void tearDownTest() {
        stopMonitor();
    }

    /**
     * Get saved factory.
     * 
     * @return the original integrity monitor factory
     */
    static Factory getSavedFactory() {
        return savedFactory;
    }

    /**
     * Stops the IntegrityMonitor instance.
     */
    private static void stopMonitor() {
        try {
            IntegrityMonitor.deleteInstance();

        } catch (IntegrityMonitorException e) {
            // no need to log, as exception was already logged
        }
    }
    
    /**
     * Get current test time.
     * 
     * @return the "current" time, in milliseconds
     */
    protected static long getCurrentTestTime() {
        return testTime.getMillis();
    }

    /**
     * Makes a new Property set that's a clone of {@link #properties}.
     * 
     * @return a new Property set containing all of a copy of all of the {@link #properties}
     */
    protected static Properties makeProperties() {
        Properties props = new Properties();
        props.putAll(properties);
        return props;
    }

    /**
     * Waits for a semaphore to be acquired.
     * 
     * @param sem semaphore to wait on
     * @throws InterruptedException if the thread is interrupted
     * @throws AssertionError if the semaphore was not acquired within the allotted time
     */
    protected void waitSem(Semaphore sem) throws InterruptedException {
        assertTrue(sem.tryAcquire(WAIT_MS, TimeUnit.MILLISECONDS));
    }

    /**
     * Applies a function on an object, expecting it to succeed. Catches any exceptions thrown by
     * the function.
     * 
     * @param arg the object to apply the function on
     * @param func the function
     * @throws AssertionError if an exception is thrown by the function
     */
    protected <T> void assertNoException(T arg, VoidFunction<T> func) {
        try {
            func.apply(arg);

        } catch (Exception e) {
            System.out.println("startTransaction exception: " + e);
            fail("action failed");
        }
    }

    /**
     * Applies a function on an object, expecting it to fail. Catches any exceptions thrown by the
     * function.
     * 
     * @param arg the object to apply the function on
     * @param func the function
     * @throws AssertionError if no exception is thrown by the function
     */
    protected <T> void assertException(T arg, VoidFunction<T> func) {
        try {
            func.apply(arg);
            fail("missing exception");
        } catch (Exception e) {
            System.out.println("action found expected exception: " + e);
        }
    }
    
    /**
     * Factory with overrides for junit testing.
     */
    public static class TestFactory extends Factory {
        @Override
        public String getPersistenceUnit() {
            return PERSISTENCE_UNIT;
        }
    }

    @FunctionalInterface
    protected static interface VoidFunction<T> {
        public void apply(T arg) throws Exception;
    }
}