aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/CapabilityTypeOperationTest.java
blob: 79af87bc10e4d9999d219ea7ad73f8a70954032a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*-
 * ============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;

import fj.data.Either;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openecomp.sdc.be.dao.titan.TitanGenericDao;
import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
import org.openecomp.sdc.be.model.CapabilityTypeDefinition;
import org.openecomp.sdc.be.model.ModelTestBase;
import org.openecomp.sdc.be.model.PropertyConstraint;
import org.openecomp.sdc.be.model.PropertyDefinition;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
import org.openecomp.sdc.be.model.tosca.ToscaType;
import org.openecomp.sdc.be.model.tosca.constraints.GreaterThanConstraint;
import org.openecomp.sdc.be.model.tosca.constraints.InRangeConstraint;
import org.openecomp.sdc.be.model.tosca.constraints.LessOrEqualConstraint;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:application-context-test.xml")
public class CapabilityTypeOperationTest extends ModelTestBase {

	@Resource(name = "titan-generic-dao")
	private TitanGenericDao titanDao;

	@Resource(name = "capability-type-operation")
	private CapabilityTypeOperation capabilityTypeOperation;

	@BeforeClass
	public static void setupBeforeClass() {
		ModelTestBase.init();

	}

	@Test
	public void testDummy() {

		assertTrue(capabilityTypeOperation != null);

	}

	@Test
	public void testAddCapabilityType() {

		CapabilityTypeDefinition capabilityTypeDefinition = new CapabilityTypeDefinition();
		capabilityTypeDefinition.setDescription("desc1");
		capabilityTypeDefinition.setType("tosca.capabilities.Container1");

		Either<CapabilityTypeDefinition, StorageOperationStatus> addCapabilityType1 = capabilityTypeOperation.addCapabilityType(capabilityTypeDefinition, true);
		assertEquals("check capability type added", true, addCapabilityType1.isLeft());

		CapabilityTypeDefinition capabilityTypeAdded = addCapabilityType1.left().value();
		compareBetweenCreatedToSent(capabilityTypeDefinition, capabilityTypeAdded);

		Either<CapabilityTypeDefinition, TitanOperationStatus> capabilityTypeByUid = capabilityTypeOperation.getCapabilityTypeByUid(capabilityTypeAdded.getUniqueId());
		compareBetweenCreatedToSent(capabilityTypeByUid.left().value(), capabilityTypeDefinition);

		Either<CapabilityTypeDefinition, StorageOperationStatus> addCapabilityType2 = capabilityTypeOperation.addCapabilityType(capabilityTypeDefinition, true);
		assertEquals("check capability type failed", true, addCapabilityType2.isRight());
		assertEquals("check returned error", StorageOperationStatus.SCHEMA_VIOLATION, addCapabilityType2.right().value());

	}

	@Test
	public void testAddDerviedCapabilityType() {

		CapabilityTypeDefinition capabilityTypeDefinition = new CapabilityTypeDefinition();
		capabilityTypeDefinition.setDescription("desc1");
		capabilityTypeDefinition.setType("tosca.capabilities.Container2");
		capabilityTypeDefinition.setDerivedFrom("derivedFrom");

		Either<CapabilityTypeDefinition, StorageOperationStatus> addCapabilityType1 = capabilityTypeOperation.addCapabilityType(capabilityTypeDefinition, true);
		assertEquals("check capability type parent not exist", StorageOperationStatus.INVALID_ID, addCapabilityType1.right().value());
	}

	public CapabilityTypeDefinition createCapability(String capabilityTypeName) {

		CapabilityTypeDefinition capabilityTypeDefinition = new CapabilityTypeDefinition();
		capabilityTypeDefinition.setDescription("desc1");
		capabilityTypeDefinition.setType(capabilityTypeName);

		Map<String, PropertyDefinition> properties = new HashMap<String, PropertyDefinition>();

		String propName1 = "disk_size";
		String propName2 = "num_cpus";

		PropertyDefinition property1 = buildProperty1();

		properties.put(propName1, property1);

		PropertyDefinition property2 = buildProperty2();

		properties.put(propName2, property2);

		capabilityTypeDefinition.setProperties(properties);

		Either<CapabilityTypeDefinition, StorageOperationStatus> addCapabilityType1 = capabilityTypeOperation.addCapabilityType(capabilityTypeDefinition, true);

		CapabilityTypeDefinition capabilityTypeDefinitionCreated = addCapabilityType1.left().value();
		Either<CapabilityTypeDefinition, StorageOperationStatus> capabilityType = capabilityTypeOperation.getCapabilityType(capabilityTypeDefinitionCreated.getUniqueId(), true);
		assertEquals("check capability type fetched", true, capabilityType.isLeft());
		CapabilityTypeDefinition fetchedCTD = capabilityType.left().value();

		Map<String, PropertyDefinition> fetchedProps = fetchedCTD.getProperties();

		compareProperties(fetchedProps, properties);

		return fetchedCTD;

	}

