Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 94 #107

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
733 changes: 370 additions & 363 deletions src/main/java/org/olf/folio/order/OrderImport.java

Large diffs are not rendered by default.

32 changes: 18 additions & 14 deletions src/main/java/org/olf/folio/order/util/MarcUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.marc4j.MarcException;
import org.marc4j.MarcJsonWriter;
import org.marc4j.marc.DataField;
import org.marc4j.marc.MarcFactory;
Expand Down Expand Up @@ -76,19 +77,22 @@ public String getTitle(DataField twoFourFive) {
* @param nineEightyOne
* @return
*/
public String getPrice(DataField nineEightyOne) {
public String getPrice(DataField nineEightyOne) throws Exception {
String price = new String();
if (nineEightyOne != null) {
price = nineEightyOne.getSubfieldsAsString(PRICE);
if (price == null) {
price = "0.00";
throw new MarcException("Price missing from NineEightyOne field");
} else {
return normalizePrice(price);
try {
return normalizePrice(price);
} catch (Exception e) {
throw e;
}
}
} else {
price = "0.00";
}
return price;
throw new MarcException("price (981$i) field missing");
}
}

public String getQuantity(DataField nineEighty ) {
Expand Down Expand Up @@ -432,14 +436,14 @@ public List<String> getSeriesFields(Record record) {
return seriesFields;
}

public String normalizePrice(String priceStr) {
try {
double f = Double.parseDouble(priceStr);
return String.format("%.2f", new BigDecimal(f));
} catch (NumberFormatException e) {
return "0.00";
}
}
public String normalizePrice(String priceStr) throws NumberFormatException {
try {
double f = Double.parseDouble(priceStr);
return String.format("%.2f", new BigDecimal(f));
} catch (NumberFormatException e) {
throw new NumberFormatException("Price format is invalid. price is: "+ priceStr);
}
}

public String matchYear(String pubDate) {
String year = new String();
Expand Down
69 changes: 49 additions & 20 deletions src/test/java/org/olf/folio/order/services/GetBudgetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.FileOutputStream;
import java.io.PrintStream;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Ignore;
import org.junit.jupiter.api.Test;

public class GetBudgetTest extends ApiBaseTest {


boolean debug = false;
PrintStream stdout = System.out;

public GetBudgetTest() {
// TODO Auto-generated constructor stub
}
Expand All @@ -26,15 +31,27 @@ public void nullTest() {
@Test
public void testGetAllBudget() {

String budgetEndpoint = getBaseOkapEndpoint() + "finance/budgets?limit=1";
String budgetEndpoint = getBaseOkapEndpoint() + "finance/budgets?limit=1000";
//System.out.println("endpoint: "+ budgetEndpoint);
try {

String budgetResponse = getApiService().callApiGet(budgetEndpoint, getToken());
JSONObject budgetsObject = new JSONObject(budgetResponse);
//System.out.println(budgetsObject.toString(3));

JSONArray budgetsArray = budgetsObject.getJSONArray("budgets");
JSONArray budgetsArray = budgetsObject.getJSONArray("budgets");
if (debug) {
System.setOut(new PrintStream(new FileOutputStream("/cul/src/order-import-poc/output.json")));
//System.out.println(budgetsObject.toString(3));
for (int i = 0; i < budgetsArray.length(); i++) {
JSONObject fundObj = (JSONObject) budgetsArray.get(i);

System.out.println("name: " + fundObj.get("name"));
//System.out.println("id: " + fundObj.get("id"));
System.out.println("available: " + fundObj.get("available"));
System.out.println();
}
System.setOut(stdout);
}

assertNotNull(budgetsArray);
assertTrue(budgetsArray.length() > 0);

Expand All @@ -45,19 +62,25 @@ public void testGetAllBudget() {

@Test
public void testGetBudget() {
String fundCode = "p6793";
String badFundCode = "bad";
String fiscalYearCode = "FY2021";
String fundCode = "6945";
String badFundCode = "p2651";
String fiscalYearCode = "FY2023";
String budgetEndpoint = getBaseOkapEndpoint() + "finance/budgets?query=(name==" + fundCode + "-" + fiscalYearCode + ")";
//System.out.println("endpoint: "+ budgetEndpoint);

try {
String budgetResponse = getApiService().callApiGet(budgetEndpoint, getToken());
JSONObject budgetsObject = new JSONObject(budgetResponse);
assertNotNull(budgetsObject);
int totalRecords = (Integer) budgetsObject.get("totalRecords");
assertNotNull(totalRecords);
assertEquals(totalRecords, 1);
//System.out.println(budgetsObject.toString(3));
if (debug) {
System.out.println("endpoint: "+ budgetEndpoint);
System.out.println("fundcode: "+ fundCode);
System.out.println(budgetsObject.toString(3));
} else {
assertNotNull(budgetsObject);
int totalRecords = (Integer) budgetsObject.get("totalRecords");
assertNotNull(totalRecords);
assertEquals(totalRecords, 1);
//System.out.println(budgetsObject.toString(3));
}
} catch (Exception e) {
fail(e.getMessage());
}
Expand All @@ -66,14 +89,20 @@ public void testGetBudget() {
try {
String budgetResponse = getApiService().callApiGet(budgetEndpoint, getToken());
JSONObject budgetsObject = new JSONObject(budgetResponse);
assertNotNull(budgetsObject);
int totalRecords = (Integer) budgetsObject.get("totalRecords");
assertNotNull(totalRecords);
assertEquals(totalRecords, 0);
if (debug) {
System.out.println("endpoint: "+ budgetEndpoint);
System.out.println("bad fundcode: "+ badFundCode);
System.out.println(budgetsObject.toString(3));
} else {
assertNotNull(budgetsObject);
int totalRecords = (Integer) budgetsObject.get("totalRecords");
assertNotNull(totalRecords);
assertEquals(totalRecords, 1);
//System.out.println(budgetsObject.toString(3));
}
} catch (Exception e) {
fail(e.getMessage());
}
}

}

}
42 changes: 25 additions & 17 deletions src/test/java/org/olf/folio/order/services/GetFundCodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Ignore;
import org.junit.Ignore;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

public class GetFundCodeTest extends ApiBaseTest {

boolean debug = false;

public GetFundCodeTest() {
// TODO Auto-generated constructor stub
Expand All @@ -23,28 +25,32 @@ public void nullTest() {
//
}

@Test
@Disabled
public void testGetAllFundCode() {


String fundEndpoint = getBaseOkapEndpoint() + "finance/funds?limit=1";
String fundEndpoint = getBaseOkapEndpoint() + "finance/funds?limit=200";
try {

String fundResponse = getApiService().callApiGet(fundEndpoint, getToken());
JSONObject fundsObject = new JSONObject(fundResponse);
//System.out.println(fundsObject.toString(3));
JSONArray fundsArray = fundsObject.getJSONArray("funds");
assertNotNull(fundsArray);
assertEquals(fundsArray.length(), 1);
//for (int i=0; i < fundsArray.length(); i++) {
// JSONObject fundObj = (JSONObject) fundsArray.get(i);
// System.out.println("code: "+ fundObj.get("code"));
// System.out.println("name: "+ fundObj.get("name"));
// System.out.println("id: "+ fundObj.get("id"));
// System.out.println();
//}
String fundId = (String) fundsArray.getJSONObject(0).get("id");
assertNotNull(fundId);
if (debug) {
for (int i=0; i < fundsArray.length(); i++) {
JSONObject fundObj = (JSONObject) fundsArray.get(i);
System.out.println("code: "+ fundObj.get("code"));
System.out.println("name: "+ fundObj.get("name"));
System.out.println("id: "+ fundObj.get("id"));
System.out.println();
}
} else {
assertNotNull(fundsArray);
assertEquals(fundsArray.length(), 1);

String fundId = (String) fundsArray.getJSONObject(0).get("id");
assertNotNull(fundId);
}
} catch (Exception e) {
fail(e.getMessage());
}
Expand All @@ -53,14 +59,16 @@ public void testGetAllFundCode() {
@Test
public void testGetFundCode() {

String fundCode = "1010";
//String fundCode = "p2755";
//String fundCode = "1010";
String fundCode = "p2651";
String fundEndpoint = getBaseOkapEndpoint() + "finance/funds?limit=3&offset=0&query=((code='" + fundCode + "'))";
try {
String fundResponse = getApiService().callApiGet(fundEndpoint, getToken());
JSONObject fundsObject = new JSONObject(fundResponse);
JSONArray fundsArray = fundsObject.getJSONArray("funds");
//System.out.println(fundsObject.toString(3));
if (debug) {
System.out.println(fundsObject.toString(3));
}
String returnCode = (String) fundsArray.getJSONObject(0).get("code");
assertEquals(fundCode, returnCode);
} catch (Exception e) {
Expand Down
26 changes: 13 additions & 13 deletions src/test/java/org/olf/folio/order/services/GetHoldingsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

public class GetHoldingsTest extends ApiBaseTest {
public class GetHoldingsTest extends ApiBaseTest {

boolean debug = false;

@Ignore
public void nullTest() {
Expand All @@ -34,45 +36,43 @@ public void testGetHoldings() {
try {
String holdingsResponse = getApiService().callApiGet(holdingsEndpoint, getToken());
JSONObject holdingsObject = new JSONObject(holdingsResponse);
//System.out.println(holdingsObject.toString(3));
if (debug) System.out.println(holdingsObject.toString(3));
JSONObject holdingsAsJson = new JSONObject(holdingsResponse);


JSONArray holdingsArray = holdingsAsJson.getJSONArray("holdingsRecords");
System.out.println("holdingsArray size: "+ holdingsArray.length());
if (debug) System.out.println("holdingsArray size: "+ holdingsArray.length());

Iterator holdingsIter = holdingsArray.iterator();
while (holdingsIter.hasNext() ) {
JSONObject holdingsRecord = (JSONObject) holdingsIter.next();
String holdingsId = holdingsRecord.getString("id");
System.out.println("holdingsId: "+ holdingsId);
if (debug) System.out.println("holdingsId: "+ holdingsId);

String queryString = "holdingsRecordId==" +holdingsId+ " not barcode=\"\"";
String encodedQS = URLEncoder.encode(queryString, StandardCharsets.UTF_8.name());
String itemsEndpoint = getBaseOkapEndpoint() + "inventory/items?query=(" + encodedQS + ")";
System.out.println("itemsEndpoint: "+ itemsEndpoint);
if (debug) System.out.println("itemsEndpoint: "+ itemsEndpoint);
String itemsResponse = getApiService().callApiGet(itemsEndpoint, getToken());
//System.out.println("itemsObject");
JSONObject itemsObject = new JSONObject(itemsResponse);
//System.out.println(itemsObject.toString(3));

JSONArray itemsArray = itemsObject.getJSONArray("items");
System.out.println("number of items: "+itemsArray.length());
if (debug) System.out.println("number of items: "+itemsArray.length());
Iterator itemsIter = itemsArray.iterator();
while (itemsIter.hasNext()) {
JSONObject itemRecord = (JSONObject) itemsIter.next();

String itemId = itemRecord.getString("id");
System.out.println("item record: "+ itemId);
System.out.println(itemRecord.toString(3));
if (debug) {
System.out.println("item record: "+ itemId);
System.out.println(itemRecord.toString(3));
}

}


}



}

} catch (Exception e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import java.nio.charset.StandardCharsets;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Ignore;
import org.junit.Ignore;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -19,6 +20,7 @@

public class GetOrganizationsTest extends ApiBaseTest {

boolean debug = false;

public GetOrganizationsTest() {
// TODO Auto-generated constructor stub
Expand All @@ -37,9 +39,12 @@ public void testGetAllOrganizations() {
String orgLookupResponse = getApiService().callApiGet(organizationEndpoint, getToken());
JSONObject orgObject = new JSONObject(orgLookupResponse);
JSONArray jsonArray = orgObject.getJSONArray("organizations");
assertNotNull(jsonArray);
assertTrue(jsonArray.length() > 0);

if (debug) {
System.out.println(orgObject.toString(3));
} else {
assertNotNull(jsonArray);
assertTrue(jsonArray.length() > 0);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Expand All @@ -48,21 +53,25 @@ public void testGetAllOrganizations() {

@Test
public void testGetOrganization() {
String orgCode = "HARRASS/E";
String orgCode = "AFRICARES";

try {
String encodedOrgCode = URLEncoder.encode("\"" + orgCode + "\"", StandardCharsets.UTF_8.name());

String organizationEndpoint = getBaseOkapEndpoint() + "organizations-storage/organizations?query=(code==" + encodedOrgCode + ")";
try {
String orgLookupResponse = getApiService().callApiGet(organizationEndpoint, getToken());
JSONObject orgObject = new JSONObject(orgLookupResponse);
JSONArray jsonArray = orgObject.getJSONArray("organizations");
assertNotNull(jsonArray);
String vendorId = (String) jsonArray.getJSONObject(0).get("id");
assertTrue(StringUtils.length(vendorId) > 0);
UUID uuid = UUID.fromString(vendorId);
//System.out.println("vendorId: "+ vendorId);
JSONObject orgObject = new JSONObject(orgLookupResponse);
JSONArray jsonArray = orgObject.getJSONArray("organizations");
if (debug) {
System.out.println(orgObject.toString(3));
} else {
assertNotNull(jsonArray);
String vendorId = (String) jsonArray.getJSONObject(0).get("id");
assertTrue(StringUtils.length(vendorId) > 0);
UUID uuid = UUID.fromString(vendorId);
//System.out.println("vendorId: "+ vendorId);
}
} catch (IllegalArgumentException e) {
fail("vendorID was not a valid UUID");
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void getMaterialsTest() {
System.out.println("id: "+ map.get(key));
}
} else {
String testKey = "Music";
String testKey = "Book";
assertNotNull(map);
assertTrue(map.containsKey(testKey));
assertNotNull(map.get(testKey));
Expand Down
Loading