1 package com.atlassian.plugins.rest.common.expand;
2
3 import java.lang.annotation.Annotation;
4 import java.util.Collections;
5
6 import com.atlassian.plugins.rest.common.expand.parameter.DefaultExpandParameter;
7 import com.atlassian.plugins.rest.common.expand.parameter.ExpandParameter;
8
9 import org.hamcrest.FeatureMatcher;
10 import org.hamcrest.Matcher;
11 import org.junit.Test;
12
13 import static org.hamcrest.MatcherAssert.assertThat;
14 import static org.hamcrest.Matchers.is;
15 import static org.hamcrest.Matchers.not;
16
17
18
19
20 public class DefaultExpandParameterTest {
21 @Test
22 public void testExpandParameterWithNull() {
23 final ExpandParameter parameter = new DefaultExpandParameter(null);
24 assertThat(parameter, emptyParameter());
25 }
26
27 @Test
28 public void testExpandParameterWithEmptyString() {
29 final ExpandParameter parameter = new DefaultExpandParameter(Collections.singleton(""));
30 assertThat(parameter, emptyParameter());
31 }
32
33 @Test
34 public void testExpandParameterWithValidString1() {
35 final String parameterValue = "value";
36 final ExpandParameter parameter = new DefaultExpandParameter(Collections.singleton(parameterValue));
37
38 assertThat(parameter, not(emptyParameter()));
39 assertThat(parameter.getExpandParameter(getExpandableAnnotation(parameterValue)), emptyParameter());
40 assertThat(parameter, shouldExpand(parameterValue));
41 assertThat(parameter, shouldNotExpand("shouldnot"));
42 }
43
44 @Test
45 public void testExpandParameterWithValidString2() {
46 final String value1 = "value1";
47 final String value2 = "value2";
48
49 final ExpandParameter parameter = new DefaultExpandParameter(Collections.singleton(value1 + "," + value2));
50
51 assertThat(parameter, not(emptyParameter()));
52 assertThat(parameter.getExpandParameter(getExpandableAnnotation(value1)), emptyParameter());
53 assertThat(parameter.getExpandParameter(getExpandableAnnotation(value2)), emptyParameter());
54 assertThat(parameter, shouldExpand(value1));
55 assertThat(parameter, shouldExpand(value2));
56 assertThat(parameter, shouldNotExpand("shouldnot"));
57 }
58
59 @Test
60 public void testExpandParameterWithValidString3() {
61 final String value1 = "value1";
62 final String value2 = "value2";
63
64 final ExpandParameter parameter = new DefaultExpandParameter(Collections.singleton(value1 + "." + value2));
65
66 assertThat(parameter, not(emptyParameter()));
67 assertThat(parameter.getExpandParameter(getExpandableAnnotation(value1)), not(emptyParameter()));
68 assertThat(parameter.getExpandParameter(getExpandableAnnotation(value2)), emptyParameter());
69 assertThat(parameter, shouldExpand(value1));
70 assertThat(parameter, shouldNotExpand(value2));
71 assertThat(parameter.getExpandParameter(getExpandableAnnotation(value1)), shouldExpand(value2));
72 assertThat(parameter, shouldNotExpand("shouldnot"));
73 }
74
75 @Test
76 public void testExpandParameterWithWilcard1() {
77 final String value1 = "value1";
78 final String value2 = "value2";
79
80 final ExpandParameter parameter = new DefaultExpandParameter(Collections.singleton("*" + "." + value2));
81
82 assertThat(parameter, not(emptyParameter()));
83 assertThat(parameter.getExpandParameter(getExpandableAnnotation(value1)), not(emptyParameter()));
84 assertThat(parameter.getExpandParameter(getExpandableAnnotation(value2)), not(emptyParameter()));
85 assertThat(parameter, shouldExpand(value1));
86 assertThat(parameter, shouldExpand(value2));
87 assertThat(parameter.getExpandParameter(getExpandableAnnotation(value1)), shouldExpand(value2));
88 assertThat(parameter, shouldExpand("should"));
89 }
90
91 @Test
92 public void testExpandParameterWithWilcard2() {
93 final String value1 = "value1";
94 final String value2 = "value2";
95
96 final ExpandParameter parameter = new DefaultExpandParameter(Collections.singleton(value1 + "." + "*"));
97
98 assertThat(parameter, not(emptyParameter()));
99 assertThat(parameter.getExpandParameter(getExpandableAnnotation(value1)), not(emptyParameter()));
100 assertThat(parameter.getExpandParameter(getExpandableAnnotation(value2)), emptyParameter());
101 assertThat(parameter, shouldExpand(value1));
102 assertThat(parameter, shouldNotExpand(value2));
103 assertThat(parameter.getExpandParameter(getExpandableAnnotation(value1)), shouldExpand(value2));
104 assertThat(parameter, shouldNotExpand("shouldnot"));
105 assertThat(parameter.getExpandParameter(getExpandableAnnotation(value1)), shouldExpand("should"));
106 }
107
108 @Test
109 public void testExpandParameterWithMultipleSubitemsSamePrefix() {
110 final ExpandParameter parameter = new DefaultExpandParameter(Collections.singleton("root.child1,root.child2,root.child2.grandchild"));
111
112 assertThat(parameter, not(emptyParameter()));
113 assertThat(parameter.shouldExpand(getExpandableAnnotation("root")), is(true));
114
115 ExpandParameter root = parameter.getExpandParameter(getExpandableAnnotation("root"));
116 assertThat(root, not(emptyParameter()));
117
118 assertThat(root, shouldExpand("child1"));
119 assertThat(root, shouldExpand("child2"));
120
121 ExpandParameter child2 = root.getExpandParameter(getExpandableAnnotation("child2"));
122 assertThat(child2, shouldExpand("grandchild"));
123 }
124
125 @Test
126 public void testExpandParameterWithMultipleSubitemsSameMultilevelPrefix() {
127 ExpandParameter parameter = new DefaultExpandParameter(Collections.singleton("results.result.comments.comment,results.result.jiraIssues"));
128 assertThat(parameter, not(emptyParameter()));
129 assertThat(parameter.shouldExpand(getExpandableAnnotation("results")), is(true));
130
131 ExpandParameter results = parameter.getExpandParameter(getExpandableAnnotation("results"));
132 assertThat(results, not(emptyParameter()));
133 assertThat(results, shouldExpand("result"));
134
135 ExpandParameter result = results.getExpandParameter(getExpandableAnnotation("result"));
136 assertThat(result, not(emptyParameter()));
137 assertThat(result, shouldExpand("comments"));
138 assertThat(result, shouldExpand("jiraIssues"));
139 }
140
141 private Expandable getExpandableAnnotation(final String parameterValue) {
142 return new Expandable() {
143 public String value() {
144 return parameterValue;
145 }
146
147 public Class<? extends Annotation> annotationType() {
148 return Expandable.class;
149 }
150 };
151 }
152
153 private Matcher<ExpandParameter> emptyParameter() {
154 return new FeatureMatcher<ExpandParameter, Boolean>(is(true), "an ExpandParameter for which isEmpty", "isEmpty") {
155 @Override
156 protected Boolean featureValueOf(final ExpandParameter expandParameter) {
157 return expandParameter.isEmpty();
158 }
159 };
160 }
161
162 private Matcher<ExpandParameter> shouldExpand(final String parameterValue) {
163 return new FeatureMatcher<ExpandParameter, Boolean>(is(true), "ExpandParameter for which shouldExpand", "shouldExpand") {
164 @Override
165 protected Boolean featureValueOf(final ExpandParameter expandParameter) {
166 return expandParameter.shouldExpand(getExpandableAnnotation(parameterValue));
167 }
168 };
169 }
170
171 private Matcher<ExpandParameter> shouldNotExpand(String parameterValue) {
172 return not(shouldExpand(parameterValue));
173 }
174 }