Fix Sonar complains 41/6141/2
authorClaudio D. Gasparini <claudio.gasparini@intl.att.com>
Tue, 25 May 2021 15:00:14 +0000 (17:00 +0200)
committerClaudio D. Gasparini <claudio.gasparini@intl.att.com>
Wed, 26 May 2021 06:48:36 +0000 (08:48 +0200)
- increment test coverage

Issue-ID: OAM-215
Signed-off-by: Claudio D. Gasparini <claudio.gasparini@intl.att.com>
Change-Id: I03622c68556ad72bd04b819f26f03eff85ffa66d

ves-nf-oam-adopter/ves-nf-oam-adopter-app/docs/api/swagger/openapi.yaml
ves-nf-oam-adopter/ves-nf-oam-adopter-app/src/main/java/org/o/ran/oam/nf/oam/adopter/app/controller/RestExceptionHandler.java
ves-nf-oam-adopter/ves-nf-oam-adopter-app/src/test/java/org/o/ran/oam/nf/oam/adopter/app/AdapterApplicationTest.java
ves-nf-oam-adopter/ves-nf-oam-adopter-pm-manager/src/main/java/org/o/ran/oam/nf/oam/adopter/pm/rest/manager/mapper/PerformanceManagementFile2VesMapper.java
ves-nf-oam-adopter/ves-nf-oam-adopter-pm-sb-rest-client/src/test/java/org/o/ran/oam/nf/oam/adopter/pm/sb/rest/client/DefaultHttpRestClientTest.java
ves-nf-oam-adopter/ves-nf-oam-adopter-snmp-manager/src/main/java/org/o/ran/oam/nf/oam/adopter/snmp/manager/SnmpTrapListener.java

index 6a02e63..d3ae8e8 100644 (file)
@@ -47,8 +47,9 @@ paths:
                 items:
                   type: string
         '400':
-          $ref: '#/components/responses/400Error'
-
+          $ref: '#/components/responses/BadRequest'
+        '401':
+          $ref: '#/components/responses/Unauthorized'
 
   /adapter/{host}:
     delete:
@@ -65,13 +66,13 @@ paths:
 
       responses:
         '200':
-          description: Successfully deleted an adapter
+          $ref: '#/components/responses/Success'
         '400':
-          description: Invalid request
+          $ref: '#/components/responses/BadRequest'
         '401':
-          description: Unauthorized
+          $ref: '#/components/responses/Unauthorized'
         '404':
-          description: adapter not found
+          $ref: '#/components/responses/NotFound'
 
   /adapter:
     post:
@@ -87,12 +88,23 @@ paths:
               $ref: '#/components/schemas/Adapter'
       responses:
         '200':
-          description: Successfully returned a list of adapters
+          $ref: '#/components/responses/Success'
         '400':
-          $ref: '#/components/responses/400Error'
+          $ref: '#/components/responses/BadRequest'
+        '401':
+          $ref: '#/components/responses/Unauthorized'
 
 components:
   schemas:
+    ErrorMessage:
+      type: object
+      title: Error
+      properties:
+        status:
+          type: string
+        message:
+          type: string
+
     Adapter:
       type: object
       properties:
@@ -113,12 +125,24 @@ components:
         - mechId
 
   responses:
-    400Error:
+    Success:
+      description: Succesfully excecuted
+    BadRequest:
       description: Invalid request
       content:
         application/json:
           schema:
