Skip to content
Snippets Groups Projects
Commit 5df49eaf authored by Martin Lowe's avatar Martin Lowe :flag_ca:
Browse files

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's avatarMartin Lowe <martin.lowe@eclipse-foundation.org>
parent d16bcf88
No related branches found
No related tags found
1 merge request!31Code clean up
Showing
with 221 additions and 301 deletions
......@@ -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;
......
......@@ -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
......@@ -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
}
......@@ -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);
......
......@@ -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);
}
}
/* 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);
}
}
......@@ -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));
......
......@@ -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));
......
......@@ -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;
}
......
......@@ -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;
}
......
......@@ -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;
}
......
......@@ -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
*/
......
......@@ -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();
}
/**
......
......@@ -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"));
......
......@@ -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() {
}
}
......@@ -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";
......
......@@ -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);
......
......@@ -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));
}
......
......@@ -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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment