Merge "Format ONAP-XACML and add JUnit"
[policy/engine.git] / ONAP-REST / src / test / java / org / onap / policy / rest / util / PolicyValidationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-REST
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.rest.util;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotSame;
26
27 import java.text.SimpleDateFormat;
28 import java.util.Date;
29 import java.util.UUID;
30
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.api.PolicyConfigType;
35 import org.onap.policy.api.PolicyParameters;
36 import org.onap.policy.rest.adapter.PolicyRestAdapter;
37
38 public class PolicyValidationTest {
39
40     @Before
41     public void setUp() throws Exception {
42     }
43
44     @After
45     public void tearDown() throws Exception {
46     }
47
48     @Test
49     public void microServicePolicyTests() throws Exception {
50         PolicyParameters policyParameters = new PolicyParameters();
51
52         policyParameters.setPolicyConfigType(PolicyConfigType.MicroService);
53         policyParameters.setPolicyName("Test.junitPolicy");
54         policyParameters.setPolicyDescription("This is a sample Micro Service policy Create example");
55         policyParameters.setOnapName("DCAE");
56         policyParameters.setPriority("1");
57         String msJsonString = "{\"service\":\"TOSCA_namingJenny\",\"location\":\"Test  DictMSLoc\","
58                         + "\"uuid\":\"testDict  DCAEUIID\",\"policyName\":\"testModelValidation\","
59                         + "\"description\":\"test\",\"configName\":\"testDict  MSConfName\","
60                         + "\"templateVersion\":\"1607\",\"version\":\"gw12181031\",\"priority\":\"5\","
61                         + "\"policyScope\":\"resource=ResourcetypeVenktest1,service=ServiceName1707,type=Name1707,"
62                         + "closedLoopControlName=Retest_retest1\",\"riskType\":\"Test\",\"riskLevel\":\"3\","
63                         + "\"guard\":\"True\",\"content\":{\"police-instance-name\":\"testing\","
64                         + "\"naming-models\":[{\"naming-properties\":[{\"property-value\":\"test\","
65                         + "\"source-endpoint\":\"test\",\"property-name\":\"testPropertyname\","
66                         + "\"increment-sequence\":{\"scope\":\"VNF\",\"start-value\":\"1\",\"length\":\"3\","
67                         + "\"increment\":\"2\"},\"source-system\":\"TOSCA\"}],\"naming-type\":\"testNamingType\","
68                         + "\"naming-recipe\":\"testNamingRecipe\"}]}}";
69         ;
70         policyParameters.setConfigBody(msJsonString);
71         policyParameters.setRequestID(UUID.randomUUID());
72         SimpleDateFormat dateformat3 = new SimpleDateFormat("dd/MM/yyyy");
73         Date date = dateformat3.parse("15/10/2016");
74         policyParameters.setTtlDate(date);
75         policyParameters.setGuard(true);
76         policyParameters.setRiskLevel("5");
77         policyParameters.setRiskType("TEST");
78         policyParameters.setRequestID(UUID.randomUUID());
79
80         PolicyValidationRequestWrapper wrapper = new PolicyValidationRequestWrapper();
81         PolicyRestAdapter policyData = wrapper.populateRequestParameters(policyParameters);
82         PolicyValidation validation = new PolicyValidation();
83         StringBuilder responseString = validation.validatePolicy(policyData);
84
85         assertNotSame("success", responseString.toString());
86
87     }
88
89     @Test
90     public final void testEmailValidation() {
91         PolicyValidation validation = new PolicyValidation();
92         String result = validation.emailValidation("testemail@test.com", "SUCCESS");
93         assertEquals("success", result);
94     }
95
96 }