1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany
|
---|
2 | //
|
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
---|
4 | // you may not use this file except in compliance with the License.
|
---|
5 | // You may obtain a copy of the License at
|
---|
6 | //
|
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0
|
---|
8 | //
|
---|
9 | // Unless required by applicable law or agreed to in writing, software
|
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS,
|
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
12 | // See the License for the specific language governing permissions and
|
---|
13 | // limitations under the License.
|
---|
14 |
|
---|
15 | package de.ugoe.cs.autoquest.plugin.http.eventcore;
|
---|
16 |
|
---|
17 | import java.util.Collections;
|
---|
18 | import java.util.HashMap;
|
---|
19 | import java.util.Map;
|
---|
20 | import java.util.Random;
|
---|
21 | import java.util.Set;
|
---|
22 |
|
---|
23 | import org.apache.commons.collections4.set.ListOrderedSet;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * <p>
|
---|
27 | * Handles all request bodies of equal SOAP events. Can be used to view either all request bodies sent to an operation or to randomly draw one of the bodies.
|
---|
28 | * </p>
|
---|
29 | *
|
---|
30 | * @author Steffen Herbold
|
---|
31 | */
|
---|
32 | public class EqualSOAPDataMap {
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Singleton. Handle to only instance of this class
|
---|
36 | */
|
---|
37 | public final static EqualSOAPDataMap INSTANCE = new EqualSOAPDataMap();
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Map with all soapRequestBodies for all equal {@link SimpleSOAPEventType}s
|
---|
41 | */
|
---|
42 | private final Map<SimpleSOAPEventType, ListOrderedSet<String>> soapRequestBodies = new HashMap<>();
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * random number generator for picking a random request body
|
---|
46 | */
|
---|
47 | private final Random random = new Random(1); // TODO static seed for testing
|
---|
48 |
|
---|
49 | private EqualSOAPDataMap() {
|
---|
50 | // private constructor to avoid initialization
|
---|
51 | }
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * <p>
|
---|
55 | * Adds a new body to the map.
|
---|
56 | * </p>
|
---|
57 | *
|
---|
58 | * @param simpleSOAPEventType
|
---|
59 | */
|
---|
60 | public void add(SimpleSOAPEventType simpleSOAPEventType, String soapRequestBody) {
|
---|
61 | if( soapRequestBody!=null ) {
|
---|
62 | ListOrderedSet<String> requestBodySet = soapRequestBodies.get(simpleSOAPEventType);
|
---|
63 | if( requestBodySet==null ) {
|
---|
64 | requestBodySet = new ListOrderedSet<>();
|
---|
65 | soapRequestBodies.put(simpleSOAPEventType, requestBodySet);
|
---|
66 | }
|
---|
67 | requestBodySet.add(soapRequestBody);
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * <p>
|
---|
73 | * Retrieves all bodies associated with the simpleSoapEventType; null if not found
|
---|
74 | * </p>
|
---|
75 | *
|
---|
76 | * @param simpleSoapEventType
|
---|
77 | * @return
|
---|
78 | */
|
---|
79 | public Set<String> getAll(SimpleSOAPEventType simpleSoapEventType) {
|
---|
80 | return Collections.unmodifiableSet(soapRequestBodies.get(simpleSoapEventType));
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * <p>
|
---|
85 | * Randomly draws one of the SOAP event type bodies associated with the event
|
---|
86 | * </p>
|
---|
87 | *
|
---|
88 | * @param simpleSOAPEventType
|
---|
89 | * @return
|
---|
90 | */
|
---|
91 | public String getRandom(SimpleSOAPEventType simpleSOAPEventType) {
|
---|
92 | ListOrderedSet<String> requestBodySet = soapRequestBodies.get(simpleSOAPEventType);
|
---|
93 | if( requestBodySet==null || requestBodySet.isEmpty() ) {
|
---|
94 | throw new RuntimeException("no request body known for SimpleSOAPEventType: " + simpleSOAPEventType);
|
---|
95 | }
|
---|
96 |
|
---|
97 | return requestBodySet.get(random.nextInt(requestBodySet.size()));
|
---|
98 | }
|
---|
99 | } |
---|