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

Add the ability update and set tracking codes #766

Merged
merged 13 commits into from
May 19, 2020
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## Next Release
- Add support for setting Tracking Codes ([#766](https://github.com/box/box-java-sdk/pull/766))
- Fix issue for `getIsExternallyOwned()` for Files and Folders ([#808](https://github.com/box/box-java-sdk/pull/808))

## 2.47.0 [2020-04-23]
Expand Down
29 changes: 28 additions & 1 deletion src/main/java/com/box/sdk/BoxUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -1059,12 +1059,39 @@ public String getHostname() {

/**
* Gets the tracking defined for each entity.
* @return a Map with traking codes.
* @return a Map with tracking codes.
*/
public Map<String, String> getTrackingCodes() {
return this.trackingCodes;
}

/**
* Allows admin to set attributes specific for a group of users.
* @param trackingCodes a Map representing the user's new tracking codes
*/
public void setTrackingCodes(Map<String, String> trackingCodes) {
this.trackingCodes = trackingCodes;
this.addPendingChange("tracking_codes", this.trackingCodesJson());
}

/**
* Allows the admin to append new tracking codes to the previous existing list.
* @param name the name or `key` of the attribute to set.
* @param value the value of the attribute to set.
*/
public void appendTrackingCodes(String name, String value) {
this.getTrackingCodes().put(name, value);
this.addPendingChange("tracking_codes", this.trackingCodesJson());
}

private JsonObject trackingCodesJson() {
PJSimon marked this conversation as resolved.
Show resolved Hide resolved
JsonObject trackingCodesJson = new JsonObject();
for (String attrKey : this.trackingCodes.keySet()) {
trackingCodesJson.set(attrKey, this.trackingCodes.get(attrKey));
}
return trackingCodesJson;
}

@Override
protected void parseJSONMember(JsonObject.Member member) {
super.parseJSONMember(member);
Expand Down
16 changes: 16 additions & 0 deletions src/test/Fixtures/BoxUser/CreateTrackingCodes200.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"type": "user",
"id": "12345",
"tracking_codes": [
{
"type": "tracking_code",
"name": "Employee ID",
"value": "12345"
},
{
"type": "tracking_code",
"name": "Department ID",
"value": "8675"
}
]
}
21 changes: 21 additions & 0 deletions src/test/Fixtures/BoxUser/GetUserThreeTrackingCodes200.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"type": "user",
"id": "12345",
"tracking_codes": [
{
"type": "tracking_code",
"name": "Employee ID",
"value": "12345"
},
{
"type": "tracking_code",
"name": "Department ID",
"value": "8675"
},
{
"type": "tracking_code",
"name": "Company ID",
"value": "1701"
}
]
}
16 changes: 16 additions & 0 deletions src/test/Fixtures/BoxUser/GetUserTwoTrackingCodes200.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"type": "user",
"id": "12345",
"tracking_codes": [
{
"type": "tracking_code",
"name": "Employee ID",
"value": "12345"
},
{
"type": "tracking_code",
"name": "Department ID",
"value": "8675"
}
]
}
21 changes: 21 additions & 0 deletions src/test/Fixtures/BoxUser/UpdateTrackingCodes200.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"type": "user",
"id": "12345",
"tracking_codes": [
{
"type": "tracking_code",
"name": "Employee ID",
"value": "12345"
},
{
"type": "tracking_code",
"name": "Department ID",
"value": "8675"
},
{
"type": "tracking_code",
"name": "Company ID",
"value": "1701"
}
]
}
95 changes: 94 additions & 1 deletion src/test/java/com/box/sdk/BoxUserTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.box.sdk;

import com.github.tomakehurst.wiremock.stubbing.Scenario;
import java.io.*;
import java.nio.file.Files;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

import static org.hamcrest.Matchers.equalTo;
Expand All @@ -15,6 +17,7 @@
import static org.junit.Assert.assertThat;

import com.github.tomakehurst.wiremock.junit.WireMockClassRule;
import java.util.Map;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.ClassRule;
Expand Down Expand Up @@ -296,7 +299,7 @@ public void testDeleteUserSucceeds() throws IOException {

@Test
@Category(UnitTest.class)
public void testGreateEmailAliasSucceeds() throws IOException {
public void testCreateEmailAliasSucceeds() throws IOException {
String result = "";
final String userID = "12345";
final String emailAliasURL = "/users/" + userID + "/email_aliases";
Expand Down Expand Up @@ -477,4 +480,94 @@ public void testDeserializationException() throws IOException {
BoxUser.Info userInfo = new BoxUser(this.api, userID).getInfo();
Assert.assertEquals("12345", userInfo.getID());
}

@Test
@Category(UnitTest.class)
public void testCreateReadAddTrackingCodesSucceeds() throws IOException {
final String userID = "12345";
final String departmentID = "8675";
final String companyID = "1701";
final String usersURL = "/users/" + userID;

// Mock: Create two tracking codes
Map<String, String> createTrackingCodes = new HashMap<String, String>();
createTrackingCodes.put("Employee ID", userID);
createTrackingCodes.put("Department ID", departmentID);
String createBody = this.trackingCodesJson(createTrackingCodes).toString();
String createResponse = TestConfig.getFixture("BoxUser/CreateTrackingCodes200");
WIRE_MOCK_CLASS_RULE.stubFor(WireMock.put(WireMock.urlPathEqualTo(usersURL))
.withRequestBody(WireMock.equalToJson(createBody))
.willReturn(WireMock.aResponse()
.withHeader("Content-Type", "application/json")
.withBody(createResponse)));

// Mock: Verify change
String twoTrackingCodesResponse = TestConfig.getFixture("BoxUser/GetUserTwoTrackingCodes200");
WIRE_MOCK_CLASS_RULE.stubFor(WireMock.get(WireMock.urlPathEqualTo(usersURL))
.withQueryParam("fields", WireMock.equalTo("tracking_codes"))
.inScenario("Get Tracking Code Scenario")
.whenScenarioStateIs(Scenario.STARTED)
.willReturn(WireMock.aResponse()
.withHeader("Content-Type", "application/json")
.withBody(twoTrackingCodesResponse))
.willSetStateTo("1st Request"));

// Mock: Add one more tracking code
Map<String, String> appendTrackingCodes = new HashMap<String, String>();
appendTrackingCodes.put("Employee ID", userID);
appendTrackingCodes.put("Department ID", departmentID);
appendTrackingCodes.put("Company ID", companyID);
String updateBody = this.trackingCodesJson(appendTrackingCodes).toString();
String updateResponse = TestConfig.getFixture("BoxUser/UpdateTrackingCodes200");
WIRE_MOCK_CLASS_RULE.stubFor(WireMock.put(WireMock.urlPathEqualTo(usersURL))
.withRequestBody(WireMock.equalToJson(updateBody))
.willReturn(WireMock.aResponse()
.withHeader("Content-Type", "application/json")
.withBody(updateResponse)));

// Mock: Verify change
String threeTrackingCodesResponse = TestConfig.getFixture("BoxUser/GetUserThreeTrackingCodes200");
WIRE_MOCK_CLASS_RULE.stubFor(WireMock.get(WireMock.urlPathEqualTo(usersURL))
.withQueryParam("fields", WireMock.equalTo("tracking_codes"))
.inScenario("Get Tracking Code Scenario")
.whenScenarioStateIs("1st Request")
.willReturn(WireMock.aResponse()
.withHeader("Content-Type", "application/json")
.withBody(threeTrackingCodesResponse))
.willSetStateTo("2nd Request"));

// Create two tracking codes
BoxUser user = new BoxUser(this.api, userID);
BoxUser.Info info = user.new Info();
info.setTrackingCodes(createTrackingCodes);
user.updateInfo(info);

// Verify change
user = new BoxUser(this.api, userID);
info = user.getInfo("tracking_codes");
Map<String, String> receivedTrackingCodes = info.getTrackingCodes();
Assert.assertEquals(createTrackingCodes, receivedTrackingCodes);

// Add one more tracking code
info.appendTrackingCodes("Company ID", companyID);
user.updateInfo(info);

// Verify change
user = new BoxUser(this.api, userID);
info = user.getInfo("tracking_codes");
receivedTrackingCodes = info.getTrackingCodes();
Assert.assertEquals(appendTrackingCodes, receivedTrackingCodes);
}

private JsonObject trackingCodesJson(Map<String, String> trackingCodes) {
PJSimon marked this conversation as resolved.
Show resolved Hide resolved
JsonObject trackingCodesJsonValue = new JsonObject();
for (String attrKey : trackingCodes.keySet()) {
trackingCodesJsonValue.set(attrKey, trackingCodes.get(attrKey));
}

JsonObject trackingCodesJson = new JsonObject();
trackingCodesJson.set("tracking_codes", trackingCodesJsonValue);

return trackingCodesJson;
}
}