View Javadoc
1   /*
2    * Copyright 2019 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.bremersee.test.web;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.List;
22  import lombok.EqualsAndHashCode;
23  import org.springframework.util.StringUtils;
24  
25  /**
26   * The rest api tester path.
27   *
28   * @author Christian Bremer
29   */
30  public interface RestApiTesterPath {
31  
32    /**
33     * Creates a new path builder.
34     *
35     * @return the path builder
36     */
37    static RestApiTesterPath.Builder pathBuilder() {
38      return new BuilderImpl();
39    }
40  
41    /**
42     * To path builder.
43     *
44     * @return the path builder
45     */
46    RestApiTesterPath.Builder toPathBuilder();
47  
48    /**
49     * The path builder interface.
50     */
51    interface Builder {
52  
53      /**
54       * Add step and value.
55       *
56       * @param stepType the step type
57       * @param value the value
58       * @return the builder
59       */
60      Builder add(PathType stepType, String value);
61  
62      /**
63       * Build rest api tester path.
64       *
65       * @return the rest api tester path
66       */
67      RestApiTesterPath build();
68    }
69  
70    /**
71     * The rest api tester path implementation.
72     */
73    class Impl extends ArrayList<RestApiTesterPathValues> implements RestApiTesterPath {
74  
75      private Impl(Collection<? extends RestApiTesterPathValues> collection) {
76        super(collection);
77      }
78  
79      @Override
80      public RestApiTesterPath.Builder toPathBuilder() {
81        return new BuilderImpl(this);
82      }
83  
84      @Override
85      public String toString() {
86        return StringUtils.collectionToDelimitedString(this, " -> ");
87      }
88    }
89  
90    /**
91     * The path builder implementation.
92     */
93    class BuilderImpl implements RestApiTesterPath.Builder {
94  
95      private final List<RestApiTesterPathValues> values = new ArrayList<>();
96  
97      private BuilderImpl() {
98      }
99  
100     private BuilderImpl(Collection<? extends RestApiTesterPathValues> collection) {
101       if (collection != null) {
102         values.addAll(collection);
103       }
104     }
105 
106     @Override
107     public Builder add(PathType stepType, String value) {
108       values.add(new RestApiTesterPathValues(stepType, value));
109       return this;
110     }
111 
112     @Override
113     public RestApiTesterPath build() {
114       return new Impl(new ArrayList<>(values));
115     }
116   }
117 
118   /**
119    * The path type.
120    */
121   enum PathType {
122 
123     /**
124      * Path type Class.
125      */
126     CLASS("Class"),
127 
128     /**
129      * Path type Method.
130      */
131     METHOD("Method"),
132 
133     /**
134      * Path type Method parameter.
135      */
136     METHOD_PARAMETER("Parameter"),
137 
138     /**
139      * Path type Annotation.
140      */
141     ANNOTATION("Annotation"),
142 
143     /**
144      * Path type Attribute.
145      */
146     ATTRIBUTE("Attribute");
147 
148     private final String value;
149 
150     PathType(String value) {
151       this.value = value;
152     }
153 
154     @Override
155     public String toString() {
156       return value;
157     }
158   }
159 
160   /**
161    * The path values.
162    */
163   @EqualsAndHashCode(doNotUseGetters = true)
164   class RestApiTesterPathValues {
165 
166     private final PathType type;
167 
168     private final String value;
169 
170     /**
171      * Instantiates a new path model.
172      *
173      * @param type the type
174      * @param value the value
175      */
176     RestApiTesterPathValues(PathType type, String value) {
177       this.type = type;
178       this.value = value;
179     }
180 
181     @Override
182     public String toString() {
183       return type + " (" + value + ")";
184     }
185   }
186 }