Skip to content

Commit 9d78435

Browse files
committed
Closes #135
1 parent 250081f commit 9d78435

File tree

4 files changed

+73
-11
lines changed

4 files changed

+73
-11
lines changed

kikaha-modules/kikaha-urouting-mustache/source/kikaha/mustache/HtmlMustacheSerializer.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package kikaha.mustache;
22

3+
import com.github.mustachejava.MustacheNotFoundException;
34
import io.undertow.server.HttpServerExchange;
45

56
import java.io.IOException;
67

8+
import kikaha.core.NotFoundHandler;
79
import kikaha.urouting.api.ContentType;
810
import kikaha.urouting.api.Mimes;
911
import kikaha.urouting.api.Serializer;
12+
import org.omg.CosNaming.NamingContextPackage.NotFound;
13+
import org.omg.CosNaming.NamingContextPackage.NotFoundHelper;
1014

1115
import javax.enterprise.inject.Typed;
1216
import javax.inject.Inject;
@@ -20,10 +24,25 @@ public class HtmlMustacheSerializer implements Serializer {
2024
@Inject
2125
MustacheSerializerFactory factory;
2226

27+
@Inject
28+
NotFoundHandler notFoundHandler;
29+
2330
@Override
2431
public <T> void serialize( final T object, final HttpServerExchange exchange ) throws IOException {
25-
final MustacheTemplate template = (MustacheTemplate)object;
26-
String serialized = factory.serializer().serialize( template );
27-
exchange.getResponseSender().send( serialized );
32+
try {
33+
final MustacheTemplate template = (MustacheTemplate) object;
34+
String serialized = factory.serializer().serialize(template);
35+
exchange.getResponseSender().send(serialized);
36+
} catch ( MustacheNotFoundException cause ) {
37+
handleNotFound( exchange );
38+
}
39+
}
40+
41+
private void handleNotFound( final HttpServerExchange exchange ) throws IOException {
42+
try {
43+
notFoundHandler.handleRequest( exchange );
44+
} catch (Exception e) {
45+
throw new IOException( e );
46+
}
2847
}
2948
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package kikaha.mustache;
2+
3+
import com.github.mustachejava.MustacheNotFoundException;
4+
import kikaha.core.NotFoundHandler;
5+
import lombok.SneakyThrows;
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import org.mockito.InjectMocks;
9+
import org.mockito.Mock;
10+
import org.mockito.Spy;
11+
import org.mockito.runners.MockitoJUnitRunner;
12+
13+
import static org.mockito.Matchers.any;
14+
import static org.mockito.Mockito.*;
15+
16+
/**
17+
*
18+
*/
19+
@RunWith(MockitoJUnitRunner.class)
20+
public class HtmlMustacheSerializerTest {
21+
22+
@Mock
23+
MustacheSerializerFactory factory;
24+
25+
@Mock
26+
NotFoundHandler notFoundHandler;
27+
28+
@Mock
29+
MustacheSerializer serializer;
30+
31+
@Spy
32+
@InjectMocks
33+
HtmlMustacheSerializer htmlSerializer;
34+
35+
@Test
36+
@SneakyThrows
37+
public void ensureThatHandleNotFoundException() {
38+
doReturn( serializer ).when( factory ).serializer();
39+
doThrow(MustacheNotFoundException.class).when(serializer).serialize( any() );
40+
htmlSerializer.serialize( new MustacheTemplate().templateName("any.mustache"), null );
41+
verify( notFoundHandler ).handleRequest( any() );
42+
}
43+
}

kikaha-modules/kikaha-urouting-mustache/tests/kikaha/mustache/MustacheSerializerTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.github.mustachejava.DefaultMustacheFactory;
44
import kikaha.config.Config;
5+
import kikaha.core.NotFoundHandler;
56
import kikaha.core.test.KikahaRunner;
67
import lombok.SneakyThrows;
78
import lombok.val;
@@ -20,6 +21,7 @@
2021
import static org.junit.Assert.assertTrue;
2122
import static org.mockito.Matchers.eq;
2223
import static org.mockito.Mockito.doReturn;
24+
import static org.mockito.Mockito.mock;
2325
import static org.mockito.Mockito.spy;
2426

2527
@RunWith( KikahaRunner.class )

kikaha-modules/kikaha-urouting/source/kikaha/urouting/api/DefaultResponse.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
package kikaha.urouting.api;
22

3-
import java.net.URI;
4-
import java.util.ArrayList;
5-
import java.util.List;
6-
73
import lombok.Getter;
8-
import lombok.Setter;
94
import lombok.NoArgsConstructor;
105
import lombok.NonNull;
11-
import lombok.RequiredArgsConstructor;
6+
import lombok.Setter;
127
import lombok.experimental.Accessors;
138

9+
import java.net.URI;
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
1413
@Getter
1514
@Setter
1615
@Accessors( fluent=true )
1716
@NoArgsConstructor
18-
@RequiredArgsConstructor
1917
public class DefaultResponse implements Response {
2018

21-
@NonNull Object entity;
19+
@NonNull Object entity = "";
2220
@NonNull Integer statusCode = 200;
2321
@NonNull String encoding = "UTF-8";
2422
@NonNull String contentType = Mimes.PLAIN_TEXT;

0 commit comments

Comments
 (0)