use of org.eclipse.winery.topologygraph.matching.MockNamespaceManager in project winery by eclipse.
the class ToscaComponentPatternMatcherTest method isCompatible.
@Test
public void isCompatible() {
// same type
TNodeTemplate refinementTemplate = new TNodeTemplate(new TNodeTemplate.Builder("d", new QName("type1")));
ToscaNode refinement = new ToscaNode();
refinement.addTEntityType(new TNodeType(new TNodeType.Builder("type1")));
refinement.setNodeTemplate(refinementTemplate);
TNodeTemplate candidateTemplate = new TNodeTemplate(new TNodeTemplate.Builder("c", new QName("type1")));
ToscaNode candidate = new ToscaNode();
candidate.addTEntityType(new TNodeType(new TNodeType.Builder("type1")));
candidate.setNodeTemplate(candidateTemplate);
OTPatternRefinementModel prm = new OTPatternRefinementModel(new OTPatternRefinementModel.Builder().setRefinementStructure(new TTopologyTemplate(new TTopologyTemplate.Builder().addNodeTemplate(refinementTemplate))));
// needs to be swapped manually as only prms retrieved from repo are swapped automatically
PatternDetectionUtils.swapDetectorWithRefinement(prm);
NamespaceManager namespaceManager = new MockNamespaceManager() {
@Override
public boolean isPatternNamespace(String namespace) {
return namespace.equals("patternNs");
}
};
ToscaComponentPatternMatcher matcher = new ToscaComponentPatternMatcher(prm, namespaceManager, new ArrayList<>(), new HashMap<>());
assertTrue(matcher.isCompatible(refinement, candidate));
// different type
refinementTemplate.setType(new QName("type1"));
refinement.getTypes().clear();
refinement.addTEntityType(new TNodeType(new TNodeType.Builder("type1")));
candidateTemplate.setType(new QName("type2"));
candidate.getTypes().clear();
candidate.addTEntityType(new TNodeType(new TNodeType.Builder("type2")));
assertFalse(matcher.isCompatible(refinement, candidate));
// share supertype
refinement.addTEntityType(new TNodeType(new TNodeType.Builder("super")));
candidate.addTEntityType(new TNodeType(new TNodeType.Builder("super")));
assertTrue(matcher.isCompatible(refinement, candidate));
// stay mapping exists
OTStayMapping stayMapping = new OTStayMapping(new OTStayMapping.Builder().setDetectorElement(null).setRefinementElement(refinementTemplate));
List<OTStayMapping> stayMappings = new ArrayList<>();
stayMappings.add((OTStayMapping) PatternDetectionUtils.swapDetectorWithRefinement(stayMapping));
prm.setStayMappings(stayMappings);
assertFalse(matcher.isCompatible(refinement, candidate));
assertNotNull(prm.getStayMappings());
prm.getStayMappings().clear();
assertTrue(matcher.isCompatible(refinement, candidate));
// component pattern mapping exists
List<OTPermutationMapping> componentPatternMappings = new ArrayList<>();
OTPermutationMapping componentPatternMapping = new OTPermutationMapping(new OTPermutationMapping.Builder().setDetectorElement(new TNodeTemplate.Builder("detector", QName.valueOf("{ns}type")).build()).setRefinementElement(refinementTemplate));
componentPatternMappings.add((OTPermutationMapping) PatternDetectionUtils.swapDetectorWithRefinement(componentPatternMapping));
prm.setPermutationMappings(componentPatternMappings);
assertFalse(matcher.isCompatible(refinement, candidate));
prm.getPermutationMappings().clear();
assertTrue(matcher.isCompatible(refinement, candidate));
// different behavior patterns
List<TPolicy> refinementPolicies = new ArrayList<>();
refinementPolicies.add(new TPolicy.Builder(QName.valueOf("{patternNs}type1")).build());
refinementTemplate.setPolicies(refinementPolicies);
List<TPolicy> candidatePolicies = new ArrayList<>();
candidateTemplate.setPolicies(candidatePolicies);
// detector has behavior pattern, candidate doesn't
assertFalse(matcher.isCompatible(refinement, candidate));
candidatePolicies.add(new TPolicy.Builder(QName.valueOf("{patternNs}type1")).build());
// detector and candidate have same behavior pattern
assertTrue(matcher.isCompatible(refinement, candidate));
refinementPolicies.clear();
// candidate has behavior pattern, detector doesn't
assertFalse(matcher.isCompatible(refinement, candidate));
candidatePolicies.clear();
assertTrue(matcher.isCompatible(refinement, candidate));
// detector supertype of candidate
refinementTemplate.setType(new QName("super"));
refinement.getTypes().clear();
refinement.addTEntityType(new TNodeType(new TNodeType.Builder("super")));
candidateTemplate.setType(new QName("type"));
candidate.getTypes().clear();
candidate.addTEntityType(new TNodeType(new TNodeType.Builder("type")));
candidate.addTEntityType(new TNodeType(new TNodeType.Builder("super")));
assertTrue(matcher.isCompatible(refinement, candidate));
// candidate supertype of detector
refinementTemplate.setType(new QName("type"));
refinement.getTypes().clear();
refinement.addTEntityType(new TNodeType(new TNodeType.Builder("type")));
refinement.addTEntityType(new TNodeType(new TNodeType.Builder("super")));
candidateTemplate.setType(new QName("super"));
candidate.getTypes().clear();
candidate.addTEntityType(new TNodeType(new TNodeType.Builder("super")));
assertTrue(matcher.isCompatible(refinement, candidate));
// different supertypes
refinementTemplate.setType(new QName("type1"));
refinement.getTypes().clear();
refinement.addTEntityType(new TNodeType(new TNodeType.Builder("type1")));
refinement.addTEntityType(new TNodeType(new TNodeType.Builder("super1")));
candidateTemplate.setType(new QName("type2"));
candidate.getTypes().clear();
candidate.addTEntityType(new TNodeType(new TNodeType.Builder("type2")));
candidate.addTEntityType(new TNodeType(new TNodeType.Builder("super2")));
assertFalse(matcher.isCompatible(refinement, candidate));
}
use of org.eclipse.winery.topologygraph.matching.MockNamespaceManager in project winery by eclipse.
the class ToscaPatternMatcherTest method behaviorPatternsCompatible.
@Test
public void behaviorPatternsCompatible() {
List<TPolicy> detectorPolicies = new ArrayList<>();
detectorPolicies.add(new TPolicy.Builder(QName.valueOf("{ns}type1")).build());
List<TPolicy> candidatePolicies = new ArrayList<>();
TNodeTemplate detector = new TNodeTemplate.Builder("detector", QName.valueOf("{ns}type")).setPolicies(detectorPolicies).build();
ToscaNode detectorNode = new ToscaNode();
detectorNode.setNodeTemplate(detector);
TNodeTemplate candidate = new TNodeTemplate.Builder("candidate", QName.valueOf("{ns}type")).setPolicies(candidatePolicies).build();
ToscaNode candidateNode = new ToscaNode();
candidateNode.setNodeTemplate(candidate);
NamespaceManager namespaceManager = new MockNamespaceManager() {
@Override
public boolean isPatternNamespace(String namespace) {
return namespace.equals("patternNs");
}
};
ToscaPatternMatcher matcher = new ToscaPatternMatcher(null, namespaceManager) {
};
// detector has policy, candidate doesn't
assertTrue(matcher.behaviorPatternsCompatible(detectorNode, candidateNode));
detectorPolicies.clear();
candidatePolicies.add(new TPolicy(new TPolicy.Builder(QName.valueOf("{ns}type1"))));
// candidate has policy, detector doesn't
assertTrue(matcher.behaviorPatternsCompatible(detectorNode, candidateNode));
detectorPolicies.add(new TPolicy(new TPolicy.Builder(QName.valueOf("{patternNs}type2"))));
// detector has behavior pattern, candidate doesn't
assertFalse(matcher.behaviorPatternsCompatible(detectorNode, candidateNode));
candidatePolicies.add(new TPolicy(new TPolicy.Builder(QName.valueOf("{patternNs}type2"))));
// detector and candidate have same behavior pattern
assertTrue(matcher.behaviorPatternsCompatible(detectorNode, candidateNode));
candidatePolicies.add(new TPolicy(new TPolicy.Builder(QName.valueOf("{patternNs}type3"))));
// candidate has different behavior pattern than detector
assertFalse(matcher.behaviorPatternsCompatible(detectorNode, candidateNode));
}
Aggregations