/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2019 Nordix Foundation.
+ * Copyright (C) 2019-2020 Nordix Foundation.
+ * Modifications Copyright (C) 2019-2020 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.
package org.onap.policy.models.tosca.simple.concepts;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
import java.util.ArrayList;
import java.util.List;
-
import org.junit.Test;
import org.onap.policy.models.base.PfConceptKey;
-import org.onap.policy.models.base.PfReferenceKey;
import org.onap.policy.models.base.PfValidationResult;
-import org.onap.policy.models.tosca.simple.concepts.JpaToscaConstraint;
-import org.onap.policy.models.tosca.simple.concepts.JpaToscaConstraintLogical.Operation;
-import org.onap.policy.models.tosca.simple.concepts.JpaToscaConstraintLogicalString;
-import org.onap.policy.models.tosca.simple.concepts.JpaToscaEntrySchema;
/**
* DAO test for ToscaEntrySchema.
*/
public class JpaToscaEntrySchemaTest {
+ private static final String A_DESCRIPTION = "A Description";
+
@Test
public void testEntrySchemaPojo() {
- assertNotNull(new JpaToscaEntrySchema());
- assertNotNull(new JpaToscaEntrySchema(new PfReferenceKey()));
- assertNotNull(new JpaToscaEntrySchema(new PfReferenceKey(), new PfConceptKey()));
- assertNotNull(new JpaToscaEntrySchema(new JpaToscaEntrySchema()));
-
- try {
- new JpaToscaEntrySchema((PfReferenceKey) null);
- fail("test should throw an exception");
- } catch (Exception exc) {
- assertEquals("key is marked @NonNull but is null", exc.getMessage());
- }
-
- try {
- new JpaToscaEntrySchema((JpaToscaEntrySchema) null);
- fail("test should throw an exception");
- } catch (Exception exc) {
- assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage());
- }
-
- PfReferenceKey esKey = new PfReferenceKey("entrySchemaParent", "0.0.1", "entrySchema");
+ assertNotNull(new JpaToscaEntrySchema(new PfConceptKey()));
+ assertNotNull(new JpaToscaEntrySchema(new JpaToscaEntrySchema(new PfConceptKey())));
+
+ assertThatThrownBy(() -> new JpaToscaEntrySchema((PfConceptKey) null))
+ .hasMessageMatching("type is marked .*on.*ull but is null");
+
+ assertThatThrownBy(() -> new JpaToscaEntrySchema((JpaToscaEntrySchema) null))
+ .hasMessageMatching("copyConcept is marked .*on.*ull but is null");
+
PfConceptKey typeKey = new PfConceptKey("type", "0.0.1");
- JpaToscaEntrySchema tes = new JpaToscaEntrySchema(esKey, typeKey);
+ JpaToscaEntrySchema tes = new JpaToscaEntrySchema(typeKey);
- tes.setDescription("A Description");
- assertEquals("A Description", tes.getDescription());
+ tes.setDescription(A_DESCRIPTION);
+ assertEquals(A_DESCRIPTION, tes.getDescription());
List<JpaToscaConstraint> constraints = new ArrayList<>();
- JpaToscaConstraintLogicalString lsc =
- new JpaToscaConstraintLogicalString(new PfReferenceKey(esKey, "sc"), Operation.EQ, "hello");
+ JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
constraints.add(lsc);
tes.setConstraints(constraints);
assertEquals(constraints, tes.getConstraints());
assertEquals(tes, tdtClone0);
assertEquals(0, tes.compareTo(tdtClone0));
- JpaToscaEntrySchema tdtClone1 = new JpaToscaEntrySchema();
- tes.copyTo(tdtClone1);
+ JpaToscaEntrySchema tdtClone1 = new JpaToscaEntrySchema(tes);
assertEquals(tes, tdtClone1);
assertEquals(0, tes.compareTo(tdtClone1));
assertEquals(-1, tes.compareTo(null));
assertEquals(0, tes.compareTo(tes));
- assertFalse(tes.compareTo(tes.getKey()) == 0);
- PfReferenceKey otherEsKey = new PfReferenceKey("entrySchemaParent", "0.0.1", "otherEntrySchema");
- JpaToscaEntrySchema otherEs = new JpaToscaEntrySchema(otherEsKey);
+ JpaToscaEntrySchema otherEs = new JpaToscaEntrySchema(typeKey);
- assertFalse(tes.compareTo(otherEs) == 0);
- otherEs.setKey(esKey);
- assertFalse(tes.compareTo(otherEs) == 0);
+ assertNotEquals(0, tes.compareTo(otherEs));
otherEs.setType(typeKey);
- assertFalse(tes.compareTo(otherEs) == 0);
- otherEs.setDescription("A Description");
- assertFalse(tes.compareTo(otherEs) == 0);
+ assertNotEquals(0, tes.compareTo(otherEs));
+ otherEs.setDescription(A_DESCRIPTION);
+ assertNotEquals(0, tes.compareTo(otherEs));
otherEs.setConstraints(constraints);
assertEquals(0, tes.compareTo(otherEs));
- try {
- tes.copyTo(null);
- fail("test should throw an exception");
- } catch (Exception exc) {
- assertEquals("target is marked @NonNull but is null", exc.getMessage());
- }
+ assertThatThrownBy(() -> tes.copyTo(null)).hasMessageMatching("target is marked .*on.*ull but is null");
- assertEquals(3, tes.getKeys().size());
- assertEquals(2, new JpaToscaEntrySchema().getKeys().size());
+ assertEquals(1, tes.getKeys().size());
+ assertEquals(1, new JpaToscaEntrySchema(typeKey).getKeys().size());
- new JpaToscaEntrySchema().clean();
+ new JpaToscaEntrySchema(typeKey).clean();
tes.clean();
assertEquals(tdtClone0, tes);
- assertFalse(new JpaToscaEntrySchema().validate(new PfValidationResult()).isValid());
+ assertTrue(new JpaToscaEntrySchema(typeKey).validate(new PfValidationResult()).isValid());
assertTrue(tes.validate(new PfValidationResult()).isValid());
tes.setType(PfConceptKey.getNullKey());
tes.setType(typeKey);
assertTrue(tes.validate(new PfValidationResult()).isValid());
- tes.setDescription("");;
+ tes.setDescription("");
+
assertFalse(tes.validate(new PfValidationResult()).isValid());
- tes.setDescription("A Description");
+ tes.setDescription(A_DESCRIPTION);
assertTrue(tes.validate(new PfValidationResult()).isValid());
tes.getConstraints().add(null);
tes.getConstraints().remove(null);
assertTrue(tes.validate(new PfValidationResult()).isValid());
- try {
- tes.validate(null);
- fail("test should throw an exception");
- } catch (Exception exc) {
- assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());
- }
+ assertThatThrownBy(() -> tes.validate(null)).hasMessageMatching("resultIn is marked .*on.*ull but is null");
}
}