2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ============LICENSE_END=========================================================
19 package org.onap.policy.database.operationshistory;
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertNull;
23 import static org.mockito.ArgumentMatchers.anyString;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
27 import com.att.research.xacml.api.Attribute;
28 import com.att.research.xacml.api.AttributeValue;
29 import com.att.research.xacml.api.XACML3;
30 import com.att.research.xacml.api.pip.PIPException;
31 import com.att.research.xacml.api.pip.PIPFinder;
32 import com.att.research.xacml.api.pip.PIPRequest;
33 import com.att.research.xacml.api.pip.PIPResponse;
34 import com.att.research.xacml.std.pip.StdPIPResponse;
35 import java.io.FileInputStream;
37 import java.time.Instant;
38 import java.util.Collection;
39 import java.util.Properties;
40 import java.util.UUID;
41 import javax.persistence.EntityManager;
42 import javax.persistence.EntityManagerFactory;
43 import javax.persistence.NoResultException;
44 import javax.persistence.Persistence;
45 import org.junit.AfterClass;
46 import org.junit.Before;
47 import org.junit.BeforeClass;
48 import org.junit.Test;
49 import org.onap.policy.database.ToscaDictionary;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
53 public class GetOperationOutcomePipTest {
54 private static final String ID = "issuer";
55 private static final String TEST_CL1 = "testcl1";
56 private static final String TEST_TARGET1 = "testtarget1";
57 private static final String TEST_TARGET2 = "testtarget2";
58 private static final String ACTOR = "Controller";
59 private static final String RECIPE = "operationA";
60 private static final String EXPECTED_EXCEPTION = "expected exception";
61 private static final String ISSUER_PREFIX = ToscaDictionary.GUARD_ISSUER_PREFIX + "-my-issuer:clname:";
62 private static final String ISSUER = ISSUER_PREFIX + TEST_CL1;
63 private static final Logger LOGGER = LoggerFactory.getLogger(GetOperationOutcomePipTest.class);
65 private static MyPip pipEngine;
66 private static Properties properties;
68 private static EntityManagerFactory emf;
69 private static EntityManager em;
71 private PIPRequest req;
74 * Create an instance of our engine and also the persistence
77 * @throws Exception connectivity issues
80 public static void setUpBeforeClass() throws Exception {
81 LOGGER.info("Setting up PIP Testing");
85 pipEngine = new MyPip(TEST_TARGET1);
87 // Load our test properties to use
89 properties = new Properties();
90 try (FileInputStream is = new FileInputStream("src/test/resources/test.properties")) {
94 // Configure it using properties
96 pipEngine.configure(ID, properties);
97 LOGGER.info("PIP configured now creating our entity manager");
98 LOGGER.info("properties {}", properties);
100 // Connect to in-mem db
102 String persistenceUnit = GetOperationOutcomePip.ISSUER_NAME + ".persistenceunit";
103 LOGGER.info("persistenceunit {}", persistenceUnit);
104 emf = Persistence.createEntityManagerFactory(properties.getProperty(persistenceUnit), properties);
105 em = emf.createEntityManager();
109 LOGGER.info("Configured own entity manager", em.toString());
113 public void setUp() {
114 req = mock(PIPRequest.class);
115 when(req.getIssuer()).thenReturn(ISSUER);
118 private void insertEntry(String cl, String target, String outcome) {
122 Dbao newEntry = new Dbao();
123 newEntry.setClosedLoopName(cl);
124 newEntry.setTarget(target);
125 newEntry.setOutcome(outcome);
126 newEntry.setActor(ACTOR);
127 newEntry.setOperation(RECIPE);
128 newEntry.setStarttime(Date.from(Instant.now().minusMillis(20000)));
129 newEntry.setEndtime(Date.from(Instant.now()));
130 newEntry.setRequestId(UUID.randomUUID().toString());
134 em.getTransaction().begin();
135 em.persist(newEntry);
136 em.getTransaction().commit();
141 public void testAttributesRequired() {
142 assertEquals(1, pipEngine.attributesRequired().size());
146 public void testGetAttributes_InvalidRequestInfo() throws PIPException {
147 // invalid request - null issuer
148 when(req.getIssuer()).thenReturn(null);
149 assertEquals(StdPIPResponse.PIP_RESPONSE_EMPTY, pipEngine.getAttributes(req, null));
152 * Make a valid issuer in the request, for subsequent tests.
154 when(req.getIssuer()).thenReturn(ISSUER);
157 MyPip pip = new MyPip(null);
158 pip.configure(ID, properties);
159 assertEquals(StdPIPResponse.PIP_RESPONSE_EMPTY, pip.getAttributes(req, null));
163 public void testDoDatabaseQuery() throws Exception {
167 insertEntry(TEST_CL1, TEST_TARGET1, "1");
169 // outcome should be "1"
171 assertEquals("1", getOutcome(pipEngine.getAttributes(req, null)));
173 // Insert more entries
175 insertEntry(TEST_CL1, TEST_TARGET1, "2");
176 insertEntry("testcl2", TEST_TARGET2, "3");
177 insertEntry(TEST_CL1, TEST_TARGET2, "4");
181 assertEquals("2", getOutcome(TEST_CL1, TEST_TARGET1));
183 assertEquals("3", getOutcome("testcl2", TEST_TARGET2));
185 assertEquals("4", getOutcome(TEST_CL1, TEST_TARGET2));
189 public void testDoDatabaseQuery_NoResult() throws Exception {
190 MyPip pip = new MyPip(TEST_TARGET1) {
192 public void configure(String id, Properties properties) throws PIPException {
193 em = mock(EntityManager.class);
194 when(em.createQuery(anyString())).thenThrow(new NoResultException());
197 pip.configure(ID, properties);
199 assertNull(getOutcome(pip.getAttributes(req, null)));
203 public void testDoDatabaseQuery_EmException() throws Exception {
204 MyPip pip = new MyPip(TEST_TARGET1) {
206 public void configure(String id, Properties properties) throws PIPException {
207 em = mock(EntityManager.class);
208 when(em.createQuery(anyString())).thenThrow(new RuntimeException(EXPECTED_EXCEPTION));
211 pip.configure(ID, properties);
213 assertEquals(null, getOutcome(pip.getAttributes(req, null)));
216 private String getOutcome(String clname, String target) throws PIPException {
217 req = mock(PIPRequest.class);
218 when(req.getIssuer()).thenReturn(ISSUER_PREFIX + clname);
220 MyPip pip = new MyPip(target);
221 pip.configure(ID, properties);
223 return getOutcome(pip.getAttributes(req, null));
226 private String getOutcome(PIPResponse resp) {
227 Collection<Attribute> attrs = resp.getAttributes();
228 assertEquals(1, attrs.size());
230 Attribute attr = attrs.iterator().next();
231 assertEquals(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, attr.getCategory());
232 assertEquals(ToscaDictionary.ID_RESOURCE_GUARD_OPERATIONOUTCOME, attr.getAttributeId());
234 Collection<AttributeValue<?>> values = attr.getValues();
235 assertEquals(1, values.size());
237 AttributeValue<?> value = values.iterator().next();
238 return (String) value.getValue();
242 * Close the entity manager.
245 public static void cleanup() {
251 private static class MyPip extends GetOperationOutcomePip {
252 private String target;
254 public MyPip(String target) {
255 this.target = target;
259 protected String getActor(PIPFinder pipFinder) {
264 protected String getRecipe(PIPFinder pipFinder) {
269 protected String getTarget(PIPFinder pipFinder) {