1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import javax.naming.Context;
  25 import javax.naming.NamingException;
  26 import javax.naming.directory.Attributes;
  27 import javax.naming.directory.InitialDirContext;
  28 import java.util.Hashtable;
  29 
  30 public abstract class EnvTestBase extends DNSTestBase {
  31     private static final String[] MANDATORY_ATTRIBUTES = { "A", "MX", "HINFO",
  32             "TXT", "29" };
  33     private static final String[] OPTIONAL_ATTRIBUTES = {};
  34 
  35     private String key;
  36     private String fqdnUrl;
  37     private String foreignFqdnUrl;
  38 
  39     public EnvTestBase() {
  40         // set default test data
  41         setKey("host1");
  42     }
  43 
  44     @Override public void setupTest() {
  45         super.setupTest();
  46         String fqdn = DNSTestUtils.buildFqdn(key, env(), true);
  47 
  48         String foreignLeaf = (String) env().get("FOREIGN_LEAF");
  49         String foreignFqdn = DNSTestUtils.buildFqdn(foreignLeaf, env(), false);
  50 
  51         fqdnUrl = DNSTestUtils.getRootUrl(env()) + "/" + fqdn;
  52         foreignFqdnUrl = DNSTestUtils.getRootUrl(env()) + "/" + foreignFqdn;
  53     }
  54 
  55     public void initContext() throws Exception {
  56         setContext(new InitialDirContext(env()));
  57     }
  58 
  59     public void addToEnvAndVerifyOldValIsNull(String propName, Object propVal)
  60             throws NamingException {
  61         addToEnvAndVerifyOldValIsNull(context(), propName, propVal);
  62     }
  63 
  64     public void addToEnvAndVerifyOldValIsNull(Context context, String propName,
  65             Object propVal) throws NamingException {
  66         Object oldValue = context.addToEnvironment(propName, propVal);
  67         DNSTestUtils.debug("Old Value for " + propName + " : " + oldValue);
  68         if (oldValue != null) {
  69             throw new RuntimeException(
  70                     "Failed: old value expected to be null for " + propName
  71                             + " but actual is : " + oldValue);
  72         }
  73     }
  74 
  75     public void verifyEnvProperty(Hashtable<?, ?> env, String propName,
  76             Object expectedVal) {
  77         boolean equals = true;
  78         Object actualVal = env.get(propName);
  79         if (actualVal != null && expectedVal != null) {
  80             if (!expectedVal.equals(actualVal)) {
  81                 equals = false;
  82             }
  83         } else {
  84             if (actualVal != null || expectedVal != null) {
  85                 equals = false;
  86             }
  87         }
  88 
  89         if (!equals) {
  90             throw new RuntimeException(
  91                     "Failed: value not match for " + propName + " expected: "
  92                             + expectedVal + " actual: " + actualVal);
  93         }
  94     }
  95 
  96     public void retrieveAndVerifyData(String name, String[] attrIds)
  97             throws NamingException {
  98         Attributes retAttrs = context().getAttributes(name, attrIds);
  99         DNSTestUtils.verifySchema(retAttrs, MANDATORY_ATTRIBUTES,
 100                 OPTIONAL_ATTRIBUTES);
 101     }
 102 
 103     public String getKey() {
 104         return key;
 105     }
 106 
 107     public void setKey(String key) {
 108         this.key = key;
 109     }
 110 
 111     public String getFqdnUrl() {
 112         return fqdnUrl;
 113     }
 114 
 115     public String getForeignFqdnUrl() {
 116         return foreignFqdnUrl;
 117     }
 118 }