	@Test
	public void testAddCapabilityTypeWithProperties() {

		CapabilityTypeDefinition capabilityTypeDefinition = new CapabilityTypeDefinition();
		capabilityTypeDefinition.setDescription("desc1");
		capabilityTypeDefinition.setType("tosca.capabilities.Container3");

		Map<String, PropertyDefinition> properties = new HashMap<String, PropertyDefinition>();

		String propName1 = "disk_size";
		String propName2 = "num_cpus";

		PropertyDefinition property1 = buildProperty1();

		properties.put(propName1, property1);

		PropertyDefinition property2 = buildProperty2();

		properties.put(propName2, property2);

		capabilityTypeDefinition.setProperties(properties);

		Either<CapabilityTypeDefinition, StorageOperationStatus> addCapabilityType1 = capabilityTypeOperation.addCapabilityType(capabilityTypeDefinition, true);

		CapabilityTypeDefinition capabilityTypeDefinitionCreated = addCapabilityType1.left().value();
		Either<CapabilityTypeDefinition, StorageOperationStatus> capabilityType = capabilityTypeOperation.getCapabilityType(capabilityTypeDefinitionCreated.getUniqueId());
		assertEquals("check capability type fetched", true, capabilityType.isLeft());
		CapabilityTypeDefinition fetchedCTD = capabilityType.left().value();

		Map<String, PropertyDefinition> fetchedProps = fetchedCTD.getProperties();

		compareProperties(fetchedProps, properties);
	}

	private void compareProperties(Map<String, PropertyDefinition> first, Map<String, PropertyDefinition> second) {

		assertTrue("check properties are full or empty", ((first == null && second == null) || (first != null && second != null)));
		if (first != null) {
			assertEquals("check properties size", first.size(), second.size());

			for (Entry<String, PropertyDefinition> entry : first.entrySet()) {

				String propName = entry.getKey();
				PropertyDefinition secondPD = second.get(propName);
				assertNotNull("Cannot find property " + propName + " in " + second, secondPD);

				PropertyDefinition firstPD = entry.getValue();

				comparePropertyDefinition(firstPD, secondPD);
			}

		}

	}

	@Test
	public void testGetCapabilityTypeNotFound() {

		Either<CapabilityTypeDefinition, StorageOperationStatus> capabilityType = capabilityTypeOperation.getCapabilityType("not_exists");
		assertEquals("check not found is returned", StorageOperationStatus.NOT_FOUND, capabilityType.right().value());

	}

	private void comparePropertyDefinition(PropertyDefinition first, PropertyDefinition second) {

		assertTrue("check objects are full or empty", ((first == null && second == null) || (first != null && second != null)));
		if (first != null) {
			assertTrue("check property default value", compareValue(first.getDefaultValue(), second.getDefaultValue()));
			assertTrue("check property description", compareValue(first.getDescription(), second.getDescription()));
			assertTrue("check property type", compareValue(first.getType(), second.getType()));
			compareList(first.getConstraints(), second.getConstraints());
		}

	}

	private void compareList(List<PropertyConstraint> first, List<PropertyConstraint> second) {

		assertTrue("check lists are full or empty", ((first == null && second == null) || (first != null && second != null)));
		if (first != null) {
			assertEquals("check list size", first.size(), second.size());
		}
	}

	private PropertyDefinition buildProperty2() {
		PropertyDefinition property2 = new PropertyDefinition();
		property2.setDefaultValue("2");
		property2.setDescription("Number of (actual or virtual) CPUs associated with the Compute node.");
		property2.setType(ToscaType.INTEGER.name().toLowerCase());
		List<PropertyConstraint> constraints3 = new ArrayList<PropertyConstraint>();
		List<String> range = new ArrayList<String>();
		range.add("1");
		range.add("4");

		InRangeConstraint propertyConstraint3 = new InRangeConstraint(range);
		constraints3.add(propertyConstraint3);
		// property2.setConstraints(constraints3);
		property2.setConstraints(constraints3);
		return property2;
	}

	private PropertyDefinition buildProperty1() {
		PropertyDefinition property1 = new PropertyDefinition();
		property1.setDefaultValue("10");
		property1.setDescription("Size of the local disk, in Gigabytes (GB), available to applications running on the Compute node.");
		property1.setType(ToscaType.INTEGER.name().toLowerCase());
		List<PropertyConstraint> constraints = new ArrayList<PropertyConstraint>();
		GreaterThanConstraint propertyConstraint1 = new GreaterThanConstraint("0");
		constraints.add(propertyConstraint1);

		LessOrEqualConstraint propertyConstraint2 = new LessOrEqualConstraint("10");
		constraints.add(propertyConstraint2);

		property1.setConstraints(constraints);
		return property1;
	}

	private void compareBetweenCreatedToSent(CapabilityTypeDefinition x, CapabilityTypeDefinition y) {

		assertTrue(compareValue(x.getDerivedFrom(), y.getDerivedFrom()));
		assertTrue(compareValue(x.getType(), y.getType()));
		assertTrue(compareValue(x.getDescription(), y.getDescription()));

	}

	public boolean compareValue(String first, String second) {

		if (first == null && second == null) {
			return true;
		}
		if (first != null) {
			return first.equals(second);
		} else {
			return false;
		}
	}
}