ServiceDefinitionUtil { public static java.util.Map<String,ServiceDefinition> mergeSnapshots( java.util.List<java.util.Map<String,ServiceDefinition>> snapshots) { return (mergeSnapshots(snapshots, false)); } private  ServiceDefinitionUtil(); static byte[] serializeServiceDefinition(ServiceDefinition sdef); static ServiceDefinition deserializeServiceDefinition(byte[] sdef); static java.util.Collection<ServiceDefinition> derive(ActivityUnit actUnit); static java.util.Map<String,ServiceDefinition> mergeSnapshots(
                    java.util.List<java.util.Map<String,ServiceDefinition>> snapshots); static java.util.Map<String,ServiceDefinition> mergeSnapshots(
                    java.util.List<java.util.Map<String,ServiceDefinition>> snapshots,
                    boolean retainContexts);  }
@Test public void testMergeSnapshots() { ServiceDefinition st1=new ServiceDefinition(); st1.setServiceType(SERVICE_TYPE_1); st1.getContext().add(new Context(Context.Type.Conversation, "c1")); InterfaceDefinition idef1=new InterfaceDefinition(); idef1.setInterface(INTERFACE_1); st1.getInterfaces().add(idef1); OperationDefinition op1=new OperationDefinition(); idef1.getOperations().add(op1); op1.setName(OPERATION_1); RequestResponseDefinition nrd1=new RequestResponseDefinition(); nrd1.getMetrics().setCount(10); nrd1.getMetrics().setFaults(0); nrd1.getMetrics().setAverage(1000); nrd1.getMetrics().setMin(500); nrd1.getMetrics().setMax(1500); nrd1.getMetrics().setCountChange(+5); nrd1.getMetrics().setAverageChange(+2); nrd1.getMetrics().setMinChange(-5); nrd1.getMetrics().setMaxChange(+20); op1.setRequestResponse(nrd1); RequestFaultDefinition frd1=new RequestFaultDefinition(); frd1.setFault("fault1"); frd1.getMetrics().setCount(20); frd1.getMetrics().setFaults(20); frd1.getMetrics().setAverage(2000); frd1.getMetrics().setMin(1500); frd1.getMetrics().setMax(2500); frd1.getMetrics().setCountChange(-10); frd1.getMetrics().setAverageChange(+6); frd1.getMetrics().setMinChange(0); frd1.getMetrics().setMaxChange(+10); op1.getRequestFaults().add(frd1); ServiceDefinition st2=new ServiceDefinition(); st2.setServiceType(SERVICE_TYPE_2); st2.getContext().add(new Context(Context.Type.Conversation, "c2")); InterfaceDefinition idef2=new InterfaceDefinition(); idef2.setInterface(INTERFACE_2); st2.getInterfaces().add(idef2); OperationDefinition op2=new OperationDefinition(); idef2.getOperations().add(op2); op2.setName(OPERATION_2); RequestResponseDefinition nrd2=new RequestResponseDefinition(); nrd2.getMetrics().setCount(10); nrd2.getMetrics().setFaults(0); nrd2.getMetrics().setAverage(1000); nrd2.getMetrics().setMin(500); nrd2.getMetrics().setMax(1500); nrd2.getMetrics().setCountChange(+5); nrd2.getMetrics().setAverageChange(+2); nrd2.getMetrics().setMinChange(-5); nrd2.getMetrics().setMaxChange(+20); op2.setRequestResponse(nrd1); RequestFaultDefinition frd2=new RequestFaultDefinition(); frd2.setFault("fault2"); frd2.getMetrics().setCount(20); frd2.getMetrics().setFaults(20); frd2.getMetrics().setAverage(2000); frd2.getMetrics().setMin(1500); frd2.getMetrics().setMax(2500); frd2.getMetrics().setCountChange(-10); frd2.getMetrics().setAverageChange(+6); frd2.getMetrics().setMinChange(0); frd2.getMetrics().setMaxChange(+10); op2.getRequestFaults().add(frd2); ServiceDefinition st3=new ServiceDefinition(); st3.setServiceType(SERVICE_TYPE_1); st3.getContext().add(new Context(Context.Type.Conversation, "c3")); InterfaceDefinition idef3=new InterfaceDefinition(); idef3.setInterface(INTERFACE_1); st3.getInterfaces().add(idef3); OperationDefinition op3=new OperationDefinition(); idef3.getOperations().add(op3); op3.setName(OPERATION_1); RequestResponseDefinition nrd3=new RequestResponseDefinition(); nrd3.getMetrics().setCount(5); nrd3.getMetrics().setFaults(0); nrd3.getMetrics().setAverage(500); nrd3.getMetrics().setMin(250); nrd3.getMetrics().setMax(750); nrd3.getMetrics().setCountChange(+2); nrd3.getMetrics().setAverageChange(+1); nrd3.getMetrics().setMinChange(-2); nrd3.getMetrics().setMaxChange(+10); op3.setRequestResponse(nrd3); RequestFaultDefinition frd3=new RequestFaultDefinition(); frd3.setFault("fault3"); frd3.getMetrics().setCount(20); frd3.getMetrics().setFaults(20); frd3.getMetrics().setAverage(2000); frd3.getMetrics().setMin(1500); frd3.getMetrics().setMax(2500); frd3.getMetrics().setCountChange(-10); frd3.getMetrics().setAverageChange(+6); frd3.getMetrics().setMinChange(0); frd3.getMetrics().setMaxChange(+10); op3.getRequestFaults().add(frd3); RequestFaultDefinition frd4=new RequestFaultDefinition(); frd4.setFault("fault1"); frd4.getMetrics().setCount(20); frd4.getMetrics().setFaults(20); frd4.getMetrics().setAverage(2000); frd4.getMetrics().setMin(1500); frd4.getMetrics().setMax(2500); frd4.getMetrics().setCountChange(-10); frd4.getMetrics().setAverageChange(+6); frd4.getMetrics().setMinChange(0); frd4.getMetrics().setMaxChange(+10); op3.getRequestFaults().add(frd4); java.util.Map<String,ServiceDefinition> sds1=new java.util.HashMap<String,ServiceDefinition>(); sds1.put(st1.getServiceType(), st1); sds1.put(st2.getServiceType(), st2); java.util.Map<String,ServiceDefinition> sds2=new java.util.HashMap<String,ServiceDefinition>(); sds2.put(st3.getServiceType(), st3); java.util.List<java.util.Map<String,ServiceDefinition>> list= new java.util.ArrayList<java.util.Map<String,ServiceDefinition>>(); list.add(sds1); list.add(sds2); java.util.Map<String,ServiceDefinition> merged=ServiceDefinitionUtil.mergeSnapshots(list, false); if (merged == null) { fail("No merged results"); } if (merged.size() != 2) { fail("Two service defintions expected"); } ServiceDefinition sd1=merged.get(SERVICE_TYPE_1); ServiceDefinition sd2=merged.get(SERVICE_TYPE_2); if (sd1 == null) { fail("SD1 is null"); } if (sd2 == null) { fail("SD2 is null"); } if (sd1.getContext().size() != 0) { fail("SD1 No context should be retained"); } if (sd2.getContext().size() != 0) { fail("SD2 No context should be retained"); } InterfaceDefinition idef1res=sd1.getInterface(INTERFACE_1); if (idef1res == null) { fail("Failed to get interface defn1"); } InterfaceDefinition idef2res=sd2.getInterface(INTERFACE_2); if (idef2res == null) { fail("Failed to get interface defn2"); } if (idef1res.getOperations().size() != 1) { fail("SD1 ops should be 1: "+idef1res.getOperations().size()); } if (idef2res.getOperations().size() != 1) { fail("SD2 ops should be 1: "+idef2res.getOperations().size()); } OperationDefinition opd1=idef1res.getOperations().get(0); OperationDefinition opd2=idef2res.getOperations().get(0); if (opd1.getRequestFaults().size() != 2) { fail("OP1 should have two faults: "+opd1.getRequestFaults().size()); } if (opd2.getRequestFaults().size() != 1) { fail("OP2 should have 1 fault: "+opd2.getRequestFaults().size()); } if (opd1.getRequestResponse().getMetrics().getCount() != 15) { fail("Expecting count 15: "+opd1.getRequestResponse().getMetrics().getCount()); } if (opd1.getRequestFaults().get(0).getMetrics().getFaults() != 40) { fail("Expecting faults 40: "+opd1.getRequestFaults().get(0).getMetrics().getFaults()); } }
@Test public void testMergeSnapshotsWithContext() { ServiceDefinition st1=new ServiceDefinition(); st1.setServiceType(SERVICE_TYPE_1); st1.getContext().add(new Context(Context.Type.Conversation, "c1")); InterfaceDefinition idef1=new InterfaceDefinition(); idef1.setInterface(INTERFACE_1); OperationDefinition op1=new OperationDefinition(); idef1.getOperations().add(op1); op1.setName(OPERATION_1); RequestResponseDefinition nrd1=new RequestResponseDefinition(); nrd1.getMetrics().setCount(10); nrd1.getMetrics().setAverage(1000); nrd1.getMetrics().setMin(500); nrd1.getMetrics().setMax(1500); nrd1.getMetrics().setCountChange(+5); nrd1.getMetrics().setAverageChange(+2); nrd1.getMetrics().setMinChange(-5); nrd1.getMetrics().setMaxChange(+20); op1.setRequestResponse(nrd1); RequestFaultDefinition frd1=new RequestFaultDefinition(); frd1.setFault("fault1"); frd1.getMetrics().setCount(20); frd1.getMetrics().setAverage(2000); frd1.getMetrics().setMin(1500); frd1.getMetrics().setMax(2500); frd1.getMetrics().setCountChange(-10); frd1.getMetrics().setAverageChange(+6); frd1.getMetrics().setMinChange(0); frd1.getMetrics().setMaxChange(+10); op1.getRequestFaults().add(frd1); ServiceDefinition st2=new ServiceDefinition(); st2.setServiceType(SERVICE_TYPE_1); st2.getContext().add(new Context(Context.Type.Conversation, "c2")); InterfaceDefinition idef2=new InterfaceDefinition(); idef2.setInterface(INTERFACE_1); st2.getInterfaces().add(idef2); OperationDefinition op2=new OperationDefinition(); idef2.getOperations().add(op2); op2.setName(OPERATION_2); RequestResponseDefinition nrd2=new RequestResponseDefinition(); nrd2.getMetrics().setCount(10); nrd2.getMetrics().setAverage(1000); nrd2.getMetrics().setMin(500); nrd2.getMetrics().setMax(1500); nrd2.getMetrics().setCountChange(+5); nrd2.getMetrics().setAverageChange(+2); nrd2.getMetrics().setMinChange(-5); nrd2.getMetrics().setMaxChange(+20); op2.setRequestResponse(nrd1); RequestFaultDefinition frd2=new RequestFaultDefinition(); frd2.setFault("fault2"); frd2.getMetrics().setCount(20); frd2.getMetrics().setAverage(2000); frd2.getMetrics().setMin(1500); frd2.getMetrics().setMax(2500); frd2.getMetrics().setCountChange(-10); frd2.getMetrics().setAverageChange(+6); frd2.getMetrics().setMinChange(0); frd2.getMetrics().setMaxChange(+10); op2.getRequestFaults().add(frd2); ServiceDefinition st3=new ServiceDefinition(); st3.setServiceType(SERVICE_TYPE_1); st3.getContext().add(new Context(Context.Type.Conversation, "c3")); InterfaceDefinition idef3=new InterfaceDefinition(); idef3.setInterface(INTERFACE_1); st3.getInterfaces().add(idef3); OperationDefinition op3=new OperationDefinition(); idef3.getOperations().add(op3); op3.setName(OPERATION_1); RequestResponseDefinition nrd3=new RequestResponseDefinition(); nrd3.getMetrics().setCount(5); nrd3.getMetrics().setAverage(500); nrd3.getMetrics().setMin(250); nrd3.getMetrics().setMax(750); nrd3.getMetrics().setCountChange(+2); nrd3.getMetrics().setAverageChange(+1); nrd3.getMetrics().setMinChange(-2); nrd3.getMetrics().setMaxChange(+10); op3.setRequestResponse(nrd3); RequestFaultDefinition frd3=new RequestFaultDefinition(); frd3.setFault("fault3"); frd3.getMetrics().setCount(20); frd3.getMetrics().setAverage(2000); frd3.getMetrics().setMin(1500); frd3.getMetrics().setMax(2500); frd3.getMetrics().setCountChange(-10); frd3.getMetrics().setAverageChange(+6); frd3.getMetrics().setMinChange(0); frd3.getMetrics().setMaxChange(+10); op3.getRequestFaults().add(frd3); java.util.Map<String,ServiceDefinition> sds1=new java.util.HashMap<String,ServiceDefinition>(); sds1.put(st1.getServiceType(), st1); java.util.Map<String,ServiceDefinition> sds2=new java.util.HashMap<String,ServiceDefinition>(); sds2.put(st2.getServiceType(), st2); java.util.Map<String,ServiceDefinition> sds3=new java.util.HashMap<String,ServiceDefinition>(); sds3.put(st3.getServiceType(), st3); java.util.List<java.util.Map<String,ServiceDefinition>> list= new java.util.ArrayList<java.util.Map<String,ServiceDefinition>>(); list.add(sds1); list.add(sds2); list.add(sds3); java.util.Map<String,ServiceDefinition> merged=ServiceDefinitionUtil.mergeSnapshots(list, true); if (merged == null) { fail("No merged results"); } if (merged.size() != 1) { fail("One service defintion expected"); } ServiceDefinition sd=merged.get(SERVICE_TYPE_1); if (sd == null) { fail("SD is null"); } if (sd.getContext().size() != 3) { fail("Expecting 3 context to be retained: "+sd.getContext().size()); } }