Skip to content

Commit

Permalink
Changes after first code review
Browse files Browse the repository at this point in the history
  • Loading branch information
CDellaGiusta committed Feb 3, 2025
1 parent 49f0ce7 commit 3be7264
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1225,11 +1225,7 @@ public static void addErrataToChannel(Set<Long> eids, Long cid) {
* @return list of all channels
*/
public static List<Channel> listAllChannels() {
List<Channel> result = singleton.listObjectsByNamedQuery("Channel.findAllChannels", Map.of());
if (result != null) {
return result;
}
return new ArrayList<>();
return getSession().createNamedQuery("Channel.findAllChannels", Channel.class).list();
}

/**
Expand Down
121 changes: 64 additions & 57 deletions java/code/src/com/suse/manager/hub/test/HubControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

package com.suse.manager.hub.test;

import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand All @@ -22,14 +21,12 @@
import com.redhat.rhn.common.conf.ConfigDefaults;
import com.redhat.rhn.common.hibernate.ConnectionManager;
import com.redhat.rhn.common.hibernate.ConnectionManagerFactory;
import com.redhat.rhn.common.hibernate.HibernateFactory;
import com.redhat.rhn.common.hibernate.ReportDbHibernateFactory;
import com.redhat.rhn.domain.channel.Channel;
import com.redhat.rhn.domain.channel.ChannelFactory;
import com.redhat.rhn.domain.org.Org;
import com.redhat.rhn.domain.role.RoleFactory;
import com.redhat.rhn.frontend.xmlrpc.channel.software.ChannelSoftwareHandler;
import com.redhat.rhn.taskomatic.task.ReportDBHelper;
import com.redhat.rhn.testing.ChannelTestUtils;
import com.redhat.rhn.testing.JMockBaseTestCaseWithUser;
import com.redhat.rhn.testing.TestUtils;
import com.redhat.rhn.testing.UserTestUtils;
Expand All @@ -44,7 +41,6 @@

import com.google.gson.JsonObject;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -75,13 +71,6 @@ public void setUp() throws Exception {
dummyHubController.initRoutes();
}

@Override
@AfterEach
public void tearDown() throws Exception {
super.tearDown();
HibernateFactory.rollbackTransaction();
}

private String createTestUserName() {
return "testUser" + TestUtils.randomString();
}
Expand Down Expand Up @@ -151,12 +140,13 @@ public void ensureGetApisNotWorkingWithPost(HttpMethod apiMethod, String apiEndp
ControllerTestUtils utils = new ControllerTestUtils();

assertThrows(IllegalStateException.class, () ->
utils.withServerFqdn(DUMMY_SERVER_FQDN)
.withApiEndpoint(apiEndpoint)
.withHttpMethod(HttpMethod.post)
.withRole(apiRole)
.withBearerTokenInHeaders()
.simulateControllerApiCall());
utils.withServerFqdn(DUMMY_SERVER_FQDN)
.withApiEndpoint(apiEndpoint)
.withHttpMethod(HttpMethod.post)
.withRole(apiRole)
.withBearerTokenInHeaders()
.simulateControllerApiCall(),
apiEndpoint + " get API not failing when called with post method");
}

@ParameterizedTest
Expand All @@ -165,12 +155,13 @@ public void ensurePostApisNotWorkingWithGet(HttpMethod apiMethod, String apiEndp
ControllerTestUtils utils = new ControllerTestUtils();

assertThrows(IllegalStateException.class, () ->
utils.withServerFqdn(DUMMY_SERVER_FQDN)
.withApiEndpoint(apiEndpoint)
.withHttpMethod(HttpMethod.get)
.withRole(apiRole)
.withBearerTokenInHeaders()
.simulateControllerApiCall());
utils.withServerFqdn(DUMMY_SERVER_FQDN)
.withApiEndpoint(apiEndpoint)
.withHttpMethod(HttpMethod.get)
.withRole(apiRole)
.withBearerTokenInHeaders()
.simulateControllerApiCall(),
apiEndpoint + " post API not failing when called with get method");
}

@ParameterizedTest
Expand All @@ -187,7 +178,7 @@ public void ensureHubApisNotWorkingWithPeripheral(HttpMethod apiMethod, String a
.simulateControllerApiCall();

ResultJson<?> resultKO = Json.GSON.fromJson(answerKO, ResultJson.class);
assertFalse(resultKO.isSuccess());
assertFalse(resultKO.isSuccess(), apiEndpoint + " hub API not failing with peripheral server");
assertEquals("Token does not allow access to this resource", resultKO.getMessages().get(0));
}

Expand All @@ -205,7 +196,7 @@ public void ensurePeripheralApisNotWorkingWithHub(HttpMethod apiMethod, String a
.simulateControllerApiCall();

ResultJson<?> resultKO = Json.GSON.fromJson(answerKO, ResultJson.class);
assertFalse(resultKO.isSuccess());
assertFalse(resultKO.isSuccess(), apiEndpoint + " peripheral API not failing with hub server");
assertEquals("Token does not allow access to this resource", resultKO.getMessages().get(0));
}

Expand All @@ -222,7 +213,7 @@ public void ensureNotWorkingWithoutToken(HttpMethod apiMethod, String apiEndpoin
.withRole(apiRole)
.simulateControllerApiCall();

Assertions.fail(); //should not be here: it should have thrown
Assertions.fail(apiEndpoint + " API call should have failed without token");
}
catch (spark.HaltException ex) {
assertEquals(HttpServletResponse.SC_BAD_REQUEST, ex.statusCode());
Expand All @@ -241,7 +232,8 @@ public void checkPingApiEndpoint() throws Exception {
.simulateControllerApiCall();
JsonObject jsonObj = Json.GSON.fromJson(answer, JsonObject.class);

assertTrue(jsonObj.get("message").getAsString().startsWith("Pinged from"));
assertTrue(jsonObj.get("success").getAsBoolean(), apiUnderTest + " API call is failing");
assertTrue(jsonObj.get("message").getAsString().startsWith("Pinged from"), "Unexpected ping message");
}

@Test
Expand Down Expand Up @@ -285,7 +277,7 @@ public void checkStoreCredentialsApiEndpoint() throws Exception {
.simulateControllerApiCall();
JsonObject jsonObj = Json.GSON.fromJson(answer, JsonObject.class);

assertTrue(jsonObj.get("success").getAsBoolean());
assertTrue(jsonObj.get("success").getAsBoolean(), apiUnderTest + " API call is failing");
}

@Test
Expand All @@ -301,11 +293,11 @@ public void checkManagerinfoApiEndpoint() throws Exception {
.simulateControllerApiCall();
ManagerInfoJson mgrInfo = Json.GSON.fromJson(answer, ManagerInfoJson.class);

assertFalse(mgrInfo.getVersion().isBlank());
assertTrue(mgrInfo.hasReportDb());
assertFalse(mgrInfo.getReportDbName().isBlank());
assertFalse(mgrInfo.getReportDbHost().isBlank());
assertEquals(5432, mgrInfo.getReportDbPort());
assertFalse(mgrInfo.getVersion().isBlank(), "ManagerInfo version is blank");
assertTrue(mgrInfo.hasReportDb(), "ManagerInfo has missing report db");
assertFalse(mgrInfo.getReportDbName().isBlank(), "ManagerInfo database name is blank");
assertFalse(mgrInfo.getReportDbHost().isBlank(), "ManagerInfo database host is blank");
assertEquals(5432, mgrInfo.getReportDbPort(), "ManagerInfo database port is not 5432");
}

@Test
Expand All @@ -331,14 +323,16 @@ public void checkStoreReportDbCredentialsApiEndpoint() throws Exception {
.simulateControllerApiCall();
JsonObject jsonObj = Json.GSON.fromJson(answer, JsonObject.class);

assertTrue(jsonObj.get("success").getAsBoolean());
assertTrue(jsonObj.get("success").getAsBoolean(), apiUnderTest + " API call is failing");

//check there is one user with that username
assertTrue(existsReportDbUser(testReportDbUserName));
assertTrue(existsReportDbUser(testReportDbUserName),
apiUnderTest + " API reports no user " + testReportDbUserName);

//cleanup
cleanupReportDbUser(testReportDbUserName);
assertFalse(existsReportDbUser(testReportDbUserName));
assertFalse(existsReportDbUser(testReportDbUserName),
"cleanup of user not working for user " + testReportDbUserName);
}

@Test
Expand All @@ -352,7 +346,8 @@ public void checkRemoveReportDbCredentialsApiEndpoint() throws Exception {

//create a user
createReportDbUser(testReportDbUserName, testReportDbPassword);
assertTrue(existsReportDbUser(testReportDbUserName));
assertTrue(existsReportDbUser(testReportDbUserName),
"failed creation of user " + testReportDbUserName);

ControllerTestUtils utils = new ControllerTestUtils();
String answer = (String) utils.withServerFqdn(DUMMY_SERVER_FQDN)
Expand All @@ -364,9 +359,10 @@ public void checkRemoveReportDbCredentialsApiEndpoint() throws Exception {
.simulateControllerApiCall();
JsonObject jsonObj = Json.GSON.fromJson(answer, JsonObject.class);

assertTrue(jsonObj.get("success").getAsBoolean());
assertTrue(jsonObj.get("success").getAsBoolean(), apiUnderTest + " API call is failing");
//check the user is gone
assertFalse(existsReportDbUser(testReportDbUserName));
assertFalse(existsReportDbUser(testReportDbUserName),
apiUnderTest + " API call fails to remove user " + testReportDbUserName);
}

@Test
Expand All @@ -386,25 +382,25 @@ public void checkApiListAllPeripheralOrgs() throws Exception {
.simulateControllerApiCall();
List<OrgInfoJson> allOrgs = Arrays.asList(Json.GSON.fromJson(answer, OrgInfoJson[].class));

assertTrue(allOrgs.size() >= 3);
assertTrue(allOrgs.size() >= 3, "All 3 test test orgs are not listed");
assertTrue(allOrgs.stream()
.anyMatch(e -> (e.getOrgId() == org1.getId()) && (e.getOrgName().startsWith("org1"))));
.anyMatch(e -> (e.getOrgId() == org1.getId()) && (e.getOrgName().startsWith("org1"))),
apiUnderTest + " API call not listing test organization [org1]");
assertTrue(allOrgs.stream()
.anyMatch(e -> (e.getOrgId() == org2.getId()) && (e.getOrgName().startsWith("org2"))));
.anyMatch(e -> (e.getOrgId() == org2.getId()) && (e.getOrgName().startsWith("org2"))),
apiUnderTest + " API call not listing test organization [org2]");
assertTrue(allOrgs.stream()
.anyMatch(e -> (e.getOrgId() == org3.getId()) && (e.getOrgName().startsWith("org3"))));
.anyMatch(e -> (e.getOrgId() == org3.getId()) && (e.getOrgName().startsWith("org3"))),
apiUnderTest + " API call not listing test organization [org3]");
}

@Test
public void checkApilistAllPeripheralChannels() throws Exception {
String apiUnderTest = "/hub/listAllPeripheralChannels";

user.addPermanentRole(RoleFactory.CHANNEL_ADMIN);
ChannelSoftwareHandler csh = new ChannelSoftwareHandler(null, null);
csh.create(user, "api-test-chan-label", "apiTestChanName",
"apiTestSummary", "channel-x86_64", null);
Channel testChannel = ChannelFactory.lookupByLabel("api-test-chan-label");
assertNotNull(testChannel);
Channel testBaseChannel = ChannelTestUtils.createBaseChannel(user);
Channel testChildChannel = ChannelTestUtils.createChildChannel(user, testBaseChannel);

ControllerTestUtils utils = new ControllerTestUtils();
String answer = (String) utils.withServerFqdn(DUMMY_SERVER_FQDN)
Expand All @@ -414,14 +410,25 @@ public void checkApilistAllPeripheralChannels() throws Exception {
.withBearerTokenInHeaders()
.simulateControllerApiCall();
List<ChannelInfoJson> allChannels = Arrays.asList(Json.GSON.fromJson(answer, ChannelInfoJson[].class));
Optional<ChannelInfoJson> testChannelInfo = allChannels.stream()
.filter(e -> e.getName().equals("apiTestChanName"))
.findAny();

assertTrue(testChannelInfo.isPresent());
assertEquals("apiTestChanName", testChannelInfo.get().getName());
assertEquals("api-test-chan-label", testChannelInfo.get().getLabel());
assertEquals(user.getOrg().getId(), testChannelInfo.get().getOrgId());
assertNull(testChannelInfo.get().getParentChannelId());
Optional<ChannelInfoJson> testBaseChannelInfo = allChannels.stream()
.filter(e -> e.getName().equals(testBaseChannel.getName()))
.findAny();
assertTrue(testBaseChannelInfo.isPresent(),
apiUnderTest + " API call not listing channel " + testBaseChannel);
assertEquals(testBaseChannel.getName(), testBaseChannelInfo.get().getName());
assertEquals(testBaseChannel.getLabel(), testBaseChannelInfo.get().getLabel());
assertEquals(user.getOrg().getId(), testBaseChannelInfo.get().getOrgId());
assertNull(testBaseChannelInfo.get().getParentChannelId());

Optional<ChannelInfoJson> testChildChannelInfo = allChannels.stream()
.filter(e -> e.getName().equals(testChildChannel.getName()))
.findAny();
assertTrue(testChildChannelInfo.isPresent(),
apiUnderTest + " API call not listing channel " + testChildChannel);
assertEquals(testChildChannel.getName(), testChildChannelInfo.get().getName());
assertEquals(testChildChannel.getLabel(), testChildChannelInfo.get().getLabel());
assertEquals(user.getOrg().getId(), testChildChannelInfo.get().getOrgId());
assertEquals(testBaseChannel.getId(), testChildChannelInfo.get().getParentChannelId());
}
}

0 comments on commit 3be7264

Please sign in to comment.