/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2023 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
package org.onap.policy.pdp.xacml.application.common.operationshistory;
import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.assertEquals;
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
import com.att.research.xacml.api.Attribute;
import com.att.research.xacml.api.AttributeValue;
-import com.att.research.xacml.api.Status;
import com.att.research.xacml.api.pip.PIPException;
import com.att.research.xacml.api.pip.PIPFinder;
import com.att.research.xacml.api.pip.PIPRequest;
import com.att.research.xacml.api.pip.PIPResponse;
import com.att.research.xacml.std.pip.StdPIPResponse;
+import jakarta.persistence.EntityManager;
+import jakarta.persistence.EntityManagerFactory;
+import jakarta.persistence.Persistence;
+import jakarta.persistence.Query;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Date;
import java.util.Properties;
import java.util.Queue;
import java.util.UUID;
-import javax.persistence.EntityManager;
-import javax.persistence.Persistence;
-import javax.persistence.Query;
+import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
+import org.junit.runner.RunWith;
import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.onap.policy.guard.OperationsHistory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+@RunWith(MockitoJUnitRunner.class)
public class CountRecentOperationsPipTest {
private static final Logger LOGGER = LoggerFactory.getLogger(CountRecentOperationsPipTest.class);
private static final String TARGET = "my-target";
private static final String TEST_PROPERTIES = "src/test/resources/test.properties";
+ private static EntityManagerFactory emf;
private static EntityManager em;
@Mock
@Mock
private PIPResponse resp3;
- @Mock
- private Status okStatus;
-
private Properties properties;
private Queue<PIPResponse> responses;
private Queue<String> attributes;
//
String persistenceUnit = CountRecentOperationsPip.ISSUER_NAME + ".persistenceunit";
LOGGER.info("persistenceunit {}", persistenceUnit);
- em = Persistence.createEntityManagerFactory(props2.getProperty(persistenceUnit), props2).createEntityManager();
+ emf = Persistence.createEntityManagerFactory(props2.getProperty(persistenceUnit), props2);
+ em = emf.createEntityManager();
//
//
//
- LOGGER.info("Configured own entity manager", em.toString());
+ LOGGER.info("Configured own entity manager {}", em.toString());
}
/**
*/
@Before
public void setUp() throws Exception {
- MockitoAnnotations.initMocks(this);
-
when(pipRequest.getIssuer()).thenReturn("urn:org:onap:xacml:guard:tw:1:hour");
pipEngine = new MyPip();
properties.load(is);
}
- when(pipFinder.getMatchingAttributes(any(), eq(pipEngine))).thenReturn(resp1, resp2, resp3);
-
responses = new LinkedList<>(Arrays.asList(resp1, resp2, resp3));
attributes = new LinkedList<>(Arrays.asList(ACTOR, RECIPE, TARGET));
-
- when(resp1.getStatus()).thenReturn(okStatus);
- when(resp2.getStatus()).thenReturn(okStatus);
- when(resp3.getStatus()).thenReturn(okStatus);
-
- when(okStatus.isOk()).thenReturn(true);
}
@Test
}
@Test
- public void testConfigure_DbException() throws Exception {
- properties.put("javax.persistence.jdbc.url", "invalid");
+ public void testConfigure_DbException() {
+ properties.put("jakarta.persistence.jdbc.url", "invalid");
assertThatCode(() ->
pipEngine.configure("issuer", properties)
).doesNotThrowAnyException();
assertEquals(StdPIPResponse.PIP_RESPONSE_EMPTY, pipEngine.getAttributes(pipRequest, pipFinder));
}
+ @Test
+ public void testShutdown() {
+ pipEngine.shutdown();
+ assertThatExceptionOfType(PIPException.class).isThrownBy(() -> pipEngine.getAttributes(pipRequest, pipFinder))
+ .withMessageContaining("Engine is shutdown");
+ }
+
@Test
public void testGetCountFromDb() throws Exception {
//
//
// create entry
//
- Dbao newEntry = createEntry("cl-foobar-1", "vnf-1", "SUCCESS");
+ OperationsHistory newEntry = createEntry();
//
// No entries yet
//
//
// Directly check ground truth
//
- Query queryCount = em.createNativeQuery("select count(*) as numops from operationshistory").setParameter(1, 1);
+ Query queryCount = em.createNativeQuery("select count(*) as numops from operationshistory");
LOGGER.info("{} entries", queryCount.getSingleResult());
//
// Should count 1 entry now
}
@Test
- public void testStringToChronoUnit() throws PIPException {
+ public void testStringToChronosUnit() throws PIPException {
// not configured yet
- Dbao newEntry = createEntry("cl-foobar-1", "vnf-1", "SUCCESS");
+ OperationsHistory newEntry = createEntry();
assertEquals(-1, getCount(newEntry));
// now configure it
assertEquals(-1, getCount(newEntry));
}
- private long getCount(Dbao newEntry) throws PIPException {
+ private long getCount(OperationsHistory newEntry) throws PIPException {
responses = new LinkedList<>(Arrays.asList(resp1, resp2, resp3));
attributes = new LinkedList<>(
Arrays.asList(newEntry.getActor(), newEntry.getOperation(), newEntry.getTarget()));
return ((Number) value.getValue()).longValue();
}
- private Dbao createEntry(String cl, String target, String outcome) {
+ private OperationsHistory createEntry() {
//
// Create entry
//
- Dbao newEntry = new Dbao();
- newEntry.setClosedLoopName(cl);
- newEntry.setTarget(target);
- newEntry.setOutcome(outcome);
+ OperationsHistory newEntry = new OperationsHistory();
+ newEntry.setClosedLoopName("cl-foobar-1");
+ newEntry.setTarget("vnf-1");
+ newEntry.setOutcome("SUCCESS");
newEntry.setActor("Controller");
newEntry.setOperation("operationA");
newEntry.setStarttime(Date.from(Instant.now().minusMillis(20000)));