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 private Expandable getExpandableAnnotation(final String parameterValue) {
126 return new Expandable() {
127 public String value() {
128 return parameterValue;
129 }
130
131 public Class<? extends Annotation> annotationType() {
132 return Expandable.class;
133 }
134 };
135 }
136
137 private Matcher<ExpandParameter> emptyParameter() {
138 return new FeatureMatcher<ExpandParameter, Boolean>(is(true), "an ExpandParameter for which isEmpty", "isEmpty") {
139 @Override
140 protected Boolean featureValueOf(final ExpandParameter expandParameter) {
141 return expandParameter.isEmpty();
142 }
143 };
144 }
145
146 private Matcher<ExpandParameter> shouldExpand(final String parameterValue) {
147 return new FeatureMatcher<ExpandParameter, Boolean>(is(true), "ExpandParameter for which shouldExpand", "shouldExpand") {
148 @Override
149 protected Boolean featureValueOf(final ExpandParameter expandParameter) {
150 return expandParameter.shouldExpand(getExpandableAnnotation(parameterValue));
151 }
152 };
153 }
154
155 private Matcher<ExpandParameter> shouldNotExpand(String parameterValue) {
156 return not(shouldExpand(parameterValue));
157 }
158 }