Skip to content

Commit 6c0c3b7

Browse files
authored
Merge pull request #11378 from lubitchv/11377-empty-frequency
EditDDI API empty frequency bug
2 parents 057c3ee + 73d7abc commit 6c0c3b7

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

src/main/java/edu/harvard/iq/dataverse/api/EditDDI.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,16 @@ public Response edit(@Context ContainerRequestContext crc, InputStream body, @Pa
110110
Map<Long, VariableMetadata> mapVarToVarMet = new HashMap<Long, VariableMetadata>();
111111
Map<Long,VarGroup> varGroupMap = new HashMap<Long, VarGroup>();
112112
try {
113-
readXML(body, mapVarToVarMet,varGroupMap);
113+
readXML(body, mapVarToVarMet, varGroupMap);
114+
} catch (NullPointerException e) {
115+
return error(Response.Status.BAD_REQUEST,e.getMessage() );
114116
} catch (XMLStreamException e) {
115117
logger.warning(e.getMessage());
116-
return error(Response.Status.NOT_ACCEPTABLE, "bad xml file" );
118+
return error(Response.Status.BAD_REQUEST, "bad xml file");
117119
}
118120

121+
122+
119123
DatasetVersion latestVersion = dataFile.getOwner().getLatestVersion();
120124
Dataset dataset = latestVersion.getDataset();
121125

@@ -349,12 +353,14 @@ private boolean updateDraftVersion(ArrayList<VariableMetadata> neededToUpdateVM,
349353
return true;
350354
}
351355

352-
private void readXML(InputStream body, Map<Long,VariableMetadata> mapVarToVarMet, Map<Long,VarGroup> varGroupMap) throws XMLStreamException {
356+
private void readXML(InputStream body, Map<Long,VariableMetadata> mapVarToVarMet, Map<Long,VarGroup> varGroupMap) throws XMLStreamException, NullPointerException {
357+
353358
XMLInputFactory factory=XMLInputFactory.newInstance();
354359
XMLStreamReader xmlr=factory.createXMLStreamReader(body);
360+
355361
VariableMetadataDDIParser vmdp = new VariableMetadataDDIParser();
356362

357-
vmdp.processDataDscr(xmlr,mapVarToVarMet, varGroupMap);
363+
vmdp.processDataDscr(xmlr, mapVarToVarMet, varGroupMap);
358364

359365
}
360366

src/main/java/edu/harvard/iq/dataverse/datavariable/VariableMetadataDDIParser.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,13 +229,21 @@ private CategoryMetadata processCatStat(XMLStreamReader xmlr, VariableCategory c
229229
String _freq = parseText(xmlr);
230230
if (_freq != null && !_freq.isEmpty()) {
231231
cat.setFrequency(new Double(_freq));
232+
} else {
233+
//If frequency is declared it cannot be missing
234+
// throw exception
235+
throw new NullPointerException("Frequency is declared but missing");
232236
}
233237
} else if (wgtd != null && type != null && CAT_STAT_TYPE_FREQUENCY.equalsIgnoreCase(type) &&
234238
CAT_STAT_WGTD_FREQUENCY.equalsIgnoreCase(wgtd)) {
235239
cm = new CategoryMetadata();
236240
String wfreq = parseText(xmlr);
237241
if (wfreq != null && !wfreq.isEmpty()) {
238242
cm.setWfreq(new Double(wfreq));
243+
} else {
244+
//if weighted frequency is declared it cannot be empty
245+
//through exception
246+
throw new NullPointerException("Weighted Frequency is declared but missing");
239247
}
240248
}
241249
return cm;

src/test/java/edu/harvard/iq/dataverse/api/EditDDIIT.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,18 @@ public void testUpdateVariableMetadata() throws InterruptedException {
156156
Response editDDINotAuthResponse = UtilIT.editDDI(updatedContent, origFileId, null);
157157
assertEquals(401, editDDINotAuthResponse.getStatusCode());
158158

159+
//bad request
160+
String updatedContentBadFreq = updatedContent.replace("<catStat type=\"freq\">0</catStat>",
161+
"<catStat type=\"freq\"/>");
162+
Response editDDIBadRequestFreq = UtilIT.editDDI(updatedContentBadFreq, origFileId, apiToken);
163+
assertEquals(400, editDDIBadRequestFreq.getStatusCode());
164+
165+
//bad request
166+
String updatedContentBadWFreq = updatedContent.replace("<catStat wgtd=\"wgtd\" type=\"freq\">0</catStat>",
167+
"<catStat wgtd=\"wgtd\" type=\"freq\"></catStat>");
168+
Response editDDIBadRequestWFreq = UtilIT.editDDI(updatedContentBadWFreq, origFileId, apiToken);
169+
assertEquals(400, editDDIBadRequestWFreq.getStatusCode());
170+
159171

160172
//cleanup
161173
Response makeSuperUser = UtilIT.makeSuperUser(username);

0 commit comments

Comments
 (0)