Skip to content
This repository has been archived by the owner on Jul 29, 2021. It is now read-only.

Commit

Permalink
feat: Allows to define manual rules for the reviewer which will affec…
Browse files Browse the repository at this point in the history
…t the API quality

fix gravitee-io/issues#2601
  • Loading branch information
aelamrani committed Oct 8, 2019
1 parent 592163f commit b6ddc1c
Show file tree
Hide file tree
Showing 6 changed files with 291 additions and 10 deletions.
19 changes: 10 additions & 9 deletions pom.xml
Expand Up @@ -35,16 +35,17 @@
<gravitee-repository.version>1.30.0-SNAPSHOT</gravitee-repository.version>
<gravitee-repository-test.version>1.30.0-SNAPSHOT</gravitee-repository-test.version>
<maven-dependency-plugin.version>2.10</maven-dependency-plugin.version>
<HikariCP.version>3.3.1</HikariCP.version>
<liquibase.version>3.7.0</liquibase.version>
<HikariCP.version>3.4.1</HikariCP.version>
<liquibase.version>3.8.0</liquibase.version>
<liquibase-slf4j.version>2.0.0</liquibase-slf4j.version>
<mysql-connector-java.version>8.0.15</mysql-connector-java.version>
<postgresql.version>42.2.5</postgresql.version>
<mariaDB.version>2.3.0</mariaDB.version>
<wix-embedded-mysql.version>4.1.2</wix-embedded-mysql.version>
<postgresql-embedded.version>2.9</postgresql-embedded.version>
<testcontainers.version>1.11.1</testcontainers.version>
<mysql-connector-java.version>8.0.17</mysql-connector-java.version>
<postgresql.version>42.2.8</postgresql.version>
<mariaDB.version>2.4.0</mariaDB.version>
<wix-embedded-mysql.version>4.6.0</wix-embedded-mysql.version>
<postgresql-embedded.version>2.10</postgresql-embedded.version>
<testcontainers.version>1.12.2</testcontainers.version>
<default-database.jdbcType>postgresql-te</default-database.jdbcType>
<mssql-jdbc.version>7.4.1.jre8</mssql-jdbc.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -195,7 +196,7 @@
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>7.2.1.jre8</version>
<version>${mssql-jdbc.version}</version>
<scope>test</scope>
</dependency>

Expand Down
@@ -0,0 +1,192 @@
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.repository.jdbc.management;

import io.gravitee.repository.exceptions.TechnicalException;
import io.gravitee.repository.jdbc.orm.JdbcObjectMapper;
import io.gravitee.repository.management.api.ApiQualityRuleRepository;
import io.gravitee.repository.management.model.ApiQualityRule;
import io.gravitee.repository.management.model.ApiQualityRule;
import io.gravitee.repository.management.model.Membership;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.sql.Types;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;

import static io.gravitee.repository.jdbc.management.JdbcHelper.WHERE_CLAUSE;
import static java.lang.String.format;

