Merge "Added STD sim 2.0.0 tests"
[nonrtric.git] / policy-agent / src / test / java / org / oransc / policyagent / dmaap / DmaapMessageHandlerTest.java
index 7deef6e..f0efddc 100644 (file)
@@ -40,6 +40,7 @@ import com.google.gson.GsonBuilder;
 import com.google.gson.JsonObject;
 
 import java.io.IOException;
+import java.nio.charset.Charset;
 import java.util.Optional;
 
 import org.junit.jupiter.api.BeforeEach;
@@ -52,8 +53,10 @@ import org.oransc.policyagent.repository.PolicyType;
 import org.oransc.policyagent.utils.LoggingUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.http.HttpHeaders;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
+import org.springframework.web.reactive.function.client.WebClientResponseException;
 
 import reactor.core.publisher.Mono;
 import reactor.test.StepVerifier;
@@ -180,6 +183,26 @@ class DmaapMessageHandlerTest {
         verifyNoMoreInteractions(dmaapClient);
     }
 
+    @Test
+    void exceptionFromAgentWhenGet_thenPostError() throws IOException {
+        String errorBody = "Unavailable";
+        WebClientResponseException webClientResponseException = new WebClientResponseException(
+            HttpStatus.SERVICE_UNAVAILABLE.value(), "", (HttpHeaders) null, errorBody.getBytes(), (Charset) null);
+        doReturn(Mono.error(webClientResponseException)).when(agentClient).getForEntity(anyString());
+        doReturn(Mono.just("OK")).when(dmaapClient).post(anyString(), anyString());
+
+        StepVerifier //
+            .create(testedObject.createTask(dmaapInputMessage(Operation.GET))) //
+            .expectSubscription() //
+            .verifyComplete(); //
+
+        ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
+        verify(dmaapClient).post(anyString(), captor.capture());
+        String actualMessage = captor.getValue();
+        assertThat(actualMessage).contains(HttpStatus.SERVICE_UNAVAILABLE.toString()) //
+            .contains(errorBody);
+    }
+
     @Test
     void successfulPut() throws IOException {
         doReturn(okResponse()).when(agentClient).putForEntity(anyString(), anyString());
@@ -230,9 +253,8 @@ class DmaapMessageHandlerTest {
         ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
         verify(dmaapClient).post(anyString(), captor.capture());
         String actualMessage = captor.getValue();
-        assertThat(actualMessage.contains(HttpStatus.BAD_GATEWAY.toString()))
-            .as("Message \"%s\" sent to DMaaP contains %s", actualMessage, HttpStatus.BAD_GATEWAY) //
-            .isTrue();
+        assertThat(actualMessage).as("Message \"%s\" sent to DMaaP contains %s", actualMessage, HttpStatus.BAD_GATEWAY)
+            .contains(HttpStatus.BAD_GATEWAY.toString());
 
         verifyNoMoreInteractions(dmaapClient);
     }
@@ -248,8 +270,8 @@ class DmaapMessageHandlerTest {
         ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
         verify(dmaapClient).post(anyString(), captor.capture());
         String actualMessage = captor.getValue();
-        assertThat(actualMessage.contains("Not implemented operation")).isTrue();
-        assertThat(actualMessage.contains("BAD_REQUEST")).isTrue();
+        assertThat(actualMessage).contains("Not implemented operation") //
+            .contains("BAD_REQUEST");
     }
 
     @Test