Initial OpenECOMP policy/engine commit
[policy/engine.git] / PyPDPServer / src / test / java / testpypdp / ConfigRequestTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package testpypdp;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25
26 import java.io.StringReader;
27 import java.util.HashMap;
28 import java.util.Properties;
29
30 import javax.xml.parsers.DocumentBuilder;
31 import javax.xml.parsers.DocumentBuilderFactory;
32
33 import org.openecomp.policy.pypdp.model_pojo.PyPolicyConfig;
34
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.openecomp.policy.api.PolicyConfigStatus;
38 import org.openecomp.policy.api.PolicyType;
39 import org.openecomp.policy.std.StdPolicyConfig;
40 import org.w3c.dom.Document;
41 import org.xml.sax.InputSource;
42
43 import org.openecomp.policy.pypdp.ConfigRequest;
44
45 public class ConfigRequestTest {
46
47         private StdPolicyConfig config;
48         private ConfigRequest request;
49
50         @Before
51         public void setUp() {
52                 request = new ConfigRequest(null);
53                 config = new StdPolicyConfig();
54                 config.setPolicyConfigStatus(PolicyConfigStatus.CONFIG_RETRIEVED);
55         }
56
57         @Test
58         public void checkResponsePropertiesTest() {
59                 config.setPolicyType(PolicyType.PROPERTIES);
60                 Properties prop = new Properties();
61                 prop.put("Key", "value");
62                 config.setProperties(prop);
63                 PyPolicyConfig pConfig = request.checkResponse(config);
64                 HashMap<String, String> result = new HashMap<String, String>();
65                 result.put("Key", "value");
66                 assertEquals(pConfig.getProperty(), result);
67         }
68
69         @Test
70         public void checkResponseDocumentTest() throws Exception {
71                 config.setPolicyType(PolicyType.XML);
72                 String xmlString = "<test></test>";
73                 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
74                 DocumentBuilder builder;
75                 builder = factory.newDocumentBuilder();
76                 Document document = builder.parse(new InputSource(new StringReader(
77                                 xmlString)));
78                 config.setDocument(document);
79                 PyPolicyConfig pConfig = request.checkResponse(config);
80                 assertNotNull(pConfig.getConfig());
81         }
82 }