[DMaap-msgrtr] Update Security Vulnerabilities
[dmaap/messagerouter/msgrtr.git] / src / test / java / org / onap / dmaap / mr / test / dme2 / DME2ApiKeyTest.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
4  *  ================================================================================
5  *  Copyright © 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  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *  ============LICENSE_END=========================================================
18  *
19  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  *
21  *******************************************************************************/
22 package org.onap.dmaap.mr.test.dme2;
23
24 import com.att.aft.dme2.api.DME2Client;
25 import com.att.aft.dme2.api.DME2Exception;
26 import com.att.aft.dme2.internal.jackson.map.ObjectMapper;
27 import java.net.URI;
28 import java.net.URISyntaxException;
29 import java.util.Properties;
30 import junit.framework.TestCase;
31 import org.apache.logging.log4j.LogManager;
32 import org.apache.logging.log4j.Logger;
33
34 public class DME2ApiKeyTest extends TestCase {
35
36     private static final Logger LOGGER = LogManager.getLogger(DME2ApiKeyTest.class);
37
38     protected String url;
39
40     protected Properties props;
41
42     @Override
43     protected void setUp() throws Exception {
44         super.setUp();
45         System.setProperty("AFT_DME2_CLIENT_SSL_INCLUDE_PROTOCOLS", "SSLv3,TLSv1,TLSv1.1");
46         System.setProperty("AFT_DME2_CLIENT_IGNORE_SSL_CONFIG", "false");
47         System.setProperty("AFT_DME2_CLIENT_KEYSTORE_PASSWORD", "changeit");
48         this.props = LoadPropertyFile.getPropertyFileDataProducer();
49         String latitude = props.getProperty("Latitude");
50         String longitude = props.getProperty("Longitude");
51         String version = props.getProperty("Version");
52         String serviceName = props.getProperty("ServiceName");
53         String env = props.getProperty("Environment");
54         String partner = props.getProperty("Partner");
55         String protocol = props.getProperty("Protocol");
56         this.url =
57             protocol + "://" + serviceName + "?" + "version=" + version + "&" + "envContext=" + env
58                 + "&"
59                 + "routeOffer=" + partner + "&partner=BOT_R";
60         LoadPropertyFile.loadAFTProperties(latitude, longitude);
61     }
62
63     public void testCreateKey() {
64         LOGGER.info("Create Key test case initiated");
65         ApiKeyBean apiKeyBean = new ApiKeyBean("user1@onap.com", "Creating Api Key.m");
66         System.out.println(url);
67         returnKey(apiKeyBean, url, props);
68     }
69
70     public String returnKey(ApiKeyBean apibean, String url, Properties props) {
71         String reply = null;
72         try {
73             LOGGER.info("Call to return key ");
74             DME2Client sender = new DME2Client(new URI(url), 5000L);
75             sender.setAllowAllHttpReturnCodes(true);
76             sender.setMethod(props.getProperty("MethodTypePost"));
77             sender.setSubContext(props.getProperty("SubContextPathGetCreateKeys"));
78             String jsonStringApiBean = new ObjectMapper().writeValueAsString(apibean);
79             sender.setPayload(jsonStringApiBean);
80             sender.addHeader("content-type", props.getProperty("contenttype"));
81             sender.setCredentials(props.getProperty("user"), props.getProperty("password"));
82             LOGGER.info("creating ApiKey");
83             reply = sender.sendAndWait(5000L);
84             System.out.println("reply: " + reply);
85             assertTrue(LoadPropertyFile.isValidJsonString(reply));
86             LOGGER.info("response =" + reply);
87         } catch (DME2Exception e) {
88             e.printStackTrace();
89         } catch (URISyntaxException e) {
90             e.printStackTrace();
91         } catch (Exception e) {
92             e.printStackTrace();
93         }
94         return reply;
95     }
96
97     public void testGetAllKey() {
98         LOGGER.info("Test case Get All key initiated....");
99         try {
100             DME2Client sender = new DME2Client(new URI(this.url), 5000L);
101             sender.setAllowAllHttpReturnCodes(true);
102             sender.setMethod(this.props.getProperty("MethodTypeGet"));
103             String subcontextPath = this.props.getProperty("SubContextPathGetApiKeys");
104             // sender.setSubContext(subcontextPath);
105             sender.setPayload("");
106             sender.addHeader("content-type", props.getProperty("contenttype"));
107             sender.setCredentials(props.getProperty("user"), props.getProperty("password"));
108             LOGGER.info("Fetching all keys");
109             String reply = sender.sendAndWait(5000L);
110             System.out.println(reply);
111             assertTrue(LoadPropertyFile.isValidJsonString(reply));
112             LOGGER.info("response =" + reply);
113         } catch (DME2Exception e) {
114             e.printStackTrace();
115         } catch (URISyntaxException e) {
116             e.printStackTrace();
117         } catch (Exception e) {
118             e.printStackTrace();
119         }
120     }
121 }