-            type: object
-            properties:
-              message:
-                type: string
\ No newline at end of file
+            $ref: '#/components/schemas/ErrorMessage'
+    Unauthorized:
+      description: Unhautorized request
+      content:
+        application/json:
+          schema:
+            $ref: '#/components/schemas/ErrorMessage'
+    NotFound:
+      description: Not Found
+      content:
+        application/json:
+          schema:
+            $ref: '#/components/schemas/ErrorMessage'
+
index 4eb1625..fff799a 100644 (file)
@@ -51,10 +51,11 @@ public class RestExceptionHandler {
     /**
      * Handle Not Found Exceptions.
      */
+    @ResponseStatus(HttpStatus.NOT_FOUND)
     @ExceptionHandler({NotFoundException.class})
-    public static ResponseEntity<Object> handleNotFoundExceptions(final NotFoundException exception) {
+    public static String handleNotFoundExceptions(final NotFoundException exception) {
         LOG.error("Request failed", exception);
-        return ResponseEntity.notFound().build();
+        return exception.getMessage();
     }
 
     /**
index 73cea18..78d036c 100644 (file)
@@ -85,24 +85,32 @@ class AdapterApplicationTest {
     void testGetAllAdapters() throws Exception {
         when(deployer.getAll()).thenReturn(Collections.singletonList("mockResult"));
 
-        mockMvc.perform(get("/adapters/").secure(true).contentType(MediaType.APPLICATION_JSON)).andDo(print())
+        mockMvc.perform(get("/adapters/").secure(true)
+                                .contentType(MediaType.APPLICATION_JSON))
+                .andDo(print())
                 .andExpect(status().isOk()).andExpect(content().string(containsString("mockResult")));
     }
 
     @Test
     @WithMockUser(username = "admin", roles = "ADMIN")
     void testDeleteAdapter() throws Exception {
-        mockMvc.perform(delete("/adapters/adapter/172.10.55.3").secure(true).contentType(MediaType.APPLICATION_JSON))
+        mockMvc.perform(delete("/adapters/adapter/172.10.55.3").secure(true)
+                                .contentType(MediaType.APPLICATION_JSON))
                 .andDo(print()).andExpect(status().isOk());
     }
 
     @Test
     @WithMockUser(username = "admin", roles = "ADMIN")
     void testNotFound() throws Exception {
-        doThrow(NotFoundException.class).when(deployer).delete(anyString());
-
-        mockMvc.perform(delete("/adapters/adapter/172.10.55.3").secure(true).contentType(MediaType.APPLICATION_JSON))
-                .andDo(print()).andExpect(status().isNotFound());
+        doThrow(new NotFoundException("172.10.55.3"))
+                .when(deployer)
+                .delete(anyString());
+
+        mockMvc.perform(delete("/adapters/adapter/172.10.55.3").secure(true)
+                                .contentType(MediaType.APPLICATION_JSON))
+                .andDo(print())
+                .andExpect(status().isNotFound())
+                .andExpect(content().string(containsString("Adapter 172.10.55.3 is not present.")));
     }
 
     @Test
@@ -117,14 +125,16 @@ class AdapterApplicationTest {
         mechId.password("somePass");
         adapter.setMechId(mechId);
 
-        mockMvc.perform(post("/adapters/adapter").secure(true).contentType(MediaType.APPLICATION_JSON)
-                                .content(GSON.toJson(adapter))).andDo(print()).andExpect(status().isOk());
+        mockMvc.perform(post("/adapters/adapter").secure(true)
+                                .contentType(MediaType.APPLICATION_JSON)
+                                .content(GSON.toJson(adapter)))
+                .andDo(print())
+                .andExpect(status().isOk());
     }
 
     @Test
     @WithMockUser(username = "admin", roles = "ADMIN")
     void testAlreadyExist() throws Exception {
-
         final Adapter adapter = new Adapter();
         adapter.setHost("172.10.55.3");
 
@@ -133,16 +143,20 @@ class AdapterApplicationTest {
         mechId.password("somePass");
         adapter.setMechId(mechId);
 
-        doThrow(AlreadyPresentException.class).when(deployer).create(anyString(), anyString(), anyString());
+        doThrow(new AlreadyPresentException("172.10.55.3"))
+                .when(deployer).create(anyString(), anyString(), anyString());
 
-        mockMvc.perform(post("/adapters/adapter").secure(true).contentType(MediaType.APPLICATION_JSON)
-                                .content(GSON.toJson(adapter))).andDo(print()).andExpect(status().isBadRequest());
+        mockMvc.perform(post("/adapters/adapter")
+                                .secure(true).contentType(MediaType.APPLICATION_JSON)
+                                .content(GSON.toJson(adapter)))
+                .andDo(print())
+                .andExpect(status().isBadRequest())
+                .andExpect(content().string(containsString("Adapter 172.10.55.3 already present.")));
     }
 
     @Test
     @WithMockUser(username = "admin", roles = "ADMIN")
     void testMissingArguments() throws Exception {
-
         final Adapter adapter = new Adapter();
         adapter.setHost("172.10.55.3");
 
@@ -150,9 +164,27 @@ class AdapterApplicationTest {
         mechId.username("admin");
         adapter.setMechId(mechId);
 
+        mockMvc.perform(post("/adapters/adapter").secure(true)
+                                .contentType(MediaType.APPLICATION_JSON)
+                                .content(GSON.toJson(adapter)))
+                .andDo(print())
+                .andExpect(status().isBadRequest());
+    }
+
+    @Test
+    void testUnauthorized() throws Exception {
+        mockMvc.perform(post("/adapters/adapter").secure(true))
+                .andDo(print())
+                .andExpect(status().isUnauthorized());
+
+        mockMvc.perform(post("/adapters/adapter").secure(true))
+            .andDo(print())
+            .andExpect(status().isUnauthorized());
+
+        mockMvc.perform(delete("/adapters/adapter/172.10.55.3").secure(true))
+                .andDo(print())
+                .andExpect(status().isUnauthorized());
 
-        mockMvc.perform(post("/adapters/adapter").secure(true).contentType(MediaType.APPLICATION_JSON)
-                                .content(GSON.toJson(adapter))).andDo(print()).andExpect(status().isBadRequest());
     }
 
     @Test
index 0d40c7b..19ca5b0 100644 (file)
@@ -48,7 +48,7 @@ public class PerformanceManagementFile2VesMapper {
     private static final String CSV_EXTENSION = ".csv";
     private final PerformanceManagementMapperConfigProvider pmConfigProvider;
     private static final int THRESHOLD_SIZE  = 1000000000; // 1 GB
-    private static final int THRESHOLD_RATIO = 10;
+    private static final double THRESHOLD_RATIO = 40;
     private static final int THRESHOLD_ENTRIES = 10000;
 
     @Autowired
@@ -83,9 +83,9 @@ public class PerformanceManagementFile2VesMapper {
                     throw new IllegalStateException("File to be unzipped too big.");
                 }
 
-                final long compressionRatio = totalSizeEntry / entry.getCompressedSize();
+                final double compressionRatio = (double) totalSizeEntry / entry.getCompressedSize();
                 if (compressionRatio > THRESHOLD_RATIO) {
-                    return Single.error(new Exception("Wrong file type, threshold to high."));
+                    return Single.error(new Exception("Wrong file type, threshold to high " + compressionRatio));
                 }
 
                 if (totalEntryArchive > THRESHOLD_ENTRIES) {
index 4088e05..8285a0b 100644 (file)
@@ -177,4 +177,10 @@ class DefaultHttpRestClientTest {
         observer.assertError(throwable -> throwable.getMessage()
             .equals("Get Zone offset failed for 150.62.25.26 . Empty output received"));
     }
+
+    @Test
+    void testGetTimeOffsetFailExecutionException() {
+        final TestObserver<ZoneId> observer = restClient.getTimeZone(ADAPTER).test();
+        observer.assertError(throwable -> throwable.getMessage().equals("Failed to get Zone ID for 150.62.25.26"));
+    }
 }
\ No newline at end of file
index 597505e..6c75680 100644 (file)
@@ -50,8 +50,7 @@ final class SnmpTrapListener implements Runnable {
 
     @Override
     public synchronized void run() {
-        try (final var snmpTarget = new DefaultUdpTransportMapping(
-                new UdpAddress(hostPortAddress))) {
+        try (final var snmpTarget = new DefaultUdpTransportMapping(new UdpAddress(hostPortAddress))) {
             final var threadPool = ThreadPool.create("SNMP_V2_Listener", THREADS_SIZE);
             final var dispatcher = new MultiThreadedMessageDispatcher(threadPool, new MessageDispatcherImpl());
             dispatcher.addMessageProcessingModel(new MPv2c());