+ /**
+ * Validate whether ApiDelcaration contains Apis with concrete path and whether this Apis contain specified POST
+ * operations.
+ */
+ private void validateSwaggerApisForPost(final ApiDeclaration doc) {
+ // two POST URI with concrete schema name in summary
+ Api lstApi = findApi("/config/toaster2:lst/", doc);
+ assertNotNull("Api /config/toaster2:lst/ wasn't found", lstApi);
+ assertTrue("POST for cont1 in lst is missing",
+ findOperation(lstApi.getOperations(), "POST", "(config)lstPOST", "(config)lst1", "(config)cont1"));
+
+ Api cont1Api = findApi("/config/toaster2:lst/cont1/", doc);
+ assertNotNull("Api /config/toaster2:lst/cont1/ wasn't found", cont1Api);
+ assertTrue("POST for cont11 in cont1 is missing",
+ findOperation(cont1Api.getOperations(), "POST", "(config)cont1POST", "(config)cont11", "(config)lst11"));
+
+ // no POST URI
+ Api cont11Api = findApi("/config/toaster2:lst/cont1/cont11/", doc);
+ assertNotNull("Api /config/toaster2:lst/cont1/cont11/ wasn't found", cont11Api);
+ assertTrue("POST operation shouldn't be present.", findOperations(cont11Api.getOperations(), "POST").isEmpty());
+
+ }
+
+ /**
+ * Tries to find operation with name {@code operationName} and with summary {@code summary}
+ */
+ private boolean findOperation(List<Operation> operations, String operationName, String type,
+ String... searchedParameters) {
+ Set<Operation> filteredOperations = findOperations(operations, operationName);
+ for (Operation operation : filteredOperations) {
+ if (operation.getType().equals(type)) {
+ List<Parameter> parameters = operation.getParameters();
+ return containAllParameters(parameters, searchedParameters);
+ }
+ }
+ return false;
+ }
+
+ private Set<Operation> findOperations(final List<Operation> operations, final String operationName) {
+ final Set<Operation> filteredOperations = new HashSet<>();
+ for (Operation operation : operations) {
+ if (operation.getMethod().equals(operationName)) {
+ filteredOperations.add(operation);
+ }
+ }
+ return filteredOperations;
+ }
+
+ private boolean containAllParameters(final List<Parameter> searchedIns, String[] searchedWhats) {
+ for (String searchedWhat : searchedWhats) {
+ boolean parameterFound = false;
+ for (Parameter searchedIn : searchedIns) {
+ if (searchedIn.getType().equals(searchedWhat)) {
+ parameterFound = true;
+ }
+ }
+ if (!parameterFound) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * Tries to find {@code Api} with path {@code path}
+ */
+ private Api findApi(final String path, final ApiDeclaration doc) {
+ for (Api api : doc.getApis()) {
+ if (api.getPath().equals(path)) {
+ return api;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Validates whether doc {@code doc} contains concrete specified models.
+ */
+ private void validateSwaggerModules(ApiDeclaration doc) {
+ JSONObject models = doc.getModels();
+ assertNotNull(models);
+ try {
+ JSONObject configLst = models.getJSONObject("(config)lst");
+ assertNotNull(configLst);
+
+ containsReferences(configLst, "lst1");
+ containsReferences(configLst, "cont1");
+
+ JSONObject configLst1 = models.getJSONObject("(config)lst1");
+ assertNotNull(configLst1);
+
+ JSONObject configCont1 = models.getJSONObject("(config)cont1");
+ assertNotNull(configCont1);
+
+ containsReferences(configCont1, "cont11");
+ containsReferences(configCont1, "lst11");
+
+ JSONObject configCont11 = models.getJSONObject("(config)cont11");
+ assertNotNull(configCont11);
+
+ JSONObject configLst11 = models.getJSONObject("(config)lst11");
+ assertNotNull(configLst11);
+ } catch (JSONException e) {
+ fail("JSONException wasn't expected");
+ }
+
+ }
+
+ /**
+ * Checks whether object {@code mainObject} contains in properties/items key $ref with concrete value.
+ */
+ private void containsReferences(final JSONObject mainObject, final String childObject) throws JSONException {
+ JSONObject properties = mainObject.getJSONObject("properties");
+ assertNotNull(properties);
+
+ JSONObject nodeInProperties = properties.getJSONObject(childObject);
+ assertNotNull(nodeInProperties);
+
+ JSONObject itemsInNodeInProperties = nodeInProperties.getJSONObject("items");
+ assertNotNull(itemsInNodeInProperties);
+
+ String itemRef = itemsInNodeInProperties.getString("$ref");
+ assertEquals("(config)" + childObject, itemRef);
+ }
+