Skip to content

Commit 6e97470

Browse files
authored
Merge pull request #126 from InseeFr/125-augmenter-la-couverture-de-test
feature : CodeListTest.java and FilesUtilsTest.java
2 parents 37ddd2e + 86dc059 commit 6e97470

File tree

8 files changed

+237
-5
lines changed

8 files changed

+237
-5
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package fr.insee.rmes.exceptions;
2+
3+
import org.junit.jupiter.api.Test;
4+
import java.util.ArrayList;
5+
import java.util.Arrays;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
8+
class RmesExceptionIOTest {
9+
10+
11+
@Test
12+
void shouldReturnRestMessageObjectParametersPossibilities() {
13+
14+
ArrayList<Integer> status = new ArrayList<>(Arrays.asList(ErrorCodes.LINK_CREATION_RIGHTS_DENIED,-1));
15+
ArrayList<String> messages = new ArrayList<>(Arrays.asList("Unauthorized",null));
16+
ArrayList<String> details = new ArrayList<>(Arrays.asList("Invalid password or tocken",null));
17+
18+
RmesExceptionIO rmesExceptionIOExampleOne= new RmesExceptionIO(status.getFirst(), messages.getFirst(), details.getFirst());
19+
RmesExceptionIO rmesExceptionIOExampleTwo= new RmesExceptionIO(status.getLast(),messages.getLast(),details.getLast());
20+
21+
assertTrue(rmesExceptionIOExampleOne.getMessage()!=null && rmesExceptionIOExampleTwo.getMessage()==null);
22+
}
23+
24+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package fr.insee.rmes.metadata.service;
2+
3+
import org.junit.jupiter.api.Test;
4+
import static org.junit.jupiter.api.Assertions.*;
5+
6+
class MetadataServiceImplTest {
7+
8+
@Test
9+
void shouldReturnListOfUnits() throws Exception {
10+
11+
MetadataServiceImpl metadataServiceImpl = new MetadataServiceImpl();
12+
13+
String actual =metadataServiceImpl.getUnits().toString();
14+
String expected = "[Unit [uri=http://id.insee.fr/unit/euro, label=€], Unit [uri=http://id.insee.fr/unit/keuro, label=k€], Unit [uri=http://id.insee.fr/unit/percent, label=%], Unit [uri=http://id.insee.fr/unit/heure, label=heures], Unit [uri=http://id.insee.fr/unit/jour, label=jours], Unit [uri=http://id.insee.fr/unit/semaine, label=semaines], Unit [uri=http://id.insee.fr/unit/mois, label=mois], Unit [uri=http://id.insee.fr/unit/annee, label=années], Unit [uri=http://id.insee.fr/unit/an, label=ans], Unit [uri=http://id.insee.fr/unit/watt, label=W], Unit [uri=http://id.insee.fr/unit/kilowatt, label=kW], Unit [uri=http://id.insee.fr/unit/megawatt, label=MW], Unit [uri=http://id.insee.fr/unit/megawattheurepcs, label=MWh PCS], Unit [uri=http://id.insee.fr/unit/megawattheure, label=MWh], Unit [uri=http://id.insee.fr/unit/megawattpcs, label=MW PCS], Unit [uri=http://id.insee.fr/unit/kilowattthermique, label=kWth], Unit [uri=http://id.insee.fr/unit/kg, label=kg], Unit [uri=http://id.insee.fr/unit/tonne, label=tonnes], Unit [uri=http://id.insee.fr/unit/tonnematiereseche, label=tonnes matières sèches], Unit [uri=http://id.insee.fr/unit/degrecelsius, label=°C], Unit [uri=http://id.insee.fr/unit/bar, label=bars], Unit [uri=http://id.insee.fr/unit/litre, label=litres], Unit [uri=http://id.insee.fr/unit/metre, label=mètres], Unit [uri=http://id.insee.fr/unit/centimetre, label=centimètres], Unit [uri=http://id.insee.fr/unit/metrecarre, label=mètres carrés]]";
15+
16+
assertEquals(expected,actual);
17+
}
18+
19+
}

src/test/java/fr/insee/rmes/model/CodeListTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,70 @@
11
package fr.insee.rmes.model;
22

3+
import org.json.JSONArray;
4+
import org.json.JSONException;
5+
import org.json.JSONObject;
6+
import org.junit.jupiter.api.Test;
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
310
import org.junit.jupiter.api.Test;
411
import java.util.List;
512
import static org.junit.jupiter.api.Assertions.*;
613

714
class CodeListTest {
815

916
@Test
17+
void testToString() throws JSONException {
18+
String itemType ="itemType";
19+
String agencyId ="agencyId";
20+
String version ="version";
21+
String identifier ="identifier";
22+
String item ="item";
23+
String versionDate ="versionDate";
24+
String versionResponsibility ="versionResponsibility";
25+
boolean isPublished =true;
26+
boolean isDeprecated =true;
27+
boolean isProvisional =true;
28+
String itemFormat ="itemFormat";
29+
JSONObject versionRationale = new JSONObject();
30+
versionRationale.put("versionRationale","versionRationaleExampleOne");
31+
versionRationale.put("versionRationale","versionRationaleExampleTwo");
32+
33+
JSONObject noteExampleOne = new JSONObject();
34+
noteExampleOne.put("noteFirst", "1");
35+
noteExampleOne.put("noteSecond", "2");
36+
37+
JSONObject noteExampleTwo = new JSONObject();
38+
noteExampleTwo.put("noteFirst", "1");
39+
noteExampleTwo.put("noteSecond", "2");
40+
41+
JSONArray notes = new JSONArray();
42+
notes.put(noteExampleOne);
43+
notes.put(noteExampleTwo);
44+
45+
boolean isUniversallyUnique = true;
46+
47+
Code code = new Code();
48+
49+
List<Code> codeList = new ArrayList<>();
50+
codeList.add(code);
51+
52+
String response = "CodeList [codeList=" + codeList + ", isUniversallyUnique=" + isUniversallyUnique + ", itemType="
53+
+ itemType + ", agencyId=" + agencyId + ", version=" + version + ", identifier=" + identifier
54+
+ ", item=" + item + ", versionDate=" + versionDate + ", versionResponsibility=" + versionResponsibility
55+
+ ", isPublished=" + isPublished + ", isDeprecated=" + isDeprecated + ", isProvisional=" + isProvisional
56+
+ ", itemFormat=" + itemFormat + ", versionRationale=" + versionRationale + ", notes=" + notes + "]";
57+
58+
String expected = "CodeList [codeList=[Code [levelNumber=0, value=null, categoryReference=null, category=null, isDiscrete=false, agencyId=null, version=null, identifier=null]], isUniversallyUnique=true, itemType=itemType, agencyId=agencyId, version=version, identifier=identifier, item=item, versionDate=versionDate, versionResponsibility=versionResponsibility, isPublished=true, isDeprecated=true, isProvisional=true, itemFormat=itemFormat, versionRationale={\"versionRationale\":\"versionRationaleExampleTwo\"}, notes=[{\"noteFirst\":\"1\",\"noteSecond\":\"2\"},{\"noteFirst\":\"1\",\"noteSecond\":\"2\"}]]";
59+
60+
assertEquals(expected,response);
61+
}
62+
63+
64+
65+
66+
67+
1068
void shouldTestToString() {
1169

1270
Code code2025 = new Code();

src/test/java/fr/insee/rmes/tocolecticaapi/controller/ControllerUtilsTest.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
11
package fr.insee.rmes.tocolecticaapi.controller;
22

33
import org.junit.jupiter.api.Test;
4+
import org.springframework.http.ResponseEntity;
5+
import java.util.List;
6+
import java.util.Objects;
47
import static org.junit.jupiter.api.Assertions.*;
58

69
class ControllerUtilsTest {
710

811
@Test
12+
void shouldReturnResponsesForDifferentXMLEntities() {
13+
14+
List<String> xmlContents = List.of("<para> It's truly amazing.</para>","<para> It's \\\"\\\" truly amazing.</para>","<para> It's \" truly amazing.</para>");
15+
List<ResponseEntity<String>> responseEntities = List.of(ControllerUtils.xmltoResponseEntity(xmlContents.getFirst()),ControllerUtils.xmltoResponseEntity(xmlContents.get(2)),ControllerUtils.xmltoResponseEntity(xmlContents.getLast()));
16+
17+
List<String> prefixActual = List.of(responseEntities.getFirst().toString().substring(0,17),responseEntities.get(1).toString().substring(0,17),responseEntities.getLast().toString().substring(0,17));
18+
List<String> prefixExpected = List.of("<200 OK OK,<para>","<200 OK OK,<para>","<200 OK OK,<para>");
19+
20+
List<Boolean> responseEntitiesContainsSymbol = List.of(responseEntities.getFirst().toString().contains(""),responseEntities.get(1).toString().contains(""),responseEntities.getLast().toString().contains(""));
21+
List<Boolean> responseEntitiesContainsSlash= List.of(responseEntities.getFirst().toString().contains("\""),responseEntities.get(1).toString().contains("\""),responseEntities.getLast().toString().contains("\""));
22+
23+
boolean identicalPrefix = Objects.equals(prefixActual.toString(), prefixExpected.toString());
24+
boolean containsSymbol = responseEntitiesContainsSymbol.equals(List.of(false, false, false));
25+
boolean containsSlash = responseEntitiesContainsSlash.equals(List.of(false,true,true));
26+
27+
assertTrue(identicalPrefix && containsSymbol && containsSlash);
28+
}
29+
930
void shouldReturnXmlToResponseEntity() {
1031
String sentence = "This\\\"is an \"\"example.";
1132
String actual = ControllerUtils.xmltoResponseEntity(sentence).toString();
@@ -18,5 +39,4 @@ void shouldReturnNullPointerExceptionWhenNullStringForXmlToResponseEntity() {
1839
assertTrue(exception.getMessage().contains("Cannot invoke \"String.replace(java.lang.CharSequence, java.lang.CharSequence)\" because \"xmlContent\" is null"));
1940
}
2041

21-
2242
}

src/test/java/fr/insee/rmes/tocolecticaapi/models/FragmentDataTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,46 @@
11
package fr.insee.rmes.tocolecticaapi.models;
22

33
import org.junit.jupiter.api.Test;
4+
import java.util.List;
45
import static org.junit.jupiter.api.Assertions.*;
56

67
class FragmentDataTest {
78

89
@Test
10+
void shouldIndicateIfTwoFragmentDateAreIdentical() {
11+
FragmentData fragmentDataExampleOne = new FragmentData("identifier","agencyId", "version");
12+
FragmentData fragmentDataExampleTwo = new FragmentData("identifier","agencyId", "version");
13+
FragmentData fragmentDataExampleThree = new FragmentData("example","agencyId", "version");
14+
assertTrue(fragmentDataExampleOne.equals(fragmentDataExampleTwo) && ! fragmentDataExampleOne.equals(fragmentDataExampleThree));
15+
}
16+
17+
@Test
18+
void shouldReturnDifferentHashCodeForEachDifferentFragmentData() {
19+
20+
FragmentData fragmentDataExampleOne = new FragmentData("identifierExampleOne","identifierExampleOne", "identifierExampleOne");
21+
FragmentData fragmentDataExampleTwo = new FragmentData("identifierExampleTwo","agencyIdExampleTwo", "versionExampleTwo");
22+
FragmentData fragmentDataExampleThree = new FragmentData("identifierExampleThree","agencyIdExampleThree", "versionExampleThree");
23+
24+
boolean compareHshCodeOneAndTwo = fragmentDataExampleOne.hashCode() ==fragmentDataExampleTwo.hashCode() ;
25+
boolean compareHshCodeTwoAndThree = fragmentDataExampleTwo.hashCode() ==fragmentDataExampleThree.hashCode() ;
26+
boolean compareHshCodeThreeAndOne = fragmentDataExampleThree.hashCode() ==fragmentDataExampleOne.hashCode() ;
27+
28+
assertEquals(List.of(false,false,false),List.of(compareHshCodeOneAndTwo,compareHshCodeTwoAndThree,compareHshCodeThreeAndOne));
29+
}
30+
31+
@Test
32+
void shouldReturnSameHashCodeSameFragmentData() {
33+
34+
FragmentData fragmentDataExampleOne = new FragmentData("identifierExampleOne","identifierExampleOne", "identifierExampleOne");
35+
FragmentData fragmentDataExampleTwo = new FragmentData("identifierExampleOne","identifierExampleOne", "identifierExampleOne");
36+
37+
boolean compareHshCodeOneAndTwo = fragmentDataExampleOne.hashCode() ==fragmentDataExampleTwo.hashCode() ;
38+
39+
assertTrue(compareHshCodeOneAndTwo);
40+
41+
}
42+
43+
944
void shouldReturnBooleanWhenTestEquals() {
1045
FragmentData fragmentData = new FragmentData("identifier", "agencyId", "version");
1146
FragmentData mockFragmentDataOne = new FragmentData("identifier", "agencyId", "version");

src/test/java/fr/insee/rmes/utils/FilesUtilsTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,37 @@
11
package fr.insee.rmes.utils;
22

33
import org.junit.jupiter.api.Test;
4+
import java.util.ArrayList;
5+
import java.util.List;
46
import org.junit.jupiter.params.ParameterizedTest;
57
import org.junit.jupiter.params.provider.ValueSource;
68
import static org.junit.jupiter.api.Assertions.*;
79

810
class FilesUtilsTest {
911

12+
@Test
13+
void shouldReduceFileNameSize() {
14+
String fileName = "DDI_ACCESS_SERVICES_PROJECT.java";
15+
int maxLength = 10;
16+
String response = FilesUtils.reduceFileNameSize(fileName,maxLength);
17+
assertEquals("DDI_ACCESS",response);
18+
19+
}
20+
21+
@Test
22+
void removeAsciiCharacters() {
23+
List<String> fileNames = List.of("nœud","nŒud","ÀöBÎCčDžpEFsàGíHáIýJdK","Ddi access-SERVICE!?");
24+
List<String> actual = new ArrayList<>();
25+
26+
for (String element : fileNames){
27+
actual.add(FilesUtils.removeAsciiCharacters(element));
28+
}
29+
30+
List<String> expected = List.of("noeud", "nOEud", "AoBICcDzpEFsaGiHaIyJdK", "Ddi accessSERVICE");
31+
assertEquals(expected,actual);
32+
33+
}
34+
1035
@ParameterizedTest
1136
@ValueSource(ints = { 0,1,2,3,4,5,6,7,8 })
1237
void shouldReduceFileNameSize(int maxLength) {
Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,52 @@
11
package fr.insee.rmes.utils;
22

33
import org.junit.jupiter.api.Test;
4+
import org.springframework.http.ContentDisposition;
5+
import org.springframework.http.HttpHeaders;
6+
import org.springframework.http.MediaType;
47
import org.xmlunit.builder.DiffBuilder;
58
import org.xmlunit.builder.Input;
6-
79
import java.io.IOException;
10+
import java.util.Objects;
811

912
import static org.junit.jupiter.api.Assertions.*;
1013

1114
class HttpUtilsTest {
1215

16+
1317
@Test
1418
void filterBOM() throws IOException {
15-
var filteredContentWithBom=HttpUtils.filterBOM(HttpUtilsTest.class.getResourceAsStream("/utf8-bom/fichierAvecBom.xml").readAllBytes());
16-
var filteredContentWithoutBom=HttpUtils.filterBOM(HttpUtilsTest.class.getResourceAsStream("/utf8-bom/fichierSansBom.xml").readAllBytes());
17-
var diff=DiffBuilder.compare(Input.fromString(filteredContentWithBom))
19+
var filteredContentWithBom = HttpUtils.filterBOM(HttpUtilsTest.class.getResourceAsStream("/utf8-bom/fichierAvecBom.xml").readAllBytes());
20+
var filteredContentWithoutBom = HttpUtils.filterBOM(HttpUtilsTest.class.getResourceAsStream("/utf8-bom/fichierSansBom.xml").readAllBytes());
21+
var diff = DiffBuilder.compare(Input.fromString(filteredContentWithBom))
1822
.withTest(Input.fromString(filteredContentWithoutBom))
1923
.ignoreWhitespace()
2024
.build();
2125
assertFalse(diff.hasDifferences(), diff.toString());
2226
}
27+
28+
29+
@Test
30+
void shouldTestIfObjectsHaveBom() throws IOException {
31+
byte[] filteredContentWithBom = HttpUtilsTest.class.getResourceAsStream("/utf8-bom/fichierAvecBom.xml").readAllBytes();
32+
byte[] filteredContentWithoutBom = HttpUtilsTest.class.getResourceAsStream("/utf8-bom/fichierSansBom.xml").readAllBytes();
33+
boolean hasBomFilteredContentWithBom = filteredContentWithBom.length > 2 && filteredContentWithBom[0] == (byte) 0xEF && filteredContentWithBom[1] == (byte) 0xBB && filteredContentWithBom[2] == (byte) 0xBF;
34+
boolean hasBomFilteredContentWithoutBom = filteredContentWithoutBom.length > 2 && filteredContentWithoutBom[0] == (byte) 0xEF && filteredContentWithoutBom[1] == (byte) 0xBB && filteredContentWithoutBom[2] == (byte) 0xBF;
35+
assertTrue(hasBomFilteredContentWithBom && !hasBomFilteredContentWithoutBom);
36+
}
37+
38+
@Test
39+
void shouldGenerateHttpHeadersWithCorrectsComponents(){
40+
HttpHeaders httpHeaders = HttpUtils.generateHttpHeaders("MyProject", FileExtension.ODT_EXTENSION, 32);
41+
42+
MediaType mediaTypeExpected = new MediaType("application", "vnd.oasis.opendocument.text");
43+
String accessControlExposeHeadersExpected ="[Content-Disposition, X-Missing-Documents, Access-Control-Allow-Origin, Access-Control-Allow-Credentials]";
44+
ContentDisposition contentDispositionExpected = ContentDisposition.builder(HttpUtils.ATTACHMENT).filename(FilesUtils.reduceFileNameSize(FilesUtils.removeAsciiCharacters("MyProject"), 32)).build();
45+
46+
boolean testMediaType = Objects.equals(mediaTypeExpected,httpHeaders.getContentType());
47+
boolean testAccessControlExposeHeaders = Objects.equals(accessControlExposeHeadersExpected,httpHeaders.getAccessControlExposeHeaders().toString());
48+
boolean testContentDisposition = Objects.equals(contentDispositionExpected,httpHeaders.getContentDisposition());
49+
assertTrue(testMediaType && testAccessControlExposeHeaders && testContentDisposition);
50+
51+
}
2352
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package fr.insee.rmes.utils.export;
2+
3+
import org.junit.jupiter.api.Test;
4+
import javax.xml.transform.Source;
5+
import javax.xml.transform.TransformerException;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
import static org.junit.jupiter.api.Assertions.*;
10+
11+
class ClasspathUriResolverTest {
12+
13+
@Test
14+
void shouldReturnNotNullSources() throws TransformerException {
15+
ClasspathUriResolver classpathUriResolver = new ClasspathUriResolver();
16+
List<Source> sources = new ArrayList<>();
17+
sources.add(classpathUriResolver.resolve("../..test","anyString"));
18+
sources.add(classpathUriResolver.resolve("..test","anyString"));
19+
sources.add(classpathUriResolver.resolve("test","anyString"));
20+
assertTrue(sources.getFirst()!=null && sources.get(1)!=null && sources.get(2)!=null);
21+
}
22+
}

0 commit comments

Comments
 (0)