import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.when;
+
import com.mockrunner.mock.web.MockServletInputStream;
+
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.SQLException;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
+
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
import org.onap.policy.pap.xacml.rest.policycontroller.PolicyCreation;
import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
import org.onap.policy.rest.dao.CommonClassDao;
+import org.onap.policy.rest.jpa.ActionPolicyDict;
import org.onap.policy.rest.jpa.BRMSParamTemplate;
import org.onap.policy.rest.jpa.Category;
+import org.onap.policy.rest.jpa.FunctionDefinition;
import org.onap.policy.rest.jpa.PolicyEditorScopes;
import org.onap.policy.rest.jpa.UserInfo;
import org.onap.policy.utils.PolicyUtils;
import org.springframework.mock.web.MockServletConfig;
import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
-
public class XACMLPAPTest {
private static final Log logger = LogFactory.getLog(XACMLPAPTest.class);
private static SessionFactory sessionFactory;
private static CommonClassDao commonClassDao;
- private static final String DEFAULT_DB_DRIVER = "org.h2.Driver";
- private static final String DEFAULT_DB_USER = "sa";
- private static final String DEFAULT_DB_PWD = "";
-
@BeforeClass
public static void beforeClassSetup() throws ServletException {
sessionFactory = PolicyDBDaoTest.setupH2DbDaoImpl("xacmlpaptest");
public void testInit() {
httpServletRequest = Mockito.mock(HttpServletRequest.class);
httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
+ logger.info(httpServletResponse);
Mockito.when(httpServletRequest.getHeaderNames()).thenReturn(Collections.enumeration(headers));
Mockito.when(httpServletRequest.getAttributeNames()).thenReturn(Collections.enumeration(headers));
CommonClassDaoImpl.setSessionfactory(sessionFactory);
MockServletInputStream mockInput =
new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
-
+ logger.info(httpServletRequest);
+ logger.info(httpServletResponse);
pap.service(httpServletRequest, httpServletResponse);
Mockito.verify(httpServletResponse).addHeader("operation", "create");
}
Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
setPolicyCreation();
+ logger.info(httpServletRequest);
+ logger.info(httpServletResponse);
pap.service(httpServletRequest, httpServletResponse);
Mockito.verify(httpServletResponse).addHeader("operation", "create");
}
Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
setPolicyCreation();
+ logger.info(httpServletRequest);
+ logger.info(httpServletResponse);
pap.service(httpServletRequest, httpServletResponse);
Mockito.verify(httpServletResponse).addHeader("operation", "create");
}
Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
setPolicyCreation();
+ logger.info(httpServletRequest);
+ logger.info(httpServletResponse);
pap.service(httpServletRequest, httpServletResponse);
Mockito.verify(httpServletResponse).addHeader("operation", "create");
}
new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
+ logger.info(httpServletRequest);
+ logger.info(httpServletResponse);
pap.service(httpServletRequest, httpServletResponse);
Mockito.verify(httpServletResponse).addHeader("operation", "create");
}
new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
+ logger.info(httpServletRequest);
+ logger.info(httpServletResponse);
pap.service(httpServletRequest, httpServletResponse);
Mockito.verify(httpServletResponse).addHeader("operation", "create");
}
matchingAttributes.put("guardActiveEnd", "10:00");
StdPAPPolicy newPAPPolicy =
- new StdPAPPolicy(
- StdPAPPolicyParams.builder().policyName("testGuardMinMax").description("test rule").onapName("PDPD")
- .providerComboBox("GUARD_MIN_MAX").dynamicFieldConfigAttributes(matchingAttributes)
- .editPolicy(false).domain("test").highestVersion(0).build());
+ new StdPAPPolicy(StdPAPPolicyParams.builder().policyName("testGuardMinMax").description("test rule")
+ .onapName("PDPD").providerComboBox("GUARD_MIN_MAX")
+ .dynamicFieldConfigAttributes(matchingAttributes).editPolicy(false).domain("test")
+ .highestVersion(0).build());
MockServletInputStream mockInput =
new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
+ logger.info(httpServletRequest);
+ logger.info(httpServletResponse);
pap.service(httpServletRequest, httpServletResponse);
Mockito.verify(httpServletResponse).addHeader("operation", "create");
}
-
@Test
public void testDecisonBLGuardPolicy() throws IOException, ServletException, SQLException {
httpServletRequest = Mockito.mock(HttpServletRequest.class);
new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
+ logger.info(httpServletRequest);
+ logger.info(httpServletResponse);
pap.service(httpServletRequest, httpServletResponse);
Mockito.verify(httpServletResponse).addHeader("operation", "create");
}
new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
+ logger.info(httpServletRequest);
+ logger.info(httpServletResponse);
+ pap.service(httpServletRequest, httpServletResponse);
+ Mockito.verify(httpServletResponse).addHeader("operation", "create");
+ }
+
+ @Test
+ public void testActionPolicy() throws IOException, ServletException, SQLException {
+ setPolicyCreation();
+ httpServletRequest = Mockito.mock(HttpServletRequest.class);
+ Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
+ Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
+ Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("API");
+ Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
+ Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Action");
+
+ CommonClassDao commonClassDao = Mockito.mock(CommonClassDao.class);
+ PolicyCreation.setCommonClassDao(commonClassDao);
+ ActionPolicyDict actionDict = new ActionPolicyDict();
+ actionDict.setBody("{\"test\":\"test\"}");
+ actionDict.setHeader("test122=test12:test22=test34");
+ actionDict.setType("REST");
+ actionDict.setMethod("GET");
+ actionDict.setUrl("testsomeurl.com");
+ Mockito.when(commonClassDao.getEntityItem(ActionPolicyDict.class, "attributeName", "test"))
+ .thenReturn(actionDict);
+ FunctionDefinition funcDefn = new FunctionDefinition();
+ funcDefn.setXacmlid("urn:oasis:names:tc:xacml:1.0:function:and");
+ Mockito.when(commonClassDao.getEntityItem(FunctionDefinition.class, "short_name", "and")).thenReturn(funcDefn);
+ funcDefn.setXacmlid("urn:oasis:names:tc:xacml:1.0:function:integer-equal");
+ Mockito.when(commonClassDao.getEntityItem(FunctionDefinition.class, "short_name", "integer-equal"))
+ .thenReturn(funcDefn);
+ funcDefn.setXacmlid("urn:oasis:names:tc:xacml:3.0:function:string-contains");
+ Mockito.when(commonClassDao.getEntityItem(FunctionDefinition.class, "short_name", "string-contains"))
+ .thenReturn(funcDefn);
+ funcDefn.setXacmlid("urn:oasis:names:tc:xacml:1.0:function:integer-greater-than");
+ Mockito.when(commonClassDao.getEntityItem(FunctionDefinition.class, "short_name", "integer-greater-than"))
+ .thenReturn(funcDefn);
+ funcDefn.setXacmlid("urn:oasis:names:tc:xacml:1.0:function:or");
+ Mockito.when(commonClassDao.getEntityItem(FunctionDefinition.class, "short_name", "or")).thenReturn(funcDefn);
+
+ Map<String, String> componentAttributes = new HashMap<>();
+ componentAttributes.put("java", "test");
+ StdPAPPolicy newPapPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder().policyName("test").description("test")
+ .dynamicFieldConfigAttributes(componentAttributes)
+ .dynamicRuleAlgorithmLabels(Arrays.asList("A1", "A2", "A3", "A4", "A5", "A6", "A7"))
+ .dynamicRuleAlgorithmCombo(Arrays.asList("integer-equal", "string-contains", "integer-equal", "and",
+ "integer-greater-than", "or", "and"))
+ .dynamicRuleAlgorithmField1(Arrays.asList("cobal", "cap", "cobal", "A2", "Config", "A4", "A1"))
+ .dynamicRuleAlgorithmField2(Arrays.asList("90", "ca", "90", "A3", "45", "A5", "A6"))
+ .actionPerformer("PDP").actionAttribute("test").editPolicy(false).domain("com").highestVersion(0)
+ .build());
+ newPapPolicy.setActionBody("{\"test\":\"test\"}");
+
+ MockServletInputStream mockInput =
+ new MockServletInputStream(PolicyUtils.objectToJsonString(newPapPolicy).getBytes());
+ Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
+
+ logger.info(httpServletRequest);
+ logger.info(httpServletResponse);
pap.service(httpServletRequest, httpServletResponse);
Mockito.verify(httpServletResponse).addHeader("operation", "create");
}
template.setRule(rule);
Mockito.when(commonClassDao.getEntityItem(BRMSParamTemplate.class, "ruleName", "testPolicy"))
.thenReturn(template);
-
}
@Test
// Verify
Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
-
httpServletRequest = Mockito.mock(HttpServletRequest.class);
httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
json = "{\"dictionaryFields\":{\"attributeName\":\"TestMMrestAPI1\",\"type\":\"REST\",\"url\":\"testsomeurl"
try {
Mockito.when(httpServletResponse.getOutputStream()).thenReturn(mockOutput);
} catch (IOException e) {
- fail();
+ fail(e.getMessage());
}
try {
pap.service(httpServletRequest, httpServletResponse);
assertTrue(true);
} catch (Exception e) {
- fail();
+ fail(e.getMessage());
}
}