Untitled

 avatar
vas
plain_text
3 months ago
4.2 kB
4
Indexable
@Test
    public void testUpdateDeviceObject_deviceFoundAndProductDetailsNotNull() throws Exception {
        String orgId = "org123";
        String deviceId = "device123";
        String token = "token123";
        
        // Create the product details (simulated for the test)
        JsonNode productDetails = objectMapper.readTree("{\"productName\": \"Test Product\"}");

        // Simulate the response from the searchDevice endpoint
        mockWebServer.enqueue(new MockResponse()
                .setResponseCode(200)
                .setBody("{\"resourceType\": \"Device\", \"id\": \"" + deviceId + "\"}")
                .addHeader("Content-Type", "application/json"));

        // Simulate the response for the mapPatientToDevice endpoint
        mockWebServer.enqueue(new MockResponse()
                .setResponseCode(200)
                .setBody("{\"message\": \"Device mapped successfully\"}")
                .addHeader("Content-Type", "application/json"));

        // Call the method to update the device object
        Mono<JsonNode> result = goalService.updateDeviceObject(productDetails, orgId, deviceId, token);

        // Wait for the result and validate it
        JsonNode response = result.block(); // block() to wait for Mono to complete

        assertNotNull(response);
        assertEquals("Device mapped successfully", response.get("message").asText());

        // Verify the MockWebServer interactions
        assertEquals(2, mockWebServer.getRequestCount()); // Two requests (search and map)
    }

    @Test
    public void testUpdateDeviceObject_deviceNotFoundAndProductDetailsNotNull() throws Exception {
        String orgId = "org123";
        String deviceId = null; // No device ID provided
        String token = "token123";
        
        // Create the product details (simulated for the test)
        JsonNode productDetails = objectMapper.readTree("{\"productName\": \"New Product\"}");

        // Simulate the response for creating a new device (no deviceId provided)
        mockWebServer.enqueue(new MockResponse()
                .setResponseCode(201)
                .setBody("{\"resourceType\": \"Device\", \"id\": \"new_device\"}")
                .addHeader("Content-Type", "application/json"));

        // Call the method to update the device object (creating a new device)
        Mono<JsonNode> result = goalService.updateDeviceObject(productDetails, orgId, deviceId, token);

        // Wait for the result and validate it
        JsonNode response = result.block(); // block() to wait for Mono to complete

        assertNotNull(response);
        assertEquals("Device created successfully", response.get("message").asText());

        // Verify the MockWebServer interactions
        assertEquals(1, mockWebServer.getRequestCount()); // One request (create device)
    }

    @Test
    public void testUpdateDeviceObject_noProductDetails() throws Exception {
        String orgId = "org123";
        String deviceId = "device123";
        String token = "token123";

        // Simulate the response for the device search (deviceId is valid)
        mockWebServer.enqueue(new MockResponse()
                .setResponseCode(200)
                .setBody("{\"resourceType\": \"Device\", \"id\": \"" + deviceId + "\"}")
                .addHeader("Content-Type", "application/json"));

        // Simulate a response when no product details are provided (no updates needed)
        mockWebServer.enqueue(new MockResponse()
                .setResponseCode(200)
                .setBody("{\"message\": \"No updates required\"}")
                .addHeader("Content-Type", "application/json"));

        // Call the method to update the device object
        Mono<JsonNode> result = goalService.updateDeviceObject(null, orgId, deviceId, token);

        // Wait for the result and validate it
        JsonNode response = result.block(); // block() to wait for Mono to complete

        assertNotNull(response);
        assertEquals("No updates required", response.get("message").asText());

        // Verify the MockWebServer interactions
        assertEquals(2, mockWebServer.getRequestCount()); // Two requests (search and no update)
    }
}
Editor is loading...
Leave a Comment