From 5df49eaf6a01e26f530c46a5dd4c1e3f85e7994f Mon Sep 17 00:00:00 2001 From: Martin Lowe <martin.lowe@eclipse-foundation.org> Date: Thu, 10 Oct 2019 15:12:20 -0400 Subject: [PATCH] Code clean up Reduce duplicates by extracting NodeBase from DTOs, clean up unneeded Database field name entries. Fix some minor JS code smells. Change-Id: Ic53f2656964f22346edad031cc9c28c2900a69e2 Signed-off-by: Martin Lowe <martin.lowe@eclipse-foundation.org> --- .../config/SecretConfigSource.java | 4 +- .../marketplace/dto/Catalog.java | 75 +++++++---------- .../marketplace/dto/Category.java | 83 +------------------ .../marketplace/dto/Listing.java | 80 ++++-------------- .../marketplace/dto/Market.java | 72 ++++++---------- .../marketplace/dto/NodeBase.java | 82 ++++++++++++++++++ .../marketplace/dto/codecs/CatalogCodec.java | 8 +- .../marketplace/dto/codecs/ListingCodec.java | 4 +- .../marketplace/dto/codecs/MarketCodec.java | 4 +- .../dto/converters/CategoryConverter.java | 13 +-- .../dto/converters/TagConverter.java | 12 +-- .../marketplace/dto/filter/CatalogFilter.java | 7 +- .../marketplace/model/MongoQuery.java | 2 +- .../marketplace/model/RequestWrapper.java | 45 ++++++---- .../namespace/DatabaseFieldNames.java | 11 +-- .../namespace/UrlParameterNames.java | 5 ++ src/main/node/hammer.js | 2 +- src/main/node/index.js | 8 +- .../resource/ListingResourceTest.java | 5 -- 19 files changed, 221 insertions(+), 301 deletions(-) create mode 100644 src/main/java/org/eclipsefoundation/marketplace/dto/NodeBase.java diff --git a/src/main/java/org/eclipsefoundation/marketplace/config/SecretConfigSource.java b/src/main/java/org/eclipsefoundation/marketplace/config/SecretConfigSource.java index bcac9a4..5d74a8a 100644 --- a/src/main/java/org/eclipsefoundation/marketplace/config/SecretConfigSource.java +++ b/src/main/java/org/eclipsefoundation/marketplace/config/SecretConfigSource.java @@ -32,8 +32,6 @@ import org.slf4j.LoggerFactory; */ public class SecretConfigSource implements ConfigSource { private static final Logger LOGGER = LoggerFactory.getLogger(SecretConfigSource.class); - - private String secretPath; private Map<String, String> secrets; @@ -41,7 +39,7 @@ public class SecretConfigSource implements ConfigSource { public Map<String, String> getProperties() { if (secrets == null) { this.secrets = new HashMap<>(); - this.secretPath = System.getProperty("config.secret.path"); + String secretPath = System.getProperty("config.secret.path"); if (StringUtils.isEmpty(secretPath)) { LOGGER.error("Configuration 'config.secret.path' not set, cannot generate secret properties"); return this.secrets; diff --git a/src/main/java/org/eclipsefoundation/marketplace/dto/Catalog.java b/src/main/java/org/eclipsefoundation/marketplace/dto/Catalog.java index 3c5b496..dec87b5 100644 --- a/src/main/java/org/eclipsefoundation/marketplace/dto/Catalog.java +++ b/src/main/java/org/eclipsefoundation/marketplace/dto/Catalog.java @@ -11,16 +11,14 @@ package org.eclipsefoundation.marketplace.dto; import java.util.ArrayList; import java.util.List; +import java.util.Objects; /** * Represents a listing catalog. * * @author Martin Lowe */ -public class Catalog { - private String id; - private String title; - private String url; +public class Catalog extends NodeBase { private boolean selfContained; private boolean searchEnabled; private String icon; @@ -28,48 +26,6 @@ public class Catalog { private String dependenciesRepository; private List<Tab> tabs; - /** - * @return the id - */ - public String getId() { - return id; - } - - /** - * @param id the id to set - */ - public void setId(String id) { - this.id = id; - } - - /** - * @return the title - */ - public String getTitle() { - return title; - } - - /** - * @param title the title to set - */ - public void setTitle(String title) { - this.title = title; - } - - /** - * @return the url - */ - public String getUrl() { - return url; - } - - /** - * @param url the url to set - */ - public void setUrl(String url) { - this.url = url; - } - /** * @return the selfContained */ @@ -154,4 +110,31 @@ public class Catalog { this.tabs = new ArrayList<>(tabs); } + @Override + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result = prime * result + + Objects.hash(dependenciesRepository, description, icon, searchEnabled, selfContained, tabs); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!super.equals(obj)) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + Catalog other = (Catalog) obj; + return Objects.equals(dependenciesRepository, other.dependenciesRepository) + && Objects.equals(description, other.description) && Objects.equals(icon, other.icon) + && searchEnabled == other.searchEnabled && selfContained == other.selfContained + && Objects.equals(tabs, other.tabs); + } + } \ No newline at end of file diff --git a/src/main/java/org/eclipsefoundation/marketplace/dto/Category.java b/src/main/java/org/eclipsefoundation/marketplace/dto/Category.java index 1d9dbb4..789e255 100644 --- a/src/main/java/org/eclipsefoundation/marketplace/dto/Category.java +++ b/src/main/java/org/eclipsefoundation/marketplace/dto/Category.java @@ -9,8 +9,6 @@ */ package org.eclipsefoundation.marketplace.dto; -import java.util.Objects; - import io.quarkus.runtime.annotations.RegisterForReflection; /** @@ -20,83 +18,6 @@ import io.quarkus.runtime.annotations.RegisterForReflection; * */ @RegisterForReflection -public class Category { - private String id; - private String name; - private String url; - - /** - * @return the id - */ - public String getId() { - return id; - } - - /** - * @param id the id to set - */ - public void setId(String id) { - this.id = id; - } - - /** - * @return the name - */ - public String getName() { - return name; - } - - /** - * @param name the name to set - */ - public void setName(String name) { - this.name = name; - } - - /** - * @return the url - */ - public String getUrl() { - return url; - } - - /** - * @param url the url to set - */ - public void setUrl(String url) { - this.url = url; - } - - @Override - public int hashCode() { - return Objects.hash(id, name, url); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - Category other = (Category) obj; - return Objects.equals(id, other.id) && Objects.equals(name, other.name) && Objects.equals(url, other.url); - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("Category [id="); - builder.append(id); - builder.append(", name="); - builder.append(name); - builder.append(", url="); - builder.append(url); - builder.append("]"); - return builder.toString(); - } +public class Category extends NodeBase { + // only needs bare node } diff --git a/src/main/java/org/eclipsefoundation/marketplace/dto/Listing.java b/src/main/java/org/eclipsefoundation/marketplace/dto/Listing.java index c4819e0..a645065 100644 --- a/src/main/java/org/eclipsefoundation/marketplace/dto/Listing.java +++ b/src/main/java/org/eclipsefoundation/marketplace/dto/Listing.java @@ -27,12 +27,8 @@ import io.quarkus.runtime.annotations.RegisterForReflection; * @author Martin Lowe */ @RegisterForReflection -public class Listing { +public class Listing extends NodeBase { - private String id; - @SortableField - private String title; - private String url; private String supportUrl; private String homepageUrl; private String teaser; @@ -80,48 +76,6 @@ public class Listing { this.categories = new ArrayList<>(); } - /** - * @return the id - */ - public String getId() { - return id; - } - - /** - * @param id the id to set - */ - public void setId(String id) { - this.id = id; - } - - /** - * @return the title - */ - public String getTitle() { - return title; - } - - /** - * @param title the title to set - */ - public void setTitle(String title) { - this.title = title; - } - - /** - * @return the url - */ - public String getUrl() { - return url; - } - - /** - * @param url the url to set - */ - public void setUrl(String url) { - this.url = url; - } - /** * @return the supportUrl */ @@ -394,12 +348,15 @@ public class Listing { Objects.requireNonNull(versions); this.versions = new ArrayList<>(versions); } - + @Override public int hashCode() { - return Objects.hash(authors, body, categories, categoryIds, creationDate, favoriteCount, foundationMember, - homepageUrl, id, installsRecent, installsTotal, license, logo, organizations, status, supportUrl, tags, - teaser, title, updateDate, url, versions); + final int prime = 31; + int result = super.hashCode(); + result = prime * result + Objects.hash(authors, body, categories, categoryIds, creationDate, favoriteCount, + foundationMember, homepageUrl, installsRecent, installsTotal, license, logo, organizations, status, + supportUrl, tags, teaser, updateDate, versions); + return result; } @Override @@ -407,7 +364,7 @@ public class Listing { if (this == obj) { return true; } - if (obj == null) { + if (!super.equals(obj)) { return false; } if (getClass() != obj.getClass()) { @@ -418,21 +375,20 @@ public class Listing { && Objects.equals(categories, other.categories) && Objects.equals(categoryIds, other.categoryIds) && creationDate == other.creationDate && favoriteCount == other.favoriteCount && foundationMember == other.foundationMember && Objects.equals(homepageUrl, other.homepageUrl) - && Objects.equals(id, other.id) && installsRecent == other.installsRecent - && installsTotal == other.installsTotal && Objects.equals(license, other.license) - && Objects.equals(logo, other.logo) && Objects.equals(organizations, other.organizations) - && Objects.equals(status, other.status) && Objects.equals(supportUrl, other.supportUrl) - && Objects.equals(tags, other.tags) && Objects.equals(teaser, other.teaser) - && Objects.equals(title, other.title) && updateDate == other.updateDate - && Objects.equals(url, other.url) && Objects.equals(versions, other.versions); + && installsRecent == other.installsRecent && installsTotal == other.installsTotal + && Objects.equals(license, other.license) && Objects.equals(logo, other.logo) + && Objects.equals(organizations, other.organizations) && Objects.equals(status, other.status) + && Objects.equals(supportUrl, other.supportUrl) && Objects.equals(tags, other.tags) + && Objects.equals(teaser, other.teaser) && updateDate == other.updateDate + && Objects.equals(versions, other.versions); } @Override public String toString() { StringBuilder sb = new StringBuilder(); - sb.append(", id=").append(id); - sb.append(", title=").append(title); - sb.append(", url=").append(url); + sb.append(", id=").append(getId()); + sb.append(", name=").append(getName()); + sb.append(", url=").append(getUrl()); sb.append(", supportUrl=").append(supportUrl); sb.append(", homepageUrl=").append(homepageUrl); sb.append(", teaser=").append(teaser); diff --git a/src/main/java/org/eclipsefoundation/marketplace/dto/Market.java b/src/main/java/org/eclipsefoundation/marketplace/dto/Market.java index 2b1c2e0..b7ce51a 100644 --- a/src/main/java/org/eclipsefoundation/marketplace/dto/Market.java +++ b/src/main/java/org/eclipsefoundation/marketplace/dto/Market.java @@ -12,6 +12,7 @@ package org.eclipsefoundation.marketplace.dto; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; +import java.util.Objects; import javax.json.bind.annotation.JsonbTransient; @@ -24,13 +25,11 @@ import io.quarkus.runtime.annotations.RegisterForReflection; * @since 05/2019 */ @RegisterForReflection -public class Market { - private String id; - private String name; - private String url; +public class Market extends NodeBase { private List<String> categoryIds; private List<Category> categories; + /** * Default constructor. Creates an empty linkedlist for categories, as its * unknown how many categories the market will reference. @@ -40,48 +39,6 @@ public class Market { this.categoryIds = new LinkedList<>(); } - /** - * @return the id - */ - public String getId() { - return id; - } - - /** - * @param id the id to set - */ - public void setId(String id) { - this.id = id; - } - - /** - * @return the name - */ - public String getName() { - return name; - } - - /** - * @param name the name to set - */ - public void setName(String name) { - this.name = name; - } - - /** - * @return the url - */ - public String getUrl() { - return url; - } - - /** - * @param url the url to set - */ - public void setUrl(String url) { - this.url = url; - } - /** * @return the categories */ @@ -111,4 +68,27 @@ public class Market { public void setCategoryIds(List<String> categoryIds) { this.categoryIds = new ArrayList<>(categoryIds); } + + @Override + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result = prime * result + Objects.hash(categories, categoryIds); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!super.equals(obj)) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + Market other = (Market) obj; + return Objects.equals(categories, other.categories) && Objects.equals(categoryIds, other.categoryIds); + } } diff --git a/src/main/java/org/eclipsefoundation/marketplace/dto/NodeBase.java b/src/main/java/org/eclipsefoundation/marketplace/dto/NodeBase.java new file mode 100644 index 0000000..278f294 --- /dev/null +++ b/src/main/java/org/eclipsefoundation/marketplace/dto/NodeBase.java @@ -0,0 +1,82 @@ +/* Copyright (c) 2019 Eclipse Foundation and others. + * This program and the accompanying materials are made available + * under the terms of the Eclipse Public License 2.0 + * which is available at http://www.eclipse.org/legal/epl-v20.html, + * SPDX-License-Identifier: EPL-2.0 + */ +package org.eclipsefoundation.marketplace.dto; + +import java.util.Objects; + +/** + * Contains the basic fields for a node within Mongo + * + * @author Martin Lowe + */ +public class NodeBase { + private String id; + private String name; + private String url; + + /** + * @return the id + */ + public String getId() { + return id; + } + + /** + * @param id the id to set + */ + public void setId(String id) { + this.id = id; + } + + /** + * @return the name + */ + public String getName() { + return name; + } + + /** + * @param name the name to set + */ + public void setName(String name) { + this.name = name; + } + + /** + * @return the url + */ + public String getUrl() { + return url; + } + + /** + * @param url the url to set + */ + public void setUrl(String url) { + this.url = url; + } + + @Override + public int hashCode() { + return Objects.hash(id, name, url); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + NodeBase other = (NodeBase) obj; + return Objects.equals(id, other.id) && Objects.equals(name, other.name) && Objects.equals(url, other.url); + } +} diff --git a/src/main/java/org/eclipsefoundation/marketplace/dto/codecs/CatalogCodec.java b/src/main/java/org/eclipsefoundation/marketplace/dto/codecs/CatalogCodec.java index cbaa4dc..15626de 100644 --- a/src/main/java/org/eclipsefoundation/marketplace/dto/codecs/CatalogCodec.java +++ b/src/main/java/org/eclipsefoundation/marketplace/dto/codecs/CatalogCodec.java @@ -51,8 +51,8 @@ public class CatalogCodec implements CollectibleCodec<Catalog> { Document doc = new Document(); doc.put(DatabaseFieldNames.DOCID, value.getId()); - doc.put(DatabaseFieldNames.CATALOG_TITLE, value.getTitle()); - doc.put(DatabaseFieldNames.CATALOG_URL, value.getUrl()); + doc.put(DatabaseFieldNames.NAME, value.getName()); + doc.put(DatabaseFieldNames.URL, value.getUrl()); doc.put(DatabaseFieldNames.CATALOG_ICON, value.getIcon()); doc.put(DatabaseFieldNames.CATALOG_SELF_CONTAINED, value.isSelfContained()); doc.put(DatabaseFieldNames.CATALOG_SEARCH_ENABLED, value.isSearchEnabled()); @@ -72,8 +72,8 @@ public class CatalogCodec implements CollectibleCodec<Catalog> { Document document = documentCodec.decode(reader, decoderContext); Catalog out = new Catalog(); out.setId(document.getString(DatabaseFieldNames.DOCID)); - out.setUrl(document.getString(DatabaseFieldNames.CATALOG_URL)); - out.setTitle(document.getString(DatabaseFieldNames.CATALOG_TITLE)); + out.setUrl(document.getString(DatabaseFieldNames.URL)); + out.setName(document.getString(DatabaseFieldNames.NAME)); out.setIcon(document.getString(DatabaseFieldNames.CATALOG_ICON)); out.setSelfContained(document.getBoolean(DatabaseFieldNames.CATALOG_SELF_CONTAINED)); out.setSearchEnabled(document.getBoolean(DatabaseFieldNames.CATALOG_SEARCH_ENABLED)); diff --git a/src/main/java/org/eclipsefoundation/marketplace/dto/codecs/ListingCodec.java b/src/main/java/org/eclipsefoundation/marketplace/dto/codecs/ListingCodec.java index 993e0d0..7962ee2 100644 --- a/src/main/java/org/eclipsefoundation/marketplace/dto/codecs/ListingCodec.java +++ b/src/main/java/org/eclipsefoundation/marketplace/dto/codecs/ListingCodec.java @@ -65,7 +65,7 @@ public class ListingCodec implements CollectibleCodec<Listing> { // for each of the fields, get the value from the unencoded object and set it doc.put(DatabaseFieldNames.DOCID, value.getId()); - doc.put(DatabaseFieldNames.LISTING_TITLE, value.getTitle()); + doc.put(DatabaseFieldNames.NAME, value.getName()); doc.put(DatabaseFieldNames.URL, value.getUrl()); doc.put(DatabaseFieldNames.SUPPORT_PAGE_URL, value.getSupportUrl()); doc.put(DatabaseFieldNames.HOME_PAGE_URL, value.getHomepageUrl()); @@ -106,7 +106,7 @@ public class ListingCodec implements CollectibleCodec<Listing> { // for each field, get the value from the encoded object and set it in POJO out.setId(document.getString(DatabaseFieldNames.DOCID)); - out.setTitle(document.getString(DatabaseFieldNames.LISTING_TITLE)); + out.setName(document.getString(DatabaseFieldNames.NAME)); out.setUrl(document.getString(DatabaseFieldNames.URL)); out.setSupportUrl(document.getString(DatabaseFieldNames.SUPPORT_PAGE_URL)); out.setHomepageUrl(document.getString(DatabaseFieldNames.HOME_PAGE_URL)); diff --git a/src/main/java/org/eclipsefoundation/marketplace/dto/codecs/MarketCodec.java b/src/main/java/org/eclipsefoundation/marketplace/dto/codecs/MarketCodec.java index 52db3a9..610a036 100644 --- a/src/main/java/org/eclipsefoundation/marketplace/dto/codecs/MarketCodec.java +++ b/src/main/java/org/eclipsefoundation/marketplace/dto/codecs/MarketCodec.java @@ -47,7 +47,7 @@ public class MarketCodec implements CollectibleCodec<Market> { doc.put(DatabaseFieldNames.DOCID, value.getId()); doc.put(DatabaseFieldNames.URL, value.getUrl()); - doc.put(DatabaseFieldNames.MARKET_NAME, value.getName()); + doc.put(DatabaseFieldNames.NAME, value.getName()); doc.put(DatabaseFieldNames.CATEGORY_IDS, value.getCategoryIds()); documentCodec.encode(writer, doc, encoderContext); @@ -64,7 +64,7 @@ public class MarketCodec implements CollectibleCodec<Market> { Market out = new Market(); out.setId(document.getString(DatabaseFieldNames.DOCID)); out.setUrl(document.getString(DatabaseFieldNames.URL)); - out.setName(document.getString(DatabaseFieldNames.MARKET_NAME)); + out.setName(document.getString(DatabaseFieldNames.NAME)); return out; } diff --git a/src/main/java/org/eclipsefoundation/marketplace/dto/converters/CategoryConverter.java b/src/main/java/org/eclipsefoundation/marketplace/dto/converters/CategoryConverter.java index c307fcc..d887ae2 100644 --- a/src/main/java/org/eclipsefoundation/marketplace/dto/converters/CategoryConverter.java +++ b/src/main/java/org/eclipsefoundation/marketplace/dto/converters/CategoryConverter.java @@ -11,8 +11,9 @@ import org.eclipsefoundation.marketplace.dto.Category; import org.eclipsefoundation.marketplace.namespace.DatabaseFieldNames; /** - * @author martin - * + * Converter implementation for the {@link Category} object. + * + * @author Martin Lowe */ public class CategoryConverter implements Converter<Category> { @@ -20,8 +21,8 @@ public class CategoryConverter implements Converter<Category> { public Category convert(Document src) { Category out = new Category(); out.setId(src.getString(DatabaseFieldNames.DOCID)); - out.setName(src.getString(DatabaseFieldNames.CATEGORY_NAME)); - out.setUrl(src.getString(DatabaseFieldNames.CATEGORY_URL)); + out.setName(src.getString(DatabaseFieldNames.NAME)); + out.setUrl(src.getString(DatabaseFieldNames.URL)); return out; } @@ -29,8 +30,8 @@ public class CategoryConverter implements Converter<Category> { public Document convert(Category src) { Document doc = new Document(); doc.put(DatabaseFieldNames.DOCID, src.getId()); - doc.put(DatabaseFieldNames.CATEGORY_NAME, src.getName()); - doc.put(DatabaseFieldNames.CATEGORY_URL, src.getUrl()); + doc.put(DatabaseFieldNames.NAME, src.getName()); + doc.put(DatabaseFieldNames.URL, src.getUrl()); return doc; } diff --git a/src/main/java/org/eclipsefoundation/marketplace/dto/converters/TagConverter.java b/src/main/java/org/eclipsefoundation/marketplace/dto/converters/TagConverter.java index a82d5a0..5d06635 100644 --- a/src/main/java/org/eclipsefoundation/marketplace/dto/converters/TagConverter.java +++ b/src/main/java/org/eclipsefoundation/marketplace/dto/converters/TagConverter.java @@ -18,13 +18,10 @@ public class TagConverter implements Converter<Tag> { @Override public Tag convert(Document src) { - Tag org = new Tag(); - - org.setId(src.getString("id")); - org.setName(src.getString("name")); - org.setUrl(src.getString("url")); - - return org; + Tag tag = new Tag(); + tag.setId(src.getString("id")); + tag.setName(src.getString("name")); + return tag; } @Override @@ -32,7 +29,6 @@ public class TagConverter implements Converter<Tag> { Document doc = new Document(); doc.put("name", src.getName()); doc.put("id", src.getId()); - doc.put("url", src.getUrl()); return doc; } diff --git a/src/main/java/org/eclipsefoundation/marketplace/dto/filter/CatalogFilter.java b/src/main/java/org/eclipsefoundation/marketplace/dto/filter/CatalogFilter.java index cb4df1a..1d044a3 100644 --- a/src/main/java/org/eclipsefoundation/marketplace/dto/filter/CatalogFilter.java +++ b/src/main/java/org/eclipsefoundation/marketplace/dto/filter/CatalogFilter.java @@ -6,7 +6,6 @@ */ package org.eclipsefoundation.marketplace.dto.filter; -import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -17,11 +16,7 @@ import org.eclipsefoundation.marketplace.dto.Catalog; import org.eclipsefoundation.marketplace.model.RequestWrapper; /** - * Filter implementation for the Listing class. Checks the following fields: - * - * <ul> - - * </ul> + * Filter implementation for the Catalog class. * * @author Martin Lowe */ diff --git a/src/main/java/org/eclipsefoundation/marketplace/model/MongoQuery.java b/src/main/java/org/eclipsefoundation/marketplace/model/MongoQuery.java index f1f3aee..c1ae7dc 100644 --- a/src/main/java/org/eclipsefoundation/marketplace/model/MongoQuery.java +++ b/src/main/java/org/eclipsefoundation/marketplace/model/MongoQuery.java @@ -186,7 +186,7 @@ public class MongoQuery<T> { * @return the docType */ public Class<T> getDocType() { - return (Class<T>) qps.getAttribute(AnnotationClassInjectionFilter.ATTRIBUTE_NAME); + return (Class<T>) qps.getAttribute(AnnotationClassInjectionFilter.ATTRIBUTE_NAME).get(); } /** diff --git a/src/main/java/org/eclipsefoundation/marketplace/model/RequestWrapper.java b/src/main/java/org/eclipsefoundation/marketplace/model/RequestWrapper.java index 341ca97..b6f103c 100644 --- a/src/main/java/org/eclipsefoundation/marketplace/model/RequestWrapper.java +++ b/src/main/java/org/eclipsefoundation/marketplace/model/RequestWrapper.java @@ -20,8 +20,6 @@ import javax.ws.rs.core.UriInfo; import org.apache.commons.lang3.StringUtils; import org.jboss.resteasy.core.ResteasyContext; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * Wrapper class for query parameter functionality, wrapping a Map of String to @@ -33,17 +31,17 @@ import org.slf4j.LoggerFactory; */ @RequestScoped public class RequestWrapper { - private static final Logger LOGGER = LoggerFactory.getLogger(RequestWrapper.class); private static final String EMPTY_KEY_MESSAGE = "Key must not be null or blank"; private Map<String, List<String>> params; - + private UriInfo uriInfo; private HttpServletRequest request; private UserAgent userAgent; /** - * Generates a wrapper around the + * Generates a wrapper around the + * * @param uriInfo */ RequestWrapper() { @@ -51,7 +49,7 @@ public class RequestWrapper { this.request = ResteasyContext.getContextData(HttpServletRequest.class); this.userAgent = null; } - + /** * Retrieves the first value set in a list from the map for a given key. * @@ -91,7 +89,7 @@ public class RequestWrapper { } return vals; } - + /** * Adds the given value for the given key, preserving previous values if they * exist. @@ -109,14 +107,15 @@ public class RequestWrapper { } /** - * Returns this QueryParams object as a Map of param values indexed by the param name. + * Returns this QueryParams object as a Map of param values indexed by the param + * name. * * @return a copy of the internal param map */ public Map<String, List<String>> asMap() { return new HashMap<>(getParams()); } - + private Map<String, List<String>> getParams() { if (params == null) { params = new HashMap<>(); @@ -126,23 +125,41 @@ public class RequestWrapper { } return this.params; } - + /** * Returns the endpoint for the current call + * * @return */ public String getEndpoint() { return uriInfo.getPath(); } - - public Object getAttribute(String key) { - return request.getAttribute(key); + + /** + * Retrieve a request attribute + * + * @param key attribute key + * @return the attribute value, or an empty optional if missing. + */ + public Optional<Object> getAttribute(String key) { + return Optional.ofNullable(request.getAttribute(key)); } + /** + * Retrieve a request header value as an optional value. + * + * @param key the headers key value + * @return the value, or an empty optional if missing. + */ public String getHeader(String key) { return request.getHeader(key); } - + + /** + * Get the wrapped user agent object for the current request. + * + * @return the wrapped UserAgent object. + */ public UserAgent getUserAgent() { if (userAgent == null) { this.userAgent = new UserAgent(getHeader("user-agent")); diff --git a/src/main/java/org/eclipsefoundation/marketplace/namespace/DatabaseFieldNames.java b/src/main/java/org/eclipsefoundation/marketplace/namespace/DatabaseFieldNames.java index 2fd71bc..dbcac25 100644 --- a/src/main/java/org/eclipsefoundation/marketplace/namespace/DatabaseFieldNames.java +++ b/src/main/java/org/eclipsefoundation/marketplace/namespace/DatabaseFieldNames.java @@ -20,9 +20,9 @@ public final class DatabaseFieldNames { // base fields public static final String DOCID = "_id"; public static final String URL = "url"; + public static final String NAME = "name"; // listing fields - public static final String LISTING_TITLE = "title"; public static final String LISTING_TEASER = "teaser"; public static final String LISTING_BODY = "body"; public static final String LISTING_AUTHORS = "authors"; @@ -49,18 +49,9 @@ public final class DatabaseFieldNames { public static final String CATALOG_SELF_CONTAINED = "self_contained"; public static final String CATALOG_SEARCH_ENABLED = "search_enabled"; public static final String CATALOG_ICON = "icon"; - public static final String CATALOG_URL = "url"; public static final String CATALOG_DESCRIPTION = "description"; - public static final String CATALOG_TITLE = "title"; public static final String CATALOG_DEPENDENCIES_REPOSITORY = "dependencies_repository"; - // category fields - public static final String MARKET_IDS = "market_ids"; - public static final String CATEGORY_NAME = "name"; - public static final String CATEGORY_URL = "url"; - - public static final String MARKET_NAME = "name"; - private DatabaseFieldNames() { } } diff --git a/src/main/java/org/eclipsefoundation/marketplace/namespace/UrlParameterNames.java b/src/main/java/org/eclipsefoundation/marketplace/namespace/UrlParameterNames.java index feb9216..6d9791c 100644 --- a/src/main/java/org/eclipsefoundation/marketplace/namespace/UrlParameterNames.java +++ b/src/main/java/org/eclipsefoundation/marketplace/namespace/UrlParameterNames.java @@ -9,6 +9,11 @@ */ package org.eclipsefoundation.marketplace.namespace; +/** + * Namespace containing URL parameters used throughout the API. + * + * @author Martin Lowe + */ public final class UrlParameterNames { public static final String QUERY_STRING = "q"; diff --git a/src/main/node/hammer.js b/src/main/node/hammer.js index 9778db0..3f0e344 100644 --- a/src/main/node/hammer.js +++ b/src/main/node/hammer.js @@ -40,7 +40,7 @@ async function toodles() { } function report(result) { var d = result.data; - for (l in d) { + for (var l in d) { var id = d[l].id; if (arr.indexOf(id) == -1) { arr.push(id); diff --git a/src/main/node/index.js b/src/main/node/index.js index fd12f21..03d5f45 100644 --- a/src/main/node/index.js +++ b/src/main/node/index.js @@ -25,7 +25,7 @@ for (var i=0;i<20;i++) { categoryIds.push(uuid.v4()); } const marketIds = []; -for (var i=0;i<5;i++) { +for (i=0;i<5;i++) { marketIds.push(uuid.v4()); } @@ -61,7 +61,7 @@ function createListing(count) { } count++; axios.post(argv.s+"/listings/", generateJSON(uuid.v4())) - .then(createListing(count)) + .then(() => createListing(count)) .catch(err => console.log(err)); } @@ -71,7 +71,7 @@ function createCategory(count) { } axios.post(argv.s+"/categories/", generateCategoryJSON(categoryIds[count++])) - .then(createCategory(count)) + .then(() => createCategory(count)) .catch(err => console.log(err)); } @@ -81,7 +81,7 @@ function createMarket(count) { } axios.post(argv.s+"/markets/", generateMarketJSON(marketIds[count++])) - .then(createMarket(count)) + .then(() => createMarket(count)) .catch(err => console.log(err)); } diff --git a/src/test/java/org/eclipsefoundation/marketplace/resource/ListingResourceTest.java b/src/test/java/org/eclipsefoundation/marketplace/resource/ListingResourceTest.java index 5d62b2f..272baf5 100644 --- a/src/test/java/org/eclipsefoundation/marketplace/resource/ListingResourceTest.java +++ b/src/test/java/org/eclipsefoundation/marketplace/resource/ListingResourceTest.java @@ -21,11 +21,6 @@ import io.quarkus.test.junit.QuarkusTest; @QuarkusTest public class ListingResourceTest { - @Test - public void testListingIdEndpointBadId() { - given().when().get("/listings/test").then().statusCode(400); - } - @Test public void testListingIdEndpoint() { given().when().get("/listings/1").then().statusCode(200); -- GitLab