diff options
Diffstat (limited to 'catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/util')
4 files changed, 1596 insertions, 0 deletions
diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/util/DataTypeValidatorTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/util/DataTypeValidatorTest.java new file mode 100644 index 0000000000..2b9c296eb5 --- /dev/null +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/util/DataTypeValidatorTest.java @@ -0,0 +1,1004 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 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.operations.impl.util; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertNotNull; + +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.Map.Entry; + +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.junit.Test; +import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition; +import org.openecomp.sdc.be.model.DataTypeDefinition; +import org.openecomp.sdc.be.model.PropertyDefinition; +import org.openecomp.sdc.be.model.tosca.ToscaPropertyType; +import org.openecomp.sdc.be.model.tosca.converters.ListConverter; +import org.openecomp.sdc.be.model.tosca.converters.MapConverter; +import org.openecomp.sdc.be.model.tosca.validators.DataTypeValidatorConverter; +import org.openecomp.sdc.be.model.tosca.validators.ListValidator; +import org.openecomp.sdc.be.model.tosca.validators.MapValidator; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.google.gson.reflect.TypeToken; + +import fj.data.Either; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class DataTypeValidatorTest { + private static Logger log = LoggerFactory.getLogger(DataTypeValidatorTest.class.getName()); + private static Gson gson = new Gson(); + + DataTypeValidatorConverter dataTypeValidator = DataTypeValidatorConverter.getInstance(); + + @Test + public void testDerivedFromPrimitiveEmptyValue() { + + Map<String, DataTypeDefinition> allDataTypes = new HashMap<String, DataTypeDefinition>(); + allDataTypes.put("integer", getPrimitiveDataType("integer")); + + DataTypeDefinition fromIntegerType = buildDerivedFromIntegerType(); + ImmutablePair<JsonElement, Boolean> validate = dataTypeValidator.validateAndUpdate("", fromIntegerType, + allDataTypes); + + assertTrue("check result is valid", validate.right.booleanValue()); + assertEquals("check value is the same as sent", null, validate.left); + + validate = dataTypeValidator.validateAndUpdate(null, fromIntegerType, allDataTypes); + + assertTrue("check result is valid", validate.right.booleanValue()); + assertEquals("check value is the same as sent", null, validate.left); + + validate = dataTypeValidator.validateAndUpdate("88", fromIntegerType, allDataTypes); + + assertTrue("check result is valid", validate.right.booleanValue()); + assertEquals("check value is the same as sent", "88", validate.left.toString()); + + } + + @Test + public void testCompositeWithParameterDerivedFromPrimitiveEmptyValue() { + + DataTypeDefinition derivedFromIntegerType = buildDerivedFromIntegerType(); + Map<String, DataTypeDefinition> allDataTypes = new HashMap<String, DataTypeDefinition>(); + allDataTypes.put("myinteger", derivedFromIntegerType); + + DataTypeDefinition personDataType = buildPersonDataType(); + + Person person = new Person("my address", 32); + String json = gson.toJson(person); + log.debug(json); + + ImmutablePair<JsonElement, Boolean> validate = dataTypeValidator.validateAndUpdate(json, personDataType, + allDataTypes); + assertTrue("check valid value", validate.right.booleanValue()); + + person = new Person("my address", 32); + json = gson.toJson(person); + json = json.replace("32", "32a"); + log.debug(json); + + validate = dataTypeValidator.validateAndUpdate(json, personDataType, allDataTypes); + assertFalse("check valid value", validate.right.booleanValue()); + + } + + @Test + public void testCompositeWithEmptyListValue() { + + DataTypeDefinition dataTypeDefinition = buildCredentialDataType(); + + String[] strArr = {}; + List<String> strList = Arrays.asList(strArr); + + // Check empty list + Credential credential = new Credential("protcol<br>>", 5, "token_type", "token", null, "user", true, strList); + City mycity = new City("myadd<br><<br>", 55); + credential.setMycity(mycity); + + String json = gson.toJson(credential); + log.debug(json); + + Map<String, DataTypeDefinition> allDataTypes = new HashMap<String, DataTypeDefinition>(); + DataTypeDefinition cityDataType = buildCityDataType(); + allDataTypes.put("city", cityDataType); + + ImmutablePair<JsonElement, Boolean> validate = dataTypeValidator.validateAndUpdate(json, dataTypeDefinition, + allDataTypes); + assertTrue("check valid value", validate.right.booleanValue()); + + Credential credentialRes = gson.fromJson(validate.left.toString(), Credential.class); + assertEquals("check empty list", 0, credentialRes.getMylist().size()); + + log.debug("Result is = {}", validate.left.toString()); + + } + + @Test + public void testCompositeWithListNullValue() { + DataTypeDefinition dataTypeDefinition = buildCredentialDataType(); + + Map<String, DataTypeDefinition> allDataTypes = new HashMap<String, DataTypeDefinition>(); + DataTypeDefinition cityDataType = buildCityDataType(); + allDataTypes.put("city", cityDataType); + + // Check list is NULL + Credential credential = new Credential("protcol<br>>", 5, "token_type", "token", null, "user", true, null); + City mycity = new City("myadd<br><<br>", 55); + credential.setMycity(mycity); + + String json = gson.toJson(credential); + + ImmutablePair<JsonElement, Boolean> validate = dataTypeValidator.validateAndUpdate(json, dataTypeDefinition, + allDataTypes); + assertTrue("check valid value", validate.right.booleanValue()); + + Credential credentialRes = gson.fromJson(validate.left.toString(), Credential.class); + assertNull("check list is null", credentialRes.getMylist()); + log.debug("Result is = {}", validate.left.toString()); + + } + + @Test + public void testCompositeWithUserNullValue() { + DataTypeDefinition dataTypeDefinition = buildCredentialDataType(); + + Map<String, DataTypeDefinition> allDataTypes = new HashMap<String, DataTypeDefinition>(); + DataTypeDefinition cityDataType = buildCityDataType(); + allDataTypes.put("city", cityDataType); + + // Check user is null + Credential credential = new Credential("protcol<br>>", 5, "token_type", "token", null, null, true, null); + City mycity = new City("myadd<br><<br>", 55); + credential.setMycity(mycity); + + String json = gson.toJson(credential); + + ImmutablePair<JsonElement, Boolean> validate = dataTypeValidator.validateAndUpdate(json, dataTypeDefinition, + allDataTypes); + assertTrue("check valid value", validate.right.booleanValue()); + + Credential credentialRes = gson.fromJson(validate.left.toString(), Credential.class); + assertNull("check list is null", credentialRes.getUser()); + log.debug("Result is = {}", validate.left.toString()); + } + + @Test + public void testCompositeWithEmptyUserValue() { + + DataTypeDefinition dataTypeDefinition = buildCredentialDataType(); + + Map<String, DataTypeDefinition> allDataTypes = new HashMap<String, DataTypeDefinition>(); + DataTypeDefinition cityDataType = buildCityDataType(); + allDataTypes.put("city", cityDataType); + // Check user is empty + Credential credential = new Credential("protcol<br>>", 5, "token_type", "token", null, "", true, null); + City mycity = new City("myadd<br><<br>", 55); + credential.setMycity(mycity); + + String json = gson.toJson(credential); + log.debug(json); + + ImmutablePair<JsonElement, Boolean> validate = dataTypeValidator.validateAndUpdate(json, dataTypeDefinition, + allDataTypes); + assertTrue("check valid value", validate.right.booleanValue()); + + Credential credentialRes = gson.fromJson(validate.left.toString(), Credential.class); + assertNotNull("check list is not null", credentialRes.getUser()); + assertEquals("check user is empty", "", credentialRes.getUser()); + log.debug("Result is = {}", validate.left.toString()); + + } + + @Test + public void testCompositeWithSumNullValue() { + DataTypeDefinition dataTypeDefinition = buildCredentialDataType(); + + Map<String, DataTypeDefinition> allDataTypes = new HashMap<String, DataTypeDefinition>(); + DataTypeDefinition cityDataType = buildCityDataType(); + allDataTypes.put("city", cityDataType); + + // Check user is null + Credential credential = new Credential("protcol<br>>", null, "token_type", "token", null, null, true, null); + City mycity = new City("myadd<br><<br>", 55); + credential.setMycity(mycity); + + String json = gson.toJson(credential); + + ImmutablePair<JsonElement, Boolean> validate = dataTypeValidator.validateAndUpdate(json, dataTypeDefinition, + allDataTypes); + assertTrue("check valid value", validate.right.booleanValue()); + + Credential credentialRes = gson.fromJson(validate.left.toString(), Credential.class); + assertNull("check list is null", credentialRes.getSum()); + log.debug("Result is = {}", validate.left.toString()); + } + + @Test + public void testInvalidJson() { + DataTypeDefinition dataTypeDefinition = buildCredentialDataType(); + + Map<String, DataTypeDefinition> allDataTypes = new HashMap<String, DataTypeDefinition>(); + DataTypeDefinition cityDataType = buildCityDataType(); + allDataTypes.put("city", cityDataType); + + // Check user is null + Credential credential = new Credential("protcol<br>>", null, "token_type", "token", null, null, true, null); + City mycity = new City("myadd<br><<br>", 55); + credential.setMycity(mycity); + + String json = gson.toJson(credential); + + json += "fdfd"; + + ImmutablePair<JsonElement, Boolean> validate = dataTypeValidator.validateAndUpdate(json, dataTypeDefinition, + allDataTypes); + assertFalse("check valid value", validate.right.booleanValue()); + + } + + @Test + public void testInvalidInnerValue() { + + DataTypeDefinition dataTypeDefinition = buildCredentialDataType(); + + Map<String, DataTypeDefinition> allDataTypes = new HashMap<String, DataTypeDefinition>(); + DataTypeDefinition cityDataType = buildCityDataType(); + allDataTypes.put("city", cityDataType); + + // Check user is null + Credential credential = new Credential("protcol<br>>", null, "token_type", "token", null, null, true, null); + City mycity = new City("myadd<br><<br>", 55); + credential.setMycity(mycity); + + String json = gson.toJson(credential); + + json = json.replace("55", "a55b"); + + ImmutablePair<JsonElement, Boolean> validate = dataTypeValidator.validateAndUpdate(json, dataTypeDefinition, + allDataTypes); + assertFalse("check valid value", validate.right.booleanValue()); + + } + + @Test + public void testInvalidInnerJson() { + + DataTypeDefinition dataTypeDefinition = buildCredentialDataType(); + + Map<String, DataTypeDefinition> allDataTypes = new HashMap<String, DataTypeDefinition>(); + DataTypeDefinition cityDataType = buildCityDataType(); + allDataTypes.put("city", cityDataType); + + // Check user is null + Credential credential = new Credential("protcol<br>>", null, "token_type", "token", null, null, true, null); + City mycity = new City("", null); + + credential.setMycity(mycity); + + String json = gson.toJson(credential); + + json = json.replace("{\"address\":\"\"}", "scalar"); + + ImmutablePair<JsonElement, Boolean> validate = dataTypeValidator.validateAndUpdate(json, dataTypeDefinition, + allDataTypes); + assertFalse("check valid value", validate.right.booleanValue()); + + } + + @Test + public void testInvalidPropertyJson() { + + DataTypeDefinition dataTypeDefinition = buildCredentialDataType(); + + Map<String, DataTypeDefinition> allDataTypes = new HashMap<String, DataTypeDefinition>(); + DataTypeDefinition cityDataType = buildCityDataType(); + allDataTypes.put("city", cityDataType); + + // Check user is null + Credential credential = new Credential("protcol<br>>", null, "token_type", "token", null, null, true, null); + City mycity = new City("myadd<br><<br>", 55); + credential.setMycity(mycity); + + String json = gson.toJson(credential); + + json = json.replace("55", "a55b"); + + ImmutablePair<JsonElement, Boolean> validate = dataTypeValidator.validateAndUpdate(json, dataTypeDefinition, + allDataTypes); + assertFalse("check valid value", validate.right.booleanValue()); + + } + + @Test + public void testCompositeDataTypeWithInternalComposite() { + + DataTypeDefinition dataTypeDefinition = buildCredentialDataType(); + + String[] strArr = { "aaa", "bbb", "c<br>dcc" }; + List<String> strList = Arrays.asList(strArr); + + Credential credential = new Credential("protcol<br>>", 5, "token_type", "token", null, "user", true, strList); + City mycity = new City("myadd<br><<br>", 55); + credential.setMycity(mycity); + + String json = gson.toJson(credential); + + Map<String, DataTypeDefinition> allDataTypes = new HashMap<String, DataTypeDefinition>(); + DataTypeDefinition cityDataType = buildCityDataType(); + allDataTypes.put("city", cityDataType); + + ImmutablePair<JsonElement, Boolean> validate = dataTypeValidator.validateAndUpdate(json, dataTypeDefinition, + allDataTypes); + assertTrue("check valid value", validate.right.booleanValue()); + + log.debug("Result is = {}", validate.left.toString()); + + } + + @Test + public void testMapValidator() { + + MapValidator validator = new MapValidator(); + Gson gson = new Gson(); + // Happy Scenarios + // 1 - Map<String,Integer> check OK + Map<String, Integer> map_1 = new HashMap<>(); + map_1.put("key1", 2); + map_1.put("key2", 3); + String value = gson.toJson(map_1); + String innerType = "integer"; + assertTrue("Test Map validation with inner integer type", validator.isValid(value, innerType, null)); + + // 2 - Map<String,Boolean> check OK + Map<String, Boolean> map_2 = new HashMap<>(); + map_2.put("key1", true); + map_2.put("key2", false); + value = gson.toJson(map_2); + innerType = "boolean"; + assertTrue("Test Map validation with inner boolean type", validator.isValid(value, innerType, null)); + + // 3 - give integer with quotes + innerType = "integer"; + value = "{\"key1\":\"5\",\"key2\":\"7\"}"; + assertTrue("Test Map validation with inner integer type, but qouted values", + validator.isValid(value, innerType, null)); + + // 4 - empty default value + innerType = "float"; + value = ""; + assertTrue("Test Map validation with inner float type", validator.isValid(value, innerType, null)); + + // Faulty Scenarios + // 5 - mismatch in data type + value = gson.toJson(map_1); + innerType = "boolean"; + assertFalse("Test Map faulty validation with inner boolean type", validator.isValid(value, innerType, null)); + // 6 - mismatch in data type + value = gson.toJson(map_2); + innerType = "integer"; + assertFalse("Test Map faulty validation with inner integer type", validator.isValid(value, innerType, null)); + + } + + @Test + public void testMapConverter() { + + MapConverter converter = new MapConverter(); + Gson gson = new Gson(); + // Happy Scenarios + Map<String, String> map_1 = new HashMap<>(); + Map<String, String> resMap_1 = new HashMap<>(); + + // 1 - check Spaces eliminated + html square brackets eliminated + map_1.put("key1", "<b>test</b>"); + map_1.put("key2", " test"); + resMap_1.put("key1", "test"); + resMap_1.put("key2", " test"); + String value = gson.toJson(map_1); + String expectedVal = gson.toJson(resMap_1); + String innerType = "string"; + assertEquals("Test Map validation with inner string type", expectedVal, + converter.convert(value, innerType, null)); + + // 2 - float converter + innerType = "float"; + value = "{\"key1\":0.4545,\"key2\":0.2f}"; + expectedVal = "{\"key1\":0.4545,\"key2\":0.2}"; + assertEquals("Test Map validation with inner float type", expectedVal, + converter.convert(value, innerType, null)); + + // 3 - check default empty value converter + innerType = "float"; + value = ""; + expectedVal = ""; + assertEquals("Test Map validation with inner float type", expectedVal, + converter.convert(value, innerType, null)); + + // 4 - invalid json + // 3 - check default empty value converter + innerType = "float"; + value = "{1345234556@#("; + expectedVal = null; + assertEquals("Test Map validation with inner float type", expectedVal, + converter.convert(value, innerType, null)); + + } + + @Test + public void testCompositeDataTypeWithMapComposite() { + + DataTypeDefinition fileDataTypeDefinition = buildFileDataType(); + Map<String, DataTypeDefinition> allDataTypes = new HashMap<String, DataTypeDefinition>(); + DataTypeDefinition cityDataType = buildCityDataType(); + allDataTypes.put("city", cityDataType); + + MyFile myFile = new MyFile(); + myFile.setAge(88); + Map<String, City> attributes = new HashMap<>(); + attributes.put("key1", new City("address1<br>", 11)); + attributes.put("key2", new City("address2<br>", 22)); + myFile.setAttributes(attributes); + + String str = gson.toJson(myFile); + log.debug(str); + + ImmutablePair<JsonElement, Boolean> convert = dataTypeValidator.validateAndUpdate(str, fileDataTypeDefinition, + allDataTypes); + + assertTrue("check map converter succeed", convert.right); + + JsonElement convertedValue = convert.left; + + log.debug("{}", convertedValue); + MyFile fromJson = gson.fromJson(convertedValue, MyFile.class); + + assertEquals("check age", 88, fromJson.getAge().intValue()); + assertEquals("check address 1", "address1", fromJson.getAttributes().get("key1").getAddress()); + assertEquals("check address 2", "address2", fromJson.getAttributes().get("key2").getAddress()); + + } + + @Test + public void testMapConverterWithComplexInnerType() { + + Map<String, DataTypeDefinition> allDataTypes = new HashMap<String, DataTypeDefinition>(); + DataTypeDefinition credentialDataTypeDefinition = buildCredentialDataType(); + DataTypeDefinition cityDataType = buildCityDataType(); + allDataTypes.put("city", cityDataType); + allDataTypes.put("credential", credentialDataTypeDefinition); + + Gson gson = new Gson(); + // Happy Scenarios + Map<String, Object> map_1 = new HashMap<>(); + + // 1 - check Spaces eliminated + html square brackets eliminated + + String[] strArr = { "aaa", "bbb", "c<br>dcc" }; + List<String> strList = Arrays.asList(strArr); + Credential credential1 = new Credential("protocol;:,.\"<br>>", 5, "token_type", "token", null, "user", true, + strList); + City mycity1 = new City("myadd<br><<br>", 55); + credential1.setMycity(mycity1); + + Credential credential2 = new Credential("protocol;:,.\"<br>>", 5, "token_type", "token", null, "user", true, + strList); + City mycity2 = new City("myadd<br><<br>", 66); + credential2.setMycity(mycity2); + + map_1.put("key1", credential1); + map_1.put("key2", credential2); + + String str = gson.toJson(map_1); + log.debug(str); + + MapConverter mapConverter = new MapConverter(); + Either<String, Boolean> convert = mapConverter.convertWithErrorResult(str, "credential", allDataTypes); + + assertTrue("check map converter succeed", convert.isLeft()); + + String convertedValue = convert.left().value(); + + Type type = new TypeToken<Map<String, Credential>>() { + }.getType(); + + Map<String, Credential> fromJson = gson.fromJson(convertedValue, type); + + Credential actualCredential1 = fromJson.get("key1"); + assertEquals("check sum", 5, actualCredential1.getSum().intValue()); + assertEquals("check protocol", "protocol;:,.\">", actualCredential1.getProtocol()); + String[] convertedStrArr = { "aaa", "bbb", "cdcc" }; + List<String> convertedStrList = Arrays.asList(convertedStrArr); + assertEquals("check list", convertedStrList, actualCredential1.getMylist()); + + assertEquals("check city address", "myadd<", actualCredential1.getMycity().getAddress()); + assertEquals("check city address", 55, actualCredential1.getMycity().getAge().intValue()); + + Credential actualCredential2 = fromJson.get("key2"); + assertEquals("check city address", 66, actualCredential2.getMycity().getAge().intValue()); + + } + + @Test + public void testListConverterWithComplexInnerType() { + + Map<String, DataTypeDefinition> allDataTypes = new HashMap<String, DataTypeDefinition>(); + DataTypeDefinition credentialDataTypeDefinition = buildCredentialDataType(); + DataTypeDefinition cityDataType = buildCityDataType(); + allDataTypes.put("city", cityDataType); + allDataTypes.put("credential", credentialDataTypeDefinition); + + Gson gson = new Gson(); + + List<Object> list = buildListOf2CredentialObjects(); + + String str = gson.toJson(list); + log.debug(str); + + ListConverter listConverter = new ListConverter(); + + Either<String, Boolean> convert = listConverter.convertWithErrorResult(str, "credential", allDataTypes); + + assertTrue("check map converter succeed", convert.isLeft()); + + String convertedValue = convert.left().value(); + + validateListOfCredential(gson, convertedValue); + + list.add(null); + + str = gson.toJson(list); + log.debug(str); + + convert = listConverter.convertWithErrorResult(str, "credential", allDataTypes); + + assertTrue("check map converter succeed", convert.isLeft()); + + validateListOfCredential(gson, convertedValue); + } + + @Test + public void testListValidatorWithComplexInnerType() { + + Map<String, DataTypeDefinition> allDataTypes = new HashMap<String, DataTypeDefinition>(); + DataTypeDefinition credentialDataTypeDefinition = buildCredentialDataType(); + DataTypeDefinition cityDataType = buildCityDataType(); + allDataTypes.put("city", cityDataType); + allDataTypes.put("credential", credentialDataTypeDefinition); + + Gson gson = new Gson(); + // Happy Scenarios + List<Object> list = buildListOf2CredentialObjects(); + + String str = gson.toJson(list); + log.debug(str); + + ListValidator listValidator = new ListValidator(); + + boolean isValid = listValidator.isValid(str, "credential", allDataTypes); + + assertTrue("check valid value", isValid); + + String badStr = str.replace("protocol", "protocol1"); + + isValid = listValidator.isValid(badStr, "credential", allDataTypes); + + assertFalse("check valid value", isValid); + + badStr = str.replace("55", "\"aa\""); + + isValid = listValidator.isValid(badStr, "credential", allDataTypes); + + assertFalse("check valid value", isValid); + + } + + private List<Object> buildListOf2CredentialObjects() { + List<Object> list = new ArrayList<>(); + + String[] strArr = { "aaa", "bbb", "c<br>dcc" }; + List<String> strList = Arrays.asList(strArr); + Credential credential1 = new Credential("protocol.,\":;<br>>", 5, "token_type", "token", null, "user", true, + strList); + City mycity1 = new City("myadd<br><<br>", 55); + credential1.setMycity(mycity1); + + Credential credential2 = new Credential("protocol.,\":;<br>>", 5, "token_type", "token", null, "user", true, + strList); + City mycity2 = new City("myadd<br><<br>", 66); + credential2.setMycity(mycity2); + + list.add(credential1); + list.add(credential2); + return list; + } + + private void validateListOfCredential(Gson gson, String convertedValue) { + + log.debug(convertedValue); + Type type = new TypeToken<List<Credential>>() { + }.getType(); + + List<Credential> fromJson = gson.fromJson(convertedValue, type); + + assertEquals("check list size", 2, fromJson.size()); + + // Credential actualCredential1 = gson.fromJson(list.get(0).toString(), + // Credential.class); + Credential actualCredential1 = fromJson.get(0); + assertEquals("check sum", 5, actualCredential1.getSum().intValue()); + assertEquals("check protocol", "protocol.,\":;>", actualCredential1.getProtocol()); + String[] convertedStrArr = { "aaa", "bbb", "cdcc" }; + List<String> convertedStrList = Arrays.asList(convertedStrArr); + assertEquals("check list", convertedStrList, actualCredential1.getMylist()); + + assertEquals("check city address", "myadd<", actualCredential1.getMycity().getAddress()); + assertEquals("check city address", 55, actualCredential1.getMycity().getAge().intValue()); + + // Credential actualCredential2 = gson.fromJson(list.get(1).toString(), + // Credential.class); + Credential actualCredential2 = fromJson.get(1); + assertEquals("check city address", 66, actualCredential2.getMycity().getAge().intValue()); + } + + private DataTypeDefinition buildCredentialDataType() { + DataTypeDefinition dataTypeDefinition = new DataTypeDefinition(); + dataTypeDefinition.setName("datatype.1"); + List<PropertyDefinition> properties = new ArrayList<>(); + PropertyDefinition propertyDefinition1 = new PropertyDefinition(); + propertyDefinition1.setName("sum"); + propertyDefinition1.setType(ToscaPropertyType.INTEGER.getType()); + PropertyDefinition propertyDefinition2 = new PropertyDefinition(); + propertyDefinition2.setName("protocol"); + propertyDefinition2.setType(ToscaPropertyType.STRING.getType()); + PropertyDefinition propertyDefinition3 = new PropertyDefinition(); + propertyDefinition3.setName("token_type"); + propertyDefinition3.setType(ToscaPropertyType.STRING.getType()); + PropertyDefinition propertyDefinition4 = new PropertyDefinition(); + propertyDefinition4.setName("token"); + propertyDefinition4.setType(ToscaPropertyType.STRING.getType()); + PropertyDefinition propertyDefinition5 = new PropertyDefinition(); + propertyDefinition5.setName("keys"); + propertyDefinition5.setType(ToscaPropertyType.MAP.getType()); + PropertyDefinition propertyDefinition6 = new PropertyDefinition(); + propertyDefinition6.setName("mylist"); + propertyDefinition6.setType(ToscaPropertyType.LIST.getType()); + SchemaDefinition entrySchema = new SchemaDefinition(); + PropertyDataDefinition property = new PropertyDataDefinition(); + property.setType("string"); + entrySchema.setProperty(property); + propertyDefinition6.setSchema(entrySchema); + PropertyDefinition propertyDefinition7 = new PropertyDefinition(); + propertyDefinition7.setName("user"); + propertyDefinition7.setType(ToscaPropertyType.STRING.getType()); + PropertyDefinition propertyDefinition8 = new PropertyDefinition(); + propertyDefinition8.setName("isMandatory"); + propertyDefinition8.setType(ToscaPropertyType.BOOLEAN.getType()); + + PropertyDefinition propertyDefinition9 = new PropertyDefinition(); + propertyDefinition9.setName("mycity"); + propertyDefinition9.setType("city"); + + properties.add(propertyDefinition1); + properties.add(propertyDefinition2); + properties.add(propertyDefinition3); + properties.add(propertyDefinition4); + properties.add(propertyDefinition5); + properties.add(propertyDefinition6); + properties.add(propertyDefinition7); + properties.add(propertyDefinition8); + properties.add(propertyDefinition9); + + dataTypeDefinition.setProperties(properties); + return dataTypeDefinition; + } + + private static DataTypeDefinition buildCityDataType() { + DataTypeDefinition cityDataType = new DataTypeDefinition(); + cityDataType.setName("city"); + List<PropertyDefinition> cityProperties = new ArrayList<>(); + PropertyDefinition cityPropertyDefinition1 = new PropertyDefinition(); + cityPropertyDefinition1.setName("age"); + cityPropertyDefinition1.setType(ToscaPropertyType.INTEGER.getType()); + PropertyDefinition cityPropertyDefinition2 = new PropertyDefinition(); + cityPropertyDefinition2.setName("address"); + cityPropertyDefinition2.setType(ToscaPropertyType.STRING.getType()); + + cityProperties.add(cityPropertyDefinition1); + cityProperties.add(cityPropertyDefinition2); + + cityDataType.setProperties(cityProperties); + return cityDataType; + } + + private static DataTypeDefinition buildPersonDataType() { + DataTypeDefinition personDataType = new DataTypeDefinition(); + personDataType.setName("person"); + List<PropertyDefinition> personProperties = new ArrayList<>(); + PropertyDefinition personPropertyDefinition1 = new PropertyDefinition(); + personPropertyDefinition1.setName("age"); + personPropertyDefinition1.setType("myinteger"); + PropertyDefinition personPropertyDefinition2 = new PropertyDefinition(); + personPropertyDefinition2.setName("address"); + personPropertyDefinition2.setType(ToscaPropertyType.STRING.getType()); + + personProperties.add(personPropertyDefinition1); + personProperties.add(personPropertyDefinition2); + + personDataType.setProperties(personProperties); + return personDataType; + } + + private static DataTypeDefinition buildFileDataType() { + DataTypeDefinition fileDataType = new DataTypeDefinition(); + fileDataType.setName("file"); + List<PropertyDefinition> fileProperties = new ArrayList<>(); + PropertyDefinition filePropertyDefinition1 = new PropertyDefinition(); + filePropertyDefinition1.setName("age"); + filePropertyDefinition1.setType("integer"); + + PropertyDefinition filePropertyDefinition2 = new PropertyDefinition(); + filePropertyDefinition2.setName("attributes"); + filePropertyDefinition2.setType(ToscaPropertyType.MAP.getType()); + + fileProperties.add(filePropertyDefinition1); + fileProperties.add(filePropertyDefinition2); + + SchemaDefinition entrySchema = new SchemaDefinition(); + PropertyDataDefinition property = new PropertyDataDefinition(); + property.setType("city"); + entrySchema.setProperty(property); + filePropertyDefinition2.setSchema(entrySchema); + + fileDataType.setProperties(fileProperties); + return fileDataType; + } + + private static DataTypeDefinition getPrimitiveDataType(String type) { + + DataTypeDefinition derivedFrom = new DataTypeDefinition(); + derivedFrom.setName(type); + + return derivedFrom; + + } + + private static DataTypeDefinition buildDerivedFromIntegerType() { + + DataTypeDefinition derivedFrom = getPrimitiveDataType("integer"); + + DataTypeDefinition myIntegerDataType = new DataTypeDefinition(); + myIntegerDataType.setDerivedFrom(derivedFrom); + + myIntegerDataType.setName("myinteger"); + + return myIntegerDataType; + } + + public static class MyFile { + + Integer age; + + Map<String, City> attributes; + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + + public Map<String, City> getAttributes() { + return attributes; + } + + public void setAttributes(Map<String, City> attributes) { + this.attributes = attributes; + } + + } + + public static class City { + + String address; + Integer age; + + public City(String address, Integer age) { + super(); + this.address = address; + this.age = age; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + + } + + public static class Person { + + String address; + Integer age; + + public Person(String address, Integer age) { + super(); + this.address = address; + this.age = age; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + + @Override + public String toString() { + return "Person [address=" + address + ", age=" + age + "]"; + } + + } + + public static class Credential { + + String protocol; + Integer sum; + String token_type; + String token; + Map<String, String> keys; + String user; + Boolean isMandatory; + List<String> mylist; + City mycity; + + public Credential(String protocol, Integer sum, String token_type, String token, Map<String, String> keys, + String user, Boolean isMandatory, List<String> mylist) { + super(); + this.protocol = protocol; + this.sum = sum; + this.token_type = token_type; + this.token = token; + this.keys = keys; + this.user = user; + this.isMandatory = isMandatory; + this.mylist = mylist; + } + + public String getProtocol() { + return protocol; + } + + public void setProtocol(String protocol) { + this.protocol = protocol; + } + + public String getToken_type() { + return token_type; + } + + public void setToken_type(String token_type) { + this.token_type = token_type; + } + + public String getToken() { + return token; + } + + public void setToken(String token) { + this.token = token; + } + + public Map<String, String> getKeys() { + return keys; + } + + public void setKeys(Map<String, String> keys) { + this.keys = keys; + } + + public String getUser() { + return user; + } + + public void setUser(String user) { + this.user = user; + } + + public Boolean getIsMandatory() { + return isMandatory; + } + + public void setIsMandatory(Boolean isMandatory) { + this.isMandatory = isMandatory; + } + + public Integer getSum() { + return sum; + } + + public void setSum(Integer sum) { + this.sum = sum; + } + + public List<String> getMylist() { + return mylist; + } + + public void setMylist(List<String> mylist) { + this.mylist = mylist; + } + + public City getMycity() { + return mycity; + } + + public void setMycity(City mycity) { + this.mycity = mycity; + } + + @Override + public String toString() { + return "Credential [protocol=" + protocol + ", token_type=" + token_type + ", token=" + token + ", keys=" + + keys + ", user=" + user + ", isMandatory=" + isMandatory + "]"; + } + + } + +} diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/util/OperationTestsUtil.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/util/OperationTestsUtil.java new file mode 100644 index 0000000000..e356b49a10 --- /dev/null +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/util/OperationTestsUtil.java @@ -0,0 +1,95 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 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.operations.impl.util; + +import org.openecomp.sdc.be.dao.neo4j.GraphEdgeLabels; +import org.openecomp.sdc.be.dao.titan.TitanGenericDao; +import org.openecomp.sdc.be.dao.titan.TitanOperationStatus; +import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; +import org.openecomp.sdc.be.model.User; +import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder; +import org.openecomp.sdc.be.resources.data.ResourceCategoryData; +import org.openecomp.sdc.be.resources.data.ServiceCategoryData; +import org.openecomp.sdc.be.resources.data.UserData; +import org.openecomp.sdc.be.resources.data.category.CategoryData; +import org.openecomp.sdc.be.resources.data.category.SubCategoryData; +import org.openecomp.sdc.common.util.ValidationUtils; + +import fj.data.Either; + +public class OperationTestsUtil { + + public static String deleteAndCreateServiceCategory(String category, TitanGenericDao titanDao) { + CategoryData categoryData = new CategoryData(NodeTypeEnum.ServiceNewCategory); + categoryData.getCategoryDataDefinition().setName(category); + categoryData.getCategoryDataDefinition() + .setNormalizedName(ValidationUtils.normalizeCategoryName4Uniqueness(category)); + categoryData.getCategoryDataDefinition().setUniqueId(UniqueIdBuilder.buildCategoryUid( + ValidationUtils.normalizeCategoryName4Uniqueness(category), NodeTypeEnum.ServiceNewCategory)); + titanDao.deleteNode(categoryData, CategoryData.class); + Either<CategoryData, TitanOperationStatus> createNode = titanDao.createNode(categoryData, CategoryData.class); + return (String) createNode.left().value().getUniqueId(); + } + + public static String deleteAndCreateResourceCategory(String category, String subcategory, + TitanGenericDao titanDao) { + + CategoryData categoryData = new CategoryData(NodeTypeEnum.ResourceNewCategory); + categoryData.getCategoryDataDefinition().setName(category); + categoryData.getCategoryDataDefinition() + .setNormalizedName(ValidationUtils.normalizeCategoryName4Uniqueness(category)); + categoryData.getCategoryDataDefinition().setUniqueId(UniqueIdBuilder.buildCategoryUid( + ValidationUtils.normalizeCategoryName4Uniqueness(category), NodeTypeEnum.ResourceNewCategory)); + + SubCategoryData subcategoryData = new SubCategoryData(NodeTypeEnum.ResourceSubcategory); + subcategoryData.getSubCategoryDataDefinition().setName(subcategory); + subcategoryData.getSubCategoryDataDefinition() + .setNormalizedName(ValidationUtils.normalizeCategoryName4Uniqueness(subcategory)); + subcategoryData.getSubCategoryDataDefinition().setUniqueId(UniqueIdBuilder + .buildSubCategoryUid(categoryData.getCategoryDataDefinition().getUniqueId(), subcategory)); + titanDao.deleteNode(categoryData, CategoryData.class); + titanDao.deleteNode(subcategoryData, SubCategoryData.class); + Either<CategoryData, TitanOperationStatus> createNode = titanDao.createNode(categoryData, CategoryData.class); + titanDao.createNode(subcategoryData, SubCategoryData.class); + titanDao.createRelation(categoryData, subcategoryData, GraphEdgeLabels.SUB_CATEGORY, null); + return (String) createNode.left().value().getUniqueId(); + } + + public static void deleteServiceCategory(String category, TitanGenericDao titanDao) { + ServiceCategoryData categoryData = new ServiceCategoryData(category); + titanDao.deleteNode(categoryData, ServiceCategoryData.class); + } + + public static void deleteResourceCategory(String category, String subcategory, TitanGenericDao titanDao) { + ResourceCategoryData categoryData = new ResourceCategoryData(category, subcategory); + titanDao.deleteNode(categoryData, ResourceCategoryData.class); + } + + public static User convertUserDataToUser(UserData modifierData) { + User modifier = new User(); + modifier.setUserId(modifierData.getUserId()); + modifier.setEmail(modifierData.getEmail()); + modifier.setFirstName(modifierData.getFirstName()); + modifier.setLastName(modifierData.getLastName()); + modifier.setRole(modifierData.getRole()); + return modifier; + } +} diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/util/PrintGraph.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/util/PrintGraph.java new file mode 100644 index 0000000000..b58ce5598d --- /dev/null +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/util/PrintGraph.java @@ -0,0 +1,461 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 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.operations.impl.util; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import org.apache.tinkerpop.gremlin.structure.Direction; +import org.apache.tinkerpop.gremlin.structure.Edge; +import org.apache.tinkerpop.gremlin.structure.Element; +import org.apache.tinkerpop.gremlin.structure.Property; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.apache.tinkerpop.gremlin.structure.util.ElementHelper; +import org.openecomp.sdc.be.dao.neo4j.GraphEdgeLabels; +import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary; +import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; + +import com.thinkaurelius.titan.core.TitanEdge; +import com.thinkaurelius.titan.core.TitanGraph; +import com.thinkaurelius.titan.core.TitanVertex; +//import com.tinkerpop.blueprints.Direction; +//import com.tinkerpop.blueprints.Edge; +//import com.tinkerpop.blueprints.Vertex; +//import com.tinkerpop.blueprints.util.ElementHelper; + +public class PrintGraph { + + public void printGraphVertices(TitanGraph graph) { + + Iterable<TitanVertex> vertices = graph.query().vertices(); + + if (vertices != null) { + Iterator<TitanVertex> iterator = vertices.iterator(); + while (iterator.hasNext()) { + Vertex vertex = iterator.next(); + + // System.out.println(vertex); + // System.out.println(ElementHelper.getProperties(vertex)); + // System.out.println("======================================="); + } + + } + // graph.commit(); + graph.tx().commit(); + } + + public void printGraphEdges(TitanGraph graph) { + + Iterable<TitanEdge> vertices = graph.query().edges(); + + if (vertices != null) { + Iterator<TitanEdge> iterator = vertices.iterator(); + while (iterator.hasNext()) { + Edge edge = iterator.next(); + + // System.out.println(edge); + // System.out.println("edge=" + edge.getLabel() + ", + // properties="+ ElementHelper.getProperties(edge)); + // System.out.println("edge=" + edge.label() + ", properties="+ + // getProperties(edge)); + // System.out.println("======================================="); + } + + } + // graph.commit(); + graph.tx().commit(); + } + + public String buildGraphForWebgraphWiz(TitanGraph graph) { + + StringBuilder builder = new StringBuilder(); + builder.append("digraph finite_state_machine {\n"); + builder.append("rankdir=LR;\n"); + builder.append("size=\"15,10\" \n"); + // node [shape = doublecircle]; LR_0 LR_3 LR_4 LR_8; + // node [shape = circle]; + + Iterable<TitanVertex> vertices = graph.query().vertices(); + + if (vertices != null) { + Iterator<TitanVertex> iterator = vertices.iterator(); + while (iterator.hasNext()) { + Vertex vertex = iterator.next(); + + // System.out.println(vertex); + // System.out.println(ElementHelper.getProperties(vertex)); + // System.out.println(getProperties(vertex)); + // System.out.println("======================================="); + + Map<String, Object> properties = getProperties(vertex); + + String nodeLabel = (String) properties.get(GraphPropertiesDictionary.LABEL.getProperty()); + + String color = getColorByNodeType(nodeLabel); + + String uid = getNodeIdByLabel(nodeLabel, properties); + + // System.out.println("uid=" + uid); + + String nodeRecord = buildNodeRecord(uid, color, properties); + + // System.out.println(nodeRecord); + + builder.append(nodeRecord); + + // if (nodeLabel.equals(NodeTypeEnum.Category)) { + // + // String + // + // } else (nodeLabel.equals(NodeTypeEnum.User)) { + // + // } + + } + + } + + Iterable<TitanEdge> edges = graph.query().edges(); + + if (edges != null) { + Iterator<TitanEdge> iterator = edges.iterator(); + while (iterator.hasNext()) { + Edge edge = iterator.next(); + + // Vertex vertexFrom = edge.getVertex(Direction.OUT); + // Vertex vertexTo = edge.getVertex(Direction.IN); + Vertex vertexFrom = edge.outVertex(); + Vertex vertexTo = edge.inVertex(); + + // String fromUid = + // getNodeIdByLabel((String)vertexFrom.getProperty(GraphPropertiesDictionary.LABEL.getProperty()), + // ElementHelper.getProperties(vertexFrom)); + // String toUid = + // getNodeIdByLabel((String)vertexTo.getProperty(GraphPropertiesDictionary.LABEL.getProperty()), + // ElementHelper.getProperties(vertexTo)); + String fromUid = getNodeIdByLabel(vertexFrom.value(GraphPropertiesDictionary.LABEL.getProperty()), + getProperties(vertexFrom)); + String toUid = getNodeIdByLabel(vertexTo.value(GraphPropertiesDictionary.LABEL.getProperty()), + getProperties(vertexTo)); + + // String edgeLabel = edge.getLabel(); + String edgeLabel = edge.label(); + + // String edgeRecord = buildEdgeRecord(fromUid, toUid, + // edgeLabel, ElementHelper.getProperties(edge)); + String edgeRecord = buildEdgeRecord(fromUid, toUid, edgeLabel, getProperties(edge)); + + builder.append(edgeRecord); + + // System.out.println(edge); + // System.out.println("edge=" + edge.getLabel() + ", + // properties=" + // + ElementHelper.getProperties(edge)); + // System.out.println("edge=" + edge.label() + ", properties=" + // + getProperties(edge)); + // System.out.println("======================================="); + } + + } + + builder.append(" } "); + + return builder.toString(); + + } + + // LR_0 [ style = "bold" color = "red" shape = "Mrecord" label = + // "hello\nworld | { name | apache } | { version | 1.0 } | { uid | + // apache.1.0 } | { state| CERTIFIED } |{ b |{c|<here> d|e}| f}| g | h" + // ] + + // LR_0 -> LR_2 [ label = "SS(B)" ]; + // LR_0 -> LR_1 [ label = "SS(S)" ]; + // LR_1 -> LR_3 [ label = "S($end)" ]; + // LR_2 -> LR_6 [ label = "SS(b)" ]; + // LR_2 -> LR_5 [ label = "SS(a)" ]; + // LR_2 -> LR_4 [ label = "S(A)" ]; + // LR_5 -> LR_7 [ label = "S(b)" ]; + // LR_5 -> LR_5 [ label = "S(a)" ]; + // LR_6 -> LR_6 [ label = "S(b)" ]; + // LR_6 -> LR_5 [ label = "S(a)" ]; + // LR_7 -> LR_8 [ label = "S(b)" ]; + // LR_7 -> LR_5 [ label = "S(a)" ]; + // LR_8 -> LR_6 [ label = "S(b)" ]; + // LR_8 -> LR_5 [ label = "S(a)" ]; + + private String buildEdgeRecord(String fromUid, String toUid, String edgeLabel, Map<String, Object> properties) { + + StringBuilder builder = new StringBuilder(); + // LR_0 -> LR_2 [ label = "SS(B)" ]; + + String generatedProps = generateStringFromProperties(properties); + + String color = getEdgeColorByLabel(edgeLabel); + + builder.append("\"" + fromUid + "\"" + " -> " + "\"" + toUid + "\"" + " [ color = " + color + " label = \"" + + edgeLabel + "(" + generatedProps + ")\"" + " ] " + "\n"); + + return builder.toString(); + } + + private String getEdgeColorByLabel(String edgeLabel) { + + GraphEdgeLabels edgeLabelEnum = GraphEdgeLabels.getByName(edgeLabel); + + String color = "black"; + + switch (edgeLabelEnum) { + case PROPERTY: + color = "orange"; + break; + case CAPABILITY: + break; + case DERIVED_FROM: + color = "red"; + default: + break; + } + + return color; + } + + private String generateStringFromProperties(Map<String, Object> properties) { + + StringBuilder builder = new StringBuilder(); + + if (properties != null) { + for (Entry<String, Object> entry : properties.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue().toString(); + builder.append(key + "=" + value + "__"); + } + } + return builder.toString(); + + } + + private String buildNodeRecord(String uid, String color, Map<String, Object> properties) { + + StringBuilder builder = new StringBuilder(); + + builder.append("\"" + uid + "\"" + " [ "); + builder.append("style = \"bold\" "); + builder.append(" color = \"" + color + "\""); + builder.append("shape = \"Mrecord\" "); + + String label = ""; + int maxKeyLength = 0; + for (Entry<String, Object> entry1 : properties.entrySet()) { + String key = entry1.getKey(); + int keyLength = key.length(); + if (keyLength > maxKeyLength) { + maxKeyLength = keyLength; + } + } + + boolean first = true; + for (Entry<String, Object> entry : properties.entrySet()) { + + String key = entry.getKey(); + String value = entry.getValue().toString(); + + if (key.equals(GraphPropertiesDictionary.CONSTRAINTS.getProperty())) { + value = value.replaceAll("[^\\w\\s]", "_"); + } + + key = padKey(key, maxKeyLength); + + if (first) { + first = false; + } else { + label += " | "; + } + label += " { " + key + " | " + value + " } "; + } + + builder.append("label = \"" + label + "\" "); + builder.append(" ] "); + + // LR_0 [ style = "bold" color = "red" shape = "Mrecord" label = + // "hello\nworld | { name | apache } | { version | 1.0 } | { uid | + // apache.1.0 } | { state| CERTIFIED } |{ b |{c|<here> d|e}| f}| g | h" + // ] + + builder.append(" \n "); + return builder.toString(); + } + + private String getNodeIdByLabel(String nodeLabel, Map<String, Object> properties) { + + NodeTypeEnum typeEnum = NodeTypeEnum.getByName(nodeLabel); + + String uid = null; + switch (typeEnum) { + + case User: + uid = (String) properties.get(GraphPropertiesDictionary.USER_ID.getProperty()); + break; + case ServiceCategory: + case ResourceCategory: + case Tag: + uid = (String) properties.get(GraphPropertiesDictionary.NAME.getProperty()); + break; + + default: + uid = (String) properties.get(GraphPropertiesDictionary.UNIQUE_ID.getProperty()); + break; + } + + return uid; + } + + private String getColorByNodeType(String nodeLabel) { + + NodeTypeEnum typeEnum = NodeTypeEnum.getByName(nodeLabel); + + String color = "red"; + switch (typeEnum) { + case ServiceCategory: + color = "blue"; + break; + case ResourceCategory: + color = "blue"; + break; + case Resource: + color = "forestgreen"; + break; + case User: + color = "green"; + break; + case Capability: + color = "lightgreen"; + break; + case CapabilityType: + color = "gray"; + break; + case Property: + color = "cyan"; + break; + case RelationshipType: + color = "darkorchid"; + break; + case Requirement: + color = "gold"; + break; + case RequirementImpl: + // color = "forestgreen"; + color = "gold"; + break; + case Service: + color = "cyan4"; + break; + case Tag: + color = "dimgrey"; + break; + default: + break; + + } + + return color; + } + + private String padKey(String key, int maxKeyLength) { + + int len = key.length(); + for (int i = len; i < maxKeyLength; i++) { + key += " "; + } + + return key; + } + + public int getNumberOfVertices(TitanGraph graph) { + int counter = 0; + Iterable<TitanVertex> vertices = graph.query().vertices(); + + if (vertices != null) { + Iterator<TitanVertex> iterator = vertices.iterator(); + while (iterator.hasNext()) { + Vertex vertex = iterator.next(); + counter++; + } + } + return counter; + } + + public Set<String> getVerticesSet(TitanGraph titanGraph) { + + Set<String> set = new HashSet<String>(); + + Iterable<TitanVertex> vertices = titanGraph.query().vertices(); + + if (vertices != null) { + Iterator<TitanVertex> iterator = vertices.iterator(); + while (iterator.hasNext()) { + Vertex vertex = iterator.next(); + + // System.out.println(vertex); + // System.out.println(ElementHelper.getProperties(vertex)); + // System.out.println(getProperties(vertex)); + // System.out.println("======================================="); + + // Map<String, Object> properties = + // ElementHelper.getProperties(vertex); + Map<String, Object> properties = getProperties(vertex); + + String nodeLabel = (String) properties.get(GraphPropertiesDictionary.LABEL.getProperty()); + + String uid = getNodeIdByLabel(nodeLabel, properties); + + set.add(uid); + } + } + + return set; + + } + + public Map<String, Object> getProperties(Element element) { + + Map<String, Object> result = null; + + if (element.keys() != null && element.keys().size() > 0) { + Map<String, Property> propertyMap = ElementHelper.propertyMap(element, + element.keys().toArray(new String[element.keys().size()])); + result = new HashMap<String, Object>(); + + for (Entry<String, Property> entry : propertyMap.entrySet()) { + String key = entry.getKey(); + Object value = entry.getValue().value(); + + result.put(key, value); + } + } + return result; + } + +} diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/util/ResourceCreationUtils.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/util/ResourceCreationUtils.java new file mode 100644 index 0000000000..b6b951bdc4 --- /dev/null +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/util/ResourceCreationUtils.java @@ -0,0 +1,36 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 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.operations.impl.util; + +public class ResourceCreationUtils { + + public static String ATT_UID = "jm007a"; + public static String FIRST_NAME = "Julia"; + public static String LAST_NAME = "Hendrix"; + public static String DEFAULT_RESOURCE_NAME = "my-resource"; + public static String DEFAULT_RESOURCE_VERSION = "1.0"; + public static String DEFAULT_USER_ID = "jh0003"; + + public static String MODIFIER_ATT_UID = "jk9990"; + public static String MODIFIER_FIRST_NAME = "Roki"; + public static String MODIFIER_LAST_NAME = "Balaboa"; + +} |