1 package com.atlassian.plugins.rest.common.expand;
2
3 import static org.junit.Assert.*;
4 import org.junit.Test;
5
6 import java.lang.annotation.Annotation;
7 import java.util.Collections;
8
9 import com.atlassian.plugins.rest.common.expand.parameter.DefaultExpandParameter;
10 import com.atlassian.plugins.rest.common.expand.parameter.ExpandParameter;
11
12
13
14
15 public class DefaultExpandParameterTest
16 {
17 @Test
18 public void testExpandParameterWithNull()
19 {
20 final ExpandParameter parameter = new DefaultExpandParameter(null);
21 assertTrue(parameter.isEmpty());
22 }
23
24 @Test
25 public void testExpandParameterWithEmptyString()
26 {
27 final ExpandParameter parameter = new DefaultExpandParameter(Collections.singleton(""));
28 assertTrue(parameter.isEmpty());
29 }
30
31 @Test
32 public void testExpandParameterWithValidString1()
33 {
34 final String parameterValue = "value";
35 final ExpandParameter parameter = new DefaultExpandParameter(Collections.singleton(parameterValue));
36
37 assertFalse(parameter.isEmpty());
38 assertTrue(parameter.getExpandParameter(getExpandableAnnotation(parameterValue)).isEmpty());
39 assertTrue(parameter.shouldExpand(getExpandableAnnotation(parameterValue)));
40 assertFalse(parameter.shouldExpand(getExpandableAnnotation("shouldnot")));
41 }
42
43 @Test
44 public void testExpandParameterWithValidString2()
45 {
46 final String value1 = "value1";
47 final String value2 = "value2";
48
49 final ExpandParameter parameter = new DefaultExpandParameter(Collections.singleton(value1 + "," + value2));
50
51 assertFalse(parameter.isEmpty());
52 assertTrue(parameter.getExpandParameter(getExpandableAnnotation(value1)).isEmpty());
53 assertTrue(parameter.getExpandParameter(getExpandableAnnotation(value2)).isEmpty());
54 assertTrue(parameter.shouldExpand(getExpandableAnnotation(value1)));
55 assertTrue(parameter.shouldExpand(getExpandableAnnotation(value2)));
56 assertFalse(parameter.shouldExpand(getExpandableAnnotation("shouldnot")));
57 }
58
59 @Test
60 public void testExpandParameterWithValidString3()
61 {
62 final String value1 = "value1";
63 final String value2 = "value2";
64
65 final ExpandParameter parameter = new DefaultExpandParameter(Collections.singleton(value1 + "." + value2));
66
67 assertFalse(parameter.isEmpty());
68 assertFalse(parameter.getExpandParameter(getExpandableAnnotation(value1)).isEmpty());
69 assertTrue(parameter.getExpandParameter(getExpandableAnnotation(value2)).isEmpty());
70 assertTrue(parameter.shouldExpand(getExpandableAnnotation(value1)));
71 assertFalse(parameter.shouldExpand(getExpandableAnnotation(value2)));
72 assertTrue(parameter.getExpandParameter(getExpandableAnnotation(value1)).shouldExpand(getExpandableAnnotation(value2)));
73 assertFalse(parameter.shouldExpand(getExpandableAnnotation("shouldnot")));
74 }
75
76 @Test
77 public void testExpandParameterWithWilcard1()
78 {
79 final String value1 = "value1";
80 final String value2 = "value2";
81
82 final ExpandParameter parameter = new DefaultExpandParameter(Collections.singleton("*" + "." + value2));
83
84 assertFalse(parameter.isEmpty());
85 assertFalse(parameter.getExpandParameter(getExpandableAnnotation(value1)).isEmpty());
86 assertFalse(parameter.getExpandParameter(getExpandableAnnotation(value2)).isEmpty());
87 assertTrue(parameter.shouldExpand(getExpandableAnnotation(value1)));
88 assertTrue(parameter.shouldExpand(getExpandableAnnotation(value2)));
89 assertTrue(parameter.getExpandParameter(getExpandableAnnotation(value1)).shouldExpand(getExpandableAnnotation(value2)));
90 assertTrue(parameter.shouldExpand(getExpandableAnnotation("should")));
91 }
92
93 @Test
94 public void testExpandParameterWithWilcard2()
95 {
96 final String value1 = "value1";
97 final String value2 = "value2";
98
99 final ExpandParameter parameter = new DefaultExpandParameter(Collections.singleton(value1 + "." + "*"));
100
101 assertFalse(parameter.isEmpty());
102 assertFalse(parameter.getExpandParameter(getExpandableAnnotation(value1)).isEmpty());
103 assertTrue(parameter.getExpandParameter(getExpandableAnnotation(value2)).isEmpty());
104 assertTrue(parameter.shouldExpand(getExpandableAnnotation(value1)));
105 assertFalse(parameter.shouldExpand(getExpandableAnnotation(value2)));
106 assertTrue(parameter.getExpandParameter(getExpandableAnnotation(value1)).shouldExpand(getExpandableAnnotation(value2)));
107 assertFalse(parameter.shouldExpand(getExpandableAnnotation("shouldnot")));
108 assertTrue(parameter.getExpandParameter(getExpandableAnnotation(value1)).shouldExpand(getExpandableAnnotation("should")));
109 }
110
111 private Expandable getExpandableAnnotation(final String parameterValue)
112 {
113 return new Expandable()
114 {
115 public String value()
116 {
117 return parameterValue;
118 }
119
120 public Class<? extends Annotation> annotationType()
121 {
122 return Expandable.class;
123 }
124 };
125 }
126 }