Search in sources :

Example 1 with MockNamespaceManager

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));
}
Also used : MockNamespaceManager(org.eclipse.winery.topologygraph.matching.MockNamespaceManager) NamespaceManager(org.eclipse.winery.repository.backend.NamespaceManager) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) TNodeType(org.eclipse.winery.model.tosca.TNodeType) MockNamespaceManager(org.eclipse.winery.topologygraph.matching.MockNamespaceManager) ToscaNode(org.eclipse.winery.topologygraph.model.ToscaNode) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) OTStayMapping(org.eclipse.winery.model.tosca.extensions.OTStayMapping) TPolicy(org.eclipse.winery.model.tosca.TPolicy) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) OTPatternRefinementModel(org.eclipse.winery.model.tosca.extensions.OTPatternRefinementModel) OTPermutationMapping(org.eclipse.winery.model.tosca.extensions.OTPermutationMapping) Test(org.junit.jupiter.api.Test)

Example 2 with MockNamespaceManager

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));
}
Also used : MockNamespaceManager(org.eclipse.winery.topologygraph.matching.MockNamespaceManager) NamespaceManager(org.eclipse.winery.repository.backend.NamespaceManager) MockNamespaceManager(org.eclipse.winery.topologygraph.matching.MockNamespaceManager) ToscaNode(org.eclipse.winery.topologygraph.model.ToscaNode) TPolicy(org.eclipse.winery.model.tosca.TPolicy) ArrayList(java.util.ArrayList) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) Test(org.junit.jupiter.api.Test)

Aggregations

ArrayList (java.util.ArrayList)2 TNodeTemplate (org.eclipse.winery.model.tosca.TNodeTemplate)2 TPolicy (org.eclipse.winery.model.tosca.TPolicy)2 NamespaceManager (org.eclipse.winery.repository.backend.NamespaceManager)2 MockNamespaceManager (org.eclipse.winery.topologygraph.matching.MockNamespaceManager)2 ToscaNode (org.eclipse.winery.topologygraph.model.ToscaNode)2 Test (org.junit.jupiter.api.Test)2 QName (javax.xml.namespace.QName)1 TNodeType (org.eclipse.winery.model.tosca.TNodeType)1 TTopologyTemplate (org.eclipse.winery.model.tosca.TTopologyTemplate)1 OTPatternRefinementModel (org.eclipse.winery.model.tosca.extensions.OTPatternRefinementModel)1 OTPermutationMapping (org.eclipse.winery.model.tosca.extensions.OTPermutationMapping)1 OTStayMapping (org.eclipse.winery.model.tosca.extensions.OTStayMapping)1