View Javadoc
1   package emissary.server.mvc.internal;
2   
3   import emissary.core.Namespace;
4   import emissary.pickup.WorkSpace;
5   import emissary.server.mvc.EndpointTestBase;
6   
7   import jakarta.ws.rs.client.Entity;
8   import jakarta.ws.rs.core.MultivaluedHashMap;
9   import jakarta.ws.rs.core.Response;
10  import org.junit.jupiter.api.AfterEach;
11  import org.junit.jupiter.api.BeforeEach;
12  import org.junit.jupiter.api.Test;
13  import org.junit.jupiter.params.ParameterizedTest;
14  import org.junit.jupiter.params.provider.NullAndEmptySource;
15  import org.junit.jupiter.params.provider.ValueSource;
16  
17  import java.util.Collections;
18  
19  import static emissary.server.mvc.adapters.WorkSpaceAdapter.WORK_BUNDLE_ID;
20  import static emissary.server.mvc.adapters.WorkSpaceAdapter.WORK_BUNDLE_STATUS;
21  import static emissary.server.mvc.internal.WorkSpaceClientSpaceTakeAction.CLIENT_NAME;
22  import static emissary.server.mvc.internal.WorkSpaceClientSpaceTakeAction.SPACE_NAME;
23  import static org.junit.jupiter.api.Assertions.assertEquals;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  import static org.mockito.Mockito.doReturn;
26  import static org.mockito.Mockito.spy;
27  
28  class WorkBundleCompletedActionTest extends EndpointTestBase {
29  
30      private MultivaluedHashMap<String, String> formParams;
31      private static final String CLIENT_KEY = "INITIAL.FILE_PICK_UP_CLIENT.INPUT.http://localhost:9001/FilePickUpClient";
32      private static final String WORKSPACE_BIND_KEY = "http://workBundleCompletedActionTest:7001/WorkSpace";
33      private static final String WORKSPACE_NAME = "WORKSPACE.WORK_SPACE.INPUT." + WORKSPACE_BIND_KEY;
34      private static final String WORK_BUNDLE_COMPLETED_ACTION = "WorkBundleCompleted.action";
35      @SuppressWarnings("unused")
36      private static final String FAILURE_RESULT = "<entryList />";
37  
38      @BeforeEach
39      public void setup() throws Exception {
40          formParams = new MultivaluedHashMap<>();
41          formParams.put(CLIENT_NAME, Collections.singletonList(CLIENT_KEY));
42          formParams.put(SPACE_NAME, Collections.singletonList(WORKSPACE_NAME));
43          formParams.put(WORK_BUNDLE_ID, Collections.singletonList("1"));
44          formParams.put(WORK_BUNDLE_STATUS, Collections.singletonList("true"));
45          WorkSpace ws = new WorkSpace();
46          Namespace.bind(WORKSPACE_BIND_KEY, ws);
47      }
48  
49      @Override
50      @AfterEach
51      public void tearDown() {
52          Namespace.unbind(WORKSPACE_BIND_KEY);
53      }
54  
55      @ParameterizedTest
56      @NullAndEmptySource
57      @ValueSource(strings = {" ", "\n", "\t"})
58      void emptyParams(String badValue) {
59          // setup
60          formParams.replace(CLIENT_NAME, Collections.singletonList(badValue));
61          formParams.replace(SPACE_NAME, Collections.singletonList(badValue));
62          formParams.replace(WORK_BUNDLE_ID, Collections.singletonList(badValue));
63          formParams.replace(WORK_BUNDLE_STATUS, Collections.singletonList(badValue));
64  
65          // test
66          try (Response response = target(WORK_BUNDLE_COMPLETED_ACTION).request().post(Entity.form(formParams))) {
67              // verify
68              final int status = response.getStatus();
69              assertEquals(500, status);
70              final String result = response.readEntity(String.class);
71              assertTrue(result.startsWith("Bad params:"));
72          }
73      }
74  
75      @Test
76      void badWorkSpaceKey() {
77          // setup
78          formParams.replace(SPACE_NAME, Collections.singletonList("ThisShouldCauseAnException"));
79  
80          // test
81          try (Response response = target(WORK_BUNDLE_COMPLETED_ACTION).request().post(Entity.form(formParams))) {
82              // verify
83              final int status = response.getStatus();
84              assertEquals(500, status);
85              final String result = response.readEntity(String.class);
86              assertTrue(result.startsWith("Bad params:"));
87          }
88      }
89  
90      @Test
91      void missingWorkSpaceKey() {
92          // setup
93          formParams.replace(SPACE_NAME, Collections.singletonList(WORKSPACE_NAME + "ThisWillMiss"));
94  
95          // test
96          try (Response response = target(WORK_BUNDLE_COMPLETED_ACTION).request().post(Entity.form(formParams))) {
97              // verify
98              final int status = response.getStatus();
99              assertEquals(500, status);
100             final String result = response.readEntity(String.class);
101             assertEquals(
102                     "There was a problem while processing the WorkBundle: Not found: http://workBundleCompletedActionTest:7001/WorkSpaceThisWillMiss",
103                     result);
104         }
105     }
106 
107     @Test
108     void badPickupClientKey() {
109         // setup
110         formParams.replace(CLIENT_NAME, Collections.singletonList("ThisIsBad"));
111 
112         // test
113         try (Response response = target(WORK_BUNDLE_COMPLETED_ACTION).request().post(Entity.form(formParams))) {
114             // verify
115             final int status = response.getStatus();
116             assertEquals(500, status);
117             final String result = response.readEntity(String.class);
118             assertTrue(result.startsWith("Bad params:"));
119         }
120     }
121 
122     @Test
123     void itemNotPresentInPending() {
124         // test
125         try (Response response = target(WORK_BUNDLE_COMPLETED_ACTION).request().post(Entity.form(formParams))) {
126             // verify
127             final int status = response.getStatus();
128             assertEquals(500, status);
129             final String result = response.readEntity(String.class);
130             assertTrue(result.startsWith(""));
131         }
132     }
133 
134     @Test
135     void successfulSubmission() throws Exception {
136         // TODO Add a better test for the WorkSpace that validates the workCompleted method
137         // setup
138         Namespace.unbind(WORKSPACE_BIND_KEY);
139         WorkSpace spyWs = spy(new WorkSpace());
140         doReturn(true).when(spyWs).workCompleted("http://localhost:9001/FilePickUpClient", "1", true);
141         Namespace.bind(WORKSPACE_BIND_KEY, spyWs);
142 
143         // test
144         try (Response response = target(WORK_BUNDLE_COMPLETED_ACTION).request().post(Entity.form(formParams))) {
145             // verify
146             final int status = response.getStatus();
147             assertEquals(200, status);
148             final String result = response.readEntity(String.class);
149             assertEquals("Work Bundle Completed", result);
150         }
151     }
152 }