From 134ca5667da901a703ecb0c78d96873712a9d0d3 Mon Sep 17 00:00:00 2001 From: xuegao Date: Mon, 22 Mar 2021 11:22:47 +0100 Subject: Improve test coverage Add unit tests to improve test coverage. Issue-ID: SDC-3428 Change-Id: I9e00af2824366ae45d47d2dcecf322fd3e9a6fea Signed-off-by: xuegao --- .../model/tosca/converters/MapConverterTest.java | 43 ++++++++---------- .../model/tosca/validators/KeyValidatorTest.java | 40 +++++++++++++++++ .../model/tosca/validators/ListValidatorTest.java | 52 ++++++++++++++++++++++ 3 files changed, 111 insertions(+), 24 deletions(-) create mode 100644 catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/validators/KeyValidatorTest.java create mode 100644 catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/validators/ListValidatorTest.java (limited to 'catalog-model/src/test/java') diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/converters/MapConverterTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/converters/MapConverterTest.java index b58949d4c2..6b3b766afa 100644 --- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/converters/MapConverterTest.java +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/converters/MapConverterTest.java @@ -22,42 +22,37 @@ package org.openecomp.sdc.be.model.tosca.converters; import java.util.Map; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.openecomp.sdc.be.model.DataTypeDefinition; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; public class MapConverterTest { - private MapConverter createTestSubject() { - return new MapConverter(); - } - - @Test public void testGetInstance() throws Exception { - MapConverter result; - - // default test - result = MapConverter.getInstance(); + assertNotNull(MapConverter.getInstance()); } @Test public void testConvert() throws Exception { - MapConverter testSubject; - String value = ""; - String innerType = ""; + MapConverter testSubject = new MapConverter(); Map dataTypes = null; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.convert(value, innerType, dataTypes); + assertTrue(testSubject.convert("", null, dataTypes).isEmpty()); + assertTrue(testSubject.convert("", "string", dataTypes).isEmpty()); + assertNull(testSubject.convert("{\"key\":}", "integer", dataTypes)); + + assertEquals("{\"key\":\"value\"}", testSubject.convert("{\"key\":\"value\"}", "list", dataTypes)); + assertEquals("{\"key\":\"value\"}", testSubject.convert("{\"key\":\"value\"}", "string", dataTypes)); + assertEquals("{\"key\":2}", testSubject.convert("{\"key\":2}", "integer", dataTypes)); + assertEquals("{\"key\":null}", testSubject.convert("{\"key\":null}", "integer", dataTypes)); + assertEquals("{\"key\":0.2}", testSubject.convert("{\"key\":0.2}", "float", dataTypes)); + assertEquals("{\"key\":null}", testSubject.convert("{\"key\":null}", "float", dataTypes)); + assertEquals("{\"key\":true}", testSubject.convert("{\"key\":true}", "boolean", dataTypes)); + assertEquals("{\"key\":null}", testSubject.convert("{\"key\":null}", "boolean", dataTypes)); } - - - - - - } diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/validators/KeyValidatorTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/validators/KeyValidatorTest.java new file mode 100644 index 0000000000..1a07314b8d --- /dev/null +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/validators/KeyValidatorTest.java @@ -0,0 +1,40 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2021 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.openecomp.sdc.be.model.tosca.validators; + + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class KeyValidatorTest { + + @Test + public void isValidTest() { + assertFalse(KeyValidator.getInstance().isValid(null, "string")); + + String veryLongString = "veryverylonglonglonglonglonglongstringveryverylonglonglonglonglonglongstringveryverylonglonglonglonglonglongstring"; + assertFalse(KeyValidator.getInstance().isValid(veryLongString, "string")); + + assertTrue(KeyValidator.getInstance().isValid("test", "string")); + } +} diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/validators/ListValidatorTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/validators/ListValidatorTest.java new file mode 100644 index 0000000000..528ac7a7c9 --- /dev/null +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/validators/ListValidatorTest.java @@ -0,0 +1,52 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2021 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.openecomp.sdc.be.model.tosca.validators; + + +import org.junit.jupiter.api.Test; +import org.openecomp.sdc.be.model.DataTypeDefinition; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class ListValidatorTest { + + @Test + public void isValidTest() { + Map map = new HashMap(); + ListValidator validator = new ListValidator(); + assertTrue(validator.isValid("", "", map)); + assertFalse(validator.isValid("test", null, map)); + + assertTrue(validator.isValid("[2,3]", "integer", map)); + assertTrue(validator.isValid("[0.2]", "float", map)); + assertTrue(validator.isValid("[true]", "boolean", map)); + assertTrue(validator.isValid("[test]", "string", map)); + assertTrue(validator.isValid("[{\"key\":1};{\"key2\":2}]", "json", map)); + + assertFalse(validator.isValid("[[1,2],[3]]", "list", map)); + assertFalse(validator.isValid("[2,true]", "integer", map)); + assertFalse(validator.isValid("test", "wrong", map)); + } +} -- cgit 1.2.3-korg