Skip to content

Commit 935b9f7

Browse files
thewheatchoran
authored andcommitted
Add tests for companies (#215)
1 parent 672d576 commit 935b9f7

File tree

3 files changed

+298
-0
lines changed

3 files changed

+298
-0
lines changed
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
package io.intercom.api;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.google.common.collect.Maps;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
import static io.intercom.api.TestSupport.load;
12+
import static org.junit.Assert.*;
13+
14+
public class CompanyTest {
15+
private static ObjectMapper mapper;
16+
17+
@BeforeClass
18+
public static void beforeClass() {
19+
mapper = MapperSupport.objectMapper();
20+
}
21+
22+
@Test
23+
public void TestCustomAttributes() throws Exception {
24+
final Map<String, CustomAttribute> customAttributes = Maps.newHashMap();
25+
final CustomAttribute booleanAttribute = CustomAttribute.newBooleanAttribute("a_boolean", false);
26+
final CustomAttribute doubleAttribute = CustomAttribute.newDoubleAttribute("a_double", 2.34);
27+
final CustomAttribute floatAttribute = CustomAttribute.newFloatAttribute("a_float", 1.2f);
28+
final CustomAttribute integerAttribute = CustomAttribute.newIntegerAttribute("an_integer", 123);
29+
final CustomAttribute longAttribute = CustomAttribute.newLongAttribute("a_long", 1517833441L);
30+
final CustomAttribute stringAttribute = CustomAttribute.newStringAttribute("a_string", "a string");
31+
customAttributes.put(booleanAttribute.getName(), booleanAttribute);
32+
customAttributes.put(doubleAttribute.getName(), doubleAttribute);
33+
customAttributes.put(floatAttribute.getName(), floatAttribute);
34+
customAttributes.put(integerAttribute.getName(), integerAttribute);
35+
customAttributes.put(longAttribute.getName(), longAttribute);
36+
customAttributes.put(stringAttribute.getName(), stringAttribute);
37+
final Company company = new Company()
38+
.setCustomAttributes(customAttributes);
39+
40+
final Company companyAddAttributeIndividually = new Company();
41+
companyAddAttributeIndividually.addCustomAttribute(booleanAttribute);
42+
companyAddAttributeIndividually.addCustomAttribute(doubleAttribute);
43+
companyAddAttributeIndividually.addCustomAttribute(floatAttribute);
44+
companyAddAttributeIndividually.addCustomAttribute(integerAttribute);
45+
companyAddAttributeIndividually.addCustomAttribute(longAttribute);
46+
companyAddAttributeIndividually.addCustomAttribute(stringAttribute);
47+
48+
assertEquals(company.getCustomAttributes().get("a_boolean"), companyAddAttributeIndividually.getCustomAttributes().get("a_boolean"));
49+
assertEquals(company.getCustomAttributes().get("a_double"), companyAddAttributeIndividually.getCustomAttributes().get("a_double"));
50+
assertEquals(company.getCustomAttributes().get("a_float"), companyAddAttributeIndividually.getCustomAttributes().get("a_float"));
51+
assertEquals(company.getCustomAttributes().get("an_integer"), companyAddAttributeIndividually.getCustomAttributes().get("an_integer"));
52+
assertEquals(company.getCustomAttributes().get("a_long"), companyAddAttributeIndividually.getCustomAttributes().get("a_long"));
53+
assertEquals(company.getCustomAttributes().get("a_string"), companyAddAttributeIndividually.getCustomAttributes().get("a_string"));
54+
}
55+
@Test
56+
public void TestPrepareUpdatableCompany() throws Exception {
57+
final long now = System.currentTimeMillis() / 1000;
58+
final CustomAttribute booleanAttribute = CustomAttribute.newBooleanAttribute("a_boolean", false);
59+
final CustomAttribute doubleAttribute = CustomAttribute.newDoubleAttribute("a_double", 2.34);
60+
final CustomAttribute floatAttribute = CustomAttribute.newFloatAttribute("a_float", 1.2f);
61+
final CustomAttribute integerAttribute = CustomAttribute.newIntegerAttribute("an_integer", 123);
62+
final CustomAttribute longAttribute = CustomAttribute.newLongAttribute("a_long", 1517833441);
63+
final CustomAttribute stringAttribute = CustomAttribute.newStringAttribute("a_string", "a string");
64+
final Company.Plan plan = new Company.Plan("my plan");
65+
final Company company = new Company()
66+
.setCompanyID("my_company_id")
67+
.addCustomAttribute(booleanAttribute)
68+
.addCustomAttribute(doubleAttribute)
69+
.addCustomAttribute(floatAttribute)
70+
.addCustomAttribute(integerAttribute)
71+
.addCustomAttribute(longAttribute)
72+
.addCustomAttribute(stringAttribute)
73+
.setId("1")
74+
.setIndustry("the industry")
75+
.setMonthlySpend(123)
76+
.setName("company name")
77+
.setPlan(plan)
78+
.setRemoteCreatedAt(now)
79+
.setSize(432)
80+
.setWebsite("https://intercom.com");
81+
82+
83+
final CompanyCollection companyCollection = new CompanyCollection();
84+
companyCollection.addCompany(company);
85+
final List<CompanyWithStringPlan> companyUpdateList = CompanyUpdateBuilder.buildUserUpdateCompanies(companyCollection, null);
86+
final CompanyWithStringPlan companyUpdate = companyUpdateList.get(0);
87+
assertEquals("my_company_id", companyUpdate.getCompanyID());
88+
assertEquals(6, companyUpdate.getCustomAttributes().size());
89+
assertEquals(false, companyUpdate.getCustomAttributes().get("a_boolean").getValue());
90+
assertEquals(2.34, companyUpdate.getCustomAttributes().get("a_double").getValue());
91+
assertEquals(1.2f, companyUpdate.getCustomAttributes().get("a_float").getValue());
92+
assertEquals(123, companyUpdate.getCustomAttributes().get("an_integer").getValue());
93+
assertEquals(1517833441L, companyUpdate.getCustomAttributes().get("a_long").getValue());
94+
assertEquals("a string", companyUpdate.getCustomAttributes().get("a_string").getValue());
95+
assertEquals("1", companyUpdate.getId());
96+
assertEquals("the industry", companyUpdate.getIndustry());
97+
assertEquals(123, companyUpdate.getMonthlySpend(), 0f);
98+
assertEquals("company name", companyUpdate.getName());
99+
assertEquals("my plan", companyUpdate.getPlan());
100+
assertEquals(now, companyUpdate.getRemoteCreatedAt());
101+
assertEquals(432, companyUpdate.getSize());
102+
assertEquals("https://intercom.com", companyUpdate.getWebsite());
103+
}
104+
105+
106+
@Test
107+
public void TestReadJsonResponse() throws Exception {
108+
String json = load("company.json");
109+
final Company company = mapper.readValue(json, Company.class);
110+
final TagCollection tagCollection = company.getTagCollection();
111+
final List<Tag> tags = tagCollection.getPage();
112+
final SegmentCollection segmentCollection = company.getSegmentCollection();
113+
final List<Segment> segments = segmentCollection.getPage();
114+
final Tag tag = tags.get(0);
115+
final Segment segment1 = segments.get(0);
116+
final Segment segment2= segments.get(1);
117+
final Company.Plan plan = company.getPlan();
118+
assertEquals("6", company.getCompanyID());
119+
assertEquals("5694b58371b5025a2a0003e6", company.getId());
120+
assertEquals("Blue Sun", company.getName());
121+
assertEquals(1452586372, company.getCreatedAt());
122+
assertEquals(49, company.getMonthlySpend(), 0f);
123+
assertEquals("plan", plan.getType());
124+
assertEquals("150154", plan.getId());
125+
assertEquals("plan1", plan.getName());
126+
assertEquals(1517835268, company.getUpdatedAt());
127+
assertEquals(1394531169, company.getRemoteCreatedAt());
128+
assertEquals(1496292261, company.getLastRequestAt());
129+
assertEquals(2, company.getSessionCount());
130+
assertEquals("Manufacturing", company.getIndustry());
131+
assertEquals("http://www.example.com", company.getWebsite());
132+
assertEquals(85, company.getSize());
133+
assertEquals("tag.list", tagCollection.getType());
134+
assertEquals("tag", tag.getType());
135+
assertEquals("1072802", tag.getId());
136+
assertEquals("CompanyTag1", tag.getName());
137+
assertEquals("segment.list", segmentCollection.getType());
138+
assertEquals(2, segments.size());
139+
assertEquals("segment", segment1.getType());
140+
assertEquals("58b7c061d5cf5649a59f7097", segment1.getId());
141+
assertEquals("segment", segment2.getType());
142+
assertEquals("593107f85950a28b168aa9d3", segment2.getId());
143+
assertEquals(new Integer(1), company.getUserCount());
144+
145+
final Map<String, CustomAttribute> customAttributes = company.getCustomAttributes();
146+
CustomAttribute customAttribute;
147+
148+
customAttribute = customAttributes.get("truthy_typed");
149+
assertEquals(Boolean.class, customAttribute.getValueClass());
150+
assertEquals(Boolean.TRUE, customAttribute.getValue());
151+
assertEquals(Boolean.TRUE, customAttribute.booleanValue());
152+
assertEquals("truthy_typed", customAttribute.getName());
153+
154+
customAttribute = customAttributes.get("floatly_typed");
155+
assertEquals(Double.class, customAttribute.getValueClass());
156+
assertEquals(155.5d, customAttribute.getValue());
157+
assertEquals(155.5d, customAttribute.doubleValue(), 0d);
158+
assertEquals("floatly_typed", customAttribute.getName());
159+
160+
customAttribute = customAttributes.get("stringly_typed");
161+
assertEquals(String.class, customAttribute.getValueClass());
162+
assertEquals("data", customAttribute.getValue());
163+
assertEquals("data", customAttribute.textValue());
164+
assertEquals("stringly_typed", customAttribute.getName());
165+
166+
customAttribute = customAttributes.get("dately_typed_at");
167+
assertEquals(Long.class, customAttribute.getValueClass());
168+
assertEquals(1397574667L, customAttribute.getValue());
169+
assertEquals(1397574667L, customAttribute.longValue());
170+
assertEquals("dately_typed_at", customAttribute.getName());
171+
172+
customAttribute = customAttributes.get("longly_typed");
173+
assertEquals(Long.class, customAttribute.getValueClass());
174+
assertEquals(221397574667L, customAttribute.getValue());
175+
assertEquals(221397574667L, customAttribute.longValue());
176+
assertEquals("longly_typed", customAttribute.getName());
177+
178+
customAttribute = customAttributes.get("intly_typed");
179+
assertEquals(Integer.class, customAttribute.getValueClass());
180+
assertEquals(1, customAttribute.getValue());
181+
assertEquals(1, customAttribute.integerValue());
182+
assertEquals("intly_typed", customAttribute.getName());
183+
184+
// null values are not ignored
185+
customAttribute = customAttributes.get("nully_typed");
186+
assertEquals(null, customAttribute);
187+
}
188+
189+
@Test
190+
public void TestReadJsonResponseNoCompanyData() throws Exception {
191+
String json = load("company_no_data_except_company_id.json");
192+
final Company company = mapper.readValue(json, Company.class);
193+
final TagCollection tagCollection = company.getTagCollection();
194+
final List<Tag> tags = tagCollection.getPage();
195+
final SegmentCollection segmentCollection = company.getSegmentCollection();
196+
final List<Segment> segments = segmentCollection.getPage();
197+
final Company.Plan plan = company.getPlan();
198+
assertEquals("6", company.getCompanyID());
199+
assertEquals("5694b58371b5025a2a0003e6", company.getId());
200+
assertEquals(null, company.getName());
201+
assertEquals(1452586372, company.getCreatedAt());
202+
assertEquals(0, company.getMonthlySpend(), 0f);
203+
assertEquals("plan", plan.getType());
204+
assertEquals(null, plan.getId());
205+
assertEquals(null, plan.getName());
206+
assertEquals(1517835268, company.getUpdatedAt());
207+
assertEquals(0, company.getRemoteCreatedAt());
208+
assertEquals(0, company.getLastRequestAt());
209+
assertEquals(0, company.getSessionCount());
210+
assertEquals(null, company.getIndustry());
211+
assertEquals(null, company.getWebsite());
212+
assertEquals(0, company.getSize());
213+
assertEquals("tag.list", tagCollection.getType());
214+
assertEquals(0, tags.size());
215+
assertEquals("segment.list", segmentCollection.getType());
216+
assertEquals(0, segments.size());
217+
assertEquals(new Integer(0), company.getUserCount());
218+
219+
final Map<String, CustomAttribute> customAttributes = company.getCustomAttributes();
220+
assertEquals(0, customAttributes.size());
221+
}
222+
223+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"type": "company",
3+
"company_id": "6",
4+
"id": "5694b58371b5025a2a0003e6",
5+
"app_id": "qji82zks",
6+
"name": "Blue Sun",
7+
"remote_created_at": 1394531169,
8+
"created_at": 1452586372,
9+
"updated_at": 1517835268,
10+
"last_request_at": 1496292261,
11+
"monthly_spend": 49,
12+
"session_count": 2,
13+
"user_count": 1,
14+
"size": 85,
15+
"website": "http://www.example.com",
16+
"industry": "Manufacturing",
17+
"tags": {
18+
"type": "tag.list",
19+
"tags": [
20+
{
21+
"type": "tag",
22+
"id": "1072802",
23+
"name": "CompanyTag1"
24+
}
25+
]
26+
},
27+
"segments": {
28+
"type": "segment.list",
29+
"segments": [
30+
{
31+
"type": "segment",
32+
"id": "58b7c061d5cf5649a59f7097"
33+
},
34+
{
35+
"type": "segment",
36+
"id": "593107f85950a28b168aa9d3"
37+
}
38+
]
39+
},
40+
"plan": {
41+
"type": "plan",
42+
"id": "150154",
43+
"name": "plan1"
44+
},
45+
"custom_attributes": {
46+
"truthy_typed": true,
47+
"floatly_typed": 155.5,
48+
"stringly_typed": "data",
49+
"dately_typed_at": 1397574667,
50+
"longly_typed": 221397574667,
51+
"intly_typed": 1,
52+
"nully_typed": null
53+
}
54+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"type": "company",
3+
"company_id": "6",
4+
"id": "5694b58371b5025a2a0003e6",
5+
"created_at": 1452586372,
6+
"updated_at": 1517835268,
7+
"monthly_spend": 0,
8+
"session_count": 0,
9+
"user_count": 0,
10+
"tags": {
11+
"type": "tag.list",
12+
"tags": []
13+
},
14+
"segments": {
15+
"type": "segment.list",
16+
"segments": []
17+
},
18+
"plan": {},
19+
"custom_attributes": {
20+
}
21+
}

0 commit comments

Comments
 (0)