Skip to content

Commit

Permalink
Implement SonarCloud quality suggestions #159
Browse files Browse the repository at this point in the history
  • Loading branch information
pkiraly committed Dec 8, 2023
1 parent 1a71c59 commit d8ecf01
Show file tree
Hide file tree
Showing 14 changed files with 180 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ private FieldCounter<Double> calculateScore() {
normalized = LanguageSaturationType.NA.value();
sum = LanguageSaturationType.NA.value();
} else {
average = sum / (double) values.size();
average = sum / values.size();
normalized = normalize(average);
sums.add(sum);
}
Expand All @@ -297,7 +297,7 @@ private FieldCounter<Double> calculateScore() {
}
}
sum = summarize(sums);
average = sum / (double) sums.size();
average = sum / sums.size();
normalized = normalize(average);
if (resultType.equals(MultilingualitySaturationCalculator.ResultTypes.EXTENDED)) {
languageMap.put(SUM, sum);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,12 @@ public List<DataElement> getCollectionPaths() {

@Override
public List<DataElement> getRootChildrenPaths() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public DataElement getPathByLabel(String label) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
throw new UnsupportedOperationException("Not supported yet.");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public List<DataElement> getPaths() {

@Override
public List<DataElement> getRootChildrenPaths() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import java.util.Map;
import java.util.Set;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.junit.Assert.*;

Expand All @@ -32,6 +34,8 @@
* @author Péter Király <peter.kiraly at gwdg.de>
*/
public class CalculatorFacadeTest {
@Rule
public ExpectedException thrown = ExpectedException.none();

public CalculatorFacadeTest() {
}
Expand Down Expand Up @@ -213,8 +217,11 @@ public void testChanged() {
assertEquals(1, calculators.size());
}

@Test(expected = IllegalArgumentException.class)
@Test
public void testTfIdfWithWrongConfiguration() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("If TF-IDF measurement is enabled, Solr configuration should not be null.");

MeasurementConfiguration configuration = new MeasurementConfiguration().enableTfIdfMeasurement();
CalculatorFacade calculator = new CalculatorFacade(configuration)
.setSchema(new EdmOaiPmhJsonSchema());
Expand All @@ -223,6 +230,8 @@ public void testTfIdfWithWrongConfiguration() {
List<Calculator> calculators = calculator.getCalculators();

assertEquals(2, calculators.size());

fail("Exception did not thrown.");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import de.gwdg.metadataqa.api.schema.Format;
import de.gwdg.metadataqa.api.schema.Schema;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
Expand All @@ -24,6 +25,8 @@
import static org.junit.Assert.*;

public class SchemaConfigurationTest {
@org.junit.Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void testReading_fromResource() {
Expand Down Expand Up @@ -221,10 +224,15 @@ public void yaml_equals() throws FileNotFoundException {
assertEquals("description", schema.getPathByLabel("about").getRules().get(0).getEquals());
}

@Test(expected = IllegalArgumentException.class)
@Test
public void yaml_equals_wrong() throws FileNotFoundException {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("about refers to a nonexistent field in 'equals: title'");

Schema schema = ConfigurationReader.readSchemaYaml("src/test/resources/configuration/schema/rules/equals_wrong.yaml").asSchema();
assertEquals("description", schema.getPathByLabel("about").getRules().get(0).getEquals());

fail("Exception did not thrown.");
}

@Test
Expand All @@ -233,10 +241,15 @@ public void yaml_lessThan() throws FileNotFoundException {
assertEquals("description", schema.getPathByLabel("about").getRules().get(0).getLessThan());
}

@Test(expected = IllegalArgumentException.class)
@Test
public void yaml_lessThan_wrong() throws FileNotFoundException {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("about refers to a nonexistent field in 'lessThan: title'");

Schema schema = ConfigurationReader.readSchemaYaml("src/test/resources/configuration/schema/rules/lessThan_wrong.yaml").asSchema();
assertEquals("description", schema.getPathByLabel("about").getRules().get(0).getLessThan());

fail("Exception did not thrown.");
}

@Test
Expand All @@ -245,10 +258,15 @@ public void yaml_lessThanOrEquals() throws FileNotFoundException {
assertEquals("description", schema.getPathByLabel("about").getRules().get(0).getLessThanOrEquals());
}

@Test(expected = IllegalArgumentException.class)
@Test
public void yaml_lessThanOrEquals_wrong() throws FileNotFoundException {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("about refers to a nonexistent field in 'lessThanOrEquals: title'");

Schema schema = ConfigurationReader.readSchemaYaml("src/test/resources/configuration/schema/rules/lessThanOrEquals_wrong.yaml").asSchema();
assertEquals("description", schema.getPathByLabel("about").getRules().get(0).getLessThanOrEquals());

fail("Exception did not thrown.");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@
import de.gwdg.metadataqa.api.model.selector.SelectorFactory;
import de.gwdg.metadataqa.api.model.selector.XmlSelector;
import de.gwdg.metadataqa.api.schema.Format;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class SelectorFactoryTest {

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void json() {
Selector<? extends XmlFieldInstance> cache = SelectorFactory.getInstance(Format.JSON, "[\"a\",\"b\"]");
Expand All @@ -30,9 +36,12 @@ public void csv() {
assertTrue(cache instanceof CsvSelector);
}

@Test(expected = IllegalArgumentException.class)
@Test
public void none() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Unrecognized format: null");
Selector<? extends XmlFieldInstance> cache = SelectorFactory.getInstance(null, "a,b");
assertTrue(cache instanceof CsvSelector);
fail("Exception did not thrown.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@
import de.gwdg.metadataqa.api.util.CsvReader;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

public class DisjointCheckerTest {
@Rule
public ExpectedException thrown = ExpectedException.none();

protected Schema schema;
protected CsvSelector cache;
Expand Down Expand Up @@ -65,19 +70,33 @@ public void failure() {
assertEquals(RuleCheckingOutputStatus.FAILED, fieldCounter.get(checker.getHeader(RuleCheckingOutputType.STATUS)).getStatus());
}

@Test(expected = IllegalArgumentException.class)
@Test
public void failure_constructor1() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("field1 should not be null");

DisjointChecker checker = new DisjointChecker(schema.getPathByLabel("nameX"), schema.getPathByLabel("altX"));

fail("Exception did not thrown.");
}

@Test(expected = IllegalArgumentException.class)
@Test
public void failure_constructor2() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("field1 should not be null");

DisjointChecker checker = new DisjointChecker(schema.getPathByLabel("nameX"), schema.getPathByLabel("altX"));

fail("Exception did not thrown.");
}

@Test(expected = IllegalArgumentException.class)
@Test
public void failure_constructor3() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("field2 should not be null");

DisjointChecker checker = new DisjointChecker(schema.getPathByLabel("name"), schema.getPathByLabel("altX"));
}

fail("Exception did not thrown.");
}
}
10 changes: 9 additions & 1 deletion src/test/java/de/gwdg/metadataqa/api/schema/BaseSchemaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import de.gwdg.metadataqa.api.rule.RuleChecker;
import de.gwdg.metadataqa.api.util.CsvReader;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.io.File;
import java.io.FileNotFoundException;
Expand All @@ -24,6 +25,8 @@
import static org.junit.Assert.*;

public class BaseSchemaTest {
@org.junit.Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void testConstructor() throws Exception {
Expand Down Expand Up @@ -641,13 +644,18 @@ public void testGetPathByLabel() {
assertEquals("url", schema.getPathByLabel("url").getLabel());
}

@Test(expected = UnsupportedOperationException.class)
@Test
public void testGetNoLanguageFields() {
thrown.expect(UnsupportedOperationException.class);
thrown.expectMessage("Not supported yet.");

Schema schema = new BaseSchema()
.setFormat(Format.CSV)
.addFields("url", "name");

schema.getNoLanguageFields();

fail("Exception did not thrown.");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package de.gwdg.metadataqa.api.schema.edm;

import de.gwdg.metadataqa.api.schema.Schema;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

public class EdmFullBeanLimitedSchemaTest {

@Rule
public ExpectedException thrown = ExpectedException.none();

Schema schema = new EdmFullBeanLimitedSchema();

@Test
Expand All @@ -19,8 +25,12 @@ public void getIndexFields() {
assertEquals(3, schema.getIndexFields().size());
}

@Test(expected = UnsupportedOperationException.class)
@Test
public void getRootChildrenPaths() {
thrown.expect(UnsupportedOperationException.class);
thrown.expectMessage("Not supported yet.");

assertEquals(3, schema.getRootChildrenPaths().size());
fail("Exception did not thrown.");
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package de.gwdg.metadataqa.api.schema.edm;

import de.gwdg.metadataqa.api.schema.Format;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.junit.Assert.assertEquals;

public class EdmOaiPmLimitedJsonSchemaTest {

@Rule
public ExpectedException thrown = ExpectedException.none();

EdmSchema schema = new EdmOaiPmLimitedJsonSchema();

@Test
Expand All @@ -18,8 +24,22 @@ public void getIndexFields() {
assertEquals(3, schema.getIndexFields().size());
}

@Test(expected = UnsupportedOperationException.class)
@Test
public void getRootChildrenPaths() {
thrown.expect(UnsupportedOperationException.class);
thrown.expectMessage("Not supported yet.");
assertEquals(3, schema.getRootChildrenPaths().size());
}

@Test
public void getPathByLabel() {
thrown.expect(UnsupportedOperationException.class);
thrown.expectMessage("Not supported yet.");
assertEquals(3, schema.getPathByLabel("label"));
}

@Test
public void getFormat() {
assertEquals(Format.JSON, schema.getFormat());
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package de.gwdg.metadataqa.api.schema.edm;

import de.gwdg.metadataqa.api.schema.Schema;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

public class EdmOaiPmhJsonSchemaTest {

@Rule
public ExpectedException thrown = ExpectedException.none();

Schema schema = new EdmOaiPmhJsonSchema();

@Test
Expand All @@ -19,8 +25,12 @@ public void getIndexFields() {
assertEquals(3, schema.getIndexFields().size());
}

@Test(expected = UnsupportedOperationException.class)
@Test
public void getRootChildrenPaths() {
thrown.expect(UnsupportedOperationException.class);
thrown.expectMessage("Not supported yet.");

assertEquals(3, schema.getRootChildrenPaths().size());
fail("Exception did not thrown.");
}
}
Loading

0 comments on commit d8ecf01

Please sign in to comment.