diff options
Diffstat (limited to 'utils/src/test/java/org/onap/policy')
4 files changed, 685 insertions, 497 deletions
diff --git a/utils/src/test/java/org/onap/policy/common/utils/properties/PropertyConfigurationTest.java b/utils/src/test/java/org/onap/policy/common/utils/properties/PropertyConfigurationTest.java index cf823b5d..121ae384 100644 --- a/utils/src/test/java/org/onap/policy/common/utils/properties/PropertyConfigurationTest.java +++ b/utils/src/test/java/org/onap/policy/common/utils/properties/PropertyConfigurationTest.java @@ -68,15 +68,9 @@ public class PropertyConfigurationTest { @Test public void testPropertyConfiguration() throws PropertyException { - class Config extends PropertyConfiguration { - - @Property(name = THE_VALUE) - private String value; - }; - props.setProperty(THE_VALUE, STRING_VALUE); - Config cfg = new Config(); + PlainStringConfig cfg = new PlainStringConfig(); assertEquals(null, cfg.value); cfg.setAllFields(props); @@ -85,18 +79,8 @@ public class PropertyConfigurationTest { @Test public void testPropertyConfigurationProperties() throws PropertyException { - class Config extends PropertyConfiguration { - - @Property(name = THE_VALUE) - private String value; - - public Config(Properties props) throws PropertyException { - super(props); - } - }; - props.setProperty(THE_VALUE, STRING_VALUE); - Config cfg = new Config(props); + PlainStringConfig cfg = new PlainStringConfig(props); assertEquals(STRING_VALUE, cfg.value); } @@ -111,6 +95,11 @@ public class PropertyConfigurationTest { @Property(name = "grandparent.value") protected boolean grandparentValue; + + @SuppressWarnings("unused") + public void setGrandparentValue(boolean grandparentValue) { + this.grandparentValue = grandparentValue; + } }; /* @@ -120,12 +109,22 @@ public class PropertyConfigurationTest { @Property(name = "parent.value") protected long parentValue; + + @SuppressWarnings("unused") + public void setParentValue(long parentValue) { + this.parentValue = parentValue; + } }; class Config extends ParentConfig { @Property(name = THE_VALUE) private String value; + + @SuppressWarnings("unused") + public void setValue(String value) { + this.value = value; + } }; @@ -158,6 +157,11 @@ public class PropertyConfigurationTest { class Config extends PropertyConfiguration { private String value; + + @SuppressWarnings("unused") + public void setValue(String value) { + this.value = value; + } }; @@ -171,18 +175,8 @@ public class PropertyConfigurationTest { @Test public void testSetValueFieldProperties_FieldSet() throws PropertyException { - class Config extends PropertyConfiguration { - - @Property(name = THE_VALUE) - private String value; - - public Config(Properties props) throws PropertyException { - super(props); - } - }; - props.setProperty(THE_VALUE, STRING_VALUE); - Config cfg = new Config(props); + PlainStringConfig cfg = new PlainStringConfig(props); assertEquals(STRING_VALUE, cfg.value); } @@ -196,6 +190,11 @@ public class PropertyConfigurationTest { public Config(Properties props) throws PropertyException { super(props); } + + @SuppressWarnings("unused") + public void setValue(String value) { + this.value = value; + } }; props.setProperty(THE_VALUE, STRING_VALUE); @@ -215,14 +214,19 @@ public class PropertyConfigurationTest { public Config(Properties props) throws PropertyException { super(props); } + + @SuppressWarnings("unused") + public void setValue(Exception value) { + this.value = value; + } }; props.setProperty(THE_VALUE, STRING_VALUE); new Config(props); } - @Test(expected = PropertyMissingException.class) - public void testSetValueFieldPropertyProperties_NoProperty_NoDefault() throws PropertyException { + @Test(expected = PropertyAccessException.class) + public void testGetSetter_NoSetter() throws PropertyException { class Config extends PropertyConfiguration { @Property(name = THE_VALUE) @@ -233,15 +237,18 @@ public class PropertyConfigurationTest { } }; + props.setProperty(THE_VALUE, STRING_VALUE); new Config(props); } - @Test(expected = PropertyInvalidException.class) - public void testSetValueFieldPropertyProperties_InvalidValue() throws PropertyException { - class Config extends PropertyConfiguration { + @Test(expected = PropertyMissingException.class) + public void testSetValueMethodFieldPropertiesProperty_NoProperty_NoDefault() throws PropertyException { + new PlainStringConfig(props); + } - @Property(name = THE_VALUE) - private int value; + @Test(expected = PropertyInvalidException.class) + public void testSetValueMethodFieldPropertiesProperty_InvalidValue() throws PropertyException { + class Config extends PlainPrimIntConfig { public Config(Properties props) throws PropertyException { super(props); @@ -260,6 +267,24 @@ public class PropertyConfigurationTest { new Config(props); } + @Test(expected = PropertyAccessException.class) + public void testSetValueMethodFieldPropertiesProperty_MethodEx() throws PropertyException { + class Config extends PlainStringConfig { + + public Config(Properties props) throws PropertyException { + super(props); + } + + @Override + public void setValue(String value) { + throw new IllegalArgumentException("expected exception"); + } + }; + + props.setProperty(THE_VALUE, STRING_VALUE); + new Config(props); + } + @Test public void testGetValue() throws PropertyException { // this class contains all of the supported field types @@ -295,6 +320,96 @@ public class PropertyConfigurationTest { public Config(Properties props) throws PropertyException { super(props); } + + @SuppressWarnings("unused") + public String getStringValue() { + return stringValue; + } + + @SuppressWarnings("unused") + public void setStringValue(String stringValue) { + this.stringValue = stringValue; + } + + @SuppressWarnings("unused") + public Boolean getBoolTrueValue() { + return boolTrueValue; + } + + @SuppressWarnings("unused") + public void setBoolTrueValue(Boolean boolTrueValue) { + this.boolTrueValue = boolTrueValue; + } + + @SuppressWarnings("unused") + public Boolean getBoolFalseValue() { + return boolFalseValue; + } + + @SuppressWarnings("unused") + public void setBoolFalseValue(Boolean boolFalseValue) { + this.boolFalseValue = boolFalseValue; + } + + @SuppressWarnings("unused") + public boolean isPrimBoolTrueValue() { + return primBoolTrueValue; + } + + @SuppressWarnings("unused") + public void setPrimBoolTrueValue(boolean primBoolTrueValue) { + this.primBoolTrueValue = primBoolTrueValue; + } + + @SuppressWarnings("unused") + public boolean isPrimBoolFalseValue() { + return primBoolFalseValue; + } + + @SuppressWarnings("unused") + public void setPrimBoolFalseValue(boolean primBoolFalseValue) { + this.primBoolFalseValue = primBoolFalseValue; + } + + @SuppressWarnings("unused") + public Integer getIntValue() { + return intValue; + } + + @SuppressWarnings("unused") + public void setIntValue(Integer intValue) { + this.intValue = intValue; + } + + @SuppressWarnings("unused") + public int getPrimIntValue() { + return primIntValue; + } + + @SuppressWarnings("unused") + public void setPrimIntValue(int primIntValue) { + this.primIntValue = primIntValue; + } + + @SuppressWarnings("unused") + public Long getLongValue() { + return longValue; + } + + @SuppressWarnings("unused") + public void setLongValue(Long longValue) { + this.longValue = longValue; + } + + @SuppressWarnings("unused") + public long getPrimLongValue() { + return primLongValue; + } + + @SuppressWarnings("unused") + public void setPrimLongValue(long primLongValue) { + this.primLongValue = primLongValue; + } }; props.setProperty("string", "a string"); @@ -331,6 +446,11 @@ public class PropertyConfigurationTest { public Config(Properties props) throws PropertyException { super(props); } + + @SuppressWarnings("unused") + public void setValue(Exception value) { + this.value = value; + } }; props.setProperty(THE_VALUE, STRING_VALUE); @@ -354,6 +474,21 @@ public class PropertyConfigurationTest { public Config(Properties props) throws PropertyException { super(props); } + + @SuppressWarnings("unused") + public void setPublicString(String publicString) { + this.publicString = publicString; + } + + @SuppressWarnings("unused") + public void setPrivateString(String privateString) { + this.privateString = privateString; + } + + @SuppressWarnings("unused") + public void setProtectedString(String protectedString) { + this.protectedString = protectedString; + } }; props.setProperty("public", "a public string"); @@ -370,7 +505,7 @@ public class PropertyConfigurationTest { @Test(expected = PropertyAccessException.class) public void testCheckModifiable_Static() throws PropertyException { props.setProperty(THE_VALUE, STRING_VALUE); - new StaticConfig(props); + new StaticPropConfig(props); } @Test(expected = PropertyAccessException.class) @@ -390,38 +525,24 @@ public class PropertyConfigurationTest { new Config(props); } + @Test(expected = PropertyAccessException.class) + public void testCheckSetter_Static() throws PropertyException { + props.setProperty(THE_VALUE, STRING_VALUE); + new StaticMethodConfig(props); + } + @Test public void testGetStringValue() throws PropertyException { - class Config extends PropertyConfiguration { - - @Property(name = THE_VALUE) - private String value; - - public Config(Properties props) throws PropertyException { - super(props); - } - }; - props.setProperty(THE_VALUE, STRING_VALUE); - Config cfg = new Config(props); + PlainStringConfig cfg = new PlainStringConfig(props); assertEquals(STRING_VALUE, cfg.value); } @Test public void testGetBooleanValue_NoDefault() throws PropertyException { - class Config extends PropertyConfiguration { - - @Property(name = THE_VALUE) - private Boolean value; - - public Config(Properties props) throws PropertyException { - super(props); - } - }; - props.setProperty(THE_VALUE, "true"); - Config cfg = new Config(props); + PlainBooleanConfig cfg = new PlainBooleanConfig(props); assertEquals(true, cfg.value); } @@ -436,6 +557,11 @@ public class PropertyConfigurationTest { public Config(Properties props) throws PropertyException { super(props); } + + @SuppressWarnings("unused") + public void setValue(Boolean value) { + this.value = value; + } }; props.setProperty(THE_VALUE, "true"); @@ -452,6 +578,11 @@ public class PropertyConfigurationTest { public Config(Properties props) throws PropertyException { super(props); } + + @SuppressWarnings("unused") + public void setValue(Boolean value) { + this.value = value; + } }; // property not defined @@ -479,6 +610,11 @@ public class PropertyConfigurationTest { public Config(Properties props) throws PropertyException { super(props); } + + @SuppressWarnings("unused") + public void setValue(Boolean value) { + this.value = value; + } }; // property not defined @@ -506,6 +642,11 @@ public class PropertyConfigurationTest { public Config(Properties props) throws PropertyException { super(props); } + + @SuppressWarnings("unused") + public void setValue(Integer value) { + this.value = value; + } }; props.setProperty(THE_VALUE, "200"); @@ -524,6 +665,11 @@ public class PropertyConfigurationTest { public Config(Properties props) throws PropertyException { super(props); } + + @SuppressWarnings("unused") + public void setValue(Integer value) { + this.value = value; + } }; props.setProperty(THE_VALUE, "200"); @@ -540,6 +686,11 @@ public class PropertyConfigurationTest { public Config(Properties props) throws PropertyException { super(props); } + + @SuppressWarnings("unused") + public void setValue(Integer value) { + this.value = value; + } }; // property not defined @@ -562,6 +713,11 @@ public class PropertyConfigurationTest { public Config(Properties props) throws PropertyException { super(props); } + + @SuppressWarnings("unused") + public void setValue(Long value) { + this.value = value; + } }; props.setProperty(THE_VALUE, "20000"); @@ -580,6 +736,11 @@ public class PropertyConfigurationTest { public Config(Properties props) throws PropertyException { super(props); } + + @SuppressWarnings("unused") + public void setValue(Long value) { + this.value = value; + } }; props.setProperty(THE_VALUE, "20000"); @@ -596,6 +757,11 @@ public class PropertyConfigurationTest { public Config(Properties props) throws PropertyException { super(props); } + + @SuppressWarnings("unused") + public void setValue(Long value) { + this.value = value; + } }; // property not defined @@ -610,18 +776,8 @@ public class PropertyConfigurationTest { @Test public void testGetPropValue_Prop_NoDefault() throws PropertyException { - class Config extends PropertyConfiguration { - - @Property(name = THE_VALUE) - private String value; - - public Config(Properties props) throws PropertyException { - super(props); - } - }; - props.setProperty(THE_VALUE, STRING_VALUE); - Config cfg = new Config(props); + PlainStringConfig cfg = new PlainStringConfig(props); assertEquals(STRING_VALUE, cfg.value); } @@ -636,6 +792,11 @@ public class PropertyConfigurationTest { public Config(Properties props) throws PropertyException { super(props); } + + @SuppressWarnings("unused") + public void setValue(String value) { + this.value = value; + } }; props.setProperty(THE_VALUE, STRING_VALUE); @@ -654,6 +815,11 @@ public class PropertyConfigurationTest { public Config(Properties props) throws PropertyException { super(props); } + + @SuppressWarnings("unused") + public void setValue(String value) { + this.value = value; + } }; props.setProperty(THE_VALUE, ""); @@ -672,6 +838,11 @@ public class PropertyConfigurationTest { public Config(Properties props) throws PropertyException { super(props); } + + @SuppressWarnings("unused") + public void setValue(String value) { + this.value = value; + } }; Config cfg = new Config(props); @@ -689,6 +860,11 @@ public class PropertyConfigurationTest { public Config(Properties props) throws PropertyException { super(props); } + + @SuppressWarnings("unused") + public void setValue(String value) { + this.value = value; + } }; Config cfg = new Config(props); @@ -706,6 +882,11 @@ public class PropertyConfigurationTest { public Config(Properties props) throws PropertyException { super(props); } + + @SuppressWarnings("unused") + public void setValue(String value) { + this.value = value; + } }; new Config(props); @@ -721,6 +902,11 @@ public class PropertyConfigurationTest { public Config(Properties props) throws PropertyException { super(props); } + + @SuppressWarnings("unused") + public void setValue(String value) { + this.value = value; + } }; Config cfg = new Config(props); @@ -730,10 +916,7 @@ public class PropertyConfigurationTest { @Test public void testGetRawPropertyValue() throws PropertyException { - class Config extends PropertyConfiguration { - - @Property(name = THE_VALUE) - private String value; + class Config extends PlainStringConfig { public Config(Properties props) throws PropertyException { super(props); @@ -747,144 +930,64 @@ public class PropertyConfigurationTest { Config cfg = new Config(props); - assertEquals(STRING_VALUE, cfg.value); + assertEquals(STRING_VALUE, cfg.getValue()); } @Test public void testMakeBoolean_True() throws PropertyException { - class Config extends PropertyConfiguration { - - @Property(name = THE_VALUE) - private Boolean value; - - public Config(Properties props) throws PropertyException { - super(props); - } - }; - props.setProperty(THE_VALUE, "true"); - Config cfg = new Config(props); + PlainBooleanConfig cfg = new PlainBooleanConfig(props); assertEquals(true, cfg.value); } @Test public void testMakeBoolean_False() throws PropertyException { - class Config extends PropertyConfiguration { - - @Property(name = THE_VALUE) - private Boolean value; - - public Config(Properties props) throws PropertyException { - super(props); - } - }; - props.setProperty(THE_VALUE, "false"); - Config cfg = new Config(props); + PlainBooleanConfig cfg = new PlainBooleanConfig(props); assertEquals(false, cfg.value); } @Test(expected = PropertyInvalidException.class) public void testMakeBoolean_Invalid() throws PropertyException { - class Config extends PropertyConfiguration { - - @Property(name = THE_VALUE) - private Boolean value; - - public Config(Properties props) throws PropertyException { - super(props); - } - }; - props.setProperty(THE_VALUE, INVALID_VALUE); - new Config(props); + new PlainBooleanConfig(props); } @Test public void testMakeInteger_Valid() throws PropertyException { - class Config extends PropertyConfiguration { - - @Property(name = THE_VALUE) - private int value; - - public Config(Properties props) throws PropertyException { - super(props); - } - }; - props.setProperty(THE_VALUE, "300"); - Config cfg = new Config(props); + PlainPrimIntConfig cfg = new PlainPrimIntConfig(props); assertEquals(300, cfg.value); } @Test(expected = PropertyInvalidException.class) public void testMakeInteger_Invalid() throws PropertyException { - class Config extends PropertyConfiguration { - - @Property(name = THE_VALUE) - private int value; - - public Config(Properties props) throws PropertyException { - super(props); - } - }; - props.setProperty(THE_VALUE, INVALID_VALUE); - new Config(props); + new PlainPrimIntConfig(props); } @Test(expected = PropertyInvalidException.class) public void testMakeInteger_TooBig() throws PropertyException { - class Config extends PropertyConfiguration { - - @Property(name = THE_VALUE) - private int value; - - public Config(Properties props) throws PropertyException { - super(props); - } - }; - props.setProperty(THE_VALUE, String.valueOf(Integer.MAX_VALUE + 10L)); - new Config(props); + new PlainPrimIntConfig(props); } @Test public void testMakeLong_Valid() throws PropertyException { - class Config extends PropertyConfiguration { - - @Property(name = THE_VALUE) - private long value; - - public Config(Properties props) throws PropertyException { - super(props); - } - }; - props.setProperty(THE_VALUE, "30000"); - Config cfg = new Config(props); + PlainPrimLongConfig cfg = new PlainPrimLongConfig(props); assertEquals(30000L, cfg.value); } @Test(expected = PropertyInvalidException.class) public void testMakeLong_Invalid() throws PropertyException { - class Config extends PropertyConfiguration { - - @Property(name = THE_VALUE) - private long value; - - public Config(Properties props) throws PropertyException { - super(props); - } - }; - props.setProperty(THE_VALUE, INVALID_VALUE); - new Config(props); + new PlainPrimLongConfig(props); } @Test @@ -897,6 +1000,11 @@ public class PropertyConfigurationTest { public Config(Properties props) throws PropertyException { super(props); } + + @SuppressWarnings("unused") + public void setValue(long value) { + this.value = value; + } }; Config cfg = new Config(props); @@ -914,6 +1022,11 @@ public class PropertyConfigurationTest { public Config(Properties props) throws PropertyException { super(props); } + + @SuppressWarnings("unused") + public void setValue(long value) { + this.value = value; + } }; new Config(props); @@ -929,6 +1042,11 @@ public class PropertyConfigurationTest { public Config(Properties props) throws PropertyException { super(props); } + + @SuppressWarnings("unused") + public void setValue(long value) { + this.value = value; + } }; new Config(props); @@ -944,6 +1062,11 @@ public class PropertyConfigurationTest { public Config(Properties props) throws PropertyException { super(props); } + + @SuppressWarnings("unused") + public void setValue(String value) { + this.value = value; + } }; // missing property - should default to "" @@ -971,6 +1094,11 @@ public class PropertyConfigurationTest { public Config(Properties props) throws PropertyException { super(props); } + + @SuppressWarnings("unused") + public void setValue(long value) { + this.value = value; + } }; new Config(props); @@ -986,6 +1114,11 @@ public class PropertyConfigurationTest { public Config(Properties props) throws PropertyException { super(props); } + + @SuppressWarnings("unused") + public void setValue(String value) { + this.value = value; + } }; Config cfg = new Config(props); @@ -1003,23 +1136,126 @@ public class PropertyConfigurationTest { public Config(Properties props) throws PropertyException { super(props); } + + @SuppressWarnings("unused") + public void setValue(long value) { + this.value = value; + } }; new Config(props); } /** - * A config whose annotated property is "static". + * Config with a String value having no qualifiers. */ - public static class StaticConfig extends PropertyConfiguration { + public class PlainStringConfig extends PropertyConfiguration { + + @Property(name = THE_VALUE) + private String value; + + public PlainStringConfig() { + + } + + public PlainStringConfig(Properties props) throws PropertyException { + super(props); + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + }; + + /** + * Config with a Boolean value having no qualifiers. + */ + public class PlainBooleanConfig extends PropertyConfiguration { + + @Property(name = THE_VALUE) + private Boolean value; + + public PlainBooleanConfig(Properties props) throws PropertyException { + super(props); + } + + public void setValue(Boolean value) { + this.value = value; + } + }; + + /** + * Config with an int value having no qualifiers. + */ + public class PlainPrimIntConfig extends PropertyConfiguration { + + @Property(name = THE_VALUE) + private int value; + + public PlainPrimIntConfig(Properties props) throws PropertyException { + super(props); + } + + public void setValue(int value) { + this.value = value; + } + }; + + /** + * Config with a long value having no qualifiers. + */ + public class PlainPrimLongConfig extends PropertyConfiguration { + + @Property(name = THE_VALUE) + private long value; + + public PlainPrimLongConfig(Properties props) throws PropertyException { + super(props); + } + + public void setValue(long value) { + this.value = value; + } + }; + + /** + * A config whose field is "static". + */ + public static class StaticPropConfig extends PropertyConfiguration { // "static" field cannot be set @Property(name = THE_VALUE) private static String value; - public StaticConfig(Properties props) throws PropertyException { + public StaticPropConfig(Properties props) throws PropertyException { super(props); } + + public static void setValue(String value) { + StaticPropConfig.value = value; + } + }; + + /** + * A config whose method is "static". + */ + public static class StaticMethodConfig extends PropertyConfiguration { + + // "static" field cannot be set + @Property(name = THE_VALUE) + private String value; + + public StaticMethodConfig(Properties props) throws PropertyException { + super(props); + } + + public static void setValue(String value) { + + } }; /** diff --git a/utils/src/test/java/org/onap/policy/common/utils/properties/SpecPropertiesTest.java b/utils/src/test/java/org/onap/policy/common/utils/properties/SpecPropertiesTest.java new file mode 100644 index 00000000..01f096d1 --- /dev/null +++ b/utils/src/test/java/org/onap/policy/common/utils/properties/SpecPropertiesTest.java @@ -0,0 +1,224 @@ +/* + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.utils.properties; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import java.util.Properties; +import org.junit.Before; +import org.junit.Test; + +public class SpecPropertiesTest { + + /** + * Property prefix of interest. + */ + private static final String MY_PREFIX = "my.prefix"; + + /** + * Specialization, which follows the prefix. + */ + private static final String MY_SPEC = "my.spec"; + + /** + * Generalized prefix (i.e., without the spec). + */ + private static final String PREFIX_GEN = MY_PREFIX + "."; + + /** + * Specialized prefix (i.e., with the spec). + */ + private static final String PREFIX_SPEC = PREFIX_GEN + MY_SPEC + "."; + + /** + * Suffix to add to property names to generate names of properties that are not + * populated. + */ + private static final String SUFFIX = ".suffix"; + + /** + * Property name without a prefix. + */ + private static final String PROP_NO_PREFIX = "other"; + + /** + * Generalized property name (i.e., without the spec). + */ + private static final String PROP_GEN = PREFIX_GEN + "generalized"; + + // property names that include the spec + private static final String PROP_SPEC = PREFIX_SPEC + "specialized"; + private static final String PROP_UNKNOWN = PREFIX_SPEC + "unknown"; + + // property values + private static final String VAL_NO_PREFIX = "no-prefix"; + private static final String VAL_GEN = "gen"; + private static final String VAL_SPEC = "spec"; + + private static final String VAL_DEFAULT = "default value"; + + private Properties supportingProps; + private SpecProperties props; + + @Before + public void setUp() { + supportingProps = new Properties(); + + supportingProps.setProperty(PROP_NO_PREFIX, VAL_NO_PREFIX); + supportingProps.setProperty(PROP_GEN, VAL_GEN); + supportingProps.setProperty(PROP_SPEC, VAL_SPEC); + + props = new SpecProperties(MY_PREFIX, MY_SPEC); + + props.putAll(supportingProps); + } + + @Test + public void testSpecPropertiesStringString() { + + // no supporting properties + props = new SpecProperties(MY_PREFIX, MY_SPEC); + + assertEquals(PREFIX_GEN, props.getPrefix()); + assertEquals(PREFIX_SPEC, props.getSpecPrefix()); + + // everything is null + assertNull(props.getProperty(gen(PROP_NO_PREFIX))); + assertNull(props.getProperty(gen(PROP_GEN))); + assertNull(props.getProperty(gen(PROP_SPEC))); + assertNull(props.getProperty(gen(PROP_UNKNOWN))); + } + + @Test + public void testSpecPropertiesStringStringProperties() { + + // use supportingProps as default properties + props = new SpecProperties(MY_PREFIX, MY_SPEC, supportingProps); + + assertEquals(PREFIX_GEN, props.getPrefix()); + assertEquals(PREFIX_SPEC, props.getSpecPrefix()); + + assertEquals(VAL_NO_PREFIX, props.getProperty(gen(PROP_NO_PREFIX))); + assertEquals(VAL_GEN, props.getProperty(gen(PROP_GEN))); + assertEquals(VAL_SPEC, props.getProperty(gen(PROP_SPEC))); + assertNull(props.getProperty(gen(PROP_UNKNOWN))); + } + + @Test + public void testSpecPropertiesStringStringProperties_EmptyPrefix() { + supportingProps = new Properties(); + + supportingProps.setProperty(PROP_NO_PREFIX, VAL_NO_PREFIX); + supportingProps.setProperty("a.value", VAL_GEN); + supportingProps.setProperty("b.value", VAL_GEN); + supportingProps.setProperty(MY_SPEC + ".b.value", VAL_SPEC); + + // no supporting properties + props = new SpecProperties("", MY_SPEC, supportingProps); + + assertEquals(VAL_NO_PREFIX, props.getProperty(gen(PROP_NO_PREFIX))); + assertEquals(VAL_GEN, props.getProperty(gen("a.value"))); + assertEquals(VAL_SPEC, props.getProperty(MY_SPEC + ".b.value")); + assertNull(props.getProperty(gen(PROP_UNKNOWN))); + } + + @Test + public void testWithTrailingDot() { + // neither has trailing dot + assertEquals(PREFIX_GEN, props.getPrefix()); + assertEquals(PREFIX_SPEC, props.getSpecPrefix()); + + // both have trailing dot + props = new SpecProperties(PREFIX_GEN, MY_SPEC + "."); + assertEquals(PREFIX_GEN, props.getPrefix()); + assertEquals(PREFIX_SPEC, props.getSpecPrefix()); + + // first is empty + props = new SpecProperties("", MY_SPEC); + assertEquals("", props.getPrefix()); + assertEquals(MY_SPEC + ".", props.getSpecPrefix()); + + // second is empty + props = new SpecProperties(PREFIX_GEN, ""); + assertEquals(PREFIX_GEN, props.getPrefix()); + assertEquals(PREFIX_GEN, props.getSpecPrefix()); + } + + @Test + public void testGetPropertyString() { + // the key does contain the prefix + assertEquals(VAL_NO_PREFIX, props.getProperty(gen(PROP_NO_PREFIX))); + assertNull(props.getProperty(gen(PROP_NO_PREFIX + SUFFIX))); + + // specialized value exists + assertEquals(VAL_GEN, props.getProperty(gen(PROP_GEN))); + assertNull(props.getProperty(gen(PROP_GEN + SUFFIX))); + + // generalized value exists + assertEquals(VAL_SPEC, props.getProperty(gen(PROP_SPEC))); + assertNull(props.getProperty(gen(PROP_SPEC + SUFFIX))); + + // not found + assertNull(props.getProperty(gen(PROP_UNKNOWN))); + assertNull(props.getProperty(gen(PROP_UNKNOWN + SUFFIX))); + } + + @Test + public void testGetPropertyStringString() { + // the key does contain the prefix + assertEquals(VAL_NO_PREFIX, props.getProperty(gen(PROP_NO_PREFIX), VAL_DEFAULT)); + assertEquals(VAL_DEFAULT, props.getProperty(gen(PROP_NO_PREFIX + SUFFIX), VAL_DEFAULT)); + + // specialized value exists + assertEquals(VAL_GEN, props.getProperty(gen(PROP_GEN), VAL_DEFAULT)); + assertEquals(VAL_DEFAULT, props.getProperty(gen(PROP_GEN + SUFFIX), VAL_DEFAULT)); + + // generalized value exists + assertEquals(VAL_SPEC, props.getProperty(gen(PROP_SPEC), VAL_DEFAULT)); + assertEquals(VAL_DEFAULT, props.getProperty(gen(PROP_SPEC + SUFFIX), VAL_DEFAULT)); + + // not found + assertEquals(VAL_DEFAULT, props.getProperty(gen(PROP_UNKNOWN), VAL_DEFAULT)); + assertEquals(VAL_DEFAULT, props.getProperty(gen(PROP_UNKNOWN + SUFFIX), VAL_DEFAULT)); + + // can return null + assertNull(props.getProperty(gen(PROP_UNKNOWN), null)); + } + + @Test(expected = UnsupportedOperationException.class) + public void testHashCode() { + props.hashCode(); + } + + @Test(expected = UnsupportedOperationException.class) + public void testEquals() { + props.equals(props); + } + + private String gen(String propnm) { + if (propnm.startsWith(PREFIX_SPEC)) { + return PREFIX_GEN + propnm.substring(PREFIX_SPEC.length()); + } + + return propnm; + } + +} diff --git a/utils/src/test/java/org/onap/policy/common/utils/properties/SpecPropertyConfigurationTest.java b/utils/src/test/java/org/onap/policy/common/utils/properties/SpecPropertyConfigurationTest.java deleted file mode 100644 index 39c8f01a..00000000 --- a/utils/src/test/java/org/onap/policy/common/utils/properties/SpecPropertyConfigurationTest.java +++ /dev/null @@ -1,331 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP Policy Engine - Common Modules - * ================================================================================ - * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.common.utils.properties; - -import static org.junit.Assert.*; -import java.util.Properties; -import org.junit.Before; -import org.junit.Test; -import org.onap.policy.common.utils.properties.exception.PropertyException; -import org.onap.policy.common.utils.properties.exception.PropertyMissingException; -import static org.onap.policy.common.utils.properties.SpecPropertyConfiguration.*; - -/** - * - */ -public class SpecPropertyConfigurationTest { - - /** - * The specializer. - */ - private static final String SPEC = "my.name"; - - /** - * Properties used when invoking constructors. - */ - private Properties props; - - /** - * @throws java.lang.Exception - */ - @Before - public void setUp() throws Exception { - props = new Properties(); - } - - /** - * Test method for {@link org.onap.policy.common.utils.properties.SpecPropertyConfiguration#getRawPropertyValue(java.util.Properties, java.lang.String)}. - * @throws PropertyException - */ - @Test - public void testGetRawPropertyValue() throws PropertyException { - class Config extends SpecPropertyConfiguration { - - // no spec - @Property(name = "prefix.suffix") - private String noSpec; - - // no spec, other type - @Property(name = "no.spec.bool") - private boolean noSpecBool; - - // type 1, no prefix - @Property(name = "{$}.suffix") - private String type1NoPrefix; - - // type 1, no suffix - @Property(name = "prefix.{$}") - private String type1NoSuffix; - - // type 1, both prefix and suffix - @Property(name = "prefix.{$}.suffix") - private String type1Both; - - // type 1, other type - @Property(name = "an.{$}.int") - private int type1Int; - - // type 2, no prefix - @Property(name = "{abc.?.def}.suffix") - private String type2NoPrefix; - - // type 2, no suffix - @Property(name = "prefix.{abc.?.def}") - private String type2NoSuffix; - - // type 2, no spec prefix - @Property(name = "prefix.{?.def}.suffix") - private String type2NoSpecPrefix; - - // type 2, no spec suffix - @Property(name = "prefix{.abc.?}.suffix") - private String type2NoSpecSuffix; - - // type 2, all components - @Property(name = "prefix.{abc.?.def.}suffix") - private String type2Both; - - // type 2, other type - @Property(name = "a.{abc.?.def.}long") - private long type2Long; - - public Config(String specialization, Properties props) throws PropertyException { - super(specialization, props); - } - }; - - props.setProperty("prefix.suffix", "no.spec"); - props.setProperty("no.spec.bool", "true"); - props.setProperty("world.suffix", "type1.no.prefix"); - props.setProperty("prefix.world", "type1.no.suffix"); - props.setProperty("prefix.world.suffix", "type1.both"); - props.setProperty("an.world.int", "200"); - props.setProperty("abc.world.def.suffix", "type2.no.prefix"); - props.setProperty("prefix.abc.world.def", "type2.no.suffix"); - props.setProperty("prefix.world.def.suffix", "type2.no.spec.prefix"); - props.setProperty("prefix.abc.world.suffix", "type2.no.spec.suffix"); - props.setProperty("prefix.abc.world.def.suffix", "type2.both"); - props.setProperty("a.abc.world.def.long", "3000"); - - Config cfg = new Config("world", props); - - assertEquals("no.spec", cfg.noSpec); - assertEquals(true, cfg.noSpecBool); - assertEquals("type1.no.prefix", cfg.type1NoPrefix); - assertEquals("type1.no.suffix", cfg.type1NoSuffix); - assertEquals("type1.both", cfg.type1Both); - assertEquals(200, cfg.type1Int); - assertEquals("type2.no.prefix", cfg.type2NoPrefix); - assertEquals("type2.no.suffix", cfg.type2NoSuffix); - assertEquals("type2.no.spec.prefix", cfg.type2NoSpecPrefix); - assertEquals("type2.no.spec.suffix", cfg.type2NoSpecSuffix); - assertEquals("type2.both", cfg.type2Both); - assertEquals(3000L, cfg.type2Long); - } - @Test - public void testGetRawPropertyValue_Type2_Generalized() throws PropertyException { - class Config extends SpecPropertyConfiguration { - - // type 2, all components - @Property(name = "prefix.{abc.?.def.}suffix") - private String value; - - public Config(String specialization, Properties props) throws PropertyException { - super(specialization, props); - } - }; - - props.setProperty("prefix.suffix", "no.spec"); - - Config cfg = new Config("world", props); - - assertEquals("no.spec", cfg.value); - } - - /** - * Test method for {@link org.onap.policy.common.utils.properties.SpecPropertyConfiguration#getRawPropertyValue(java.util.Properties, java.lang.String)}. - * @throws PropertyException - */ - @Test(expected = PropertyMissingException.class) - public void testGetRawPropertyValue_NotFound() throws PropertyException { - class Config extends SpecPropertyConfiguration { - - @Property(name = "not.found") - private String notFound; - - public Config(String specialization, Properties props) throws PropertyException { - super(specialization, props); - } - }; - - new Config("not found", props); - } - - /** - * Test method for {@link org.onap.policy.common.utils.properties.SpecPropertyConfiguration#SpecPropertyConfiguration(java.lang.String)}. - * @throws PropertyException - */ - @Test - public void testSpecPropertyConfigurationString() throws PropertyException { - final String propnm = "string.{$}.prop"; - final String propval = "hello"; - - class Config extends SpecPropertyConfiguration { - - @Property(name = propnm) - private String value; - - public Config(String specialization) { - super(specialization); - } - }; - - props.setProperty(specialize(propnm, SPEC), propval); - - Config cfg = new Config(SPEC); - assertEquals(null, cfg.value); - - cfg.setAllFields(props); - assertEquals(propval, cfg.value); - } - - /** - * Test method for {@link org.onap.policy.common.utils.properties.SpecPropertyConfiguration#SpecPropertyConfiguration(java.lang.String, java.util.Properties)}. - * @throws PropertyException - */ - @Test - public void testSpecPropertyConfigurationStringProperties() throws PropertyException { - final String propnm = "int.{$}.prop"; - final int propval = 10; - - class Config extends SpecPropertyConfiguration { - - @Property(name = propnm) - private int value; - - public Config(String specialization, Properties props) throws PropertyException { - super(specialization, props); - } - }; - - props.setProperty(specialize(propnm, SPEC), String.valueOf(propval)); - - Config cfg = new Config(SPEC, props); - - assertEquals(propval, cfg.value); - } - - /** - * Test method for {@link org.onap.policy.common.utils.properties.SpecPropertyConfiguration#generalize(java.lang.String)}. - */ - @Test - public void testGeneralize_NoSpec() { - final String xyzPdq = "xyz.pdq"; - - // no spec - assertEquals(xyzPdq, generalize(xyzPdq)); - - // spec type 1 throws an exception - we'll test it separately - - // spec type 2 - assertEquals(xyzPdq, generalize("xyz.{xxx.?.yyy.}pdq")); - } - - /** - * Test method for {@link org.onap.policy.common.utils.properties.SpecPropertyConfiguration#generalize(java.lang.String)}. - */ - @Test(expected = IllegalArgumentException.class) - public void testGeneralize_Spec1() { - generalize("abc.{$}.def"); - } - - /** - * Test method for {@link org.onap.policy.common.utils.properties.SpecPropertyConfiguration#generalizeType2(java.lang.String, java.util.regex.Matcher)}. - */ - @Test - public void testGeneralizeType2() { - assertEquals("abc.def", generalize("abc.{xyz?pdq}def")); - - assertEquals("", generalize("{xyz?pdq}")); - } - - /** - * Test method for {@link org.onap.policy.common.utils.properties.SpecPropertyConfiguration#specialize(java.lang.String, java.lang.String)}. - */ - @Test - public void testSpecialize() { - final String spec = "get.spec"; - final String abcDef = "abc.def"; - - // no spec - assertEquals(abcDef, specialize(abcDef, spec)); - - // spec type 1 - assertEquals("abc.get.spec.def", specialize("abc.{$}.def", spec)); - - // spec type 2 - assertEquals("abc.xxx.get.spec.yyy.def", specialize("abc.{xxx.?.yyy.}def", spec)); - } - - /** - * Test method for {@link org.onap.policy.common.utils.properties.SpecPropertyConfiguration#specializeType1(java.lang.String, java.lang.String, java.util.regex.Matcher)}. - */ - @Test - public void testSpecializeType1() { - final String spec = "spec1"; - - // no prefix - assertEquals("spec1.def", specialize("{$}.def", spec)); - - // no suffix - assertEquals("abc.spec1", specialize("abc.{$}", spec)); - - // with both prefix and suffix - assertEquals("abc.spec1.def", specialize("abc.{$}.def", spec)); - } - - /** - * Test method for {@link org.onap.policy.common.utils.properties.SpecPropertyConfiguration#specializeType2(java.lang.String, java.lang.String, java.util.regex.Matcher)}. - */ - @Test - public void testSpecializeType2() { - final String spec = "spec2"; - - // no prefix - assertEquals("xxx.spec2.yyy.def", specialize("{xxx.?.yyy.}def", spec)); - - // no suffix - assertEquals("abc.xxx.spec2.yyy", specialize("abc{.xxx.?.yyy}", spec)); - - // no spec prefix - assertEquals("abc.spec2.yyy.def", specialize("abc.{?.yyy.}def", spec)); - - // no spec suffix - assertEquals("abc.xxx.spec2.def", specialize("abc.{xxx.?}.def", spec)); - - // no components - assertEquals(spec, specialize("{?}", spec)); - - // all components - assertEquals("abc.xxx.spec2.yyy.def", specialize("abc.{xxx.?.yyy.}def", spec)); - } - -} diff --git a/utils/src/test/java/org/onap/policy/common/utils/time/CurrentTimeTest.java b/utils/src/test/java/org/onap/policy/common/utils/time/CurrentTimeTest.java new file mode 100644 index 00000000..694a3d21 --- /dev/null +++ b/utils/src/test/java/org/onap/policy/common/utils/time/CurrentTimeTest.java @@ -0,0 +1,59 @@ +/* + * ============LICENSE_START======================================================= + * Common Utils + * ================================================================================ + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.utils.time; + +import static org.junit.Assert.assertTrue; +import org.junit.Test; + +public class CurrentTimeTest { + + @Test + public void testGetMillis() { + long tcur = System.currentTimeMillis(); + long tval = new CurrentTime().getMillis(); + long tval2 = new CurrentTime().getMillis(); + long tend = System.currentTimeMillis(); + + assertTrue(tval >= tcur && tval <= tend); + assertTrue(tval2 >= tcur && tval2 <= tend); + } + + @Test + public void testGetDate() { + long tcur = System.currentTimeMillis(); + long tval = new CurrentTime().getDate().getTime(); + long tval2 = new CurrentTime().getDate().getTime(); + long tend = System.currentTimeMillis(); + + assertTrue(tval >= tcur && tval <= tend); + assertTrue(tval2 >= tcur && tval2 <= tend); + } + + @Test + public void testSleep() throws Exception { + long tcur = System.currentTimeMillis(); + new CurrentTime().sleep(10); + long tend = System.currentTimeMillis(); + + assertTrue(tend >= tcur + 10 - 1); + } + +} |