/**
* @author Azize ELAMRANI (azize.elamrani at graviteesource.com)
* @author GraviteeSource Team
*/
@Repository
public class JdbcApiQualityRuleRepository implements ApiQualityRuleRepository {

private static final Logger LOGGER = LoggerFactory.getLogger(JdbcApiQualityRuleRepository.class);

@Autowired
private JdbcTemplate jdbcTemplate;

private static final JdbcObjectMapper ORM = JdbcObjectMapper.builder(ApiQualityRule.class, "api_quality_rules")
.updateSql("update api_quality_rules set "
+ " api = ?"
+ " , quality_rule = ?"
+ " , checked = ?"
+ " , created_at = ? "
+ " , updated_at = ? "
+ WHERE_CLAUSE
+ " api = ? "
+ " and quality_rule = ? "
)
.addColumn("api", Types.NVARCHAR, String.class)
.addColumn("quality_rule", Types.NVARCHAR, String.class)
.addColumn("checked", Types.BOOLEAN, boolean.class)
.addColumn("created_at", Types.TIMESTAMP, Date.class)
.addColumn("updated_at", Types.TIMESTAMP, Date.class)
.build();

@Override
public Optional<ApiQualityRule> findById(String api, String qualityRule) throws TechnicalException {
LOGGER.debug("JdbcApiQualityRuleRepository.findById({}, {})", api, qualityRule);
try {
final List<ApiQualityRule> apiQualityRules = jdbcTemplate.query("select"
+ " api, quality_rule, checked, created_at, updated_at "
+ " from api_quality_rules where api = ? and quality_rule = ?"
, ORM.getRowMapper()
, api
, qualityRule
);
return apiQualityRules.stream().findFirst();
} catch (final Exception ex) {
final String error = "Failed to find apiQualityRule by id";
LOGGER.error(error, ex);
throw new TechnicalException(error, ex);
}
}

@Override
public ApiQualityRule create(ApiQualityRule apiQualityRule) throws TechnicalException {
LOGGER.debug("JdbcApiQualityRuleRepository.create({})", apiQualityRule);
try {
jdbcTemplate.update(ORM.buildInsertPreparedStatementCreator(apiQualityRule));
return findById(apiQualityRule.getApi(), apiQualityRule.getQualityRule()).orElse(null);
} catch (final Exception ex) {
final String error = "Failed to create apiQualityRule";
LOGGER.error(error, ex);
throw new TechnicalException(error, ex);
}
}

@Override
public ApiQualityRule update(ApiQualityRule apiQualityRule) throws TechnicalException {
LOGGER.debug("JdbcApiQualityRuleRepository.update({})", apiQualityRule);
if (apiQualityRule == null) {
throw new IllegalStateException("Failed to update null");
}
try {
jdbcTemplate.update(ORM.buildUpdatePreparedStatementCreator(apiQualityRule
, apiQualityRule.getApi()
, apiQualityRule.getQualityRule()
));
return findById(apiQualityRule.getApi(), apiQualityRule.getQualityRule()).orElseThrow(() ->
new IllegalStateException(format("No apiQualityRule found with id [%s, %s]", apiQualityRule.getApi(), apiQualityRule.getQualityRule())));
} catch (final IllegalStateException ex) {
throw ex;
} catch (final Exception ex) {
final String error = "Failed to update apiQualityRule";
LOGGER.error(error, ex);
throw new TechnicalException(error, ex);
}
}

@Override
public void delete(String api, String qualityRule) throws TechnicalException {
LOGGER.debug("JdbcApiQualityRuleRepository.delete({}, {})", api, qualityRule);
try {
jdbcTemplate.update("delete from api_quality_rules where api = ? and quality_rule = ? "
, api
, qualityRule
);
} catch (final Exception ex) {
final String error = "Failed to delete apiQualityRule";
LOGGER.error(error, ex);
throw new TechnicalException(error, ex);
}
}

@Override
public List<ApiQualityRule> findByQualityRule(String qualityRule) throws TechnicalException {
LOGGER.debug("JdbcApiQualityRuleRepository.findByQualityRule({})", qualityRule);
try {
final String query = "select "
+ " api, quality_rule, checked, created_at, updated_at "
+ " from api_quality_rules where quality_rule = ? ";
return jdbcTemplate.query(query, (PreparedStatement ps) -> ps.setString(1, qualityRule), ORM.getRowMapper());
} catch (final Exception ex) {
final String error = "Failed to find apiQualityRule by quality rule";
LOGGER.error(error, ex);
throw new TechnicalException(error, ex);
}
}

@Override
public List<ApiQualityRule> findByApi(String api) throws TechnicalException {
LOGGER.debug("JdbcApiQualityRuleRepository.findByApi({})", api);
try {
final String query = "select "
+ " api, quality_rule, checked, created_at, updated_at "
+ " from api_quality_rules where api = ? ";
return jdbcTemplate.query(query, (PreparedStatement ps) -> ps.setString(1, api), ORM.getRowMapper());
} catch (final Exception ex) {
final String error = "Failed to find apiQualityRule by api";
LOGGER.error(error, ex);
throw new TechnicalException(error, ex);
}
}

@Override
public void deleteByQualityRule(String qualityRule) throws TechnicalException {
LOGGER.debug("JdbcApiQualityRuleRepository.deleteByQualityRule({})", qualityRule);
try {
jdbcTemplate.update("delete from api_quality_rules where quality_rule = ? ", qualityRule);
} catch (final Exception ex) {
final String error = "Failed to delete apiQualityRule by quality rule";
LOGGER.error(error, ex);
throw new TechnicalException(error, ex);
}
}

@Override
public void deleteByApi(String api) throws TechnicalException {
LOGGER.debug("JdbcApiQualityRuleRepository.deleteByApi({})", api);
try {
jdbcTemplate.update("delete from api_quality_rules where api = ? ", api);
} catch (final Exception ex) {
final String error = "Failed to delete apiQualityRule by api";
LOGGER.error(error, ex);
throw new TechnicalException(error, ex);
}
}
}
@@ -0,0 +1,51 @@
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.repository.jdbc.management;

