View Javadoc
1   /**
2    * Copyright 2010-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.dns.dnsme;
17  
18  import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
19  import static com.google.common.base.Stopwatch.createStarted;
20  import static java.lang.String.format;
21  import static org.kuali.common.core.json.jackson.ObjectMappers.buildDefaultMapper;
22  import static org.kuali.common.dns.dnsme.Credentials.ENCRYPTED_DNSME_CREDENTIALS_PRODUCTION;
23  import static org.kuali.common.dns.dnsme.DNSMadeEasyDnsService.HTTP_OK;
24  import static org.kuali.common.dns.dnsme.DNSME.DNSME_REST_API_URL_PRODUCTION;
25  import static org.kuali.common.dns.dnsme.spring.DNSMadeEasyUtils.getServiceContext;
26  import static org.kuali.common.util.FormatUtils.getTime;
27  import static org.kuali.common.util.log.Loggers.newLogger;
28  
29  import java.util.List;
30  
31  import org.junit.Test;
32  import org.kuali.common.core.json.api.JsonService;
33  import org.kuali.common.core.json.jackson.JacksonContext;
34  import org.kuali.common.core.json.jackson.JacksonJsonService;
35  import org.kuali.common.dns.dnsme.model.DNSMadeEasyCredentials;
36  import org.kuali.common.dns.dnsme.model.DNSMadeEasyServiceContext;
37  import org.kuali.common.dns.dnsme.model.DnsMadeEasyDnsRecord;
38  import org.kuali.common.dns.model.SimpleDnsRecord;
39  import org.kuali.common.util.spring.env.BasicEnvironmentService;
40  import org.kuali.common.util.spring.env.EnvironmentService;
41  import org.slf4j.Logger;
42  
43  import com.fasterxml.jackson.databind.ObjectMapper;
44  import com.fasterxml.jackson.databind.node.ObjectNode;
45  import com.google.common.base.Stopwatch;
46  import com.google.common.collect.ImmutableList;
47  
48  public class DNSMadeEasyDnsServiceTest {
49  
50  	private static final Logger logger = newLogger();
51  
52  	@Test
53  	public void test() {
54  		try {
55  			Stopwatch sw = createStarted();
56  			DNSMadeEasyServiceContext ctx = getContext();
57  			DNSMadeEasyDnsService dns = new DNSMadeEasyDnsService(ctx);
58  			info("%s", dns.getDomain().getName());
59  			String url = dns.getAllRecordsApiUrl();
60  			info("%s", url);
61  			String records = dns.getJson(url, HTTP_OK);
62  			ObjectMapper mapper = buildDefaultMapper().configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
63  			JacksonContext jc = JacksonContext.builder().withMapper(mapper).build();
64  			JsonService json = new JacksonJsonService(jc);
65  			Class<ObjectNode[]> type = ObjectNode[].class;
66  			List<ObjectNode> nodes = ImmutableList.copyOf(json.readString(records, type));
67  			String pretty = json.writeString(nodes);
68  			List<DnsMadeEasyDnsRecord> list = ImmutableList.copyOf(json.readString(pretty, DnsMadeEasyDnsRecord[].class));
69  			List<SimpleDnsRecord> simpleRecords = dns.getRecords(list);
70  			System.out.println("\n" + json.writeString(simpleRecords) + "\n");
71  			// Make sure DnsRecord objects can be both written to and read from json
72  			json.readString(json.writeString(simpleRecords), SimpleDnsRecord[].class);
73  			elapsed(sw);
74  		} catch (Throwable e) {
75  			e.printStackTrace();
76  		}
77  	}
78  
79  	private static DNSMadeEasyServiceContext getContext() {
80  		String domain = "kuali.org";
81  		String restApiUrl = DNSME_REST_API_URL_PRODUCTION;
82  		DNSMadeEasyCredentials encrypted = ENCRYPTED_DNSME_CREDENTIALS_PRODUCTION.getCredentials();
83  		EnvironmentService env = new BasicEnvironmentService();
84  		return getServiceContext(env, restApiUrl, domain, encrypted);
85  	}
86  
87  	protected static void debug(String msg, Object... args) {
88  		logger.debug((args == null || args.length == 0) ? msg : format(msg, args));
89  	}
90  
91  	protected static void elapsed(Stopwatch sw) {
92  		info("elapsed -> %s", getTime(sw));
93  	}
94  
95  	protected static void info(String msg, Object... args) {
96  		logger.info((args == null || args.length == 0) ? msg : format(msg, args));
97  	}
98  
99  }