import io.gravitee.repository.jdbc.orm.JdbcObjectMapper;
import io.gravitee.repository.management.api.QualityRuleRepository;
import io.gravitee.repository.management.model.QualityRule;
import org.springframework.stereotype.Repository;

import java.sql.Types;
import java.util.Date;

/**
* @author Azize ELAMRANI (azize.elamrani at graviteesource.com)
* @author GraviteeSource Team
*/
@Repository
public class JdbcQualityRuleRepository extends JdbcAbstractCrudRepository<QualityRule, String> implements QualityRuleRepository {

private static final JdbcObjectMapper ORM = JdbcObjectMapper.builder(QualityRule.class, "quality_rules", "id")
.addColumn("id", Types.NVARCHAR, String.class)
.addColumn("name", Types.NVARCHAR, String.class)
.addColumn("description", Types.NVARCHAR, String.class)
.addColumn("weight", Types.INTEGER, int.class)
.addColumn("created_at", Types.TIMESTAMP, Date.class)
.addColumn("updated_at", Types.TIMESTAMP, Date.class)
.build();

@Override
protected JdbcObjectMapper getOrm() {
return ORM;
}

@Override
protected String getId(QualityRule item) {
return item.getId();
}
}
33 changes: 33 additions & 0 deletions src/main/resources/liquibase/changelogs/v1_30_0/schema.yml
@@ -0,0 +1,33 @@
databaseChangeLog:
- changeSet:
id: 1.30.0
author: GraviteeSource Team
changes:
- createTable:
tableName: quality_rules
columns:
- column: {name: id, type: nvarchar(64), constraints: { nullable: false } }
- column: {name: name, type: nvarchar(64), constraints: { nullable: false } }
- column: {name: description, type: nvarchar(256), constraints: { nullable: false } }
- column: {name: weight, type: int, constraints: { nullable: false } }
- column: {name: created_at, type: timestamp(6), constraints: { nullable: false } }
- column: {name: updated_at, type: timestamp(6), constraints: { nullable: true } }

- addPrimaryKey:
constraintName: pk_quality_rules
columnNames: id
tableName: quality_rules

- createTable:
tableName: api_quality_rules
columns:
- column: {name: api, type: nvarchar(64), constraints: { nullable: false } }
- column: {name: quality_rule, type: nvarchar(64), constraints: { nullable: false } }
- column: {name: checked, type: boolean, constraints: { nullable: false } }
- column: {name: created_at, type: timestamp(6), constraints: { nullable: false } }
- column: {name: updated_at, type: timestamp(6), constraints: { nullable: true } }

- addPrimaryKey:
constraintName: pk_api_quality_rules
columnNames: api, quality_rule
tableName: api_quality_rules
2 changes: 2 additions & 0 deletions src/main/resources/liquibase/master.yml
Expand Up @@ -53,3 +53,5 @@ databaseChangeLog:
- file: liquibase/changelogs/v1_26_0/schema.yml
- include:
- file: liquibase/changelogs/v1_27_0/schema.yml
- include:
- file: liquibase/changelogs/v1_30_0/schema.yml
Expand Up @@ -104,7 +104,9 @@ public class JdbcTestRepositoryInitializer implements TestRepositoryInitializer
"page_metadata",
"invitations",
"tag_groups",
"workflows"
"workflows",
"quality_rules",
"api_quality_rules"
);

private static final List<String> tablesToDrop = concatenate(tablesToTruncate
Expand Down

0 comments on commit b6ddc1c

Please sign in to comment.